Game.cpp 5.7 KB

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