Android game loop, how to control speed and frame rate
Asked Answered
P

1

19

I've written a game for Android, and I've tested it on the Dev Phone 1. It works perfectly, the speed is just right. However, I'm sure phone CPU's are getting faster. They may already be faster than the dev phone.

How do I make sure that my game runs at the exact same speed no matter what the device or how fast it runs? Do you know of any techniques? Should I check some kind of timer at the top of the loop each time?

I guess I'm referring to frame rate - but mostly the speed at which my game runs through the main game loop.

Any theory or experience would be great! Thank you.

Pisci answered 15/2, 2010 at 9:56 Comment(1)
Just something to watch out for: for games, sometimes newer phones are slower. I have an OpenGL game that I'm working on that does 60FPS on a G1, but only 30-40 FPS on a Droid. The renderer is different on the Droid and it has a higher resolution screen as well, which hurts performance.Tilburg
S
15

If you are targeting certain frame rate, the basic idea is that you should have a timer or thread that executes your game's tick method at desired intervals. With timers the implementation is pretty trivial: just schedule a timer to execute at regular intervals. When using threads you have to put the thread to sleep between consecutive ticks if it runs faster than the desired frame rate.

However, this alone doesn't lead to the best possible results as the interval can vary a bit between the frames. There is a very good article on this issue: http://gafferongames.com/game-physics/fix-your-timestep/.

Also, there are already slower and faster Android phones than the dev phone 1. So you have to prepare for the both cases if you are targeting all Android devices. If your game is not that CPU heavy, it might be that you can achieve the desired frame rate on all the devices. But if you don't limit the frame rate, your game will be too fast on the faster Android phones.

Smokedry answered 15/2, 2010 at 11:6 Comment(2)
Thank you, this article is a great find and an easy implementation over what I've already built.Pisci
did you implement "interpolation" part also ?Umber

© 2022 - 2024 — McMap. All rights reserved.