Game.cpp 7.5 KB

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