1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

Improve pausing behaviour with State::shouldWorldUpdate()

This commit is contained in:
Daniel Evans 2015-04-03 15:38:24 +01:00
parent bc54fac53a
commit 95b6e6a676
8 changed files with 43 additions and 16 deletions

View File

@ -419,7 +419,7 @@ void GameRenderer::renderWorld(const ViewCamera &camera, float alpha)
} }
float fadeTimer = engine->gameTime - engine->state.fadeStart; 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); glUseProgram(ssRectProgram);
glUniform2f(ssRectOffset, 0.f, 0.f); glUniform2f(ssRectOffset, 0.f, 0.f);
glUniform2f(ssRectSize, 1.f, 1.f); glUniform2f(ssRectSize, 1.f, 1.f);

View File

@ -135,9 +135,11 @@ RWGame::~RWGame()
int RWGame::run() int RWGame::run()
{ {
clock.restart(); clock.restart();
// Loop until the window is closed or we run out of state. // Loop until the window is closed or we run out of state.
while (window.isOpen() && StateManager::get().states.size()) { while (window.isOpen() && StateManager::get().states.size()) {
State* state = StateManager::get().states.back();
sf::Event event; sf::Event event;
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
switch (event.type) { switch (event.type) {
@ -155,7 +157,7 @@ int RWGame::run()
default: break; default: break;
} }
StateManager::get().states.back()->handleEvent(event); state->handleEvent(event);
} }
if(! window.isOpen() ) if(! window.isOpen() )
@ -167,31 +169,30 @@ int RWGame::run()
accum += timer * timescale; accum += timer * timescale;
while ( accum >= GAME_TIMESTEP ) { while ( accum >= GAME_TIMESTEP ) {
StateManager::get().tick(GAME_TIMESTEP);
if( ! getWorld()->isPaused() ) tick(GAME_TIMESTEP);
{
StateManager::get().tick(GAME_TIMESTEP);
tick(GAME_TIMESTEP);
}
accum -= GAME_TIMESTEP; accum -= GAME_TIMESTEP;
// Throw away time if the accumulator reaches too high. // Throw away time if the accumulator reaches too high.
if ( accum > GAME_TIMESTEP ) if ( accum > GAME_TIMESTEP * 5.f )
{ {
accum = 0.f; accum = 0.f;
} }
} }
float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP; float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP;
if( ! state->shouldWorldUpdate() )
{
alpha = 1.f;
}
render(alpha, timer); render(alpha, timer);
StateManager::get().draw(renderer); StateManager::get().draw(renderer);
window.display(); window.display();
} }
return 0; return 0;
@ -201,12 +202,13 @@ void RWGame::tick(float dt)
{ {
// Clear out any per-tick state. // Clear out any per-tick state.
engine->clearTickData(); engine->clearTickData();
// Process the Engine's background work. // Process the Engine's background work.
engine->_work->update(); engine->_work->update();
State* state = StateManager::get().states.back();
static float clockAccumulator = 0.f; static float clockAccumulator = 0.f;
if (inFocus) { if (inFocus && state->shouldWorldUpdate() ) {
engine->gameTime += dt; engine->gameTime += dt;
clockAccumulator += dt; clockAccumulator += dt;
@ -267,10 +269,10 @@ void RWGame::tick(float dt)
engine->createTraffic(p); engine->createTraffic(p);
} }
} }
// render() needs two cameras to smoothly interpolate between ticks. // render() needs two cameras to smoothly interpolate between ticks.
lastCam = nextCam; lastCam = nextCam;
nextCam = StateManager::get().states.back()->getCamera(); nextCam = state->getCamera();
} }
void RWGame::render(float alpha, float time) void RWGame::render(float alpha, float time)

View File

@ -9,6 +9,11 @@ const ViewCamera& State::getCamera()
return defaultView; return defaultView;
} }
bool State::shouldWorldUpdate()
{
return false;
}
GameWorld* State::getWorld() GameWorld* State::getWorld()
{ {
return game->getWorld(); return game->getWorld();

View File

@ -86,6 +86,12 @@ struct State
} }
virtual const ViewCamera& getCamera(); 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(); GameWorld* getWorld();
sf::RenderWindow& getWindow(); sf::RenderWindow& getWindow();

View File

@ -314,6 +314,11 @@ void IngameState::handleEvent(const sf::Event &event)
State::handleEvent(event); State::handleEvent(event);
} }
bool IngameState::shouldWorldUpdate()
{
return true;
}
const ViewCamera &IngameState::getCamera() const ViewCamera &IngameState::getCamera()
{ {
return _look; return _look;

View File

@ -31,6 +31,8 @@ public:
virtual void draw(GameRenderer* r); virtual void draw(GameRenderer* r);
virtual void handleEvent(const sf::Event& event); virtual void handleEvent(const sf::Event& event);
virtual bool shouldWorldUpdate();
const ViewCamera& getCamera(); const ViewCamera& getCamera();
}; };

View File

@ -40,6 +40,11 @@ void LoadingState::tick(float dt)
} }
} }
bool LoadingState::shouldWorldUpdate()
{
return false;
}
void LoadingState::setNextState(State* nextState) void LoadingState::setNextState(State* nextState)
{ {
next = nextState; next = nextState;

View File

@ -17,6 +17,8 @@ public:
virtual void draw(GameRenderer* r); virtual void draw(GameRenderer* r);
void setNextState(State* nextState); void setNextState(State* nextState);
virtual bool shouldWorldUpdate();
virtual void handleEvent(const sf::Event& event); virtual void handleEvent(const sf::Event& event);
}; };