Game.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 (!_initialized)
  132. {
  133. initialize();
  134. _initialized = true;
  135. }
  136. if (_state == Game::RUNNING)
  137. {
  138. // Update Time.
  139. static long lastFrameTime = Game::getGameTime();
  140. long frameTime = Game::getGameTime();
  141. long elapsedTime = (frameTime - lastFrameTime);
  142. lastFrameTime = frameTime;
  143. // Update the scheduled and running animations.
  144. _animationController->update(elapsedTime);
  145. // Fire time events to scheduled TimeListeners
  146. fireTimeEvents(frameTime);
  147. // Update the physics.
  148. _physicsController->update(elapsedTime);
  149. // Application Update.
  150. update(elapsedTime);
  151. // Audio Rendering.
  152. _audioController->update(elapsedTime);
  153. // Graphics Rendering.
  154. render(elapsedTime);
  155. // Update FPS.
  156. ++_frameCount;
  157. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  158. {
  159. _frameRate = _frameCount;
  160. _frameCount = 0;
  161. _frameLastFPS = Game::getGameTime();
  162. }
  163. }
  164. else
  165. {
  166. // Application Update.
  167. update(0);
  168. // Graphics Rendering.
  169. render(0);
  170. }
  171. }
  172. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  173. {
  174. GLbitfield bits = 0;
  175. if (flags & CLEAR_COLOR)
  176. {
  177. if (clearColor.x != _clearColor.x ||
  178. clearColor.y != _clearColor.y ||
  179. clearColor.z != _clearColor.z ||
  180. clearColor.w != _clearColor.w )
  181. {
  182. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  183. _clearColor.set(clearColor);
  184. }
  185. bits |= GL_COLOR_BUFFER_BIT;
  186. }
  187. if (flags & CLEAR_DEPTH)
  188. {
  189. if (clearDepth != _clearDepth)
  190. {
  191. glClearDepth(clearDepth);
  192. _clearDepth = clearDepth;
  193. }
  194. bits |= GL_DEPTH_BUFFER_BIT;
  195. // We need to explicitly call the static enableDepthWrite() method on StateBlock
  196. // to ensure depth writing is enabled before clearing the depth buffer (and to
  197. // update the global StateBlock render state to reflect this).
  198. RenderState::StateBlock::enableDepthWrite();
  199. }
  200. if (flags & CLEAR_STENCIL)
  201. {
  202. if (clearStencil != _clearStencil)
  203. {
  204. glClearStencil(clearStencil);
  205. _clearStencil = clearStencil;
  206. }
  207. bits |= GL_STENCIL_BUFFER_BIT;
  208. }
  209. glClear(bits);
  210. }
  211. void Game::menu()
  212. {
  213. }
  214. void Game::keyEvent(Keyboard::KeyEvent evt, int key)
  215. {
  216. }
  217. void Game::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  218. {
  219. }
  220. void Game::schedule(long timeOffset, TimeListener* timeListener, void* cookie)
  221. {
  222. assert(timeListener);
  223. TimeEvent timeEvent(getGameTime() + timeOffset, timeListener, cookie);
  224. _timeEvents.push(timeEvent);
  225. }
  226. bool Game::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  227. {
  228. return false;
  229. }
  230. void Game::updateOnce()
  231. {
  232. // Update Time.
  233. static long lastFrameTime = Game::getGameTime();
  234. long frameTime = Game::getGameTime();
  235. long elapsedTime = (frameTime - lastFrameTime);
  236. lastFrameTime = frameTime;
  237. // Update the internal controllers.
  238. _animationController->update(elapsedTime);
  239. _physicsController->update(elapsedTime);
  240. _audioController->update(elapsedTime);
  241. }
  242. void Game::fireTimeEvents(long frameTime)
  243. {
  244. while (!_timeEvents.empty())
  245. {
  246. TimeEvent* timeEvent = &_timeEvents.top();
  247. if (timeEvent->time > frameTime)
  248. {
  249. break;
  250. }
  251. timeEvent->listener->timeEvent(frameTime - timeEvent->time, timeEvent->cookie);
  252. _timeEvents.pop();
  253. }
  254. }
  255. Game::TimeEvent::TimeEvent(long time, TimeListener* timeListener, void* cookie)
  256. : time(time), listener(timeListener), cookie(cookie)
  257. {
  258. }
  259. bool Game::TimeEvent::operator<(const TimeEvent& v) const
  260. {
  261. // The first element of std::priority_queue is the greatest.
  262. return time > v.time;
  263. }
  264. }