Game.cpp 8.0 KB

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