Skip to content

Commit

Permalink
Add state transition function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeor Dicker committed Jun 29, 2023
1 parent 67f2525 commit 93c097e
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions OP2-Landlord/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace

using StateGuiFunction = std::function<Gui::AppState(void)>;
std::map<Gui::AppState, StateGuiFunction> StateFunctionTable;

using StateTransitionFunction = std::function<void(Gui&)>;
std::map<Gui::AppState, StateTransitionFunction> StateTransitionFunctionTable;
};


Expand All @@ -46,8 +49,21 @@ void mainLoop(Graphics& graphics, Gui& gui)
graphics.clear();
gui.newFrame();

const auto previousState = ApplicationState;
ApplicationState = StateFunctionTable.at(ApplicationState)();

if (previousState != ApplicationState)
{
try
{
StateTransitionFunctionTable.at(ApplicationState)(gui);
}
catch (std::out_of_range)
{
std::cout << "[Warning] No transition handler for ApplicationState '" << static_cast<int>(ApplicationState) << "'" << std::endl;
}
}

gui.endFrame();
graphics.present();
}
Expand All @@ -60,10 +76,17 @@ void checkConfig(EditorConfig& config)
}


void loadOrCreateTransition(Gui& gui)
{
}


void buildStateGuiFunctionTable(Gui& gui)
{
StateFunctionTable.emplace(Gui::AppState::InitialSetup, [&gui]() { return gui.initialSetup(); });
StateFunctionTable.emplace(Gui::AppState::CreateOrLoadMap, [&gui]() { return gui.createOrLoadMap(); });

StateTransitionFunctionTable.emplace(Gui::AppState::CreateOrLoadMap, loadOrCreateTransition);
}


Expand All @@ -81,6 +104,11 @@ int main(int argc, char* argv[])

buildStateGuiFunctionTable(gui);
checkConfig(config);

if (ApplicationState != Gui::AppState::InitialSetup)
{
loadOrCreateTransition(gui);
}

mainLoop(graphics, gui);

Expand Down

0 comments on commit 93c097e

Please sign in to comment.