下面给出上述算法的粗略的例子程序:
(引用 Grorge Foot 所著的
Allegro Vivce 9.4.3 节的例子)
volatile int target_cycle;
void target_incrementor()
{
target_cycle++;
}
END_OF_FUNCTION (target_incrementor);
int actual_cycle;
int end_game;
void game_loop()
{
LOCK_VARIABLE (target_cycle);
LOCK_FUNCTION (target_incrementor);
install_int_ex (target_incrementor,BPS_TO_TIMER(cycles_per_sec));
end_game = 0;
actual_cycle = target_cycle = 0; /* 初始化 */
do {
draw_one_frame(); /* 画一帧图 */
while (target_cycle > actual_cycle) /*如果实际的要做的落后*/
do_one_game_cycle(); /* 进行游戏循环 */
} while (!end_game);
}
|
| draw_one_frame 画一帧画, 如果需要还要进行垂直回扫同步. |
|
do_one_game_cycle 为完成一个游戏循环, 并累加 `actual_cycle'. |
|
cycles_per_sec 变量保存每秒钟进行的目标游戏循环的数量. |
|
如果你用过Allegro. 其他的应该没什么问题, 关于
程序中涉及的Allegro函数, 请自行查阅Allegro 手册 |
本文参考 George Foot 所著的
Allegro Vivace
Section 9.4.3 Regulating game speed