Game.cpp 5.5 KB

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