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);
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<std::recursive_mutex> 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<Screen> Engine::getScreen() {
}
void Engine::postRunnable(runnable callback) {
std::lock_guard<std::recursive_mutex> lock(postRunnablesMutex);
postRunnables.push(callback);
}

View File

@ -16,11 +16,13 @@
#include <stdexcept>
#include <string>
#include <vector>
#include <mutex>
class Level;
class Screen;
class EnginePaths;
class ResPaths;
class Batch2D;
namespace fs = std::filesystem;
@ -43,6 +45,7 @@ class Engine {
std::unique_ptr<Content> content = nullptr;
std::unique_ptr<ResPaths> resPaths = nullptr;
std::queue<runnable> 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();