Game.cpp 7.9 KB

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