From 0420dfb5452d67ac484b029d2a6f6818996451de Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 17 Jul 2021 18:13:28 -0500 Subject: [PATCH] [nostaliga/core] Add Drawer system and make ImGui use configurable --- src/nostalgia/core/config.hpp | 13 ++++++++++ src/nostalgia/core/context.hpp | 9 +++++++ src/nostalgia/core/gfx.cpp | 13 ++++++++++ src/nostalgia/core/gfx.hpp | 4 +++ src/nostalgia/core/glfw/gfx.cpp | 20 +++++++-------- src/nostalgia/core/userland/gfx_opengl.cpp | 29 ++++++++++++---------- 6 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/nostalgia/core/config.hpp b/src/nostalgia/core/config.hpp index ab1cd1be..b19fc472 100644 --- a/src/nostalgia/core/config.hpp +++ b/src/nostalgia/core/config.hpp @@ -8,12 +8,25 @@ #pragma once +#if __has_include() +#define IMGUI_IMPL_OPENGL_ES3 +#include +#include +#endif + #include namespace nostalgia::core { namespace config { +constexpr auto ImGuiEnabled = +#if __has_include() +true; +#else +false; +#endif + enum class SdlVsync { Adaptive = -1, Off = 0, diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp index bc36ca15..1f1af2bf 100644 --- a/src/nostalgia/core/context.hpp +++ b/src/nostalgia/core/context.hpp @@ -12,10 +12,19 @@ namespace nostalgia::core { +class Context; + +class Drawer { + public: + virtual ~Drawer() = default; + virtual void draw(Context*) noexcept = 0; +}; + // User Input Output class Context { public: ox::FileSystem *rom = nullptr; + ox::Vector drawer; private: void *m_windowerData = nullptr; void *m_rendererData = nullptr; diff --git a/src/nostalgia/core/gfx.cpp b/src/nostalgia/core/gfx.cpp index 0aa72cab..01912e8c 100644 --- a/src/nostalgia/core/gfx.cpp +++ b/src/nostalgia/core/gfx.cpp @@ -141,6 +141,19 @@ char charMap[128] = { 0, // ~ }; +void addCustomDrawer(Context *ctx, Drawer *cd) noexcept { + ctx->drawer.emplace_back(cd); +} + +void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept { + for (auto i = 0u; i < ctx->drawer.size(); ++i) { + if (ctx->drawer[i] == cd) { + oxIgnoreError(ctx->drawer.erase(i)); + break; + } + } +} + void setSprite(Context *c, const Sprite &s) noexcept { setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX); } diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 80602b0a..b416c168 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -76,6 +76,10 @@ ox::Error initGfx(Context *ctx) noexcept; ox::Error shutdownGfx(Context *ctx) noexcept; +void addCustomDrawer(Context *ctx, Drawer *cd) noexcept; + +void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept; + [[nodiscard]] int getScreenWidth(Context *ctx) noexcept; diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp index 3f066b7e..f0690726 100644 --- a/src/nostalgia/core/glfw/gfx.cpp +++ b/src/nostalgia/core/glfw/gfx.cpp @@ -7,9 +7,8 @@ */ #include -#include -#include +#include #include #include "core.hpp" @@ -38,14 +37,15 @@ ox::Error initGfx(Context *ctx) noexcept { } glfwSetWindowUserPointer(id->window, ctx); glfwMakeContextCurrent(id->window); - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - auto &io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - io.MouseDrawCursor = true; - ImGui_ImplGlfw_InitForOpenGL(id->window, true); - oxReturnError(renderer::init(ctx)); - return OxError(0); + if constexpr(config::ImGuiEnabled) { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + auto &io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.MouseDrawCursor = true; + ImGui_ImplGlfw_InitForOpenGL(id->window, true); + } + return renderer::init(ctx); } ox::Error shutdownGfx(Context *ctx) noexcept { diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 06b1b86c..84a1f762 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -10,10 +10,6 @@ #include -#define IMGUI_IMPL_OPENGL_ES3 -#include -#include - #include #include @@ -232,7 +228,7 @@ ox::Error shutdown(Context *ctx) noexcept { ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept { oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h); - const auto &id = ctx->rendererData(); + const auto id = ctx->rendererData(); auto &tex = id->backgrounds[static_cast(section)].tex; tex = loadTexture(w, h, pixels); return OxError(0); @@ -241,7 +237,7 @@ ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) n } uint8_t bgStatus(Context *ctx) noexcept { - const auto &id = ctx->rendererData(); + const auto id = ctx->rendererData(); uint8_t out = 0; for (unsigned i = 0; i < id->backgrounds.size(); ++i) { out |= id->backgrounds[i].enabled << i; @@ -250,19 +246,19 @@ uint8_t bgStatus(Context *ctx) noexcept { } void setBgStatus(Context *ctx, uint32_t status) noexcept { - const auto &id = ctx->rendererData(); + const auto id = ctx->rendererData(); for (unsigned i = 0; i < id->backgrounds.size(); ++i) { id->backgrounds[i].enabled = (status >> i) & 1; } } bool bgStatus(Context *ctx, unsigned bg) noexcept { - const auto &id = ctx->rendererData(); + const auto id = ctx->rendererData(); return id->backgrounds[bg].enabled; } void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept { - const auto &id = ctx->rendererData(); + const auto id = ctx->rendererData(); id->backgrounds[bg].enabled = status; } @@ -272,16 +268,23 @@ void draw(Context *ctx) noexcept { renderer::tickFps(id); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); + if constexpr(config::ImGuiEnabled) { + ImGui::NewFrame(); + } // clear screen glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); // render + renderer::drawBackgrounds(id); //bool showDemo = true; //ImGui::ShowDemoWindow(&showDemo); - renderer::drawBackgrounds(id); - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + for (const auto cd : ctx->drawer) { + cd->draw(ctx); + } + if constexpr(config::ImGuiEnabled) { + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + } } void clearTileLayer(Context *ctx, int layer) noexcept {