mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-25 11:52:40 +01:00
rwgame: make StateManager non-static
This allows RWGame to be relaunched multiple times in the same process
This commit is contained in:
parent
eaf719f973
commit
53b45f7f25
@ -96,17 +96,17 @@ RWGame::RWGame(Logger& log, const std::optional<RWArgConfigLayer> &args)
|
|||||||
data.loadTXD(oss.str());
|
data.loadTXD(oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
StateManager::get().enter<LoadingState>(this, [=]() {
|
stateManager.enter<LoadingState>(this, [=]() {
|
||||||
if (benchFile.has_value()) {
|
if (benchFile.has_value()) {
|
||||||
StateManager::get().enter<BenchmarkState>(this, *benchFile);
|
stateManager.enter<BenchmarkState>(this, *benchFile);
|
||||||
} else if (test) {
|
} else if (test) {
|
||||||
StateManager::get().enter<IngameState>(this, true, "test");
|
stateManager.enter<IngameState>(this, true, "test");
|
||||||
} else if (newgame) {
|
} else if (newgame) {
|
||||||
StateManager::get().enter<IngameState>(this, true);
|
stateManager.enter<IngameState>(this, true);
|
||||||
} else if (startSave.has_value()) {
|
} else if (startSave.has_value()) {
|
||||||
StateManager::get().enter<IngameState>(this, true, *startSave);
|
stateManager.enter<IngameState>(this, true, *startSave);
|
||||||
} else {
|
} else {
|
||||||
StateManager::get().enter<MenuState>(this);
|
stateManager.enter<MenuState>(this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -370,7 +370,7 @@ int RWGame::run() {
|
|||||||
|
|
||||||
// Loop until we run out of states.
|
// Loop until we run out of states.
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while (StateManager::currentState() && running) {
|
while (stateManager.currentState() && running) {
|
||||||
RW_PROFILE_FRAME_BOUNDARY();
|
RW_PROFILE_FRAME_BOUNDARY();
|
||||||
RW_PROFILE_SCOPE("Main Loop");
|
RW_PROFILE_SCOPE("Main Loop");
|
||||||
|
|
||||||
@ -397,12 +397,12 @@ int RWGame::run() {
|
|||||||
getWindow().swap();
|
getWindow().swap();
|
||||||
|
|
||||||
// Make sure the topmost state is the correct state
|
// Make sure the topmost state is the correct state
|
||||||
StateManager::get().updateStack();
|
stateManager.updateStack();
|
||||||
}
|
}
|
||||||
|
|
||||||
window.close();
|
window.close();
|
||||||
|
|
||||||
StateManager::get().clear();
|
stateManager.clear();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -413,7 +413,7 @@ float RWGame::tickWorld(const float deltaTime, float accumulatedTime) {
|
|||||||
deltaTime * world->state->basic.timeScale;
|
deltaTime * world->state->basic.timeScale;
|
||||||
|
|
||||||
while (accumulatedTime >= deltaTime) {
|
while (accumulatedTime >= deltaTime) {
|
||||||
if (!StateManager::currentState()) {
|
if (!stateManager.currentState()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,7 +423,7 @@ float RWGame::tickWorld(const float deltaTime, float accumulatedTime) {
|
|||||||
deltaTimeWithTimeScale, kMaxPhysicsSubSteps, deltaTime);
|
deltaTimeWithTimeScale, kMaxPhysicsSubSteps, deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
StateManager::get().tick(deltaTimeWithTimeScale);
|
stateManager.tick(deltaTimeWithTimeScale);
|
||||||
|
|
||||||
tick(deltaTimeWithTimeScale);
|
tick(deltaTimeWithTimeScale);
|
||||||
|
|
||||||
@ -466,9 +466,9 @@ bool RWGame::updateInput() {
|
|||||||
|
|
||||||
GameInput::updateGameInputState(&getState()->input[0], event);
|
GameInput::updateGameInputState(&getState()->input[0], event);
|
||||||
|
|
||||||
if (StateManager::currentState()) {
|
if (stateManager.currentState()) {
|
||||||
RW_PROFILE_SCOPE("State");
|
RW_PROFILE_SCOPE("State");
|
||||||
StateManager::currentState()->handleEvent(event);
|
stateManager.currentState()->handleEvent(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -476,7 +476,7 @@ bool RWGame::updateInput() {
|
|||||||
|
|
||||||
void RWGame::tick(float dt) {
|
void RWGame::tick(float dt) {
|
||||||
RW_PROFILE_SCOPE(__func__);
|
RW_PROFILE_SCOPE(__func__);
|
||||||
State* currState = StateManager::get().states.back().get();
|
State* currState = stateManager.states.back().get();
|
||||||
|
|
||||||
static float clockAccumulator = 0.f;
|
static float clockAccumulator = 0.f;
|
||||||
static float scriptTimerAccumulator = 0.f;
|
static float scriptTimerAccumulator = 0.f;
|
||||||
@ -593,8 +593,8 @@ void RWGame::render(float alpha, float time) {
|
|||||||
getRenderer().getRenderer().swap();
|
getRenderer().getRenderer().swap();
|
||||||
|
|
||||||
// Update the camera
|
// Update the camera
|
||||||
if (!StateManager::get().states.empty()) {
|
if (!stateManager.states.empty()) {
|
||||||
currentCam = StateManager::get().states.back()->getCamera(alpha);
|
currentCam = stateManager.states.back()->getCamera(alpha);
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::ivec2 windowSize = getWindow().getSize();
|
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 (!world->isPaused()) hudDrawer.drawOnScreenText(world.get(), renderer);
|
||||||
|
|
||||||
if (StateManager::currentState()) {
|
if (stateManager.currentState()) {
|
||||||
RW_PROFILE_SCOPE("state");
|
RW_PROFILE_SCOPE("state");
|
||||||
StateManager::get().draw(renderer);
|
stateManager.draw(renderer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "GameBase.hpp"
|
#include "GameBase.hpp"
|
||||||
#include "HUDDrawer.hpp"
|
#include "HUDDrawer.hpp"
|
||||||
#include "RWConfig.hpp"
|
#include "RWConfig.hpp"
|
||||||
|
#include "StateManager.hpp"
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable : 4305 5033)
|
#pragma warning(disable : 4305 5033)
|
||||||
@ -41,6 +42,8 @@ class RWGame final : public GameBase {
|
|||||||
std::unique_ptr<ScriptMachine> vm;
|
std::unique_ptr<ScriptMachine> vm;
|
||||||
SCMFile script;
|
SCMFile script;
|
||||||
|
|
||||||
|
StateManager stateManager;
|
||||||
|
|
||||||
bool inFocus = true;
|
bool inFocus = true;
|
||||||
ViewCamera currentCam;
|
ViewCamera currentCam;
|
||||||
|
|
||||||
@ -68,6 +71,10 @@ public:
|
|||||||
*/
|
*/
|
||||||
void newGame();
|
void newGame();
|
||||||
|
|
||||||
|
StateManager& getStateManager() {
|
||||||
|
return stateManager;
|
||||||
|
}
|
||||||
|
|
||||||
GameState* getState() {
|
GameState* getState() {
|
||||||
return &state;
|
return &state;
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,6 @@
|
|||||||
*/
|
*/
|
||||||
class StateManager {
|
class StateManager {
|
||||||
public:
|
public:
|
||||||
static StateManager& get() {
|
|
||||||
static StateManager m;
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::deque<std::unique_ptr<State>> states;
|
std::deque<std::unique_ptr<State>> states;
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
@ -63,11 +58,11 @@ public:
|
|||||||
states.back()->draw(r);
|
states.back()->draw(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
static State* currentState() {
|
State* currentState() {
|
||||||
if (StateManager::get().states.empty()) {
|
if (states.empty()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return StateManager::get().states.back().get();
|
return states.back().get();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -297,10 +297,10 @@ void IngameState::handleEvent(const SDL_Event& event) {
|
|||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
StateManager::get().enter<PauseState>(game);
|
game->getStateManager().enter<PauseState>(game);
|
||||||
break;
|
break;
|
||||||
case SDLK_m:
|
case SDLK_m:
|
||||||
StateManager::get().enter<DebugState>(game, _look.position,
|
game->getStateManager().enter<DebugState>(game, _look.position,
|
||||||
_look.rotation);
|
_look.rotation);
|
||||||
break;
|
break;
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
|
@ -15,14 +15,14 @@ void MenuState::enterMainMenu() {
|
|||||||
|
|
||||||
Menu menu{
|
Menu menu{
|
||||||
{{t.text(MenuDefaults::kStartGameId),
|
{{t.text(MenuDefaults::kStartGameId),
|
||||||
[=] { StateManager::get().enter<IngameState>(game); }},
|
[=] { game->getStateManager().enter<IngameState>(game); }},
|
||||||
{t.text(MenuDefaults::kLoadGameId), [=] { enterLoadMenu(); }},
|
{t.text(MenuDefaults::kLoadGameId), [=] { enterLoadMenu(); }},
|
||||||
{t.text(MenuDefaults::kDebugId),
|
{t.text(MenuDefaults::kDebugId),
|
||||||
[=] { StateManager::get().enter<IngameState>(game, true, "test"); }},
|
[=] { game->getStateManager().enter<IngameState>(game, true, "test"); }},
|
||||||
{t.text(MenuDefaults::kOptionsId),
|
{t.text(MenuDefaults::kOptionsId),
|
||||||
[] { RW_UNIMPLEMENTED("Options Menu"); }},
|
[] { RW_UNIMPLEMENTED("Options Menu"); }},
|
||||||
{t.text(MenuDefaults::kQuitGameId),
|
{t.text(MenuDefaults::kQuitGameId),
|
||||||
[] { StateManager::get().clear(); }}},
|
[=] { game->getStateManager().clear(); }}},
|
||||||
glm::vec2(200.f, 200.f)};
|
glm::vec2(200.f, 200.f)};
|
||||||
|
|
||||||
setNextMenu(std::move(menu));
|
setNextMenu(std::move(menu));
|
||||||
@ -43,7 +43,7 @@ void MenuState::enterLoadMenu() {
|
|||||||
auto name = GameStringUtil::fromString(ss.str(), FONT_ARIAL);
|
auto name = GameStringUtil::fromString(ss.str(), FONT_ARIAL);
|
||||||
name += save.basicState.saveName;
|
name += save.basicState.saveName;
|
||||||
auto loadsave = [=] {
|
auto loadsave = [=] {
|
||||||
StateManager::get().enter<IngameState>(game, false);
|
game->getStateManager().enter<IngameState>(game, false);
|
||||||
game->loadGame(save.savePath);
|
game->loadGame(save.savePath);
|
||||||
};
|
};
|
||||||
menu.lambda(name, loadsave);
|
menu.lambda(name, loadsave);
|
||||||
|
@ -10,7 +10,7 @@ PauseState::PauseState(RWGame* game) : State(game) {
|
|||||||
{t.text(MenuDefaults::kOptionsId),
|
{t.text(MenuDefaults::kOptionsId),
|
||||||
[] { std::cout << "Options" << std::endl; }},
|
[] { std::cout << "Options" << std::endl; }},
|
||||||
{t.text(MenuDefaults::kQuitGameId),
|
{t.text(MenuDefaults::kQuitGameId),
|
||||||
[] { StateManager::get().clear(); }}},
|
[=] { game->getStateManager().clear(); }}},
|
||||||
glm::vec2(200.f, 200.f)};
|
glm::vec2(200.f, 200.f)};
|
||||||
setNextMenu(menu);
|
setNextMenu(menu);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user