Game.cpp 9.2 KB

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