SDL_CreateRGBSurface(3)
π SDL_CreateRGBSurface π
okay so bestie, you want a brand new blank SDL_Surface? this is literally the function. it's part of the SDL API Reference, and honestly? it's giving arts-and-crafts day. π¨β¨
β¨ NAME β¨
SDL_CreateRGBSurface β create an empty SDL_Surface π«Ά
πΈ SYNOPSIS πΈ
#include "SDL.h"
SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height,
int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
like. eight arguments. she's high-maintenance but she's worth it. π
π¦ DESCRIPTION π¦
So what she does is allocate an empty surface. and no cap, you must call this after SDL_SetVideoMode β that's not a vibe suggestion, that's a rule. π¨
Now here's the tea about depth: if depth is 8 bits, an empty palette gets allocated for the surface. otherwise, a 'packed-pixel' SDL_PixelFormat is created using the [RGBA]mask's you provided (peep SDL_PixelFormat for the deets).
and the flags? that's you telling SDL what type of surface you want. it's an OR'd combination of these, so go wild (but responsibly): π
π the flags, one by one π
SDL_SWSURFACEβ SDL creates the surface in system memory. this is the one that makes pixel-level access faster, but the tradeoff is you might not get to take advantage of some types of hardware blitting. lowkey a fair trade, honestly.SDL_HWSURFACEβ SDL attempts to create the surface in video memory. this lets SDL take advantage of Video->Video blits, which are often accelerated. go fast, queen. ποΈSDL_SRCCOLORKEYβ turns on colourkeying for blits from this surface. and ifSDL_HWSURFACEis also specified and colourkeyed blits are hardware-accelerated, SDL will attempt to place the surface in video memory. useSDL_SetColorKeyto set or clear this flag after surface creation. πSDL_SRCALPHAβ turns on alpha-blending for blits from this surface. ifSDL_HWSURFACEis also specified and alpha-blending blits are hardware-accelerated, then the surface will be placed in video memory if possible. useSDL_SetAlphato set or clear this flag after surface creation. β¨
Note: if an alpha-channel is specified (that is, ifAmaskis nonzero), then theSDL_SRCALPHAflag is automatically set. not me getting a flag I didn't even ask for π β but you may remove it by callingSDL_SetAlphaafter surface creation. so it's fine, she's removable.
β RETURN VALUE β
Returns the created surface... or NULL upon error. π
like if you get NULL, something went wrong, bestie. don't just keep going β check it. π
π EXAMPLE π
fr fr here's a whole 32-bit surface with the bytes of each pixel in R,G,B,A order, as expected by OpenGL for textures:
/* Create a 32-bit surface with the bytes of each pixel in R,G,B,A order,
as expected by OpenGL for textures */
SDL_Surface *surface;
Uint32 rmask, gmask, bmask, amask;
/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
rmask, gmask, bmask, amask);
if(surface == NULL) {
fprintf(stderr, "CreateRGBSurface failed: %s
", SDL_GetError());
exit(1);
}
the way that endianness #if is essential β the masks literally depend on the byte order of the machine, so don't skip it. πΈ
π SEE ALSO π
SDL_CreateRGBSurfaceFromSDL_FreeSurfaceSDL_SetVideoModeSDL_LockSurfaceSDL_PixelFormatSDL_SurfaceSDL_SetAlphaSDL_SetColorKey
and that's the whole page, bestie. this one's from the SDL manual, dated Tue 11 Sep 2001, 23:01 β an actual icon of a timestamp, we love her. π β¨ go make some surfaces. π¦π
π¦ where this page comes from
- debian bookworm: libsdl1.2-dev 1.2.15+dfsg2-8 Β· LGPL-2.1-or-later
- nixos 26.05: SDL_sixel 0-unstable-2016-02-06
- nixpkgs unstable: SDL_sixel 0-unstable-2016-02-06
- ubuntu jammy: libsdl1.2-dev 1.2.15+dfsg2-6 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms