home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 August / GSSH0804.iso / Action / TumikiFighters / tf0_2.exe / tf / src / abagames / util / sdl / mainloop.d < prev    next >
Text File  |  2004-05-15  |  3KB  |  116 lines

  1. /*
  2.  * $Id: mainloop.d,v 1.2 2004/05/14 14:35:39 kenta Exp $
  3.  *
  4.  * Copyright 2003 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.sdl.mainloop;
  7.  
  8. private import std.string;
  9. private import SDL;
  10. private import abagames.util.logger;
  11. private import abagames.util.rand;
  12. private import abagames.util.prefmanager;
  13. private import abagames.util.sdl.gamemanager;
  14. private import abagames.util.sdl.screen;
  15. private import abagames.util.sdl.input;
  16. private import abagames.util.sdl.sound;
  17. private import abagames.util.sdl.sdlexception;
  18.  
  19. /**
  20.  * SDL main loop.
  21.  */
  22. public class MainLoop {
  23.  public:
  24.   const int INTERVAL_BASE = 16;
  25.   int interval = INTERVAL_BASE;
  26.   int accframe = 0;
  27.   int maxSkipFrame = 5;
  28.   SDL_Event event;
  29.  
  30.  private:
  31.   Screen screen;
  32.   Input input;
  33.   GameManager gameManager;
  34.   PrefManager prefManager;
  35.  
  36.   public this(Screen screen, Input input,
  37.           GameManager gameManager, PrefManager prefManager) {
  38.     this.screen = screen;
  39.     this.input = input;
  40.     gameManager.setMainLoop(this);
  41.     gameManager.setUIs(screen, input);
  42.     gameManager.setPrefManager(prefManager);
  43.     this.gameManager = gameManager;
  44.     this.prefManager = prefManager;
  45.   }
  46.  
  47.   // Initialize and load preference.
  48.   private void initFirst() {
  49.     prefManager.load();
  50.     try {
  51.       Sound.init();
  52.     } catch (SDLInitFailedException e) {
  53.       Logger.error(e);
  54.     }
  55.     gameManager.init();
  56.   }
  57.  
  58.   // Quit and save preference.
  59.   private void quitLast() {
  60.     gameManager.close();
  61.     Sound.close();
  62.     prefManager.save();
  63.     screen.closeSDL();
  64.     SDL_Quit();
  65.   }
  66.  
  67.   private bool done;
  68.  
  69.   public void breakLoop() {
  70.     done = true;
  71.   }
  72.  
  73.   public void loop() {
  74.     done = false;
  75.     long prvTickCount = 0;
  76.     int i;
  77.     long nowTick;
  78.     int frame;
  79.     
  80.     screen.initSDL();
  81.     initFirst();
  82.     gameManager.start();
  83.  
  84.     while (!done) {
  85.       if (SDL_PollEvent(&event) == 0)
  86.     event.type = SDL_USEREVENT;
  87.       input.handleEvent(&event);
  88.       if (event.type == SDL_QUIT)
  89.     breakLoop();
  90.       nowTick = SDL_GetTicks();
  91.       frame = cast(int) (nowTick-prvTickCount) / interval;
  92.       if (frame <= 0) {
  93.     frame = 1;
  94.     SDL_Delay(prvTickCount+interval-nowTick);
  95.     if (accframe) {
  96.       prvTickCount = SDL_GetTicks();
  97.     } else {
  98.       prvTickCount += interval;
  99.     }
  100.       } else if (frame > maxSkipFrame) {
  101.     frame = maxSkipFrame;
  102.     prvTickCount = nowTick;
  103.       } else {
  104.     prvTickCount += frame * interval;
  105.       }
  106.       for (i = 0; i < frame; i++) {
  107.     gameManager.move();
  108.       }
  109.       screen.clear();
  110.       gameManager.draw();
  111.       screen.flip();
  112.     }
  113.     quitLast();
  114.   }
  115. }
  116.