Game.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. #include "SceneLoader.h"
  8. #include "ControlFactory.h"
  9. #include "Theme.h"
  10. /** @script{ignore} */
  11. GLenum __gl_error_code = GL_NO_ERROR;
  12. /** @script{ignore} */
  13. ALenum __al_error_code = AL_NO_ERROR;
  14. /** @script{ignore} */
  15. GP_SCRIPT_EVENTS();
  16. /** @script{ignore} */
  17. GP_SCRIPT_EVENT(initialize, "");
  18. /** @script{ignore} */
  19. GP_SCRIPT_EVENT(finalize, "");
  20. /** @script{ignore} */
  21. GP_SCRIPT_EVENT(update, "f");
  22. /** @script{ignore} */
  23. GP_SCRIPT_EVENT(render, "f");
  24. /** @script{ignore} */
  25. GP_SCRIPT_EVENT(resizeEvent, "ii");
  26. /** @script{ignore} */
  27. GP_SCRIPT_EVENT(keyEvent, "[Keyboard::KeyEvent]i");
  28. /** @script{ignore} */
  29. GP_SCRIPT_EVENT(touchEvent, "[Touch::TouchEvent]iiui");
  30. /** @script{ignore} */
  31. GP_SCRIPT_EVENT(mouseEvent, "[Mouse::MouseEvent]iii");
  32. /** @script{ignore} */
  33. GP_SCRIPT_EVENT(gestureSwipeEvent, "iii");
  34. /** @script{ignore} */
  35. GP_SCRIPT_EVENT(gesturePinchEvent, "iif");
  36. /** @script{ignore} */
  37. GP_SCRIPT_EVENT(gestureTapEvent, "ii");
  38. /** @script{ignore} */
  39. GP_SCRIPT_EVENT(gestureLongTapevent, "iif");
  40. /** @script{ignore} */
  41. GP_SCRIPT_EVENT(gestureDragEvent, "ii");
  42. /** @script{ignore} */
  43. GP_SCRIPT_EVENT(gestureDropEvent, "ii");
  44. /** @script{ignore} */
  45. GP_SCRIPT_EVENT(gamepadEvent, "[Gamepad::GamepadEvent]<Gamepad>");
  46. namespace gameplay
  47. {
  48. static Game* __gameInstance = NULL;
  49. double Game::_pausedTimeLast = 0.0;
  50. double Game::_pausedTimeTotal = 0.0;
  51. /**
  52. * @script{ignore}
  53. */
  54. class GameScriptTarget : public ScriptTarget
  55. {
  56. friend class Game;
  57. public:
  58. GameScriptTarget()
  59. {
  60. GP_REGISTER_SCRIPT_EVENTS();
  61. }
  62. };
  63. Game::Game()
  64. : _initialized(false), _state(UNINITIALIZED), _pausedCount(0),
  65. _frameLastFPS(0), _frameCount(0), _frameRate(0), _width(0), _height(0),
  66. _clearDepth(1.0f), _clearStencil(0), _properties(NULL),
  67. _animationController(NULL), _audioController(NULL),
  68. _physicsController(NULL), _aiController(NULL), _audioListener(NULL),
  69. _timeEvents(NULL), _scriptController(NULL), _scriptListeners(NULL), _scriptTarget(NULL)
  70. {
  71. GP_ASSERT(__gameInstance == NULL);
  72. __gameInstance = this;
  73. _timeEvents = new std::priority_queue<TimeEvent, std::vector<TimeEvent>, std::less<TimeEvent> >();
  74. }
  75. Game::~Game()
  76. {
  77. SAFE_DELETE(_scriptTarget);
  78. SAFE_DELETE(_scriptController);
  79. // Do not call any virtual functions from the destructor.
  80. // Finalization is done from outside this class.
  81. SAFE_DELETE(_timeEvents);
  82. #ifdef GP_USE_MEM_LEAK_DETECTION
  83. Ref::printLeaks();
  84. printMemoryLeaks();
  85. #endif
  86. __gameInstance = NULL;
  87. }
  88. Game* Game::getInstance()
  89. {
  90. return __gameInstance;
  91. }
  92. void Game::initialize()
  93. {
  94. // stub
  95. }
  96. void Game::finalize()
  97. {
  98. // stub
  99. }
  100. void Game::update(float elapsedTime)
  101. {
  102. // stub
  103. }
  104. void Game::render(float elapsedTime)
  105. {
  106. // stub
  107. }
  108. double Game::getAbsoluteTime()
  109. {
  110. return Platform::getAbsoluteTime();
  111. }
  112. double Game::getGameTime()
  113. {
  114. return Platform::getAbsoluteTime() - _pausedTimeTotal;
  115. }
  116. void Game::setVsync(bool enable)
  117. {
  118. Platform::setVsync(enable);
  119. }
  120. bool Game::isVsync()
  121. {
  122. return Platform::isVsync();
  123. }
  124. int Game::run()
  125. {
  126. if (_state != UNINITIALIZED)
  127. return -1;
  128. loadConfig();
  129. _width = Platform::getDisplayWidth();
  130. _height = Platform::getDisplayHeight();
  131. // Start up game systems.
  132. if (!startup())
  133. {
  134. shutdown();
  135. return -2;
  136. }
  137. return 0;
  138. }
  139. bool Game::startup()
  140. {
  141. if (_state != UNINITIALIZED)
  142. return false;
  143. setViewport(Rectangle(0.0f, 0.0f, (float)_width, (float)_height));
  144. RenderState::initialize();
  145. FrameBuffer::initialize();
  146. _animationController = new AnimationController();
  147. _animationController->initialize();
  148. _audioController = new AudioController();
  149. _audioController->initialize();
  150. _physicsController = new PhysicsController();
  151. _physicsController->initialize();
  152. _aiController = new AIController();
  153. _aiController->initialize();
  154. _scriptController = new ScriptController();
  155. _scriptController->initialize();
  156. // Load any gamepads, ui or physical.
  157. loadGamepads();
  158. // Set script handler
  159. if (_properties)
  160. {
  161. const char* script = _properties->getString("script");
  162. if (script)
  163. {
  164. _scriptTarget = new GameScriptTarget();
  165. _scriptTarget->addScript(script);
  166. }
  167. }
  168. _state = RUNNING;
  169. return true;
  170. }
  171. void Game::shutdown()
  172. {
  173. // Call user finalization.
  174. if (_state != UNINITIALIZED)
  175. {
  176. GP_ASSERT(_animationController);
  177. GP_ASSERT(_audioController);
  178. GP_ASSERT(_physicsController);
  179. GP_ASSERT(_aiController);
  180. Platform::signalShutdown();
  181. // Call user finalize
  182. finalize();
  183. // Call script finalize
  184. if (_scriptTarget)
  185. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_finalize);
  186. // Destroy script target so no more script events are fired
  187. SAFE_DELETE(_scriptTarget);
  188. // Shutdown scripting system first so that any objects allocated in script are released before our subsystems are released
  189. if (_scriptListeners)
  190. {
  191. for (size_t i = 0; i < _scriptListeners->size(); i++)
  192. {
  193. SAFE_DELETE((*_scriptListeners)[i]);
  194. }
  195. SAFE_DELETE(_scriptListeners);
  196. }
  197. _scriptController->finalize();
  198. unsigned int gamepadCount = Gamepad::getGamepadCount();
  199. for (unsigned int i = 0; i < gamepadCount; i++)
  200. {
  201. Gamepad* gamepad = Gamepad::getGamepad(i, false);
  202. SAFE_DELETE(gamepad);
  203. }
  204. _animationController->finalize();
  205. SAFE_DELETE(_animationController);
  206. _audioController->finalize();
  207. SAFE_DELETE(_audioController);
  208. _physicsController->finalize();
  209. SAFE_DELETE(_physicsController);
  210. _aiController->finalize();
  211. SAFE_DELETE(_aiController);
  212. ControlFactory::finalize();
  213. Theme::finalize();
  214. // Note: we do not clean up the script controller here
  215. // because users can call Game::exit() from a script.
  216. SAFE_DELETE(_audioListener);
  217. FrameBuffer::finalize();
  218. RenderState::finalize();
  219. SAFE_DELETE(_properties);
  220. _state = UNINITIALIZED;
  221. }
  222. }
  223. void Game::pause()
  224. {
  225. if (_state == RUNNING)
  226. {
  227. GP_ASSERT(_animationController);
  228. GP_ASSERT(_audioController);
  229. GP_ASSERT(_physicsController);
  230. GP_ASSERT(_aiController);
  231. _state = PAUSED;
  232. _pausedTimeLast = Platform::getAbsoluteTime();
  233. _animationController->pause();
  234. _audioController->pause();
  235. _physicsController->pause();
  236. _aiController->pause();
  237. }
  238. ++_pausedCount;
  239. }
  240. void Game::resume()
  241. {
  242. if (_state == PAUSED)
  243. {
  244. --_pausedCount;
  245. if (_pausedCount == 0)
  246. {
  247. GP_ASSERT(_animationController);
  248. GP_ASSERT(_audioController);
  249. GP_ASSERT(_physicsController);
  250. GP_ASSERT(_aiController);
  251. _state = RUNNING;
  252. _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
  253. _animationController->resume();
  254. _audioController->resume();
  255. _physicsController->resume();
  256. _aiController->resume();
  257. }
  258. }
  259. }
  260. void Game::exit()
  261. {
  262. // Only perform a full/clean shutdown if GP_USE_MEM_LEAK_DETECTION is defined.
  263. // Every modern OS is able to handle reclaiming process memory hundreds of times
  264. // faster than it would take us to go through every pointer in the engine and
  265. // release them nicely. For large games, shutdown can end up taking long time,
  266. // so we'll just call ::exit(0) to force an instant shutdown.
  267. #ifdef GP_USE_MEM_LEAK_DETECTION
  268. // Schedule a call to shutdown rather than calling it right away.
  269. // This handles the case of shutting down the script system from
  270. // within a script function (which can cause errors).
  271. static ShutdownListener listener;
  272. schedule(0, &listener);
  273. #else
  274. // End the process immediately without a full shutdown
  275. ::exit(0);
  276. #endif
  277. }
  278. void Game::frame()
  279. {
  280. if (!_initialized)
  281. {
  282. // Perform lazy first time initialization
  283. initialize();
  284. if (_scriptTarget)
  285. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_initialize);
  286. _initialized = true;
  287. // Fire first game resize event
  288. Platform::resizeEventInternal(_width, _height);
  289. }
  290. static double lastFrameTime = Game::getGameTime();
  291. double frameTime = getGameTime();
  292. // Fire time events to scheduled TimeListeners
  293. fireTimeEvents(frameTime);
  294. if (_state == Game::RUNNING)
  295. {
  296. GP_ASSERT(_animationController);
  297. GP_ASSERT(_audioController);
  298. GP_ASSERT(_physicsController);
  299. GP_ASSERT(_aiController);
  300. // Update Time.
  301. float elapsedTime = (frameTime - lastFrameTime);
  302. lastFrameTime = frameTime;
  303. // Update the scheduled and running animations.
  304. _animationController->update(elapsedTime);
  305. // Update the physics.
  306. _physicsController->update(elapsedTime);
  307. // Update AI.
  308. _aiController->update(elapsedTime);
  309. // Update gamepads.
  310. Gamepad::updateInternal(elapsedTime);
  311. // Application Update.
  312. update(elapsedTime);
  313. // Update forms.
  314. Form::updateInternal(elapsedTime);
  315. // Run script update.
  316. if (_scriptTarget)
  317. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_update, elapsedTime);
  318. // Audio Rendering.
  319. _audioController->update(elapsedTime);
  320. // Graphics Rendering.
  321. render(elapsedTime);
  322. // Run script render.
  323. if (_scriptTarget)
  324. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_render, elapsedTime);
  325. // Update FPS.
  326. ++_frameCount;
  327. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  328. {
  329. _frameRate = _frameCount;
  330. _frameCount = 0;
  331. _frameLastFPS = getGameTime();
  332. }
  333. }
  334. else if (_state == Game::PAUSED)
  335. {
  336. // Update gamepads.
  337. Gamepad::updateInternal(0);
  338. // Application Update.
  339. update(0);
  340. // Update forms.
  341. Form::updateInternal(0);
  342. // Script update.
  343. if (_scriptTarget)
  344. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_update, 0);
  345. // Graphics Rendering.
  346. render(0);
  347. // Script render.
  348. if (_scriptTarget)
  349. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_render, 0);
  350. }
  351. }
  352. void Game::renderOnce(const char* function)
  353. {
  354. _scriptController->executeFunction<void>(function, NULL);
  355. Platform::swapBuffers();
  356. }
  357. void Game::updateOnce()
  358. {
  359. GP_ASSERT(_animationController);
  360. GP_ASSERT(_audioController);
  361. GP_ASSERT(_physicsController);
  362. GP_ASSERT(_aiController);
  363. // Update Time.
  364. static double lastFrameTime = getGameTime();
  365. double frameTime = getGameTime();
  366. float elapsedTime = (frameTime - lastFrameTime);
  367. lastFrameTime = frameTime;
  368. // Update the internal controllers.
  369. _animationController->update(elapsedTime);
  370. _physicsController->update(elapsedTime);
  371. _aiController->update(elapsedTime);
  372. _audioController->update(elapsedTime);
  373. if (_scriptTarget)
  374. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_update, elapsedTime);
  375. }
  376. void Game::setViewport(const Rectangle& viewport)
  377. {
  378. _viewport = viewport;
  379. glViewport((GLuint)viewport.x, (GLuint)viewport.y, (GLuint)viewport.width, (GLuint)viewport.height);
  380. }
  381. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  382. {
  383. GLbitfield bits = 0;
  384. if (flags & CLEAR_COLOR)
  385. {
  386. if (clearColor.x != _clearColor.x ||
  387. clearColor.y != _clearColor.y ||
  388. clearColor.z != _clearColor.z ||
  389. clearColor.w != _clearColor.w )
  390. {
  391. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  392. _clearColor.set(clearColor);
  393. }
  394. bits |= GL_COLOR_BUFFER_BIT;
  395. }
  396. if (flags & CLEAR_DEPTH)
  397. {
  398. if (clearDepth != _clearDepth)
  399. {
  400. glClearDepth(clearDepth);
  401. _clearDepth = clearDepth;
  402. }
  403. bits |= GL_DEPTH_BUFFER_BIT;
  404. // We need to explicitly call the static enableDepthWrite() method on StateBlock
  405. // to ensure depth writing is enabled before clearing the depth buffer (and to
  406. // update the global StateBlock render state to reflect this).
  407. RenderState::StateBlock::enableDepthWrite();
  408. }
  409. if (flags & CLEAR_STENCIL)
  410. {
  411. if (clearStencil != _clearStencil)
  412. {
  413. glClearStencil(clearStencil);
  414. _clearStencil = clearStencil;
  415. }
  416. bits |= GL_STENCIL_BUFFER_BIT;
  417. }
  418. glClear(bits);
  419. }
  420. void Game::clear(ClearFlags flags, float red, float green, float blue, float alpha, float clearDepth, int clearStencil)
  421. {
  422. clear(flags, Vector4(red, green, blue, alpha), clearDepth, clearStencil);
  423. }
  424. AudioListener* Game::getAudioListener()
  425. {
  426. if (_audioListener == NULL)
  427. {
  428. _audioListener = new AudioListener();
  429. }
  430. return _audioListener;
  431. }
  432. void Game::keyEvent(Keyboard::KeyEvent evt, int key)
  433. {
  434. // stub
  435. }
  436. void Game::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  437. {
  438. // stub
  439. }
  440. bool Game::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  441. {
  442. // stub
  443. return false;
  444. }
  445. void Game::resizeEvent(unsigned int width, unsigned int height)
  446. {
  447. // stub
  448. }
  449. bool Game::isGestureSupported(Gesture::GestureEvent evt)
  450. {
  451. return Platform::isGestureSupported(evt);
  452. }
  453. void Game::registerGesture(Gesture::GestureEvent evt)
  454. {
  455. Platform::registerGesture(evt);
  456. }
  457. void Game::unregisterGesture(Gesture::GestureEvent evt)
  458. {
  459. Platform::unregisterGesture(evt);
  460. }
  461. bool Game::isGestureRegistered(Gesture::GestureEvent evt)
  462. {
  463. return Platform::isGestureRegistered(evt);
  464. }
  465. void Game::gestureSwipeEvent(int x, int y, int direction)
  466. {
  467. // stub
  468. }
  469. void Game::gesturePinchEvent(int x, int y, float scale)
  470. {
  471. // stub
  472. }
  473. void Game::gestureTapEvent(int x, int y)
  474. {
  475. // stub
  476. }
  477. void Game::gestureLongTapEvent(int x, int y, float duration)
  478. {
  479. // stub
  480. }
  481. void Game::gestureDragEvent(int x, int y)
  482. {
  483. // stub
  484. }
  485. void Game::gestureDropEvent(int x, int y)
  486. {
  487. // stub
  488. }
  489. void Game::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
  490. {
  491. // stub
  492. }
  493. void Game::keyEventInternal(Keyboard::KeyEvent evt, int key)
  494. {
  495. keyEvent(evt, key);
  496. if (_scriptTarget)
  497. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_keyEvent, evt, key);
  498. }
  499. void Game::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  500. {
  501. touchEvent(evt, x, y, contactIndex);
  502. if (_scriptTarget)
  503. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_touchEvent, evt, x, y, contactIndex);
  504. }
  505. bool Game::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  506. {
  507. if (mouseEvent(evt, x, y, wheelDelta))
  508. return true;
  509. if (_scriptTarget)
  510. return _scriptTarget->fireScriptEvent<bool>(SCRIPT_EVENT_mouseEvent, evt, x, y, wheelDelta);
  511. return false;
  512. }
  513. void Game::resizeEventInternal(unsigned int width, unsigned int height)
  514. {
  515. // Update the width and height of the game
  516. if (_width != width || _height != height)
  517. {
  518. _width = width;
  519. _height = height;
  520. resizeEvent(width, height);
  521. if (_scriptTarget)
  522. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_resizeEvent, width, height);
  523. }
  524. }
  525. void Game::gestureSwipeEventInternal(int x, int y, int direction)
  526. {
  527. gestureSwipeEvent(x, y, direction);
  528. if (_scriptTarget)
  529. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gestureSwipeEvent, x, y, direction);
  530. }
  531. void Game::gesturePinchEventInternal(int x, int y, float scale)
  532. {
  533. gesturePinchEvent(x, y, scale);
  534. if (_scriptTarget)
  535. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gesturePinchEvent, x, y, scale);
  536. }
  537. void Game::gestureTapEventInternal(int x, int y)
  538. {
  539. gestureTapEvent(x, y);
  540. if (_scriptTarget)
  541. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gestureTapEvent, x, y);
  542. }
  543. void Game::gestureLongTapEventInternal(int x, int y, float duration)
  544. {
  545. gestureLongTapEvent(x, y, duration);
  546. if (_scriptTarget)
  547. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gestureLongTapevent, x, y, duration);
  548. }
  549. void Game::gestureDragEventInternal(int x, int y)
  550. {
  551. gestureDragEvent(x, y);
  552. if (_scriptTarget)
  553. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gestureDragEvent, x, y);
  554. }
  555. void Game::gestureDropEventInternal(int x, int y)
  556. {
  557. gestureDropEvent(x, y);
  558. if (_scriptTarget)
  559. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gestureDropEvent, x, y);
  560. }
  561. void Game::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  562. {
  563. gamepadEvent(evt, gamepad);
  564. if (_scriptTarget)
  565. _scriptTarget->fireScriptEvent<void>(SCRIPT_EVENT_gamepadEvent, evt, gamepad);
  566. }
  567. void Game::getArguments(int* argc, char*** argv) const
  568. {
  569. Platform::getArguments(argc, argv);
  570. }
  571. void Game::schedule(float timeOffset, TimeListener* timeListener, void* cookie)
  572. {
  573. GP_ASSERT(_timeEvents);
  574. TimeEvent timeEvent(getGameTime() + timeOffset, timeListener, cookie);
  575. _timeEvents->push(timeEvent);
  576. }
  577. void Game::schedule(float timeOffset, const char* function)
  578. {
  579. if (!_scriptListeners)
  580. _scriptListeners = new std::vector<ScriptListener*>();
  581. ScriptListener* listener = new ScriptListener(function);
  582. _scriptListeners->push_back(listener);
  583. schedule(timeOffset, listener, NULL);
  584. }
  585. void Game::clearSchedule()
  586. {
  587. SAFE_DELETE(_timeEvents);
  588. _timeEvents = new std::priority_queue<TimeEvent, std::vector<TimeEvent>, std::less<TimeEvent> >();
  589. }
  590. void Game::fireTimeEvents(double frameTime)
  591. {
  592. while (_timeEvents->size() > 0)
  593. {
  594. const TimeEvent* timeEvent = &_timeEvents->top();
  595. if (timeEvent->time > frameTime)
  596. {
  597. break;
  598. }
  599. if (timeEvent->listener)
  600. {
  601. timeEvent->listener->timeEvent(frameTime - timeEvent->time, timeEvent->cookie);
  602. }
  603. _timeEvents->pop();
  604. }
  605. }
  606. Game::ScriptListener::ScriptListener(const char* url)
  607. {
  608. function = Game::getInstance()->getScriptController()->loadUrl(url);
  609. }
  610. void Game::ScriptListener::timeEvent(long timeDiff, void* cookie)
  611. {
  612. Game::getInstance()->getScriptController()->executeFunction<void>(function.c_str(), "l", timeDiff);
  613. }
  614. Game::TimeEvent::TimeEvent(double time, TimeListener* timeListener, void* cookie)
  615. : time(time), listener(timeListener), cookie(cookie)
  616. {
  617. }
  618. bool Game::TimeEvent::operator<(const TimeEvent& v) const
  619. {
  620. // The first element of std::priority_queue is the greatest.
  621. return time > v.time;
  622. }
  623. Properties* Game::getConfig() const
  624. {
  625. if (_properties == NULL)
  626. const_cast<Game*>(this)->loadConfig();
  627. return _properties;
  628. }
  629. void Game::loadConfig()
  630. {
  631. if (_properties == NULL)
  632. {
  633. // Try to load custom config from file.
  634. if (FileSystem::fileExists("game.config"))
  635. {
  636. _properties = Properties::create("game.config");
  637. // Load filesystem aliases.
  638. Properties* aliases = _properties->getNamespace("aliases", true);
  639. if (aliases)
  640. {
  641. FileSystem::loadResourceAliases(aliases);
  642. }
  643. }
  644. else
  645. {
  646. // Create an empty config
  647. _properties = new Properties();
  648. }
  649. }
  650. }
  651. void Game::loadGamepads()
  652. {
  653. // Load virtual gamepads.
  654. if (_properties)
  655. {
  656. // Check if there are any virtual gamepads included in the .config file.
  657. // If there are, create and initialize them.
  658. _properties->rewind();
  659. Properties* inner = _properties->getNextNamespace();
  660. while (inner != NULL)
  661. {
  662. std::string spaceName(inner->getNamespace());
  663. // This namespace was accidentally named "gamepads" originally but we'll keep this check
  664. // for backwards compatibility.
  665. if (spaceName == "gamepads" || spaceName == "gamepad")
  666. {
  667. if (inner->exists("form"))
  668. {
  669. const char* gamepadFormPath = inner->getString("form");
  670. GP_ASSERT(gamepadFormPath);
  671. Gamepad* gamepad = Gamepad::add(gamepadFormPath);
  672. GP_ASSERT(gamepad);
  673. }
  674. }
  675. inner = _properties->getNextNamespace();
  676. }
  677. }
  678. }
  679. void Game::ShutdownListener::timeEvent(long timeDiff, void* cookie)
  680. {
  681. Game::getInstance()->shutdown();
  682. }
  683. }