minor refactor

This commit is contained in:
MihailRis 2024-03-29 16:41:56 +03:00
parent 32120d8af4
commit 62ef230af6
2 changed files with 27 additions and 14 deletions

View File

@ -155,28 +155,34 @@ void Engine::mainloop() {
screen->update(delta); screen->update(delta);
if (!Window::isIconified()) { if (!Window::isIconified()) {
screen->draw(delta); renderFrame(batch);
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);
} }
Window::swapInterval(Window::isIconified() ? 1 : settings.display.swapInterval);
while (!postRunnables.empty()) { processPostRunnables();
postRunnables.front()();
postRunnables.pop();
}
scripting::process_post_runnables();
Window::swapBuffers(); Window::swapBuffers();
Events::pollEvents(); 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<std::recursive_mutex> lock(postRunnablesMutex);
while (!postRunnables.empty()) {
postRunnables.front()();
postRunnables.pop();
}
scripting::process_post_runnables();
}
Engine::~Engine() { Engine::~Engine() {
std::cout << "-- shutting down" << std::endl; std::cout << "-- shutting down" << std::endl;
if (screen) { if (screen) {
@ -203,6 +209,7 @@ inline const std::string checkPacks(
return ""; return "";
} }
// TODO: refactor this
void Engine::loadContent() { void Engine::loadContent() {
auto resdir = paths->getResources(); auto resdir = paths->getResources();
ContentBuilder contentBuilder; ContentBuilder contentBuilder;
@ -322,5 +329,6 @@ std::shared_ptr<Screen> Engine::getScreen() {
} }
void Engine::postRunnable(runnable callback) { void Engine::postRunnable(runnable callback) {
std::lock_guard<std::recursive_mutex> lock(postRunnablesMutex);
postRunnables.push(callback); postRunnables.push(callback);
} }

View File

@ -16,11 +16,13 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <vector> #include <vector>
#include <mutex>
class Level; class Level;
class Screen; class Screen;
class EnginePaths; class EnginePaths;
class ResPaths; class ResPaths;
class Batch2D;
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -43,6 +45,7 @@ class Engine {
std::unique_ptr<Content> content = nullptr; std::unique_ptr<Content> content = nullptr;
std::unique_ptr<ResPaths> resPaths = nullptr; std::unique_ptr<ResPaths> resPaths = nullptr;
std::queue<runnable> postRunnables; std::queue<runnable> postRunnables;
std::recursive_mutex postRunnablesMutex;
uint64_t frame = 0; uint64_t frame = 0;
double lastTime = 0.0; double lastTime = 0.0;
@ -52,6 +55,8 @@ class Engine {
void updateTimers(); void updateTimers();
void updateHotkeys(); void updateHotkeys();
void renderFrame(Batch2D& batch);
void processPostRunnables();
public: public:
Engine(EngineSettings& settings, EnginePaths* paths); Engine(EngineSettings& settings, EnginePaths* paths);
~Engine(); ~Engine();