| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- #include "Base.h"
- #include "Game.h"
- #include "Platform.h"
- #include "RenderState.h"
- // Extern global variables
- GLenum __gl_error_code = GL_NO_ERROR;
- namespace gameplay
- {
- static Game* __gameInstance = NULL;
- long Game::_pausedTimeLast = 0L;
- long Game::_pausedTimeTotal = 0L;
- Game::Game()
- : _initialized(false), _state(UNINITIALIZED),
- _frameLastFPS(0), _frameCount(0), _frameRate(0),
- _clearDepth(1.0f), _clearStencil(0),
- _animationController(NULL), _audioController(NULL)
- {
- assert(__gameInstance == NULL);
- __gameInstance = this;
- }
- Game::Game(const Game& copy)
- {
- }
- Game::~Game()
- {
- // Do not call any virtual functions from the destructor.
- // Finalization is done from outside this class.
- #ifdef GAMEPLAY_MEM_LEAK_DETECTION
- Ref::printLeaks();
- printMemoryLeaks();
- #endif
- }
- Game* Game::getInstance()
- {
- return __gameInstance;
- }
- long Game::getAbsoluteTime()
- {
- return Platform::getAbsoluteTime();
- }
- long Game::getGameTime()
- {
- return Platform::getAbsoluteTime() - _pausedTimeTotal;
- }
- void Game::setVsync(bool enable)
- {
- Platform::setVsync(enable);
- }
- bool Game::isVsync()
- {
- return Platform::isVsync();
- }
- int Game::run(int width, int height)
- {
- if (_state != UNINITIALIZED)
- return -1;
- _width = width;
- _height = height;
- // Start up game systems.
- if (!startup())
- {
- shutdown();
- return -2;
- }
- return 0;
- }
- bool Game::startup()
- {
- if (_state != UNINITIALIZED)
- return false;
- RenderState::initialize();
- _animationController = new AnimationController();
- _animationController->initialize();
- _audioController = new AudioController();
- _audioController->initialize();
- _physicsController = new PhysicsController();
- _physicsController->initialize();
- _state = RUNNING;
- return true;
- }
- void Game::shutdown()
- {
- // Call user finalization.
- if (_state != UNINITIALIZED)
- {
- finalize();
- _animationController->finalize();
- SAFE_DELETE(_animationController);
- _audioController->finalize();
- SAFE_DELETE(_audioController);
- _physicsController->finalize();
- SAFE_DELETE(_physicsController);
- RenderState::finalize();
- }
- _state = UNINITIALIZED;
- }
- void Game::pause()
- {
- if (_state == RUNNING)
- {
- _state = PAUSED;
- _pausedTimeLast = Platform::getAbsoluteTime();
- _animationController->pause();
- _audioController->pause();
- _physicsController->pause();
- }
- }
- void Game::resume()
- {
- if (_state == PAUSED)
- {
- _state = RUNNING;
- _pausedTimeTotal += Platform::getAbsoluteTime() - _pausedTimeLast;
- _animationController->resume();
- _audioController->resume();
- _physicsController->resume();
- }
- }
- void Game::exit()
- {
- shutdown();
- }
- void Game::frame()
- {
- if (_state != RUNNING)
- {
- return;
- }
- else
- {
- if (!_initialized)
- initialize();
- _initialized = true;
- }
- // Update Time.
- static long lastFrameTime = Game::getGameTime();
- long frameTime = Game::getGameTime();
- long elapsedTime = (frameTime - lastFrameTime);
- lastFrameTime = frameTime;
- // Update the scheduled and running animations.
- _animationController->update(elapsedTime);
- // Update the physics.
- _physicsController->update(elapsedTime);
- // Application Update.
- update(elapsedTime);
- // Audio Rendering.
- _audioController->update(elapsedTime);
- // Graphics Rendering.
- render(elapsedTime);
- // Update FPS.
- ++_frameCount;
- if ((Game::getGameTime() - _frameLastFPS) >= 1000)
- {
- _frameRate = _frameCount;
- _frameCount = 0;
- _frameLastFPS = Game::getGameTime();
- }
- }
- void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
- {
- GLbitfield bits = 0;
- if (flags & CLEAR_COLOR)
- {
- if (clearColor.x != _clearColor.x ||
- clearColor.y != _clearColor.y ||
- clearColor.z != _clearColor.z ||
- clearColor.w != _clearColor.w )
- {
- glClearColor(clearColor.x, clearColor.y, clearColor.z, clearColor.w);
- _clearColor.set(clearColor);
- }
- bits |= GL_COLOR_BUFFER_BIT;
- }
- if (flags & CLEAR_DEPTH)
- {
- if (clearDepth != _clearDepth)
- {
- glClearDepth(clearDepth);
- _clearDepth = clearDepth;
- }
- bits |= GL_DEPTH_BUFFER_BIT;
- }
- if (flags & CLEAR_STENCIL)
- {
- if (clearStencil != _clearStencil)
- {
- glClearStencil(clearStencil);
- _clearStencil = clearStencil;
- }
- bits |= GL_STENCIL_BUFFER_BIT;
- }
- glClear(bits);
- }
- void Game::menu()
- {
- }
- void Game::keyChar(char key)
- {
- }
- void Game::keyPress(int key, int keyEvent)
- {
- }
- void Game::touch(int x, int y, int touchEvent)
- {
- }
- }
|