Game.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Game.cpp
  3. */
  4. #include "Base.h"
  5. #include "Game.h"
  6. #include "Platform.h"
  7. // Extern global variables
  8. GLenum __gl_error_code = GL_NO_ERROR;
  9. namespace gameplay
  10. {
  11. static Game* __gameInstance = NULL;
  12. long Game::_pausedTimeLast = 0L;
  13. long Game::_pausedTimeTotal = 0L;
  14. Game::Game()
  15. : _state(UNINITIALIZED),
  16. _frameLastFPS(0), _frameCount(0), _frameRate(0),
  17. _clearColor(Vector4::zero()), _clearDepth(1.0f), _clearStencil(0)
  18. {
  19. assert(__gameInstance == NULL);
  20. __gameInstance = this;
  21. }
  22. Game::Game(const Game& copy)
  23. {
  24. }
  25. Game::~Game()
  26. {
  27. // Do not call any virtual functions from the destructor.
  28. // Finalization is done from outside this class.
  29. }
  30. Game* Game::getInstance()
  31. {
  32. return __gameInstance;
  33. }
  34. long Game::getAbsoluteTime()
  35. {
  36. return Platform::getAbsoluteTime();
  37. }
  38. bool Game::isVsync()
  39. {
  40. return Platform::isVsync();
  41. }
  42. void Game::setVsync(bool enable)
  43. {
  44. Platform::setVsync(enable);
  45. }
  46. long Game::getGameTime()
  47. {
  48. return (Platform::getAbsoluteTime() - _pausedTimeTotal);
  49. }
  50. Game::State Game::getState() const
  51. {
  52. return _state;
  53. }
  54. unsigned int Game::getFrameRate() const
  55. {
  56. return _frameRate;
  57. }
  58. int Game::run(int width, int height)
  59. {
  60. if (_state != UNINITIALIZED)
  61. return -1;
  62. _width = width;
  63. _height = height;
  64. // Start up game systems.
  65. if (!startup())
  66. {
  67. shutdown();
  68. return -2;
  69. }
  70. return 0;
  71. }
  72. bool Game::startup()
  73. {
  74. if (_state != UNINITIALIZED)
  75. return false;
  76. _animationController.initialize();
  77. _audioController.initialize();
  78. // Call user initialization.
  79. initialize();
  80. _state = RUNNING;
  81. return true;
  82. }
  83. void Game::shutdown()
  84. {
  85. // Call user finalization.
  86. if (_state != UNINITIALIZED)
  87. {
  88. finalize();
  89. _animationController.finalize();
  90. _audioController.finalize();
  91. }
  92. _state = UNINITIALIZED;
  93. }
  94. void Game::pause()
  95. {
  96. if (_state == RUNNING)
  97. {
  98. _state = PAUSED;
  99. _pausedTimeLast = Platform::getAbsoluteTime();
  100. _animationController.pause();
  101. _audioController.pause();
  102. }
  103. }
  104. void Game::resume()
  105. {
  106. if (_state == PAUSED)
  107. {
  108. _state = RUNNING;
  109. _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
  110. _animationController.resume();
  111. _audioController.resume();
  112. }
  113. }
  114. void Game::exit()
  115. {
  116. shutdown();
  117. }
  118. void Game::frame()
  119. {
  120. if (_state != RUNNING)
  121. return;
  122. // Update Time.
  123. static long lastFrameTime = Game::getGameTime();
  124. long frameTime = Game::getGameTime();
  125. long elapsedTime = (frameTime - lastFrameTime);
  126. lastFrameTime = frameTime;
  127. // Update the schedule and running animations.
  128. _animationController.update(elapsedTime);
  129. // Application Update.
  130. update(elapsedTime);
  131. // Audio Rendering.
  132. _audioController.update(elapsedTime);
  133. // Graphics Rendering.
  134. render(elapsedTime);
  135. // Update FPS.
  136. ++_frameCount;
  137. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  138. {
  139. _frameRate = _frameCount;
  140. _frameCount = 0;
  141. _frameLastFPS = Game::getGameTime();
  142. }
  143. }
  144. unsigned int Game::getWidth() const
  145. {
  146. return _width;
  147. }
  148. unsigned int Game::getHeight() const
  149. {
  150. return _height;
  151. }
  152. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  153. {
  154. GLbitfield bits = 0;
  155. if (flags & CLEAR_COLOR)
  156. {
  157. if (clearColor.x != _clearColor.x ||
  158. clearColor.y != _clearColor.y ||
  159. clearColor.z != _clearColor.z ||
  160. clearColor.w != _clearColor.w )
  161. {
  162. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  163. _clearColor.set(clearColor);
  164. }
  165. bits |= GL_COLOR_BUFFER_BIT;
  166. }
  167. if (flags & CLEAR_DEPTH)
  168. {
  169. if (clearDepth != _clearDepth)
  170. {
  171. glClearDepth(clearDepth);
  172. _clearDepth = clearDepth;
  173. }
  174. bits |= GL_DEPTH_BUFFER_BIT;
  175. }
  176. if (flags & CLEAR_STENCIL)
  177. {
  178. if (clearStencil != _clearStencil)
  179. {
  180. glClearStencil(clearStencil);
  181. _clearStencil = clearStencil;
  182. }
  183. bits |= GL_STENCIL_BUFFER_BIT;
  184. }
  185. glClear(bits);
  186. }
  187. AnimationController* Game::getAnimationController()
  188. {
  189. return &_animationController;
  190. }
  191. const AudioController& Game::getAudioController() const
  192. {
  193. return _audioController;
  194. }
  195. void Game::menu()
  196. {
  197. }
  198. void Game::keyChar(char key)
  199. {
  200. }
  201. void Game::keyPress(int key, int keyEvent)
  202. {
  203. }
  204. void Game::touch(int x, int y, int touchEvent)
  205. {
  206. }
  207. }