Game.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. 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. Platform::signalShutdown();
  94. finalize();
  95. _animationController->finalize();
  96. SAFE_DELETE(_animationController);
  97. _audioController->finalize();
  98. SAFE_DELETE(_audioController);
  99. _physicsController->finalize();
  100. SAFE_DELETE(_physicsController);
  101. SAFE_DELETE(_audioListener);
  102. RenderState::finalize();
  103. SAFE_DELETE(_properties);
  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. GP_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. Properties* Game::getConfig() const
  260. {
  261. if (_properties == NULL)
  262. const_cast<Game*>(this)->loadConfig();
  263. return _properties;
  264. }
  265. void Game::loadConfig()
  266. {
  267. if (_properties == NULL)
  268. {
  269. // Try to load custom config from file.
  270. // Check if file exists.
  271. FILE* file = FileSystem::openFile("game.config", "rb");
  272. if (file)
  273. {
  274. fclose(file);
  275. _properties = Properties::create("game.config");
  276. }
  277. else
  278. {
  279. _properties = new Properties();
  280. }
  281. // Load filesystem aliases
  282. Properties* aliases = _properties->getNamespace("aliases", true);
  283. if (aliases)
  284. FileSystem::loadResourceAliases(aliases);
  285. }
  286. }
  287. void Game::fireTimeEvents(long frameTime)
  288. {
  289. while (_timeEvents->size() > 0)
  290. {
  291. const TimeEvent* timeEvent = &_timeEvents->top();
  292. if (timeEvent->time > frameTime)
  293. {
  294. break;
  295. }
  296. timeEvent->listener->timeEvent(frameTime - timeEvent->time, timeEvent->cookie);
  297. _timeEvents->pop();
  298. }
  299. }
  300. Game::TimeEvent::TimeEvent(long time, TimeListener* timeListener, void* cookie)
  301. : time(time), listener(timeListener), cookie(cookie)
  302. {
  303. }
  304. bool Game::TimeEvent::operator<(const TimeEvent& v) const
  305. {
  306. // The first element of std::priority_queue is the greatest.
  307. return time > v.time;
  308. }
  309. }