|
|
@@ -1,19 +1,25 @@
|
|
|
#include "Engine.h"
|
|
|
+#include "states/MainMenuState.h"
|
|
|
#include <SDL2/SDL_log.h>
|
|
|
#include <SDL2/SDL_mixer.h>
|
|
|
|
|
|
Engine::Engine()
|
|
|
{
|
|
|
+ // Init systems
|
|
|
InitSDL();
|
|
|
InitImGui();
|
|
|
InitAudio();
|
|
|
-// InitPython();
|
|
|
- SDL_Log("Engine::Engine()");
|
|
|
+ InitPython();
|
|
|
+
|
|
|
+ // Load assets ...
|
|
|
+
|
|
|
+ // Run MainMenuState
|
|
|
+ //stateManager.ChangeState(std::make_unique<MainMenuState>(), *this);
|
|
|
+ stateManager.PushState(std::make_unique<MainMenuState>(), *this);
|
|
|
}
|
|
|
|
|
|
Engine::~Engine()
|
|
|
{
|
|
|
- SDL_Log("Engine::~Engine()");
|
|
|
ImGui_ImplSDLRenderer2_Shutdown();
|
|
|
ImGui_ImplSDL2_Shutdown();
|
|
|
ImGui::DestroyContext();
|
|
|
@@ -23,7 +29,7 @@ Engine::~Engine()
|
|
|
if (renderer) { SDL_DestroyRenderer(renderer); }
|
|
|
if (window) { SDL_DestroyWindow(window); }
|
|
|
|
|
|
-// ShutdownPython();
|
|
|
+ ShutdownPython();
|
|
|
|
|
|
SDL_Quit();
|
|
|
}
|
|
|
@@ -75,25 +81,25 @@ void Engine::HandleEvents()
|
|
|
isRunning = false;
|
|
|
}
|
|
|
|
|
|
- // stateManager.HandleEvents(*this, e);
|
|
|
+ stateManager.HandleEvents(*this, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void Engine::Update(float dt)
|
|
|
{
|
|
|
- // stateManager.Update(*this, dt);
|
|
|
+ stateManager.Update(*this, dt);
|
|
|
}
|
|
|
|
|
|
void Engine::Render()
|
|
|
{
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
|
- // stateManager.Render(*this);
|
|
|
+ stateManager.Render(*this);
|
|
|
|
|
|
ImGui_ImplSDLRenderer2_NewFrame();
|
|
|
ImGui_ImplSDL2_NewFrame();
|
|
|
ImGui::NewFrame();
|
|
|
- // stateManager.RenderImGui(*this);
|
|
|
+ stateManager.RenderImGui(*this);
|
|
|
ImGui::Render();
|
|
|
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), renderer);
|
|
|
|
|
|
@@ -114,7 +120,16 @@ void Engine::Run()
|
|
|
Render();
|
|
|
|
|
|
// Safe State Transition
|
|
|
- // ...
|
|
|
+ if (pendingNewState)
|
|
|
+ {
|
|
|
+ stateManager.ChangeState(std::move(pendingNewState), *this);
|
|
|
+ pendingNewState = nullptr;
|
|
|
+ }
|
|
|
+ else if (pendingPop)
|
|
|
+ {
|
|
|
+ stateManager.PopState(*this);
|
|
|
+ pendingPop = false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -128,9 +143,8 @@ void Engine::InitPython()
|
|
|
|
|
|
py::module_ sys = py::module_::import("sys");
|
|
|
auto path = sys.attr("path").cast<py::list>();
|
|
|
- path.append("assets/scripts");
|
|
|
SDL_Log("Python interpreter initialized");
|
|
|
- RunPythonScript("init.py");
|
|
|
+ RunPythonScript("assets/scripts/init.py");
|
|
|
}
|
|
|
catch (const py::error_already_set& e)
|
|
|
{
|
|
|
@@ -157,4 +171,16 @@ void Engine::RunPythonScript(const std::string& filename)
|
|
|
{
|
|
|
SDL_Log("Python script error in %s:\n%s", filename.c_str(), e.what());
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+void Engine::RequestStateChange(std::unique_ptr<BaseState> newState)
|
|
|
+{
|
|
|
+ pendingNewState = std::move(newState);
|
|
|
+ pendingPop = false;
|
|
|
+}
|
|
|
+
|
|
|
+void Engine::RequestPopState()
|
|
|
+{
|
|
|
+ pendingPop = true;
|
|
|
+ pendingNewState = nullptr;
|
|
|
}
|