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

Improve state access to always get the current scene

Prevents events being recieved by states that are no-longer in effect

Fixes #292
This commit is contained in:
Daniel Evans 2017-09-17 00:43:09 +01:00
parent 02c60311ee
commit 887e0333c8
2 changed files with 14 additions and 7 deletions

View File

@ -357,9 +357,7 @@ int RWGame::run() {
// Loop until we run out of states.
bool running = true;
while (!StateManager::get().states.empty() && running) {
State* state = StateManager::get().states.back().get();
while (StateManager::currentState() && running) {
RW_PROFILE_FRAME_BOUNDARY();
RW_PROFILE_BEGIN("Input");
@ -395,7 +393,9 @@ int RWGame::run() {
GameInput::updateGameInputState(&getState()->input[0], event);
RW_PROFILE_BEGIN("State");
state->handleEvent(event);
if (StateManager::currentState()) {
StateManager::currentState()->handleEvent(event);
}
RW_PROFILE_END()
}
RW_PROFILE_END();
@ -408,7 +408,7 @@ int RWGame::run() {
RW_PROFILE_BEGIN("Update");
if (accum >= GAME_TIMESTEP) {
if (StateManager::get().states.size() == 0) {
if (!StateManager::currentState()) {
break;
}
@ -432,7 +432,7 @@ int RWGame::run() {
RW_PROFILE_END();
float alpha = fmod(accum, GAME_TIMESTEP) / GAME_TIMESTEP;
if (!state->shouldWorldUpdate()) {
if (!StateManager::currentState()->shouldWorldUpdate()) {
alpha = 1.f;
}
@ -442,7 +442,7 @@ int RWGame::run() {
RW_PROFILE_END();
RW_PROFILE_BEGIN("state");
if (StateManager::get().states.size() > 0) {
if (StateManager::currentState()) {
StateManager::get().draw(&renderer);
}
RW_PROFILE_END();

View File

@ -63,6 +63,13 @@ public:
states.back()->draw(r);
}
static State* currentState() {
if (StateManager::get().states.empty()) {
return nullptr;
}
return StateManager::get().states.back().get();
}
private:
bool cleared = false;
};