Skip to content

Commit

Permalink
bugfix game state multi-threading
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Aug 26, 2024
1 parent 9a2f0a6 commit 9d6a89f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
27 changes: 16 additions & 11 deletions application/lucre/gameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace LucreApp

GameState::GameState()
: m_State{State::SPLASH}, m_NextState{State::SPLASH}, m_LastState{State::SPLASH}, m_InputIdle{false},
m_UserInputEnabled{false}, m_DeleteScene{State::NULL_STATE}
m_UserInputEnabled{false}, m_DeleteScene{State::NULL_STATE}, m_LoadingState{State::NULL_STATE}
{
memset(m_StateLoaded, false, static_cast<int>(State::MAX_STATES) * sizeof(bool));
}
Expand All @@ -52,11 +52,7 @@ namespace LucreApp
Load(State::SETTINGS);

SetState(State::SPLASH);
#ifdef MACOSX
SetNextState(State::CUTSCENE);
#else
SetNextState(State::TERRAIN);
#endif
}

void GameState::Stop() { GetScene()->Stop(); }
Expand Down Expand Up @@ -185,6 +181,10 @@ namespace LucreApp

void GameState::LoadNextState()
{
if (m_LoadingState != State::NULL_STATE)
{
return;
}
if (!IsLoaded(m_NextState))
{
if (m_DeleteScene == State::NULL_STATE)
Expand Down Expand Up @@ -229,6 +229,11 @@ namespace LucreApp
void GameState::Load(GameState::State state)
{
ASSERT(!IsLoaded(state));
if (m_LoadingState != State::NULL_STATE)
{
return;
}
m_LoadingState = state;
switch (state)
{
case State::SPLASH:
Expand All @@ -242,7 +247,7 @@ namespace LucreApp
}
case State::MAIN:
{
auto lambda = [&]()
auto lambda = [&, state]()
{
auto scenePtr =
std::make_shared<MainScene>("main.json", "application/lucre/sceneDescriptions/main.json");
Expand All @@ -264,7 +269,7 @@ namespace LucreApp
}
case State::BEACH:
{
auto lambda = [&]()
auto lambda = [&, state]()
{
auto scenePtr =
std::make_shared<BeachScene>("beach.json", "application/lucre/sceneDescriptions/beach.json");
Expand All @@ -287,7 +292,7 @@ namespace LucreApp

case State::NIGHT:
{
auto lambda = [&]()
auto lambda = [&, state]()
{
auto scenePtr =
std::make_shared<NightScene>("night.json", "application/lucre/sceneDescriptions/night.json");
Expand All @@ -309,7 +314,7 @@ namespace LucreApp
}
case State::DESSERT:
{
auto lambda = [&]()
auto lambda = [&, state]()
{
auto scenePtr =
std::make_shared<DessertScene>("dessert.json", "application/lucre/sceneDescriptions/dessert.json");
Expand All @@ -331,9 +336,8 @@ namespace LucreApp
}
case State::TERRAIN:
{
auto lambda = [&]()
auto lambda = [&, state]()
{
// todo: use the json format
auto scenePtr =
std::make_shared<TerrainScene>("terrain.json", "application/lucre/sceneDescriptions/terrain.json");
SetupScene(state, scenePtr);
Expand Down Expand Up @@ -399,6 +403,7 @@ namespace LucreApp
{
std::lock_guard lock(m_Mutex);
m_StateLoaded[static_cast<int>(state)] = isLoaded;
m_LoadingState = State::NULL_STATE;
}

void GameState::SetupScene(const State state, const std::shared_ptr<Scene>& scene)
Expand Down
2 changes: 1 addition & 1 deletion application/lucre/gameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace LucreApp

private:
std::mutex m_Mutex;
State m_State, m_NextState, m_LastState, m_DeleteScene;
State m_State, m_NextState, m_LastState, m_DeleteScene, m_LoadingState;
std::shared_ptr<Scene> m_Scenes[static_cast<int>(State::MAX_STATES)];
bool m_UserInputEnabled;
bool m_InputIdle;
Expand Down
2 changes: 1 addition & 1 deletion application/lucre/sceneDescriptions/main.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"file format identifier": 1.2,
"description": "night scene",
"description": "main scene",
"author": "Copyright (c) 2024 Engine Development Team",
"fastgltf files":
[
Expand Down
2 changes: 1 addition & 1 deletion engine/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace GfxRenderEngine
}

std::shared_ptr<Renderer> GetRenderer() const { return m_GraphicsContext->GetRenderer(); }
bool MultiThreadingSupport() const { return false; /*m_GraphicsContext->MultiThreadingSupport(); */ }
bool MultiThreadingSupport() const { return m_GraphicsContext->MultiThreadingSupport(); }
void SetAppEventCallback(EventCallbackFunction eventCallback);

void PushLayer(Layer* layer) { m_LayerStack.PushLayer(layer); }
Expand Down

0 comments on commit 9d6a89f

Please sign in to comment.