[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
without first freeing tmp from when you loaded

At the bottom of your while(end!=1) loop you have
if ((screen->flags & SDL_DOUBLEBUF) != SDL_DOUBLEBUF) 
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

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

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_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.

More information about the SDL mailing list