SDL slowdown over time?

Started by
6 comments, last by Beren77 15 years, 10 months ago
Hi, I am currently developing a very simple 2D board game and in my main-loop I simply blit png pictures (or rather: I load the png's via SDL_Image into an SDL_Surface and blit this surface into my screen-surface, which is double-buffered). Now, my problem is that the game slows down over time. While at first, I can render 33 frames/second (more than that actually, but I constrain it), I can only render 1 frame/second after an hour or so. Now, I don't do anything else! I disabled debugging and thus, no log is generated. I also checked for memory leaks and couldn't find any (though I am not currently tracking SDL_Free-calls). Before I post any code, my question is: Have any of you ever experienced such a behavior? I googled but couldn't find any information, so I suppose that it's my code that sucks. But just maybe, it's not :). I'll try to compile a minimal running example to reproduce the behavior and post the code here. My configuration: SDL-1.2.13 SDL_Image-1.2.6 Microsoft Visual Studio 2005 Professional Windows XP SP 2 (and also Windows Vista Ultimate -- same behavior) Pentium IV 3.2 1 GB RAM If you have any ideas (or wild speculative guesses ;)), I'd really like to hear them. Thanks! Beren
Advertisement
Quote:Original post by Beren77
Hi,

I am currently developing a very simple 2D board game and in my main-loop I simply blit png pictures (or rather: I load the png's via SDL_Image into an SDL_Surface and blit this surface into my screen-surface, which is double-buffered).


Do you mean that you load the image (call IMG_Load) every frame? If so, then you need to remove that. Loading an image off the disk is an extremely so operation and should only be done during initialization.

If your program gets slower and slower, my guess would be a memory leak. Check that you free all of your resources in the appropriate places.
Sorry for the silly sentence: Of course, I load the image from disk _once_ and then blit them onto my surface each frame...

I am guessing, it's a leak, too, but so far, I didn't find any... Anyway, I'll post the code, as soon as I have it minimized (but first, I'll watch the game ;)).
classic memory leak symptoms... look for new without delete... good luck

Mathmo
Unfortunately, it's not as simple as that: I am monitoring my new/delete with a memory detector; if I don't free an object I have previously allocated, I'll get an error message at the end of the program. Besides: The memory usage stays constant (according to the task manager).
Can you trace back the problem over time? That is, are you using a source control system, so you can check previous versions?


Also, what happens when you turn on debugging? What does the log say? Can you, using the logs, figure out what code parts are causing the delays?

Actually, this makes me think of Half-Life 2's real-time profiling tool. It allows developers and designers to check how long specific areas take per frame - physics, rendering, AI, sound, etc. If your code is nicely organized, you might be able to quickly implement a similar tool. With your problem, even a rough version would help, I think. :)
Create-ivity - a game development blog Mouseover for more information.
Are you, by chance, using SDL_DisplayFormat(SDL_GetVideoSurface()) ?

I once had problems with it because I forgot to free the surface every frame...

Other than that, the only thing I see is that you are either using new without delete or loading a certain surface every frame... That looks just like a leak.

If you are on windows use Ctrl+Alt+Del to check the memory usage of the program, just to make sure if it's a leak or not.
Hmmm... I suppose I have to eat my own words: I compiled a simplistic version of my main loop (attached below) -- and it works perfectly, without slowing down. So I suppose, I'll heed your advise, Captain P, and will hunt for the slow down a) in my time (fortunately using svn) and b) if that didn't help, find the region which slows the game down.

Thanks guys.
Oh. Here's the graphic file that I load:
http://www.phbouillon.de/graphics.png

void blitGraphic(int x, int y, SDL_Surface *screen, SDL_Surface *image, int frame) {  SDL_Rect sourceRect;  SDL_Rect destRect;  sourceRect.w = destRect.w = 50;  sourceRect.h = destRect.h = 50;  sourceRect.x = 50 * (frame % 2);  sourceRect.y = 50 * (frame / 2);  destRect.x   = x;  destRect.y   = y;  SDL_BlitSurface(image, &sourceRect, screen, &destRect);}int main(int argc, char **argv) {  SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);  SDL_Surface *surface = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);  int lastRenderTime;  bool end = false;  SDL_Event event;  SDL_Surface *temp = IMG_Load("graphics.png");  SDL_Surface *image = SDL_DisplayFormatAlpha(temp);    SDL_FreeSurface(temp);  int frame = 0;  while (!end) {    lastRenderTime = SDL_GetTicks();		    SDL_FillRect(surface, 0, 0);    for (int x = 0; x < 1000; x += 50) {      for (int y = 0; y < 700; y += 50) {        blitGraphic(x, y, surface, image, frame);	      }    }    SDL_Flip(surface);  	    frame++;    if (frame == 8) {      frame = 0;    }    while (SDL_PollEvent(&event))  {            if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {        end = true;      }	    }    int dt = SDL_GetTicks() - lastRenderTime;    if (dt < 15) {      SDL_Delay(15 - dt);    }  }  SDL_FreeSurface(surface);  SDL_FreeSurface(image);  return 0;}

This topic is closed to new replies.

Advertisement