[SDL] SDL newbie question

Blake D BlakeTNC at Yahoo.com
Sat Jan 18 01:38:01 PST 2003


--- "Roger D. Vargas" <roger at eht.scu.tur.cu> wrote:
> 
> I want to write an small article about game
> programming, of course using 
> sdl. I need some revision to the following code and
> some opinion. Can it 
> be smaller or more compact? Can it be smaller or
> more compact or clear 
> in Direct X? And other libs? The code is not
> finished yet, but any 
> optimization is welcome.

A couple things I noticed:

In the loadimg() function you do
tmp=SDL_LoadBMP("sail.bmp");
without first freeing tmp from when you loaded
"backg.bmp".

At the bottom of your while(end!=1) loop you have
this:
if ((screen->flags & SDL_DOUBLEBUF) != SDL_DOUBLEBUF) 
    SDL_UpdateRect(screen,0,0,0,0);
else SDL_Flip(screen);

Really, you only need to call SDL_Flip(screen) because
it already checks to see if the passed surface has the
SDL_DOUBLEBUF flag set.

Heh, actually, there's no need to even worry about the
double buffer stuff unless you actually set
SDL_DOUBLEBUF in the first place (in
SDL_SetVideoMode).

Lastly, back up in the loadimg() function you use the
pixel color at (0,0) as the key to pass to
SDL_SetColorKey(). However, by using you get the color
from the palette and thus limit yourself to only
supporting 8bit images. Instead you could do something
more flexible like this:

Uint8 r, g, b;
SDL_GetRGB(*(Uint32 *)tmp->pixels, tmp->format, &r,
&g, &b);
SDL_SetColorKey(tmp, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
SDL_MapRGB(tmp->format, r, g, b));

That will grab the red, green, and blue components of
the color of the first pixel, so it will work even
above 8bit images.

Yeah, that's all I saw. It's a pretty small example
anyway. Nice use of the key repeat thing, though; it
simplifies the code a bit.

- Matt Borkowski


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com




More information about the SDL mailing list