1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +01:00

rwgame: make StateManager non-static

This allows RWGame to be relaunched multiple times in the same process
This commit is contained in:
Anonymous Maarten 2018-12-18 22:43:49 +01:00
parent eaf719f973
commit 53b45f7f25
6 changed files with 35 additions and 33 deletions

View File

@ -96,17 +96,17 @@ RWGame::RWGame(Logger& log, const std::optional<RWArgConfigLayer> &args)
data.loadTXD(oss.str());
}
StateManager::get().enter<LoadingState>(this, [=]() {
stateManager.enter<LoadingState>(this, [=]() {
if (benchFile.has_value()) {
StateManager::get().enter<BenchmarkState>(this, *benchFile);
stateManager.enter<BenchmarkState>(this, *benchFile);
} else if (test) {
StateManager::get().enter<IngameState>(this, true, "test");
stateManager.enter<IngameState>(this, true, "test");
} else if (newgame) {
StateManager::get().enter<IngameState>(this, true);
stateManager.enter<IngameState>(this, true);
} else if (startSave.has_value()) {
StateManager::get().enter<IngameState>(this, true, *startSave);
stateManager.enter<IngameState>(this, true, *startSave);
} else {
StateManager::get().enter<MenuState>(this);
stateManager.enter<MenuState>(this);
}
});
@ -370,7 +370,7 @@ int RWGame::run() {
// Loop until we run out of states.
bool running = true;
while (StateManager::currentState() && running) {
while (stateManager.currentState() && running) {
RW_PROFILE_FRAME_BOUNDARY();
RW_PROFILE_SCOPE("Main Loop");
@ -397,12 +397,12 @@ int RWGame::run() {
getWindow().swap();
// Make sure the topmost state is the correct state
StateManager::get().updateStack();
stateManager.updateStack();
}
window.close();
StateManager::get().clear();
stateManager.clear();
return 0;
}
@ -413,7 +413,7 @@ float RWGame::tickWorld(const float deltaTime, float accumulatedTime) {
deltaTime * world->state->basic.timeScale;
while (accumulatedTime >= deltaTime) {
if (!StateManager::currentState()) {
if (!stateManager.currentState()) {
break;
}
@ -423,7 +423,7 @@ float RWGame::tickWorld(const float deltaTime, float accumulatedTime) {
deltaTimeWithTimeScale, kMaxPhysicsSubSteps, deltaTime);
}
StateManager::get().tick(deltaTimeWithTimeScale);
stateManager.tick(deltaTimeWithTimeScale);
tick(deltaTimeWithTimeScale);
@ -466,9 +466,9 @@ bool RWGame::updateInput() {
GameInput::updateGameInputState(&getState()->input[0], event);
if (StateManager::currentState()) {
if (stateManager.currentState()) {
RW_PROFILE_SCOPE("State");
StateManager::currentState()->handleEvent(event);
stateManager.currentState()->handleEvent(event);
}
}
return true;
@ -476,7 +476,7 @@ bool RWGame::updateInput() {
void RWGame::tick(float dt) {
RW_PROFILE_SCOPE(__func__);
State* currState = StateManager::get().states.back().get();
State* currState = stateManager.states.back().get();
static float clockAccumulator = 0.f;
static float scriptTimerAccumulator = 0.f;
@ -593,8 +593,8 @@ void RWGame::render(float alpha, float time) {
getRenderer().getRenderer().swap();
// Update the camera
if (!StateManager::get().states.empty()) {
currentCam = StateManager::get().states.back()->getCamera(alpha);
if (!stateManager.states.empty()) {
currentCam = stateManager.states.back()->getCamera(alpha);
}
glm::ivec2 windowSize = getWindow().getSize();
@ -624,9 +624,9 @@ void RWGame::render(float alpha, float time) {
if (!world->isPaused()) hudDrawer.drawOnScreenText(world.get(), renderer);
if (StateManager::currentState()) {
if (stateManager.currentState()) {
RW_PROFILE_SCOPE("state");
StateManager::get().draw(renderer);
stateManager.draw(renderer);
}
}

View File

@ -5,6 +5,7 @@
#include "GameBase.hpp"
#include "HUDDrawer.hpp"
#include "RWConfig.hpp"
#include "StateManager.hpp"
#ifdef _MSC_VER
#pragma warning(disable : 4305 5033)
@ -41,6 +42,8 @@ class RWGame final : public GameBase {
std::unique_ptr<ScriptMachine> vm;
SCMFile script;
StateManager stateManager;
bool inFocus = true;
ViewCamera currentCam;
@ -68,6 +71,10 @@ public:
*/
void newGame();
StateManager& getStateManager() {
return stateManager;
}
GameState* getState() {
return &state;
}

View File

@ -19,11 +19,6 @@
*/
class StateManager {
public:
static StateManager& get() {
static StateManager m;
return m;
}
std::deque<std::unique_ptr<State>> states;
void clear() {
@ -63,11 +58,11 @@ public:
states.back()->draw(r);
}
static State* currentState() {
if (StateManager::get().states.empty()) {
State* currentState() {
if (states.empty()) {
return nullptr;
}
return StateManager::get().states.back().get();
return states.back().get();
}
private:

View File

@ -297,10 +297,10 @@ void IngameState::handleEvent(const SDL_Event& event) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
StateManager::get().enter<PauseState>(game);
game->getStateManager().enter<PauseState>(game);
break;
case SDLK_m:
StateManager::get().enter<DebugState>(game, _look.position,
game->getStateManager().enter<DebugState>(game, _look.position,
_look.rotation);
break;
case SDLK_SPACE:

View File

@ -15,14 +15,14 @@ void MenuState::enterMainMenu() {
Menu menu{
{{t.text(MenuDefaults::kStartGameId),
[=] { StateManager::get().enter<IngameState>(game); }},
[=] { game->getStateManager().enter<IngameState>(game); }},
{t.text(MenuDefaults::kLoadGameId), [=] { enterLoadMenu(); }},
{t.text(MenuDefaults::kDebugId),
[=] { StateManager::get().enter<IngameState>(game, true, "test"); }},
[=] { game->getStateManager().enter<IngameState>(game, true, "test"); }},
{t.text(MenuDefaults::kOptionsId),
[] { RW_UNIMPLEMENTED("Options Menu"); }},
{t.text(MenuDefaults::kQuitGameId),
[] { StateManager::get().clear(); }}},
[=] { game->getStateManager().clear(); }}},
glm::vec2(200.f, 200.f)};
setNextMenu(std::move(menu));
@ -43,7 +43,7 @@ void MenuState::enterLoadMenu() {
auto name = GameStringUtil::fromString(ss.str(), FONT_ARIAL);
name += save.basicState.saveName;
auto loadsave = [=] {
StateManager::get().enter<IngameState>(game, false);
game->getStateManager().enter<IngameState>(game, false);
game->loadGame(save.savePath);
};
menu.lambda(name, loadsave);

View File

@ -10,7 +10,7 @@ PauseState::PauseState(RWGame* game) : State(game) {
{t.text(MenuDefaults::kOptionsId),
[] { std::cout << "Options" << std::endl; }},
{t.text(MenuDefaults::kQuitGameId),
[] { StateManager::get().clear(); }}},
[=] { game->getStateManager().clear(); }}},
glm::vec2(200.f, 200.f)};
setNextMenu(menu);
}