Game.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. _timeEvents = new std::priority_queue<TimeEvent, std::vector<TimeEvent>, std::less<TimeEvent> >();
  21. }
  22. Game::Game(const Game& copy)
  23. {
  24. }
  25. Game::~Game()
  26. {
  27. // Do not call any virtual functions from the destructor.
  28. // Finalization is done from outside this class.
  29. delete _timeEvents;
  30. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  31. Ref::printLeaks();
  32. printMemoryLeaks();
  33. #endif
  34. }
  35. Game* Game::getInstance()
  36. {
  37. return __gameInstance;
  38. }
  39. long Game::getAbsoluteTime()
  40. {
  41. return Platform::getAbsoluteTime();
  42. }
  43. long Game::getGameTime()
  44. {
  45. return Platform::getAbsoluteTime() - _pausedTimeTotal;
  46. }
  47. void Game::setVsync(bool enable)
  48. {
  49. Platform::setVsync(enable);
  50. }
  51. bool Game::isVsync()
  52. {
  53. return Platform::isVsync();
  54. }
  55. int Game::run(int width, int height)
  56. {
  57. if (_state != UNINITIALIZED)
  58. return -1;
  59. if (width == -1)
  60. _width = Platform::getDisplayWidth();
  61. else
  62. _width = width;
  63. if (height == -1)
  64. _height = Platform::getDisplayHeight();
  65. else
  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. _state = RUNNING;
  87. return true;
  88. }
  89. void Game::shutdown()
  90. {
  91. // Call user finalization.
  92. if (_state != UNINITIALIZED)
  93. {
  94. finalize();
  95. _animationController->finalize();
  96. SAFE_DELETE(_animationController);
  97. _audioController->finalize();
  98. SAFE_DELETE(_audioController);
  99. _physicsController->finalize();
  100. SAFE_DELETE(_physicsController);
  101. SAFE_DELETE(_audioListener);
  102. RenderState::finalize();
  103. }
  104. _state = UNINITIALIZED;
  105. }
  106. void Game::pause()
  107. {
  108. if (_state == RUNNING)
  109. {
  110. _state = PAUSED;
  111. _pausedTimeLast = Platform::getAbsoluteTime();
  112. _animationController->pause();
  113. _audioController->pause();
  114. _physicsController->pause();
  115. }
  116. }
  117. void Game::resume()
  118. {
  119. if (_state == PAUSED)
  120. {
  121. _state = RUNNING;
  122. _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
  123. _animationController->resume();
  124. _audioController->resume();
  125. _physicsController->resume();
  126. }
  127. }
  128. void Game::exit()
  129. {
  130. shutdown();
  131. }
  132. void Game::frame()
  133. {
  134. if (!_initialized)
  135. {
  136. initialize();
  137. _initialized = true;
  138. }
  139. if (_state == Game::RUNNING)
  140. {
  141. // Update Time.
  142. static long lastFrameTime = Game::getGameTime();
  143. long frameTime = Game::getGameTime();
  144. long elapsedTime = (frameTime - lastFrameTime);
  145. lastFrameTime = frameTime;
  146. // Update the scheduled and running animations.
  147. _animationController->update(elapsedTime);
  148. // Fire time events to scheduled TimeListeners
  149. fireTimeEvents(frameTime);
  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. else
  168. {
  169. // Application Update.
  170. update(0);
  171. // Graphics Rendering.
  172. render(0);
  173. }
  174. }
  175. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  176. {
  177. GLbitfield bits = 0;
  178. if (flags & CLEAR_COLOR)
  179. {
  180. if (clearColor.x != _clearColor.x ||
  181. clearColor.y != _clearColor.y ||
  182. clearColor.z != _clearColor.z ||
  183. clearColor.w != _clearColor.w )
  184. {
  185. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  186. _clearColor.set(clearColor);
  187. }
  188. bits |= GL_COLOR_BUFFER_BIT;
  189. }
  190. if (flags & CLEAR_DEPTH)
  191. {
  192. if (clearDepth != _clearDepth)
  193. {
  194. glClearDepth(clearDepth);
  195. _clearDepth = clearDepth;
  196. }
  197. bits |= GL_DEPTH_BUFFER_BIT;
  198. // We need to explicitly call the static enableDepthWrite() method on StateBlock
  199. // to ensure depth writing is enabled before clearing the depth buffer (and to
  200. // update the global StateBlock render state to reflect this).
  201. RenderState::StateBlock::enableDepthWrite();
  202. }
  203. if (flags & CLEAR_STENCIL)
  204. {
  205. if (clearStencil != _clearStencil)
  206. {
  207. glClearStencil(clearStencil);
  208. _clearStencil = clearStencil;
  209. }
  210. bits |= GL_STENCIL_BUFFER_BIT;
  211. }
  212. glClear(bits);
  213. }
  214. AudioListener* Game::getAudioListener()
  215. {
  216. if (_audioListener == NULL)
  217. {
  218. _audioListener = new AudioListener();
  219. }
  220. return _audioListener;
  221. }
  222. void Game::menu()
  223. {
  224. }
  225. void Game::keyEvent(Keyboard::KeyEvent evt, int key)
  226. {
  227. }
  228. void Game::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  229. {
  230. }
  231. void Game::schedule(long timeOffset, TimeListener* timeListener, void* cookie)
  232. {
  233. assert(timeListener);
  234. TimeEvent timeEvent(getGameTime() + timeOffset, timeListener, cookie);
  235. _timeEvents->push(timeEvent);
  236. }
  237. bool Game::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  238. {
  239. return false;
  240. }
  241. void Game::updateOnce()
  242. {
  243. // Update Time.
  244. static long lastFrameTime = Game::getGameTime();
  245. long frameTime = Game::getGameTime();
  246. long elapsedTime = (frameTime - lastFrameTime);
  247. lastFrameTime = frameTime;
  248. // Update the internal controllers.
  249. _animationController->update(elapsedTime);
  250. _physicsController->update(elapsedTime);
  251. _audioController->update(elapsedTime);
  252. }
  253. void Game::fireTimeEvents(long frameTime)
  254. {
  255. while (_timeEvents->size() > 0)
  256. {
  257. const TimeEvent* timeEvent = &_timeEvents->top();
  258. if (timeEvent->time > frameTime)
  259. {
  260. break;
  261. }
  262. timeEvent->listener->timeEvent(frameTime - timeEvent->time, timeEvent->cookie);
  263. _timeEvents->pop();
  264. }
  265. }
  266. Game::TimeEvent::TimeEvent(long time, TimeListener* timeListener, void* cookie)
  267. : time(time), listener(timeListener), cookie(cookie)
  268. {
  269. }
  270. bool Game::TimeEvent::operator<(const TimeEvent& v) const
  271. {
  272. // The first element of std::priority_queue is the greatest.
  273. return time > v.time;
  274. }
  275. }