My Gameloop (code below), limits the game updates to 60 ticks per second. However, it renders as fast as the device on which it is being run can handle.
I'm using a fixed time step in my game and I want to be able to limit my rendering to a specified amount (in my particular case, that would also be 60). However, I need to keep rendering calls uncoupled from game update calls.
I tried lumping the render calls in with the update calls but the problem with this is that they aren't uncoupled. This could potentially lead to issues if the device can't handle rendering 60 fps. Lets say it drops to 40fps, then the game updates will also drop to 40 ticks per second. Everything in my game is based on the game running at 60 ticks per second. So everything will run at the wrong speed. (using a variable time step isn't an option, my whole game is build around a fixed time step) So basically, if the device drops to 40 rendering calls per second, it should continue running game updates at 60 ticks per second so everything runs OK.
I am quietly confident that (virtually) every device on which my app runs will easily be able to handle 60 ticks (game updates) per second. I've tested this on a very old handset and it copes with 60 ticks no problem.
I would rather limit the rendering because as things stand it's rendering redundant frames and this must have a negative effect on battery life.
Would be grateful for suggestions on how I can limit the frames renders while keeping rendering and game updates separate.
Code
long nextGameTick = System.currentTimeMillis();
//loop counter
int loops;
//This is the amount of frames that we allow our app to skip before logic updating is affected (generally shouldn't happen)
final int maxFrameskip = 5;
//Game updates per second
final int ticksPerSecond = 60;
//Amount of time each update should take
final int skipTicks = (1000 / ticksPerSecond);
public void onDrawFrame(GL10 gl) {
//Set/Re-set loop back to 0 to start counting again
loops=0;
while(System.currentTimeMillis() > nextGameTick && loops < maxFrameskip){
SceneManager.getInstance().getCurrentScene().updateLogic();
nextGameTick+=skipTicks;
timeCorrection += (1000d/ticksPerSecond) % 1;
nextGameTick+=timeCorrection;
timeCorrection %=1;
loops++;
tics++;
}
render();
}
Please note I've already read Fix your timestep! and the similar Android Game Loops but these articles don't mention how to limit the rendering in a loop like the one I have adapted - just the game updates.
Any help would be appreciated