Game.cpp 7.9 KB

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