[1.3] About textures and pixel format

I’m trying to implement an “universal” texture loading API for SDL
1.3, that one should work on every target without #ifdefs.

This is my attempt:

(nice version with syntax highlighting) http://pastebin.com/f7913481f

SDL_TextureID SdlRenderer::
load_image(const std::string &base)
{
std::string path = std::string("./") + res_.prefix + "" + base + “.bmp”;
SDL_DisplayMode mode;
SDL_GetDisplayMode(screen
, &mode);

if (SDL_RWops *rw = load_or_grab_image(path)) {
    if (SDL_Surface *dest = SDL_LoadBMP_RW(rw, 1)) { // rwops is

closed by loadbmp
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
&Gmask, &Bmask, &Amask);

        if (SDL_Surface *temp = SDL_CreateRGBSurface(0 , dest->w,

dest->h, bpp,
Rmask, Gmask, Bmask, Amask
)) {
SDL_BlitSurface(dest, NULL, temp, NULL);
SDL_TextureID id =
SDL_CreateTextureFromSurface(mode.format, temp);

            if (id == 0)
                std::cerr << "SDL Error: " << SDL_GetError() << '\n';

            SDL_FreeSurface(temp);
            SDL_FreeSurface(dest);
            return id;
        }
        SDL_FreeSurface(dest);
    }
}
return 0;

}

But it doesn’t work on linux, SDL reports the following error:

SDL Error: Texture format doesn’t match window format

Note that screen_ is a valid WindowID returned from SDL_CreateWindow,
and I’ve added the SDL_DisplayMode stuff just to avoid the error…

The previous version of the code was with format =
SDL_PIXELFORMAT_ABGR8888 as you can see for instance in:

SDL-1.3/XCodeiPhoneOS/Demos/src/happy.c

…for iphone compatibility (I added the SDL_PixelFormatEnumToMasks
call looking at the iphone specific examples after seeing wrong colors
in my textures on the iphone emulator)

Actually I’d like to have a code path that works on every OS if possible!

My best solution ATM (tested only on linux and iphone):

#ifdef IPHONE
int bpp, format = SDL_PIXELFORMAT_ABGR8888;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask,
&Bmask, &Amask);
#else
int bpp = 32, format = 0;
Uint32 Rmask = 0xff0000, Gmask = 0xff00, Bmask = 0xff,
Amask =0xff000000;
#endif
Bye,
Gabry

I’m trying to implement an “universal” texture loading API for SDL
1.3, that one should work on every target without #ifdefs.

I’m going to change SDL_CreateTextureFromSurface() so it does The Right Thing ™, and creates a surface of the best format if you pass 0 for the format.

You’re welcome to take a crack at it:
/* FIXME: Get the best supported texture format */

See ya!
-Sam Lantinga, Founder and President, Galaxy Gameworks LLC