一个SDL的例子分析(二)- 一切从main开始

类别:编程语言 点击:0 评论:0 推荐:

由于Pig采用的是C的代码,我们从main开始分析,从SDL的调用过程基本如下步骤:
1.通过SDL_Init初始化SDL
2.通过SDL_SetVideoMode建立主Surface
3.通过SDL_GetKeyState获取键盘状态数组
4.开始消息循环
5.通过SDL_PollEvent获取消息,并处理直到完成

以下是main函数

/*----------------------------------------------------------
 main()
----------------------------------------------------------*/
int main(int argc, char* argv[])
{
 SDL_Surface *screen;
 GAMESTATE *gs;
 int i;
 int bpp = 0;
 int last_tick, start_time, end_time;
 int dashframe;
 float logic_fps = 20.0;
 int flags = SDL_DOUBLEBUF | SDL_HWSURFACE;

 // SDL视频初始化
 SDL_Init(SDL_INIT_VIDEO);

 // 将函数SDL_Quit压入退出处理程序
 atexit(SDL_Quit);

 // 设置中断句柄,设置在Ctrl+Break和程序中止的时候,调用breakhandler
 // 在breakhandler中重新设置了中断处理句柄,并将中断标志设为1
 signal(SIGTERM, breakhandler);
 signal(SIGINT, breakhandler);

 // 处理参数
 for(i = 1; i < argc; ++i)
 {
  if(strncmp(argv[i], "-s", 2) == 0)
   flags &= ~SDL_DOUBLEBUF;
  else if(strncmp(argv[i], "-f", 2) == 0)
   flags |= SDL_FULLSCREEN;
  else
   bpp = atoi(&argv[i][1]);
 }

 // 设置Video Mode的宽度和高度,由于bpp=0,所以使用当前的颜色数
 screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, flags);
 if(!screen)
 {
  fprintf(stderr, "Failed to open screen!\n");
  return 1;
 }

 // 设置窗口的标题和ICON的名称
 SDL_WM_SetCaption("Fixed Rate Pig", "Pig");

 // 隐藏鼠标
 SDL_ShowCursor(0);

 // 初始化游戏状态结构
 gs = init_all(screen);
 if(!gs)
  return 1;

 // 取得键盘状态,键盘状态是一个Uint8的数组,包含所有按键的当前状态
 // 使用例子if ( gs->keys[SDLK_RETURN] ) ... <RETURN> is pressed.
 gs->keys = SDL_GetKeyState(&i);

 // 初始化逻辑桢的相关信息
 gs->logic_frames = 0;
 gs->rendered_frames = 0;
 gs->pe->before_objects = before_objects;

 // 初始化Pig engine中的精灵和原图片
 pig_start(gs->pe, 0);
 gs->refresh_screen = gs->pe->pages;

 // 记录当前的时间
 start_time = last_tick = SDL_GetTicks();

 // 开始消息处理循环
 while(gs->running)
 {
  int tick;
  float frames, dt;
  SDL_Event ev;

  /* Handle input */
  // SDL_PollEvent有点类似PeekMessage
  while(SDL_PollEvent(&ev) > 0)
   // 处理键盘和鼠标事件
   handle_input(gs, &ev);

  // 空函数
  handle_keys(gs);

  // 判断程序是否被中断
  // 前面通过signal设置当Ctrl+Break或者程序结束的中断发生时,break_received要设置为1
  // 注意这里并没有直接中断消息循环,所以在处理完本次循环的操作以后才能真正退出
  if(break_received)
   gs->running = 0;

  /* Calculate time since last update */
  // 这里用来计算和上次消息循环间隔的时间,并通过显示桢数参数来确定需要显示的逻辑桢的ID
  tick = SDL_GetTicks();
  dt = (tick - last_tick) * 0.001;
  frames = dt * logic_fps;

  /* Run the game logic */
  // 显示逻辑桢
  pig_animate(gs->pe, frames);

  /*
   * Limit the dashboard frame rate to 15 fps
   * when there's no wobbling going on.
   *
   * The 'dashframe' deal is about keeping the
   * pages in sync on a double buffered display.
   */
  if(gs->lives_wobble || gs->score_wobble ||
    (gs->dashboard_time > 1.0/15.0))
  {
   dashframe = gs->pe->pages;
   gs->dashboard_time = 0;
  }
  if(dashframe)
  {
   --dashframe;
   dashboard(gs);
  }

  /* Update sprites */
  if(gs->refresh_screen)
  {
   --gs->refresh_screen;
   pig_refresh_all(gs->pe);
  }
  else
   pig_refresh(gs->pe);

  /* Make the new frame visible */
  pig_flip(gs->pe);

  /* Update statistics, timers and stuff */
  ++gs->rendered_frames;
  gs->lives_wobble_time += dt;
  gs->score_wobble_time += dt;
  gs->dashboard_time += dt;

  // 记录时间方便最后的计算
  last_tick = tick;

  // nice似乎是某种效果的标志,目前还不确定
  if(gs->nice)
   SDL_Delay(10);
 }

 /* Print some statistics */
 end_time = SDL_GetTicks();
 i = end_time - start_time;
 printf("          Total time running: %d ms\n", i);
 if(!i)
  i = 1;
 printf("Average rendering frame rate: %.2f fps\n",
   gs->rendered_frames * 1000.0 / i);
 printf("    Average logic frame rate: %.2f fps\n",
   gs->logic_frames * 1000.0 / i);

 // 释放Pig engine
 pig_close(gs->pe);
 return 0;
}

注意:虽然代码中设置了窗体的标题,却没有窗体生成的部分,难道SDL自动创建了窗体?还是隐藏在未展开的代码中?相信随着代码的不断展开应该会更清楚的。

本文地址:http://com.8s8s.com/it/it23264.htm