[SDL] Problems calculating time between frames using SDL_GetTicks.

Andreas Schiffler aschiffler at ferzkopp.net
Mon Jun 2 19:32:24 PDT 2008


FYI - The SDL_gfx library has a nice set of functions for this:

The functions return 0 or value for sucess and -1 for error. All functions
use a pointer to a framerate-manager variable to operate.

void SDL_initFramerate(FPSmanager * manager);

Initialize the framerate manager, set default framerate of 30Hz and
reset delay interpolation.

int SDL_setFramerate(FPSmanager * manager, int rate);

Set a new framerate for the manager and reset delay interpolation.

int SDL_getFramerate(FPSmanager * manager);

Get the currently set framerate of the manager.

void SDL_framerateDelay(FPSmanager * manager);

Generate a delay to accomodate currently set framerate. Call once in the
graphics/rendering loop. If the computer cannot keep up with the rate (i.e.
drawing too slow), the delay is zero and the delay interpolation is reset.




Rogelio Nodal wrote:
> 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
>
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>   

-------------- next part --------------
A non-text attachment was scrubbed...
Name: aschiffler.vcf
Type: text/x-vcard
Size: 142 bytes
Desc: not available
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080602/c4ebe2b9/attachment.vcf>


More information about the SDL mailing list