Game is a real-time application.
In the section, I use some image and sample code to explain how to develop game loop.
Background
First, game status need to update in game world.
Secound, game allow interaction with player.
And last, the result need to dislpay for players, whether screen, sound or other outputs.
Game status and player input can consider as a behavior.
So game is as a combination of update and render.
Game Loop
while(!GameEnd)
{
Update();
Render();
}
void Loader()
{
thread t1 = new thread(&Update,1000/frequency);
t1.Start();
thread t2 = new thread(&Render,1000/60);
t2.Start();
}
void Update()
{
// do update
}
void Render()
{
// do render
}
long lastTime = getTimeTick();
while(!GameEnd)
{
if( (getTimeTick() - lastTime) > (1000/frequency) )
{
Update();
lastTime = getTimeTick();
}
Render();
}
First loop, update and render everytime.
When game logic is too complex, it may reduce render's performance.
Secound loop, using multi-thread to update and render.
Last loop, using a timer to record last update time.
沒有留言:
張貼留言
Hello