[SDL] Problems calculating time between frames using SDL_GetTicks.

Rogelio Nodal rnodal at gmail.com
Mon Jun 2 09:45:32 PDT 2008


Hello all:

This is my current loop code(part of it):

//  main loop
for(;;)
{


        // get time before frame
        this->beforeFrameTime = (float)SDL_GetTicks();

        // handle events
        this->handleEvents();

        // handle user staff
        // .................


        // do rendering stuff
        // ..................


        // 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));
        }



    } // end of main loop


Sometimes when I call engine->getDeltaTime(), it will return a delta
time of zero causing animations to brake. Specially things like:

x += dx * deltaTime;

Is there a way around this? Am I using SDL_GetTicks() the right way.
Thanks a lot in advance.

-r





More information about the SDL mailing list