Game.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "Base.h"
  2. #include "Game.h"
  3. #include "Platform.h"
  4. #include "RenderState.h"
  5. // Extern global variables
  6. GLenum __gl_error_code = GL_NO_ERROR;
  7. namespace gameplay
  8. {
  9. static Game* __gameInstance = NULL;
  10. long Game::_pausedTimeLast = 0L;
  11. long Game::_pausedTimeTotal = 0L;
  12. Game::Game()
  13. : _initialized(false), _state(UNINITIALIZED),
  14. _frameLastFPS(0), _frameCount(0), _frameRate(0),
  15. _clearDepth(1.0f), _clearStencil(0),
  16. _animationController(NULL), _audioController(NULL)
  17. {
  18. assert(__gameInstance == NULL);
  19. __gameInstance = this;
  20. }
  21. Game::Game(const Game& copy)
  22. {
  23. }
  24. Game::~Game()
  25. {
  26. // Do not call any virtual functions from the destructor.
  27. // Finalization is done from outside this class.
  28. #ifdef GAMEPLAY_MEM_LEAK_DETECTION
  29. Ref::printLeaks();
  30. printMemoryLeaks();
  31. #endif
  32. }
  33. Game* Game::getInstance()
  34. {
  35. return __gameInstance;
  36. }
  37. long Game::getAbsoluteTime()
  38. {
  39. return Platform::getAbsoluteTime();
  40. }
  41. long Game::getGameTime()
  42. {
  43. return Platform::getAbsoluteTime() - _pausedTimeTotal;
  44. }
  45. void Game::setVsync(bool enable)
  46. {
  47. Platform::setVsync(enable);
  48. }
  49. bool Game::isVsync()
  50. {
  51. return Platform::isVsync();
  52. }
  53. int Game::run(int width, int height)
  54. {
  55. if (_state != UNINITIALIZED)
  56. return -1;
  57. _width = width;
  58. _height = height;
  59. // Start up game systems.
  60. if (!startup())
  61. {
  62. shutdown();
  63. return -2;
  64. }
  65. return 0;
  66. }
  67. bool Game::startup()
  68. {
  69. if (_state != UNINITIALIZED)
  70. return false;
  71. RenderState::initialize();
  72. _animationController = new AnimationController();
  73. _animationController->initialize();
  74. _audioController = new AudioController();
  75. _audioController->initialize();
  76. _physicsController = new PhysicsController();
  77. _physicsController->initialize();
  78. _state = RUNNING;
  79. return true;
  80. }
  81. void Game::shutdown()
  82. {
  83. // Call user finalization.
  84. if (_state != UNINITIALIZED)
  85. {
  86. finalize();
  87. _animationController->finalize();
  88. SAFE_DELETE(_animationController);
  89. _audioController->finalize();
  90. SAFE_DELETE(_audioController);
  91. _physicsController->finalize();
  92. SAFE_DELETE(_physicsController);
  93. RenderState::finalize();
  94. }
  95. _state = UNINITIALIZED;
  96. }
  97. void Game::pause()
  98. {
  99. if (_state == RUNNING)
  100. {
  101. _state = PAUSED;
  102. _pausedTimeLast = Platform::getAbsoluteTime();
  103. _animationController->pause();
  104. _audioController->pause();
  105. _physicsController->pause();
  106. }
  107. }
  108. void Game::resume()
  109. {
  110. if (_state == PAUSED)
  111. {
  112. _state = RUNNING;
  113. _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
  114. _animationController->resume();
  115. _audioController->resume();
  116. _physicsController->resume();
  117. }
  118. }
  119. void Game::exit()
  120. {
  121. shutdown();
  122. }
  123. void Game::frame()
  124. {
  125. if (_state != RUNNING)
  126. {
  127. return;
  128. }
  129. else
  130. {
  131. if (!_initialized)
  132. initialize();
  133. _initialized = true;
  134. }
  135. // Update Time.
  136. static long lastFrameTime = Game::getGameTime();
  137. long frameTime = Game::getGameTime();
  138. long elapsedTime = (frameTime - lastFrameTime);
  139. lastFrameTime = frameTime;
  140. // Update the scheduled and running animations.
  141. _animationController->update(elapsedTime);
  142. // Update the physics.
  143. _physicsController->update(elapsedTime);
  144. // Application Update.
  145. update(elapsedTime);
  146. // Audio Rendering.
  147. _audioController->update(elapsedTime);
  148. // Graphics Rendering.
  149. render(elapsedTime);
  150. // Update FPS.
  151. ++_frameCount;
  152. if ((Game::getGameTime() - _frameLastFPS) >= 1000)
  153. {
  154. _frameRate = _frameCount;
  155. _frameCount = 0;
  156. _frameLastFPS = Game::getGameTime();
  157. }
  158. }
  159. void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
  160. {
  161. GLbitfield bits = 0;
  162. if (flags & CLEAR_COLOR)
  163. {
  164. if (clearColor.x != _clearColor.x ||
  165. clearColor.y != _clearColor.y ||
  166. clearColor.z != _clearColor.z ||
  167. clearColor.w != _clearColor.w )
  168. {
  169. glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
  170. _clearColor.set(clearColor);
  171. }
  172. bits |= GL_COLOR_BUFFER_BIT;
  173. }
  174. if (flags & CLEAR_DEPTH)
  175. {
  176. if (clearDepth != _clearDepth)
  177. {
  178. glClearDepth(clearDepth);
  179. _clearDepth = clearDepth;
  180. }
  181. bits |= GL_DEPTH_BUFFER_BIT;
  182. }
  183. if (flags & CLEAR_STENCIL)
  184. {
  185. if (clearStencil != _clearStencil)
  186. {
  187. glClearStencil(clearStencil);
  188. _clearStencil = clearStencil;
  189. }
  190. bits |= GL_STENCIL_BUFFER_BIT;
  191. }
  192. glClear(bits);
  193. }
  194. void Game::menu()
  195. {
  196. }
  197. void Game::keyChar(char key)
  198. {
  199. }
  200. void Game::keyPress(int key, int keyEvent)
  201. {
  202. }
  203. void Game::touch(int x, int y, int touchEvent)
  204. {
  205. }
  206. }