Game.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. : _initialized(false), _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. long Game::getGameTime()
  42. {
  43. return Platform::getAbsoluteTime() - _pausedTimeTotal;
  44. }
  45. void Game::setVsync(bool enable)
  46. {
  47. Platform::setVsync(enable);
  48. }
  49. bool Game::isVsync()
  50. {
  51. return Platform::isVsync();
  52. }
  53. int Game::run(int width, int height)
  54. {
  55. if (_state != UNINITIALIZED)
  56. return -1;
  57. _width = width;
  58. _height = height;
  59. // Start up game systems.
  60. if (!startup())
  61. {
  62. shutdown();
  63. return -2;
  64. }
  65. return 0;
  66. }
  67. bool Game::startup()
  68. {
  69. if (_state != UNINITIALIZED)
  70. return false;
  71. RenderState::initialize();
  72. _animationController = new AnimationController();
  73. _animationController->initialize();
  74. #if 0
  75. _audioController = new AudioController();
  76. _audioController->initialize();
  77. #endif
  78. _physicsController = new PhysicsController();
  79. _physicsController->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. SAFE_DELETE(_animationController);
  91. #if 0
  92. _audioController->finalize();
  93. SAFE_DELETE(_audioController);
  94. #endif
  95. _physicsController->finalize();
  96. SAFE_DELETE(_physicsController);
  97. RenderState::finalize();
  98. }
  99. _state = UNINITIALIZED;
  100. }
  101. void Game::pause()
  102. {
  103. if (_state == RUNNING)
  104. {
  105. _state = PAUSED;
  106. _pausedTimeLast = Platform::getAbsoluteTime();
  107. _animationController->pause();
  108. #if 0
  109. _audioController->pause();
  110. #endif
  111. _physicsController->pause();
  112. }
  113. }
  114. void Game::resume()
  115. {
  116. if (_state == PAUSED)
  117. {
  118. _state = RUNNING;
  119. _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
  120. _animationController->resume();
  121. #if 0
  122. _audioController->resume();
  123. #endif
  124. _physicsController->resume();
  125. }
  126. }
  127. void Game::exit()
  128. {
  129. shutdown();
  130. }
  131. void Game::frame()
  132. {
  133. if (_state != RUNNING)
  134. {
  135. return;
  136. }
  137. else
  138. {
  139. if (!_initialized)
  140. {
  141. initialize();
  142. _initialized = true;
  143. }
  144. }
  145. // Update Time.
  146. static long lastFrameTime = Game::getGameTime();
  147. long frameTime = Game::getGameTime();
  148. long elapsedTime = (frameTime - lastFrameTime);
  149. lastFrameTime = frameTime;
  150. // Update the scheduled and running animations.
  151. _animationController->update(elapsedTime);
  152. // Update the physics.
  153. _physicsController->update(elapsedTime);
  154. // Application Update.
  155. update(elapsedTime);
  156. // Audio Rendering.
  157. #if 0
  158. _audioController->update(elapsedTime);
  159. #endif
  160. // Graphics Rendering.
  161. render(elapsedTime);
  162. // Update FPS.
  163. ++_frameCount;
  164. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  165. {
  166. _frameRate = _frameCount;
  167. _frameCount = 0;
  168. _frameLastFPS = Game::getGameTime();
  169. }
  170. }
  171. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  172. {
  173. GLbitfield bits = 0;
  174. if (flags & CLEAR_COLOR)
  175. {
  176. if (clearColor.x != _clearColor.x ||
  177. clearColor.y != _clearColor.y ||
  178. clearColor.z != _clearColor.z ||
  179. clearColor.w != _clearColor.w )
  180. {
  181. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  182. _clearColor.set(clearColor);
  183. }
  184. bits |= GL_COLOR_BUFFER_BIT;
  185. }
  186. if (flags & CLEAR_DEPTH)
  187. {
  188. if (clearDepth != _clearDepth)
  189. {
  190. glClearDepth(clearDepth);
  191. _clearDepth = clearDepth;
  192. }
  193. bits |= GL_DEPTH_BUFFER_BIT;
  194. // We need to explicitly call the static enableDepthWrite() method on StateBlock
  195. // to ensure depth writing is enabled before clearing the depth buffer (and to
  196. // update the global StateBlock render state to reflect this).
  197. RenderState::StateBlock::enableDepthWrite();
  198. }
  199. if (flags & CLEAR_STENCIL)
  200. {
  201. if (clearStencil != _clearStencil)
  202. {
  203. glClearStencil(clearStencil);
  204. _clearStencil = clearStencil;
  205. }
  206. bits |= GL_STENCIL_BUFFER_BIT;
  207. }
  208. glClear(bits);
  209. }
  210. void Game::menu()
  211. {
  212. }
  213. void Game::keyEvent(Keyboard::KeyEvent evt, int key)
  214. {
  215. }
  216. void Game::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  217. {
  218. }
  219. }