-
Really quick question
Ok, I am following the book "Killer Game Programming" by O'Reilly. This code is from his book, I just have a few questions on what it means, because i get errors when i try and compile. The errors are that the variable period doesn't exist, which it doesn't. Here is the code:
Code:
public void run( )
/* Repeatedly: update, render, sleep so loop takes close
to period ms */
{
long beforeTime, timeDiff, sleepTime;
beforeTime = System.currentTimeMillis( );
running = true;
while(running) {
gameUpdate( );
gameRender( );
paintScreen( );
timeDiff = System.currentTimeMillis( ) - beforeTime;
sleepTime = [COLOR="Blue"]period[/COLOR] - timeDiff; // time left in this loop
if (sleepTime <= 0) // update/render took longer than period
sleepTime = 5; // sleep a bit anyway
try {
Thread.sleep(sleepTime); // in ms
}
catch(InterruptedException ex){}
beforeTime = System.currentTimeMillis( );
}
System.exit(0);
} // end of run( )
"A popular measure of how fast an animation progresses is frames per second (FPS). For GamePanel, a frame corresponds to a single pass through the update-render-sleep loop inside run( ). Therefore, the desired 100 FPS imply that each iteration of the loop should take 1000/100 == 10 ms. This iteration time is stored in the period variable in GamePanel."
Above is the paragraph in the book describing period. So is period simply a variable that you can put whatever time you want in it, or does it need to be something calculated?
-
period is a variable indeed, and it's the number of milliseconds you expect one frame to take. So for example if you want 25 FPS, your period should be (1000/25)=40. You don't need to calculate it in code.
-
ok that's what i was thinking, thanks for helping out so fast =]