One misstake i saw was the fact that the timing functions didnt span over the entire loop, the bottom half went outside everything.
One way to fix that is to NOT grab a beforeFrameTime using SDL_GetTicks() but by just copying the previous frames afterFrameTime into beforeFrameTime.
I also made a more accurate fps calculation and moved all the time code to the start of the loop.
And regarding your problem with a 0 deltatime and that position += dx * dt; wont work then thats not a problem. It should not move if no time has elapsed.
// main loop
for(;
{
// backup the previous frames afterFrameTime
this->beforeFrameTime = this->afterFrameTime;
// get time after frame
this->afterFrameTime = (float)SDL_GetTicks();
// time since last frame
this->deltaTime = this->afterFrameTime - this->beforeFrameTime;
// time since first frame
this->elapsedTime = SDL_GetTicks() - initialTime;
// fps
this->FPS = 1.0f / this->deltaTime;
// handle events
this->handleEvents();
// handle user staff
if(this->frameFunction() == EGEDONE)
{
// exit start if user says he is done
// by returning DONE
break;
}
// frame is not over so call render function to
this->renderFunction();
// increase frame count
this->frame++;
// regulate fps
if((this->maxFPS > 0) && ((SDL_GetTicks() - this->beforeFrameTime) < (1000.0 / this->maxFPS)))
{
// wait until the desired time has passed
SDL_Delay((Uint32)((1000.0 / this->maxFPS) - (SDL_GetTicks() - this->beforeFrameTime)));
}
// clear the vector of keys that were pressed and released
this->pressedKeyboardKeys.clear();
this->releasedKeyboardKeys.clear();
// clear the vector of mouse buttons that were pressed and released
this->pressedMouseButtons.clear();
this->releasedMouseButtons.clear();
// reset mouse wheel up/down movement tracking variables
this->mouseWheelMovedDown = false;
this->mouseWheelMovedUp = false;
} // end of main loop
// Tomaz - Lead 3D Tech Developer / Tension GraphicsDate: Mon, 2 Jun 2008 22:39:32 -0400
From: rnodal@gmail.com
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Problems calculating time between frames using SDL_GetTicks.
Thanks all for the answers. I will post my whole loop code to make things better. This code does not take into account the suggested changes yet:
// main loop
for(;
{
// get time before frame
this->beforeFrameTime = (float)SDL_GetTicks();
// handle events
this->handleEvents();
// handle user staff
if(this->frameFunction() == EGEDONE)
{
// exit start if user says he is done
// by returning DONE
break;
}
// frame is not over so call render function to
this->renderFunction();
// increase frame count
this->frame++;
// regulate fps
if((this->maxFPS > 0) && ((SDL_GetTicks() - this->beforeFrameTime) < (1000.0 / this->maxFPS)))
{
// wait until the desired time has passed
SDL_Delay((Uint32)((1000.0 / this->maxFPS) - (SDL_GetTicks() - this->beforeFrameTime)));
}
// get time after frame
this->afterFrameTime = (float)SDL_GetTicks();
// time since last frame
this->deltaTime = this->afterFrameTime - this->beforeFrameTime;
// time since first frame
this->elapsedTime = SDL_GetTicks() - initialTime;
// fps
if(this->elapsedTime > 1000)
{
this->FPS = (float)(this->frame / (this->elapsedTime / 1000.0));
}
// clear the vector of keys that were pressed and released
this->pressedKeyboardKeys.clear();
this->releasedKeyboardKeys.clear();
// clear the vector of mouse buttons that were pressed and released
this->pressedMouseButtons.clear();
this->releasedMouseButtons.clear();
// reset mouse wheel up/down movement tracking variables
this->mouseWheelMovedDown = false;
this->mouseWheelMovedUp = false;
} // end of main loop
I have another function called getDeltaTime and here is the code for it:
float EGE::getDeltaTime(void)
{
// return delta time
return (this->deltaTime / 1000.0);
} // getDeltaTime
Now, the problem is that sometimes I get a delta time of zero. Now mi theory was that some frames get render in such a small interval that when I call
SDL_GetTicks I get the same milliseconds like the previous call. So I was wondering is there was something that I was doing wrong.
There reason for my concern is that code like this will fail.
image_xposition += dx * dt;
I hope my problem can be understood better now. Thanks for the help.
Skaffa Messenger i mobilen!
http://windowslivemobile.msn.com/Homepage.aspx?lang=se-se