Sign in to follow this  
GerPro

SDL performance problem when realizing fog of war

Recommended Posts

Hello!

I am working with SDL-1.3.0-5538. My project is to realize a real time strategy game. First: I used the search engine and found some possible solutions, but they did not work for me. Here is my problem:

I implemented a "fog of war". If I run my game with fog of war deactivated, I have a framerate of ~25 fps (that's also too low, but this is another topic...).
If I activate the fog of war, the framerate falls down to ~8 fps.

Here is my game with fog of war enabled:

[img]http://www.abload.de/img/fogofwarecfu.png[/img]

As you can see unexplored areas are completly dark, explored but currently not visible areas are transparent gray und currently visible areas are... visible ;)

This is how I realized the fog of war:

[list][*]I [b]once[/b] create two SDL_Surfaces 'A' and 'B'[*]Both have a color key: White[*]SDL_Surface 'A' is filled with gray as color and has in addition to the white color key a tranparency value of 175. The dimensions of 'A' are the same as the screen dimensions, in this case 1024x768[*]SDL_Surface 'B' is filled with black as color and has the dimension of the map which has to be explored: w=h=4096*4096[/list]
I render the currently visible map area. Without the fog i now have ~25fps.
If i render SDL_Surface 'A' on my screen and after this Surface 'B' to archieve the result as shown in the image, the framerate is ~8fps. I know that Surface 'B' is bigger as the screen dimensions, so I only render the actual needed part.

I dont know why these simple 2 blitting operations are so incredible slow? In another topic a user said that you have to use the function 'SDL_DisplayFormat', but if I do so for Surface 'A' and 'B' the framerate falls down to ~0.2 fps.

[code]
SDL_Surface *pConverted= SDL_DisplayFormat(pSurface); // Setting color key and transparency is done befor calling SDL_DisplayFormat
SDL_FreeSurface(pSurface);
pSurface = pConverted;
[/code]


Thanks for reading... :mellow:

Share this post


Link to post
Share on other sites
SDL_DisplayFormat converts the surface to the same format as the screen surface. If you blit a surface on top of another and they have different format SDL will have to convert the surface behind the scene every time you blit, which is slow. So using using SDL_DisplayFormat you can avoid these conversions. If you blit all surfaces onto the screen surface it's strange that it cause slow down. Have you tried to use SDL_DisplayFormat on all surfaces? It's often a good thing to do.

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1312575657' post='4845199']
SDL_DisplayFormat converts the surface to the same format as the screen surface. If you blit a surface on top of another and they have different format SDL will have to convert the surface behind the scene every time you blit, which is slow. So using using SDL_DisplayFormat you can avoid these conversions. If you blit all surfaces onto the screen surface it's strange that it cause slow down. Have you tried to use SDL_DisplayFormat on all surfaces? It's often a good thing to do.
[/quote]

Problem solved: I found a call to SDL_SetVideoMode somwhere where I thought I had deleted it. This meant, I called SDL_SetVideoMode twice with different parameters. After deleting the first one I got a framerate fo ~145 fps :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this