SDL_DisplayFormat works, but not SDL_DisplayFormatAlpha

Posted by Bounderby on Game Development See other posts from Game Development or by Bounderby
Published on 2012-11-01T06:09:03Z Indexed on 2012/11/01 11:16 UTC
Read the original article Hit count: 211

Filed under:
|

The following code is intended to display a green square on a black background. It executes, but the green square does not show up. However, if I change SDL_DisplayFormatAlpha to SDL_DisplayFormat the square is rendered correctly.

So what don't I understand? It seems to me that I am creating *surface with an alpha mask and I am using SDL_MapRGBA to map my green color, so it would be consistent to use SDL_DisplayFormatAlpha as well.

(I removed error-checking for clarity, but none of the SDL API calls fail in this example.)

#include <SDL.h>

int main(int argc, const char *argv[])
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Surface *screen = SDL_SetVideoMode(
        640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF
    );

    SDL_Surface *temp = SDL_CreateRGBSurface(
        SDL_HWSURFACE, 100, 100, 32, 0, 0, 0,
        ( SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000ff : 0xff000000 )
    );

    SDL_Surface *surface = SDL_DisplayFormatAlpha( temp );

    SDL_FreeSurface( temp );

    SDL_FillRect(
        surface, &surface->clip_rect, SDL_MapRGBA(
            screen->format, 0x00, 0xff, 0x00, 0xff
        )
    );

    SDL_Rect r;
    r.x = 50;
    r.y = 50;

    SDL_BlitSurface( surface, NULL, screen, &r );

    SDL_Flip( screen );

    SDL_Delay( 1000 );

    SDL_Quit();

    return 0;
}

© Game Development or respective owner

Related posts about c++

Related posts about sdl