[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
#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>
namespace nostalgia::core {
namespace config {
constexpr auto ImGuiEnabled =
#if __has_include(<imgui.h>)
true;
#else
false;
#endif
enum class SdlVsync {
Adaptive = -1,
Off = 0,

View File

@ -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*, 5> drawer;
private:
void *m_windowerData = nullptr;
void *m_rendererData = nullptr;

View File

@ -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);
}

View File

@ -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;

View File

@ -7,9 +7,8 @@
*/
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <nostalgia/core/config.hpp>
#include <nostalgia/core/userland/gfx.hpp>
#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 {

View File

@ -10,10 +10,6 @@
#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/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 {
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;
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<renderer::GlImplData>();
const auto id = ctx->rendererData<renderer::GlImplData>();
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<renderer::GlImplData>();
const auto id = ctx->rendererData<renderer::GlImplData>();
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<renderer::GlImplData>();
const auto id = ctx->rendererData<renderer::GlImplData>();
return id->backgrounds[bg].enabled;
}
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;
}
@ -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 {