SDL_BYTESPERPIXEL(3)

πŸ’… SDL_BYTESPERPIXEL(3) β€” bestie edition

okay so we're in the SDL3 FUNCTIONS part of the manual, and today's main character is SDL_BYTESPERPIXEL πŸ’–


✨ NAME ✨

SDL_BYTESPERPIXEL β€” it's a macro that tells you how many bytes per pixel an SDL_PixelFormat has. that's it, that's the whole job, and it ate 🫢


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_pixels.h>

#define SDL_BYTESPERPIXEL(format) \
    (SDL_ISPIXELFORMAT_FOURCC(format) ? \
        ((((format) == SDL_PIXELFORMAT_YUY2) || \
          ((format) == SDL_PIXELFORMAT_UYVY) || \
          ((format) == SDL_PIXELFORMAT_YVYU) || \
          ((format) == SDL_PIXELFORMAT_P010)) ? 2 : 1) : (((format) >> 0) & 0xFF))

like. look at her. she's doing bit math AND a ternary chain AND she's a macro. multitasking queen πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

okay important thing first, and I need you to actually listen bestie:

this macro double-evaluates its parameter 😭 so do NOT pass it expressions with side-effects. that means no i++, no function calls that do stuff, nothing spicy. it'll run your thing twice and you'll be sitting there like "why is my counter wrong" πŸ’€

and second thing, about FourCC formats: they do their best here, fr fr, but ngl a lot of them just don't have a meaningful measurement of bytes per pixel. so the number you get back for those is... vibes-adjacent. it's giving "we tried our best" 🌈


πŸŽ€ MACRO PARAMETERS πŸŽ€

  • format β€” an SDL_PixelFormat to check. just hand her the format, she'll handle the rest πŸ’…

⭐ RETURN VALUE ⭐

returns the bytes-per-pixel of format. literally that. no surprises, no plot twists ✨


πŸ’œ THREAD SAFETY πŸ’œ

it is safe to call this macro from any thread. every thread. all at once. she doesn't care, she's unbothered, she's thriving 🧚


🌈 AVAILABILITY 🌈

this macro has been available since SDL 3.2.0. so if you're on something older... bestie. upgrade. πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘

  • SDL_BITSPERPIXEL(3) β€” her sister, she counts bits instead of bytes, iconic behavior honestly πŸ“

Simple Directmedia Layer β€” SDL 3.4.18 β€” that's the source and version, and the date's riding along with it in the footer, so consider this your cute lil sign-off πŸ’–

stay hydrated, read your manpages, and never double-evaluate something with side effects. mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from