Hello world - Game Loop
// Ver.1 - Easy Loop
---------------------------------------------------
while( true )
{
processInput();
update();
render();
}
// Ver. 2 - Add Time and FPS
---------------------------------------------------
MS_PER_FRAME 16 //60 FPS == 16 millisecondi per frame
while( true )
{
double start = getCurrentTime();
processInput();
update();
render();
sleep( start + MS_PER_FRAME - getCurrentTime ());
}
// Ver. 3 - elapsedTime and Update
---------------------------------------------------
double lastTime = getCurrentTime();
while (true)
{
double current = getCurrentTime();
double elapsed = current - lastTime;
processInput();
update(elapsed);
render();
lastTime = current;
}
// Ver. 4 - Fix Update and Render
---------------------------------------------------
FPS 60
double lastTime = getCurrentTime();
bool redraw = true;
while (true)
{
double current = getCurrentTime();
double elapsed = current - lastTime;
processInput();
// Wait until 1/60th of a second has passed,
// then update everything
if (elapsed >= 1.0f / FPS)
{
update(elapsed);
redraw = true;
}
// Draw stuff if ready
if (redraw)
{
render();
redraw = false;
}
lastTime = current;
}
---------------------------------------------------
---------------------------------------------------
while( true )
{
processInput();
update();
render();
}
// Ver. 2 - Add Time and FPS
---------------------------------------------------
MS_PER_FRAME 16 //60 FPS == 16 millisecondi per frame
while( true )
{
double start = getCurrentTime();
processInput();
update();
render();
sleep( start + MS_PER_FRAME - getCurrentTime ());
}
// Ver. 3 - elapsedTime and Update
---------------------------------------------------
double lastTime = getCurrentTime();
while (true)
{
double current = getCurrentTime();
double elapsed = current - lastTime;
processInput();
update(elapsed);
render();
lastTime = current;
}
// Ver. 4 - Fix Update and Render
---------------------------------------------------
FPS 60
double lastTime = getCurrentTime();
bool redraw = true;
while (true)
{
double current = getCurrentTime();
double elapsed = current - lastTime;
processInput();
// Wait until 1/60th of a second has passed,
// then update everything
if (elapsed >= 1.0f / FPS)
{
update(elapsed);
redraw = true;
}
// Draw stuff if ready
if (redraw)
{
render();
redraw = false;
}
lastTime = current;
}
---------------------------------------------------
Commenti
Posta un commento