Game.cpp 7.9 KB

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