From 62ef230af663cf4ce66aa126f15257180f8cc939 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 29 Mar 2024 16:41:56 +0300 Subject: [PATCH] minor refactor --- src/engine.cpp | 36 ++++++++++++++++++++++-------------- src/engine.h | 5 +++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index 1ae9fe06..75d8722a 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -155,28 +155,34 @@ void Engine::mainloop() { screen->update(delta); if (!Window::isIconified()) { - screen->draw(delta); - - Viewport viewport(Window::width, Window::height); - GfxContext ctx(nullptr, viewport, &batch); - gui->draw(&ctx, assets.get()); - - Window::swapInterval(settings.display.swapInterval); - } else { - Window::swapInterval(1); + renderFrame(batch); } + Window::swapInterval(Window::isIconified() ? 1 : settings.display.swapInterval); - while (!postRunnables.empty()) { - postRunnables.front()(); - postRunnables.pop(); - } - scripting::process_post_runnables(); + processPostRunnables(); Window::swapBuffers(); Events::pollEvents(); } } +void Engine::renderFrame(Batch2D& batch) { + screen->draw(delta); + + Viewport viewport(Window::width, Window::height); + GfxContext ctx(nullptr, viewport, &batch); + gui->draw(&ctx, assets.get()); +} + +void Engine::processPostRunnables() { + std::lock_guard lock(postRunnablesMutex); + while (!postRunnables.empty()) { + postRunnables.front()(); + postRunnables.pop(); + } + scripting::process_post_runnables(); +} + Engine::~Engine() { std::cout << "-- shutting down" << std::endl; if (screen) { @@ -203,6 +209,7 @@ inline const std::string checkPacks( return ""; } +// TODO: refactor this void Engine::loadContent() { auto resdir = paths->getResources(); ContentBuilder contentBuilder; @@ -322,5 +329,6 @@ std::shared_ptr Engine::getScreen() { } void Engine::postRunnable(runnable callback) { + std::lock_guard lock(postRunnablesMutex); postRunnables.push(callback); } diff --git a/src/engine.h b/src/engine.h index b8509cef..0082f8e6 100644 --- a/src/engine.h +++ b/src/engine.h @@ -16,11 +16,13 @@ #include #include #include +#include class Level; class Screen; class EnginePaths; class ResPaths; +class Batch2D; namespace fs = std::filesystem; @@ -43,6 +45,7 @@ class Engine { std::unique_ptr content = nullptr; std::unique_ptr resPaths = nullptr; std::queue postRunnables; + std::recursive_mutex postRunnablesMutex; uint64_t frame = 0; double lastTime = 0.0; @@ -52,6 +55,8 @@ class Engine { void updateTimers(); void updateHotkeys(); + void renderFrame(Batch2D& batch); + void processPostRunnables(); public: Engine(EngineSettings& settings, EnginePaths* paths); ~Engine();