From 95b6e6a676ca1a6453f5671d26a98e592f5eb6c2 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Fri, 3 Apr 2015 15:38:24 +0100 Subject: [PATCH] Improve pausing behaviour with State::shouldWorldUpdate() --- rwengine/src/render/GameRenderer.cpp | 2 +- rwgame/RWGame.cpp | 32 +++++++++++++++------------- rwgame/State.cpp | 5 +++++ rwgame/State.hpp | 6 ++++++ rwgame/ingamestate.cpp | 5 +++++ rwgame/ingamestate.hpp | 2 ++ rwgame/loadingstate.cpp | 5 +++++ rwgame/loadingstate.hpp | 2 ++ 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/rwengine/src/render/GameRenderer.cpp b/rwengine/src/render/GameRenderer.cpp index e51dc0e4..11f47006 100644 --- a/rwengine/src/render/GameRenderer.cpp +++ b/rwengine/src/render/GameRenderer.cpp @@ -419,7 +419,7 @@ void GameRenderer::renderWorld(const ViewCamera &camera, float alpha) } float fadeTimer = engine->gameTime - engine->state.fadeStart; - if( fadeTimer <= engine->state.fadeTime || !engine->state.fadeOut ) { + if( fadeTimer < engine->state.fadeTime || !engine->state.fadeOut ) { glUseProgram(ssRectProgram); glUniform2f(ssRectOffset, 0.f, 0.f); glUniform2f(ssRectSize, 1.f, 1.f); diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index d977e91d..e4899ba5 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -135,9 +135,11 @@ RWGame::~RWGame() int RWGame::run() { clock.restart(); - + // Loop until the window is closed or we run out of state. while (window.isOpen() && StateManager::get().states.size()) { + State* state = StateManager::get().states.back(); + sf::Event event; while (window.pollEvent(event)) { switch (event.type) { @@ -155,7 +157,7 @@ int RWGame::run() default: break; } - StateManager::get().states.back()->handleEvent(event); + state->handleEvent(event); } if(! window.isOpen() ) @@ -167,31 +169,30 @@ int RWGame::run() accum += timer * timescale; while ( accum >= GAME_TIMESTEP ) { + StateManager::get().tick(GAME_TIMESTEP); - if( ! getWorld()->isPaused() ) - { - StateManager::get().tick(GAME_TIMESTEP); - - tick(GAME_TIMESTEP); - } + tick(GAME_TIMESTEP); accum -= GAME_TIMESTEP; // Throw away time if the accumulator reaches too high. - if ( accum > GAME_TIMESTEP ) + if ( accum > GAME_TIMESTEP * 5.f ) { accum = 0.f; } } float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP; + if( ! state->shouldWorldUpdate() ) + { + alpha = 1.f; + } render(alpha, timer); StateManager::get().draw(renderer); window.display(); - } return 0; @@ -201,12 +202,13 @@ void RWGame::tick(float dt) { // Clear out any per-tick state. engine->clearTickData(); - // Process the Engine's background work. engine->_work->update(); - + + State* state = StateManager::get().states.back(); + static float clockAccumulator = 0.f; - if (inFocus) { + if (inFocus && state->shouldWorldUpdate() ) { engine->gameTime += dt; clockAccumulator += dt; @@ -267,10 +269,10 @@ void RWGame::tick(float dt) engine->createTraffic(p); } } - + // render() needs two cameras to smoothly interpolate between ticks. lastCam = nextCam; - nextCam = StateManager::get().states.back()->getCamera(); + nextCam = state->getCamera(); } void RWGame::render(float alpha, float time) diff --git a/rwgame/State.cpp b/rwgame/State.cpp index 202ab05f..a71e004b 100644 --- a/rwgame/State.cpp +++ b/rwgame/State.cpp @@ -9,6 +9,11 @@ const ViewCamera& State::getCamera() return defaultView; } +bool State::shouldWorldUpdate() +{ + return false; +} + GameWorld* State::getWorld() { return game->getWorld(); diff --git a/rwgame/State.hpp b/rwgame/State.hpp index 93993457..926c7975 100644 --- a/rwgame/State.hpp +++ b/rwgame/State.hpp @@ -86,6 +86,12 @@ struct State } virtual const ViewCamera& getCamera(); + + /** + * Returns false if the game world should not should + * not update while this state is active + */ + virtual bool shouldWorldUpdate(); GameWorld* getWorld(); sf::RenderWindow& getWindow(); diff --git a/rwgame/ingamestate.cpp b/rwgame/ingamestate.cpp index 8122eb81..399ed791 100644 --- a/rwgame/ingamestate.cpp +++ b/rwgame/ingamestate.cpp @@ -314,6 +314,11 @@ void IngameState::handleEvent(const sf::Event &event) State::handleEvent(event); } +bool IngameState::shouldWorldUpdate() +{ + return true; +} + const ViewCamera &IngameState::getCamera() { return _look; diff --git a/rwgame/ingamestate.hpp b/rwgame/ingamestate.hpp index 5692f3cd..19afecd5 100644 --- a/rwgame/ingamestate.hpp +++ b/rwgame/ingamestate.hpp @@ -31,6 +31,8 @@ public: virtual void draw(GameRenderer* r); virtual void handleEvent(const sf::Event& event); + + virtual bool shouldWorldUpdate(); const ViewCamera& getCamera(); }; diff --git a/rwgame/loadingstate.cpp b/rwgame/loadingstate.cpp index a7c12fb9..ddcd8b8d 100644 --- a/rwgame/loadingstate.cpp +++ b/rwgame/loadingstate.cpp @@ -40,6 +40,11 @@ void LoadingState::tick(float dt) } } +bool LoadingState::shouldWorldUpdate() +{ + return false; +} + void LoadingState::setNextState(State* nextState) { next = nextState; diff --git a/rwgame/loadingstate.hpp b/rwgame/loadingstate.hpp index 06d6502b..379b021c 100644 --- a/rwgame/loadingstate.hpp +++ b/rwgame/loadingstate.hpp @@ -17,6 +17,8 @@ public: virtual void draw(GameRenderer* r); void setNextState(State* nextState); + + virtual bool shouldWorldUpdate(); virtual void handleEvent(const sf::Event& event); };