Perfomance (FPS) problems in OpenGL and SDL. Help

Hi!

I’m having some perfomance problems when trying to stick my frame rate to 30 FPS (just because physic engine, box2d, requires a constant frame rate)

This is my game loop, as you can see there is a SDL_Delay call. In this case sometimes my game stops when I move the camera, like a random lag. If I remove SDL_Delay, everything works fine, but as FPS are variable I cannot use box2d.

Do you have the same problem with SDL_Delay?
Do you have any idea or suggestion?

Code:

void CApp::StartGameLoop() {
g_log->WriteInfo(“Starting game loop.”);
SDL_Event event;
while (!m_exit_game_loop) {
unsigned int then=SDL_GetTicks();

	// sound, input, update, render
	m_sound_manager->Update();
	while (SDL_PollEvent(&event)) ProcessPendingEventSDL(&event);
	Update();
   		Render();

	// must wait?
	int elapsed=SDL_GetTicks()-then;
	int wait=FRAME_TIME-elapsed;
	if (wait>0) SDL_Delay(wait);
	//printf("wait %d\n", wait);
}
g_log->WriteInfo("Finishing game loop.");

}

Even on the Simulator I have this problem, although it is more intensive on the iPhone.

By the way, is SDL compatible with Instruments? I can’t launch it.

FRAME_TIME is 33 (ms) in order to get no more than 30 FPS. If I uncomment printf(“wait %d\n”, wait); I see as usually after update and render, wait is about 22 ms. So the iPhone actually do not have any problems updating and rendering my world.

Thanks a lot for your help.

Hi Ricardo,

A few ideas:

  1. When you remove the SDL_Delay, you’re not using Box2D. But when you test
    with SDL_Delay, are you also not using Box2D? If you are, the problem can be
    in the delay or in the simulation…

  2. Do you know what the value in the *‘wait’ *variable is when the lag
    happens? I’m guessing the physics simulation could cause some “random” lag,
    i.e., when everything is static, the load is very light, but when an object
    hits a brick wall, a lot of objects leave the sleep state… If you print
    the absolute time at the start of each loop, you should see 33ms time slots.

Anyway, if you absolutely can’t get the wait to work, perhaps you can still
"execute steps on demand". In example, supposing that the simulation step is
10ms:

How much time has passed? (lets say 30 ms have passed since the last pass)
Execute update 3 times (10ms + 10ms + 10ms)
Draw

This also means that:

How much time has passed? (lets say 4 ms have passed since the last pass)
Don’t update
Draw

*How much time has passed? (lets say 4 ms have passed since the last pass)
*Don’t update

  • Draw*
    How much time has passed? (lets say 4 ms have passed since the last pass)
    Execute update 1 time (4+4+4 > 10, we need to execute 1 step).
    Draw
    **
    This way, your rendering won’t be limited by the simulation step. Well, I
    hope I have given you some ideas. :slight_smile:

Cheers,
Daniel

recently i have noticed some emails that have the code example centered.
at first i thought who ever was sending was formatting wrong or their
client was messed up. i have been deleting them. but started to wonder
why its centered, it me or them ? has anyone else noticed ?

mattOn Thu, 15 Oct 2009, ricardo_ruiz wrote:

Hi!

I’m having some perfomance problems when trying to stick my frame rate to 30 FPS (just because physic engine, box2d, requires a constant frame rate)

This is my game loop, as you can see there is a SDL_Delay call. In this case sometimes my game stops when I move the camera, like a random lag. If I remove SDL_Delay, everything works fine, but as FPS are variable I cannot use box2d.

Do you have the same problem with SDL_Delay?
Do you have any idea or suggestion?

Code:

void CApp::StartGameLoop() {
g_log->WriteInfo(“Starting game loop.”);
SDL_Event event;
while (!m_exit_game_loop) {
unsigned int then=SDL_GetTicks();

  // sound, input, update, render
  m_sound_manager->Update();
  while (SDL_PollEvent(&event)) ProcessPendingEventSDL(&event);
  Update();
  		Render();

  // must wait?
  int elapsed=SDL_GetTicks()-then;
  int wait=FRAME_TIME-elapsed;
  if (wait>0) SDL_Delay(wait);
  //printf("wait %d\n", wait);

}
g_log->WriteInfo(“Finishing game loop.”);
}

Even on the Simulator I have this problem, although it is more intensive on the iPhone.

By the way, is SDL compatible with Instruments? I can’t launch it.

FRAME_TIME is 33 (ms) in order to get no more than 30 FPS. If I uncomment printf(“wait %d\n”, wait); I see as usually after update and render, wait is about 22 ms. So the iPhone actually do not have any problems updating and rendering my world.

Thanks a lot for your help.

2009/10/15 :

recently i have noticed some emails that have the code example centered. at
first i thought who ever was sending was formatting wrong or their client
was messed up. ?i have been deleting them. ?but started to wonder why its
centered, it me or them ? ?has anyone else noticed ?

Pretty sure it’s just you (or your email client). At my end, it’s
just indented. Maybe your email client handles TABs by centring
whatever text follows.