• 15
• 15
• 11
• 9
• 10

# SDL performance problem when realizing fog of war

This topic is 2421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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:

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:

• I once 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
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.

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

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

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