【发布时间】:2012-10-11 02:26:51
【问题描述】:
我正在使用下面的代码结构 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 );
}
【问题讨论】:
-
我假设您的 int TICKS_PER_SECOND 是设置的帧速率。你为什么不尝试降低这个数字?
-
那是逻辑循环,它设置为 60,但“display_game”函数被尽可能频繁地调用,如果没有太多,则经常绘制超过 1000 fps,以便始终最大化处理器(在任务管理器上持续 98-99%)
标签: java timer logic frame-rate game-loop