一个游戏基本上是一个连续的循环,它完成逻辑动作并以一定的刷新率(书上以30帧/秒为例)在屏幕上绘制图像。

      针对书上所作出了一个简化的游戏循环结构

1、游戏构成

第一步:初始化

      游戏程序执行标准初始化操作,如内存分配、资源采集、从磁盘载入数据等等。

第二步:进入游戏循环

      代码运行到游戏主循环体内部。

第三步:获得玩家的输入信息

      游戏玩家的输入信息被处理或缓存,以备下一步人工智能和游戏逻辑使用。

第四步:执行人工智能和游戏逻辑

      游戏代码的主体,诸如执行人工智能、物理系统和一般的游戏逻辑,其结果用于渲染下一帧图像。

第五步:渲染下一帧图像

      游戏的输入和第四步中游戏人工智能和游戏逻辑执行的结果,被用来产生游戏的下一帧动火。这个图像通常放在不可见的缓存区内,因此玩家不会看到它逐渐被渲染的过程。随后该图像被迅速拷贝到显示存储器中并显示出来。

第六步:同步显示

      通常由于游戏复杂程序不同,游戏在计算机上运行的速度会不一。因此必须把游戏按照某个最大帧速率进行同步,并使用定时功能或等待函数来维持同步。

第七步:循环

      返回到游戏的入口并重新执行上述全部步骤。

第八步:关闭

      表示将退出主程序或游戏循环,并回到操作系统。然而,在用户进行结束之前,用户必须释放所有的资源并清理系统。

 

      其实在大多数情况下,游戏循环是一个含有大量状态的FSM(有限状态自动机)下面贴出书上的源码。

// defines for game loop states
// the game is initializing
// the game is in the menu mode
// the game is about to run
// the game is now running
// the game is going to restart
// the game is exiting
   8:  
// game globals
// start off in this state
// used to send errors back to OS
  12:  
  13:  
// main begins here
  15:  
void main()
  17: {
// implementation of main game loop
  19:  
while (game_state!=GAME_EXIT)
  21:     {
// what state is game loop in
switch(game_state)
  24:     {
// the game is initializing
  26:              {
// allocate all memory and resources
  28:             Init();
  29:  
// move to menu state
  31:             game_state = GAME_MENU;
break;
  33:  
// the game is in the menu mode
  35:               {
// call the main menu function and let it switch states
  37:               game_state = Menu();
  38:  
// note: we could force a RUN state here
break;
  41:  
// the game is about to run
  43:               {
// this state is optional, but usually used to
// set things up right before the game is run
// you might do a little more housekeeping here
  47:               Setup_For_Run();
  48:  
// switch to run state
  50:               game_state = GAME_RUN;
break;
  52:  
// the game is now running
  54:                {
// this section contains the entire game logic loop
// clear the display
  57:                Clear();
  58:  
// get the input
  60:                Get_Input();
  61:  
// perform logic and ai
  63:                Do_Logic();
// display the next frame of animation
  65:                Render_Frame();
  66:  
// synchronize the display
  68:                Wait();
  69:  
// the only way that state can be changed is
// thru user interaction in the
// input section or by maybe losing the game.
break;
  74:  
// the game is restarting
  76:                     {
// this section is a cleanup state used to
// fix up any loose ends before
// running again
  80:                     Fixup();
// switch states back to the menu
  82:                     game_state = GAME_MENU;
break;
  84:  
// the game is exiting
  86:                    {
// if the game is in this state then
// it's time to bail, kill everything
// and cross your fingers
  90:                    Release_And_Cleanup();
  91:  
// set the error word to whatever
  93:                    error = 0;
  94:  
// note: we don't have to switch states
// since we are already in this state
// on the next loop iteration the code
// will fall out of the main while and
// exit back to the OS
break;
 101:  
break;
// end switch
 104:  
// end while
// return error code to operating system
return(error);
 108:  
// end main
 110:  

相关文章:

  • 2022-01-20
  • 2021-10-30
  • 2021-12-25
  • 2022-02-07
  • 2021-09-18
猜你喜欢
  • 2021-12-11
  • 2021-11-23
  • 2022-01-03
  • 2021-06-18
  • 2021-08-07
  • 2021-11-14
相关资源
相似解决方案