Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include "Base.h"
  2. #include "Game.h"
  3. #include "Platform.h"
  4. #include "RenderState.h"
  5. #include "FileSystem.h"
  6. #include "FrameBuffer.h"
  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. double Game::_pausedTimeLast = 0.0;
  13. double Game::_pausedTimeTotal = 0.0;
  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. double Game::getAbsoluteTime()
  43. {
  44. return Platform::getAbsoluteTime();
  45. }
  46. double Game::getGameTime()
  47. {
  48. return Platform::getAbsoluteTime() - _pausedTimeTotal;
  49. }
  50. void Game::setVsync(bool enable)
  51. {
  52. Platform::setVsync(enable);
  53. }
  54. bool Game::hasMouse()
  55. {
  56. return Platform::hasMouse();
  57. }
  58. bool Game::isMouseCaptured()
  59. {
  60. return Platform::isMouseCaptured();
  61. }
  62. void Game::setMouseCapture(bool captured)
  63. {
  64. Platform::setMouseCapture(captured);
  65. }
  66. void Game::setCursorVisible(bool visible)
  67. {
  68. Platform::setCursorVisible(visible);
  69. }
  70. bool Game::isCursorVisible()
  71. {
  72. return Platform::isCursorVisible();
  73. }
  74. bool Game::isVsync()
  75. {
  76. return Platform::isVsync();
  77. }
  78. int Game::run()
  79. {
  80. if (_state != UNINITIALIZED)
  81. return -1;
  82. loadConfig();
  83. _width = Platform::getDisplayWidth();
  84. _height = Platform::getDisplayHeight();
  85. // Start up game systems.
  86. if (!startup())
  87. {
  88. shutdown();
  89. return -2;
  90. }
  91. return 0;
  92. }
  93. bool Game::startup()
  94. {
  95. if (_state != UNINITIALIZED)
  96. return false;
  97. setViewport(Rectangle(0.0f, 0.0f, (float)_width, (float)_height));
  98. RenderState::initialize();
  99. FrameBuffer::initialize();
  100. _animationController = new AnimationController();
  101. _animationController->initialize();
  102. _audioController = new AudioController();
  103. _audioController->initialize();
  104. _physicsController = new PhysicsController();
  105. _physicsController->initialize();
  106. loadGamepads();
  107. _state = RUNNING;
  108. return true;
  109. }
  110. void Game::shutdown()
  111. {
  112. // Call user finalization.
  113. if (_state != UNINITIALIZED)
  114. {
  115. GP_ASSERT(_animationController);
  116. GP_ASSERT(_audioController);
  117. GP_ASSERT(_physicsController);
  118. Platform::signalShutdown();
  119. finalize();
  120. for (std::vector<Gamepad*>::iterator itr = _gamepads.begin(); itr != _gamepads.end(); itr++)
  121. {
  122. SAFE_DELETE((*itr));
  123. }
  124. _gamepads.clear();
  125. _animationController->finalize();
  126. SAFE_DELETE(_animationController);
  127. _audioController->finalize();
  128. SAFE_DELETE(_audioController);
  129. _physicsController->finalize();
  130. SAFE_DELETE(_physicsController);
  131. SAFE_DELETE(_audioListener);
  132. RenderState::finalize();
  133. SAFE_DELETE(_properties);
  134. _state = UNINITIALIZED;
  135. }
  136. }
  137. void Game::pause()
  138. {
  139. if (_state == RUNNING)
  140. {
  141. GP_ASSERT(_animationController);
  142. GP_ASSERT(_audioController);
  143. GP_ASSERT(_physicsController);
  144. _state = PAUSED;
  145. _pausedTimeLast = Platform::getAbsoluteTime();
  146. _animationController->pause();
  147. _audioController->pause();
  148. _physicsController->pause();
  149. }
  150. }
  151. void Game::resume()
  152. {
  153. if (_state == PAUSED)
  154. {
  155. GP_ASSERT(_animationController);
  156. GP_ASSERT(_audioController);
  157. GP_ASSERT(_physicsController);
  158. _state = RUNNING;
  159. _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
  160. _animationController->resume();
  161. _audioController->resume();
  162. _physicsController->resume();
  163. }
  164. }
  165. void Game::exit()
  166. {
  167. shutdown();
  168. }
  169. void Game::frame()
  170. {
  171. if (!_initialized)
  172. {
  173. initialize();
  174. _initialized = true;
  175. }
  176. if (_state == Game::RUNNING)
  177. {
  178. GP_ASSERT(_animationController);
  179. GP_ASSERT(_audioController);
  180. GP_ASSERT(_physicsController);
  181. // Update Time.
  182. static double lastFrameTime = Game::getGameTime();
  183. double frameTime = getGameTime();
  184. float elapsedTime = (frameTime - lastFrameTime);
  185. lastFrameTime = frameTime;
  186. // Update the scheduled and running animations.
  187. _animationController->update(elapsedTime);
  188. // Fire time events to scheduled TimeListeners
  189. fireTimeEvents(frameTime);
  190. // Update the physics.
  191. _physicsController->update(elapsedTime);
  192. // Application Update.
  193. update(elapsedTime);
  194. // Audio Rendering.
  195. _audioController->update(elapsedTime);
  196. // Graphics Rendering.
  197. render(elapsedTime);
  198. // Update FPS.
  199. ++_frameCount;
  200. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  201. {
  202. _frameRate = _frameCount;
  203. _frameCount = 0;
  204. _frameLastFPS = getGameTime();
  205. }
  206. }
  207. else
  208. {
  209. // Application Update.
  210. update(0);
  211. // Graphics Rendering.
  212. render(0);
  213. }
  214. }
  215. void Game::updateOnce()
  216. {
  217. GP_ASSERT(_animationController);
  218. GP_ASSERT(_audioController);
  219. GP_ASSERT(_physicsController);
  220. // Update Time.
  221. static double lastFrameTime = getGameTime();
  222. double frameTime = getGameTime();
  223. float elapsedTime = (frameTime - lastFrameTime);
  224. lastFrameTime = frameTime;
  225. // Update the internal controllers.
  226. _animationController->update(elapsedTime);
  227. _physicsController->update(elapsedTime);
  228. _audioController->update(elapsedTime);
  229. }
  230. void Game::setViewport(const Rectangle& viewport)
  231. {
  232. _viewport = viewport;
  233. glViewport((GLuint)viewport.x, (GLuint)viewport.y, (GLuint)viewport.width, (GLuint)viewport.height);
  234. }
  235. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  236. {
  237. GLbitfield bits = 0;
  238. if (flags & CLEAR_COLOR)
  239. {
  240. if (clearColor.x != _clearColor.x ||
  241. clearColor.y != _clearColor.y ||
  242. clearColor.z != _clearColor.z ||
  243. clearColor.w != _clearColor.w )
  244. {
  245. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  246. _clearColor.set(clearColor);
  247. }
  248. bits |= GL_COLOR_BUFFER_BIT;
  249. }
  250. if (flags & CLEAR_DEPTH)
  251. {
  252. if (clearDepth != _clearDepth)
  253. {
  254. glClearDepth(clearDepth);
  255. _clearDepth = clearDepth;
  256. }
  257. bits |= GL_DEPTH_BUFFER_BIT;
  258. // We need to explicitly call the static enableDepthWrite() method on StateBlock
  259. // to ensure depth writing is enabled before clearing the depth buffer (and to
  260. // update the global StateBlock render state to reflect this).
  261. RenderState::StateBlock::enableDepthWrite();
  262. }
  263. if (flags & CLEAR_STENCIL)
  264. {
  265. if (clearStencil != _clearStencil)
  266. {
  267. glClearStencil(clearStencil);
  268. _clearStencil = clearStencil;
  269. }
  270. bits |= GL_STENCIL_BUFFER_BIT;
  271. }
  272. glClear(bits);
  273. }
  274. AudioListener* Game::getAudioListener()
  275. {
  276. if (_audioListener == NULL)
  277. {
  278. _audioListener = new AudioListener();
  279. }
  280. return _audioListener;
  281. }
  282. void Game::menuEvent()
  283. {
  284. }
  285. void Game::keyEvent(Keyboard::KeyEvent evt, int key)
  286. {
  287. }
  288. void Game::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  289. {
  290. }
  291. bool Game::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  292. {
  293. return false;
  294. }
  295. void Game::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
  296. {
  297. }
  298. void Game::schedule(float timeOffset, TimeListener* timeListener, void* cookie)
  299. {
  300. GP_ASSERT(_timeEvents);
  301. TimeEvent timeEvent(getGameTime() + timeOffset, timeListener, cookie);
  302. _timeEvents->push(timeEvent);
  303. }
  304. void Game::fireTimeEvents(double frameTime)
  305. {
  306. while (_timeEvents->size() > 0)
  307. {
  308. const TimeEvent* timeEvent = &_timeEvents->top();
  309. if (timeEvent->time > frameTime)
  310. {
  311. break;
  312. }
  313. if (timeEvent->listener)
  314. {
  315. timeEvent->listener->timeEvent(frameTime - timeEvent->time, timeEvent->cookie);
  316. }
  317. _timeEvents->pop();
  318. }
  319. }
  320. Game::TimeEvent::TimeEvent(double time, TimeListener* timeListener, void* cookie)
  321. : time(time), listener(timeListener), cookie(cookie)
  322. {
  323. }
  324. bool Game::TimeEvent::operator<(const TimeEvent& v) const
  325. {
  326. // The first element of std::priority_queue is the greatest.
  327. return time > v.time;
  328. }
  329. Properties* Game::getConfig() const
  330. {
  331. if (_properties == NULL)
  332. const_cast<Game*>(this)->loadConfig();
  333. return _properties;
  334. }
  335. void Game::loadConfig()
  336. {
  337. if (_properties == NULL)
  338. {
  339. // Try to load custom config from file.
  340. if (FileSystem::fileExists("game.config"))
  341. {
  342. _properties = Properties::create("game.config");
  343. // Load filesystem aliases.
  344. Properties* aliases = _properties->getNamespace("aliases", true);
  345. if (aliases)
  346. {
  347. FileSystem::loadResourceAliases(aliases);
  348. }
  349. }
  350. }
  351. }
  352. void Game::loadGamepads()
  353. {
  354. if (_properties)
  355. {
  356. // Check if there is a virtual keyboard included in the .config file.
  357. // If there is, try to create it and assign it to "player one".
  358. Properties* gamepadProperties = _properties->getNamespace("gamepads", true);
  359. if (gamepadProperties && gamepadProperties->exists("form"))
  360. {
  361. const char* gamepadFormPath = gamepadProperties->getString("form");
  362. GP_ASSERT(gamepadFormPath);
  363. Gamepad* gamepad = createGamepad(gamepadProperties->getId(), gamepadFormPath);
  364. GP_ASSERT(gamepad);
  365. }
  366. }
  367. }
  368. Gamepad* Game::createGamepad(const char* gamepadId, const char* gamepadFormPath)
  369. {
  370. GP_ASSERT(gamepadFormPath);
  371. Gamepad* gamepad = new Gamepad(gamepadId, gamepadFormPath);
  372. GP_ASSERT(gamepad);
  373. _gamepads.push_back(gamepad);
  374. return gamepad;
  375. }
  376. }