Game.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Game.cpp
  3. */
  4. #include "Base.h"
  5. #include "Game.h"
  6. #include "Platform.h"
  7. #include "RenderState.h"
  8. // Extern global variables
  9. GLenum __gl_error_code = GL_NO_ERROR;
  10. namespace gameplay
  11. {
  12. static Game* __gameInstance = NULL;
  13. long Game::_pausedTimeLast = 0L;
  14. long Game::_pausedTimeTotal = 0L;
  15. Game::Game()
  16. : _state(UNINITIALIZED),
  17. _frameLastFPS(0), _frameCount(0), _frameRate(0),
  18. _clearDepth(1.0f), _clearStencil(0),
  19. _animationController(NULL), _audioController(NULL)
  20. {
  21. assert(__gameInstance == NULL);
  22. __gameInstance = this;
  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. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  32. Ref::printLeaks();
  33. printMemoryLeaks();
  34. #endif
  35. }
  36. Game* Game::getInstance()
  37. {
  38. return __gameInstance;
  39. }
  40. long Game::getAbsoluteTime()
  41. {
  42. return Platform::getAbsoluteTime();
  43. }
  44. bool Game::isVsync()
  45. {
  46. return Platform::isVsync();
  47. }
  48. void Game::setVsync(bool enable)
  49. {
  50. Platform::setVsync(enable);
  51. }
  52. long Game::getGameTime()
  53. {
  54. return (Platform::getAbsoluteTime() - _pausedTimeTotal);
  55. }
  56. Game::State Game::getState() const
  57. {
  58. return _state;
  59. }
  60. unsigned int Game::getFrameRate() const
  61. {
  62. return _frameRate;
  63. }
  64. int Game::run(int width, int height)
  65. {
  66. if (_state != UNINITIALIZED)
  67. return -1;
  68. _width = width;
  69. _height = height;
  70. // Start up game systems.
  71. if (!startup())
  72. {
  73. shutdown();
  74. return -2;
  75. }
  76. return 0;
  77. }
  78. bool Game::startup()
  79. {
  80. if (_state != UNINITIALIZED)
  81. return false;
  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. // Call user initialization.
  90. initialize();
  91. _state = RUNNING;
  92. return true;
  93. }
  94. void Game::shutdown()
  95. {
  96. // Call user finalization.
  97. if (_state != UNINITIALIZED)
  98. {
  99. finalize();
  100. _animationController->finalize();
  101. SAFE_DELETE(_animationController);
  102. _audioController->finalize();
  103. SAFE_DELETE(_audioController);
  104. _physicsController->finalize();
  105. SAFE_DELETE(_physicsController);
  106. RenderState::finalize();
  107. }
  108. _state = UNINITIALIZED;
  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 (_state != RUNNING)
  139. return;
  140. // Update Time.
  141. static long lastFrameTime = Game::getGameTime();
  142. long frameTime = Game::getGameTime();
  143. long elapsedTime = (frameTime - lastFrameTime);
  144. lastFrameTime = frameTime;
  145. // Update the scheduled and running animations.
  146. _animationController->update(elapsedTime);
  147. // Update the physics.
  148. _physicsController->update(elapsedTime);
  149. // Application Update.
  150. update(elapsedTime);
  151. // Audio Rendering.
  152. _audioController->update(elapsedTime);
  153. // Graphics Rendering.
  154. render(elapsedTime);
  155. // Update FPS.
  156. ++_frameCount;
  157. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  158. {
  159. _frameRate = _frameCount;
  160. _frameCount = 0;
  161. _frameLastFPS = Game::getGameTime();
  162. }
  163. }
  164. unsigned int Game::getWidth() const
  165. {
  166. return _width;
  167. }
  168. unsigned int Game::getHeight() const
  169. {
  170. return _height;
  171. }
  172. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  173. {
  174. GLbitfield bits = 0;
  175. if (flags & CLEAR_COLOR)
  176. {
  177. if (clearColor.x != _clearColor.x ||
  178. clearColor.y != _clearColor.y ||
  179. clearColor.z != _clearColor.z ||
  180. clearColor.w != _clearColor.w )
  181. {
  182. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  183. _clearColor.set(clearColor);
  184. }
  185. bits |= GL_COLOR_BUFFER_BIT;
  186. }
  187. if (flags & CLEAR_DEPTH)
  188. {
  189. if (clearDepth != _clearDepth)
  190. {
  191. glClearDepth(clearDepth);
  192. _clearDepth = clearDepth;
  193. }
  194. bits |= GL_DEPTH_BUFFER_BIT;
  195. }
  196. if (flags & CLEAR_STENCIL)
  197. {
  198. if (clearStencil != _clearStencil)
  199. {
  200. glClearStencil(clearStencil);
  201. _clearStencil = clearStencil;
  202. }
  203. bits |= GL_STENCIL_BUFFER_BIT;
  204. }
  205. glClear(bits);
  206. }
  207. AnimationController* Game::getAnimationController()
  208. {
  209. return _animationController;
  210. }
  211. const AudioController* Game::getAudioController() const
  212. {
  213. return _audioController;
  214. }
  215. PhysicsController* Game::getPhysicsController()
  216. {
  217. return _physicsController;
  218. }
  219. void Game::menu()
  220. {
  221. }
  222. void Game::keyChar(char key)
  223. {
  224. }
  225. void Game::keyPress(int key, int keyEvent)
  226. {
  227. }
  228. void Game::touch(int x, int y, int touchEvent)
  229. {
  230. }
  231. }