Skip to content

Instantly share code, notes, and snippets.

@dghost
Last active July 17, 2021 02:03
Show Gist options
  • Select an option

  • Save dghost/87274204fc3fe744214c to your computer and use it in GitHub Desktop.

Select an option

Save dghost/87274204fc3fe744214c to your computer and use it in GitHub Desktop.
Load a BMP with white pixels transparent using SDL2
SDL_Surface* GLimp_LoadIcon(char *name)
{
int32_t size;
SDL_Surface *result = NULL;
void *buffer = NULL;
// load the BMP using engine-specific load call
size = FS_LoadFile(name,&buffer);
if (buffer)
{
SDL_RWops *rw;
// create an SDL_RWops structure for the file buffer
rw = SDL_RWFromMem(buffer,size);
if (rw)
{
uint32_t keyColor;
// load the BMP into an SDL_Surface from the SDL_RWops buffer
result = SDL_LoadBMP_RW(rw,1);
// gets the proper color for white (255,255,255)
keyColor = SDL_MapRGB(result->format, 255, 255, 255);
// enables transparency for all white pixels
SDL_SetColorKey(result, SDL_TRUE, keyColor);
}
// free buffer
Z_Free(buffer);
}
// return SDL_Surface containing the icon
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment