Skip to content

Commit

Permalink
SDL_stbimage.h: Use SDL_CreateRGBSurfaceWithFormatFrom() if available
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGibson committed Dec 21, 2021
1 parent d138a3e commit 7d02a0c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion SDL_stbimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Supports all filetypes supported by stb_image (JPEG, PNG, TGA, BMP, PSD, ...
* See stb_image.h for details).
*
* (C) 2015 Daniel Gibson
* (C) 2015-2021 Daniel Gibson
*
* Homepage: https://github.com/DanielGibson/Snippets/
*
Expand Down Expand Up @@ -248,6 +248,19 @@ typedef struct {
static SDL_Surface* STBIMG__CreateSurfaceImpl(STBIMG__image img, int freeWithSurface)
{
SDL_Surface* surf = NULL;

#if SDL_VERSION_ATLEAST(2, 0, 5)

// SDL 2.0.5 introduced SDL_CreateRGBSurfaceWithFormatFrom() and SDL_PIXELFORMAT_RGBA32
// which makes this code much simpler.

Uint32 format = (img.format == STBI_rgb) ? SDL_PIXELFORMAT_RGB24 : SDL_PIXELFORMAT_RGBA32;

surf = SDL_CreateRGBSurfaceWithFormatFrom((void*)img.data, img.w, img.h,
img.format*8, img.format*img.w, format);

#else // older SDL2 version without SDL_CreateRGBSurfaceWithFormatFrom()

Uint32 rmask, gmask, bmask, amask;
// ok, the following is pretty stupid.. SDL_CreateRGBSurfaceFrom() pretends to use
// a void* for the data, but it's really treated as endian-specific Uint32*
Expand All @@ -268,6 +281,7 @@ static SDL_Surface* STBIMG__CreateSurfaceImpl(STBIMG__image img, int freeWithSur
surf = SDL_CreateRGBSurfaceFrom((void*)img.data, img.w, img.h,
img.format*8, img.format*img.w,
rmask, gmask, bmask, amask);
#endif // SDL_VERSION_ATLEAST

if(surf == NULL)
{
Expand Down

0 comments on commit 7d02a0c

Please sign in to comment.