views:

343

answers:

3

I'm trying to find the most efficient way to alpha blend in SDL. I don't feel like going back and rewriting rendering code to use OpenGL instead (which I've read is much more efficient with alpha blending), so I'm trying to figure out how I can get the most juice out of SDL's alpha blending.

I've read that I could benefit from using hardware surfaces, but this means I'd have to run the game in fullscreen. Can anyone comment on this? Or anything else regarding alpha transparency in SDL?

A: 

Decided to just not use alpha blending for that part. Pixel blending is too much for software surfaces, and OpenGL is needed when you want the power of your hardware.

trikker
+2  A: 

I have once playing with sdl hardware surfaces ant it wasn't the most pleasant experience. SDL is really easy to use, but when it gets to efficiency you should really stick with something that was especially designed for such task. OpenGL is a good choice here. You can always mix SDL (window and event management) with openGL (graphics) and use some of the already written code.

You can find some info on hardware surfaces here and here

beermann
A: 

Use OpenGL with SDL. It's good to get to know the GL library (I hardly see a use for non-graphics accelerated stuff these days, even GUIs use it now). SDL_image has a way to check for alpha channel. My function that creates textures from a path to an image file (uses SDL_image's IMG_Load() function) has this:

// if we successfully open a file
// do some gl stuff then
SDL_PixelFormat *format = surface->format;

int width, height;
width = pow2(surface->w);
height = pow2(surface->h);

SDL_LockSurface(surface); // Call this whenever reading pixels from a surface

/* Check for alpha channel */
if (format->Amask)
  glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
else
  glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);

SDL_UnlockSurface(surface);

pow2() just rounds the number to the next closest power of 2. A lot of video cards nowadays can handle non-power of 2 values for texture sizes but as far as I can tell, they are definitely NOT optimised for it (tested framerates). Other video cards will just refuse to render, your app may crash, etc etc.

Code is here: http://www.tatsh.net/2010-06-19/using-sdlimage-and-sdlttf-opengl