home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / a7xpg0_11.zip / a7xpg / src / abagames / util / sdl / MainLoop.d < prev    next >
Text File  |  2003-09-20  |  3KB  |  116 lines

  1. /*
  2.  * $Id: MainLoop.d,v 1.1.1.1 2003/09/19 14:55:49 kenta Exp $
  3.  *
  4.  * Copyright 2003 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.sdl.MainLoop;
  7.  
  8. import string;
  9. import c.stdlib;
  10. import SDL;
  11. import abagames.util.Logger;
  12. import abagames.util.Rand;
  13. import abagames.util.GameManager;
  14. import abagames.util.PrefManager;
  15. import abagames.util.sdl.Screen;
  16. import abagames.util.sdl.Input;
  17. import abagames.util.sdl.Sound;
  18. import abagames.util.sdl.SDLInitFailedException;
  19.  
  20. /**
  21.  * SDL main loop.
  22.  */
  23. public class MainLoop {
  24.  public:
  25.   const int INTERVAL_BASE = 16;
  26.   int interval = INTERVAL_BASE;
  27.   int accframe = 0;
  28.   int maxSkipFrame = 5;
  29.   SDL_Event event;
  30.  
  31.  private:
  32.   Screen screen;
  33.   Input input;
  34.   GameManager gameManager;
  35.   PrefManager prefManager;
  36.  
  37.   public this(Screen screen, Input input,
  38.           GameManager gameManager, PrefManager prefManager) {
  39.     this.screen = screen;
  40.     this.input = input;
  41.     gameManager.setMainLoop(this);
  42.     gameManager.setUIs(screen, input);
  43.     gameManager.setPrefManager(prefManager);
  44.     this.gameManager = gameManager;
  45.     this.prefManager = prefManager;
  46.   }
  47.  
  48.   // Initialize and load preference.
  49.   private void initFirst() {
  50.     prefManager.load();
  51.     try {
  52.       Sound.init();
  53.     } catch (SDLInitFailedException e) {
  54.       Logger.error(e);
  55.     }
  56.     gameManager.init();
  57.   }
  58.  
  59.   // Quit and save preference.
  60.   private void quitLast() {
  61.     gameManager.close();
  62.     Sound.close();
  63.     prefManager.save();
  64.     screen.closeSDL();
  65.     SDL_Quit();
  66.   }
  67.  
  68.   public void loop() {
  69.     int done = 0;
  70.     long prvTickCount = 0;
  71.     int i;
  72.     long nowTick;
  73.     int frame;
  74.     
  75.     try {
  76.       screen.initSDL();
  77.     } catch (SDLInitFailedException e) {
  78.       Logger.error(e);
  79.       exit(EXIT_FAILURE);
  80.     }
  81.     initFirst();
  82.     gameManager.start();
  83.  
  84.     while (!done) {
  85.       SDL_PollEvent(&event);
  86.       input.handleEvent(&event);
  87.       if (input.keys[SDLK_ESCAPE] == SDL_PRESSED || event.type == SDL_QUIT)
  88.     done = 1;
  89.  
  90.       nowTick = SDL_GetTicks();
  91.       frame = (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.