[nostaliga/core] Add Drawer system and make ImGui use configurable

This commit is contained in:
Gary Talent 2021-07-17 18:13:28 -05:00
parent d3a3d57773
commit 0420dfb545
6 changed files with 65 additions and 23 deletions

View File

@ -8,12 +8,25 @@
#pragma once #pragma once
#if __has_include(<imgui.h>)
#define IMGUI_IMPL_OPENGL_ES3
#include <imgui_impl_opengl3.h>
#include <imgui_impl_glfw.h>
#endif
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
namespace nostalgia::core { namespace nostalgia::core {
namespace config { namespace config {
constexpr auto ImGuiEnabled =
#if __has_include(<imgui.h>)
true;
#else
false;
#endif
enum class SdlVsync { enum class SdlVsync {
Adaptive = -1, Adaptive = -1,
Off = 0, Off = 0,

View File

@ -12,10 +12,19 @@
namespace nostalgia::core { namespace nostalgia::core {
class Context;
class Drawer {
public:
virtual ~Drawer() = default;
virtual void draw(Context*) noexcept = 0;
};
// User Input Output // User Input Output
class Context { class Context {
public: public:
ox::FileSystem *rom = nullptr; ox::FileSystem *rom = nullptr;
ox::Vector<Drawer*, 5> drawer;
private: private:
void *m_windowerData = nullptr; void *m_windowerData = nullptr;
void *m_rendererData = nullptr; void *m_rendererData = nullptr;

View File

@ -141,6 +141,19 @@ char charMap[128] = {
0, // ~ 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 { void setSprite(Context *c, const Sprite &s) noexcept {
setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX); setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX);
} }

View File

@ -76,6 +76,10 @@ ox::Error initGfx(Context *ctx) noexcept;
ox::Error shutdownGfx(Context *ctx) noexcept; ox::Error shutdownGfx(Context *ctx) noexcept;
void addCustomDrawer(Context *ctx, Drawer *cd) noexcept;
void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept;
[[nodiscard]] [[nodiscard]]
int getScreenWidth(Context *ctx) noexcept; int getScreenWidth(Context *ctx) noexcept;

View File

@ -7,9 +7,8 @@
*/ */
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <nostalgia/core/config.hpp>
#include <nostalgia/core/userland/gfx.hpp> #include <nostalgia/core/userland/gfx.hpp>
#include "core.hpp" #include "core.hpp"
@ -38,14 +37,15 @@ ox::Error initGfx(Context *ctx) noexcept {
} }
glfwSetWindowUserPointer(id->window, ctx); glfwSetWindowUserPointer(id->window, ctx);
glfwMakeContextCurrent(id->window); glfwMakeContextCurrent(id->window);
IMGUI_CHECKVERSION(); if constexpr(config::ImGuiEnabled) {
ImGui::CreateContext(); IMGUI_CHECKVERSION();
auto &io = ImGui::GetIO(); ImGui::CreateContext();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; auto &io = ImGui::GetIO();
io.MouseDrawCursor = true; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui_ImplGlfw_InitForOpenGL(id->window, true); io.MouseDrawCursor = true;
oxReturnError(renderer::init(ctx)); ImGui_ImplGlfw_InitForOpenGL(id->window, true);
return OxError(0); }
return renderer::init(ctx);
} }
ox::Error shutdownGfx(Context *ctx) noexcept { ox::Error shutdownGfx(Context *ctx) noexcept {

View File

@ -10,10 +10,6 @@
#include <nostalgia/glutils/glutils.hpp> #include <nostalgia/glutils/glutils.hpp>
#define IMGUI_IMPL_OPENGL_ES3
#include <imgui_impl_opengl3.h>
#include <imgui_impl_glfw.h>
#include <ox/std/bit.hpp> #include <ox/std/bit.hpp>
#include <ox/std/fmt.hpp> #include <ox/std/fmt.hpp>
@ -232,7 +228,7 @@ ox::Error shutdown(Context *ctx) noexcept {
ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) 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); oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h);
const auto &id = ctx->rendererData<GlImplData>(); const auto id = ctx->rendererData<GlImplData>();
auto &tex = id->backgrounds[static_cast<std::size_t>(section)].tex; auto &tex = id->backgrounds[static_cast<std::size_t>(section)].tex;
tex = loadTexture(w, h, pixels); tex = loadTexture(w, h, pixels);
return OxError(0); 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 { uint8_t bgStatus(Context *ctx) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
uint8_t out = 0; uint8_t out = 0;
for (unsigned i = 0; i < id->backgrounds.size(); ++i) { for (unsigned i = 0; i < id->backgrounds.size(); ++i) {
out |= id->backgrounds[i].enabled << 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 { void setBgStatus(Context *ctx, uint32_t status) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
for (unsigned i = 0; i < id->backgrounds.size(); ++i) { for (unsigned i = 0; i < id->backgrounds.size(); ++i) {
id->backgrounds[i].enabled = (status >> i) & 1; id->backgrounds[i].enabled = (status >> i) & 1;
} }
} }
bool bgStatus(Context *ctx, unsigned bg) noexcept { bool bgStatus(Context *ctx, unsigned bg) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
return id->backgrounds[bg].enabled; return id->backgrounds[bg].enabled;
} }
void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept { void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
id->backgrounds[bg].enabled = status; id->backgrounds[bg].enabled = status;
} }
@ -272,16 +268,23 @@ void draw(Context *ctx) noexcept {
renderer::tickFps(id); renderer::tickFps(id);
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); if constexpr(config::ImGuiEnabled) {
ImGui::NewFrame();
}
// clear screen // clear screen
glClearColor(0, 0, 0, 1); glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
// render // render
renderer::drawBackgrounds(id);
//bool showDemo = true; //bool showDemo = true;
//ImGui::ShowDemoWindow(&showDemo); //ImGui::ShowDemoWindow(&showDemo);
renderer::drawBackgrounds(id); for (const auto cd : ctx->drawer) {
ImGui::Render(); cd->draw(ctx);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); }
if constexpr(config::ImGuiEnabled) {
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
} }
void clearTileLayer(Context *ctx, int layer) noexcept { void clearTileLayer(Context *ctx, int layer) noexcept {