C++ SDL Image Alpha Problem

Started by
1 comment, last by BeerNutts 13 years, 2 months ago
[font="Arial"][font="arial, verdana, tahoma, sans-serif"][font="Arial"][size=2]
Okay so I am making an old school RPG-like game. For the map background I am using a tile engine that I wrote myself. Every frame it blits only the tiles that are around the player to the screen, but when I try to blit the tiles that need to have transparency around them (such as a tree), it slows the game down and it lags horribly, but when I blit the same tiles without transparency, the games runs perfectly smooth. Here is my image loading function:


SDL_Surface *load_image( std::string filename, bool ALPHA=false ) {

SDL_Surface *get_img = IMG_Load( filename.c_str() );
SDL_Surface *loaded_img;

if( ALPHA )
loaded_img = SDL_DisplayFormatAlpha( get_img );
else
loaded_img = SDL_DisplayFormat( get_img );
SDL_FreeSurface( get_img );

return loaded_img;

}


So if I put 'true' in for the second argument, then it will load the image with its transparency, and slows the game down. But if I put 'false', then it doesn't load with transparency and my game runs perfectly. Why is this? And what can I do to solve this? Thanks in advance.[/font][/font][/font]
Advertisement
Using alpha is slower so it's nothing strange that you experiencing slow down. To speed it up you could try to use hardware surfaces if you not already does that. You can consider using some kind of dirty rectangle system so that you only draw what has to be updated. Or you could use OpenGL

If you don't need a full alpha channel you can use a colorkey which is much faster.
You may want to look into SFML instead of SDL for graphics. SFML is integrated with OpenGL, so it implicilty uses HW acceleration, whereas SDL implicitly does it in SW.

It shouldn't be a hard change assuming you've abstracted out your graphics properly.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement