Problem drawing on an SDL_Surface (bug?)

Hello, I’ve got a little problem using an SDL surface (SDL_Surface). When I create a new surface using SDL_CreateRGBSurface(), I can’t draw it to the screen, it just doesn’t appears.
This is my code:

Code:

//////////////
// main.cpp //
//////////////

#include <SDL/SDL.h>

extern SDL_Surface *screen;

int main(int argc, char *argv[])
{
game_init(); // my function, inits SDL, the window (screen) etc., window appears
// screen size is 640x480, color depth is 24 bit (no alpha, right?)
screenFill(0xFF0000); // my function, filling screen to red, works after updating screen

// a simple copy from the SDL API Reference:

/* 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 *sf;
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

sf = SDL_CreateRGBSurface(SDL_SWSURFACE, 64, 64, 24,
rmask, gmask, bmask, amask);
if(sf == NULL)
{
error(“Error!”); // I don’t get an error
exit(1);
}
// usually, because the surface (sf) is 24 bit (no alpha), it should have any color (e.g. black)
SDL_BlitSurface(sf, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
// I now only see a red screen, I can nowhere find a 64x64 square colored different on the screen
waitForClose(); // my function, waits until the user closes the window
return 0;
}

I found out, that it would work, if I just use SDL_LoadBMP(), but why does this not work?

Can anybody help me out or suggest, where the mistake might be?

It might be that your masks are 32-bit, whereas your surface is 24-bit…

Try these masks:
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_Surface* result = SDL_CreateRGBSurface(flags,width,height,24,
0xFF0000, 0x00FF00, 0x0000FF, 0);
#else
SDL_Surface* result = SDL_CreateRGBSurface(flags,width,height,24,
0x0000FF, 0x00FF00, 0xFF0000, 0);
#endif

Does that work any different?
Jonny DOn Sat, Dec 26, 2009 at 10:35 AM, asmodeus wrote:

Hello, I’ve got a little problem using an SDL surface (SDL_Surface). When
I create a new surface using SDL_CreateRGBSurface(), I can’t draw it to the
screen, it just doesn’t appears.
This is my code:

Code:

//////////////
// main.cpp //
//////////////

#include

extern SDL_Surface *screen;

int main(int argc, char *argv[])
{
game_init(); // my function, inits SDL, the window (screen) etc., window
appears
// screen size is 640x480, color depth is 24 bit (no alpha, right?)
screenFill(0xFF0000); // my function, filling screen to red, works after
updating screen

// a simple copy from the SDL API Reference:

/* 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 *sf;
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

sf = SDL_CreateRGBSurface(SDL_SWSURFACE, 64, 64, 24,
rmask, gmask, bmask, amask);
if(sf == NULL)
{
error(“Error!”); // I don’t get an error
exit(1);
}
// usually, because the surface (sf) is 24 bit (no alpha), it should have
any color (e.g. black)
SDL_BlitSurface(sf, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
// I now only see a red screen, I can nowhere find a 64x64 square colored
different on the screen
waitForClose(); // my function, waits until the user closes the window
return 0;
}

I found out, that it would work, if I just use SDL_LoadBMP(), but why does
this not work?

Can anybody help me out or suggest, where the mistake might be?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Jonny D wrote:

It might be that your masks are 32-bit, whereas your surface is 24-bit…

Try these masks:
?? ?#if SDL_BYTEORDER == SDL_BIG_ENDIAN?? ? ? ?SDL_Surface* result = SDL_CreateRGBSurface(flags,width,height,24, 0xFF0000, 0x00FF00, 0x0000FF, 0);
?? ?#else
?? ? ? ?SDL_Surface* result = SDL_CreateRGBSurface(flags,width,height,24, 0x0000FF, 0x00FF00, 0xFF0000, 0);
?? ?#endif

Does that work any different?
Jonny D

Thanks, but I’ve already found out about the masks just before you post here ^^.

Just off-topic: Does anybody else have problems with a certain forum style which makes him this forum unaccessable? I’ve got that problem so that I had to make a new account :/.