1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 00:59:47 +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;
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);

View File

@ -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)

View File

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

View File

@ -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();

View File

@ -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;

View File

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

View File

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

View File

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