我从这里http://www.koonsolo.com/news/dewitters-gameloop/使用下面的代码结构 来设置一个游戏循环,该循环基于设置的 fps 进行处理,但尽可能渲染/绘制。如何在绘图 fps 上实施上限,以免耗尽所有处理能力/电池寿命。或将其限制为垂直同步。
const int TICKS_PER_SECOND = 60;
const int SKIP_TICKS = 1000000000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;
DWORD next_game_tick = GetTickCount();
int loops;
float interpolation;
bool game_is_running = true;
while( game_is_running ) {
loops = 0;
while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
update_game();
next_game_tick += SKIP_TICKS;
loops++;
}
interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
/ float( SKIP_TICKS );
display_game( interpolation );
}