http://www.gameprogblog.com/generic-game-loop/
Java
boolean onLoop( Game game, GameState state, GameInput input, Graphics2D gr ) {
long nanosElapsed = state.tick();
updateTime += nanosElapsed;
int updateCount = 0;
while (updateTime >= updateRate && updateCount < maxUpdates) {
game.input( input );
input.clear();
if (!game.isPlaying()) {
return false;
}
state.update();
game.update( state );
if (!game.isPlaying()) {
return false;
}
updateCount++;
updateTime -= updateRate;
}
drawTime += nanosElapsed;
int drawCount = 0;
if (drawTime >= drawRate || updateCount > 0) {
state.interpolate = getStateInterpolation();
state.forward = state.interpolate * state.seconds;
state.backward = state.forward - state.seconds;
state.draw();
game.draw( state, gr );
drawCount++;
drawTime -= (drawRate == 0 ? drawTime : drawRate);
}
if (sleep && drawCount == 0 && updateCount == 0) {
long actualTime = updateTime + state.getElapsedSinceTick();
long sleep = (updateRate - actualTime) / 1000000L;
if (sleep > 1) {
try {
Thread.sleep( sleep - 1 );
} catch (Exception e) { }
}
}
return (drawCount > 0);
}
I am a beginner in game programming. I wanted to know when using fixed time step why do we have different update rates and draw rates. I have seen this in more than just the above code and if i plan to make a 30 fps game then what is a reasonable draw rate to have to match 30 fps update rate?. Can you explain in understandable terms? I appreciate your help.