Search code examples
animationspritegame-engineframe-rategame-loop

Animation synchronisation with FPS


at the moment I am programming a simple 2D Game Engine (Just because i want to do it, i know there are already lots of awesome ones)

But this just btw...

My actually Problem:

I got a normal game loop with methods like update() and draw(). Now I want to display a Sprite Animation (Just 3 Frames), but its logical, that i got more fps than 3 (about ~90.000), but i dont want the animation to be played back as fast as this. I want that in one second one play back is done. So now my question: Whats the best way to realize this? How can i translate these framerates together? I know i could do something like divide my actual FPS with the frames my animation got and take the result to know how many frames i have to wait till i can show the next frame, but i dont like that. So I need some input from you guys.

Thank you very much for reading.


Solution

  • Create a timer that fires every 1/30 of a second for 30 FPS performance and then do your drawing in that callback. The impl may need to adjust the callback time depending on how long it takes to draw and other issues. The main point is that your animation code should just need to figure out what "frame" of an animation it is on based on the time offset aka the number of times the display callback has been invoked. As long as the render time is done quickly, so that the render start at an even and smooth interval then the animation looks good.