[nostalgia] Make core::init take FileSystem and return Context, add core::setWindowTitle

This commit is contained in:
Gary Talent 2021-07-18 15:28:09 -05:00
parent 2d3602fd05
commit 643239e104
10 changed files with 38 additions and 17 deletions

View File

@ -24,12 +24,21 @@ class Drawer {
class Context {
public:
ox::FileSystem *rom = nullptr;
ox::Vector<Drawer*, 5> drawer;
ox::Vector<Drawer*, 5> drawers;
private:
void *m_windowerData = nullptr;
void *m_rendererData = nullptr;
public:
constexpr Context() noexcept = default;
constexpr Context(const Context &&other) noexcept {
rom = ox::move(other.rom);
drawers = ox::move(other.drawers);
m_windowerData = ox::move(other.m_windowerData);
m_rendererData = ox::move(other.m_rendererData);
}
constexpr void setWindowerData(void *windowerData) noexcept {
m_windowerData = windowerData;
}

View File

@ -20,7 +20,7 @@ namespace nostalgia::core {
using event_handler = int(*)(Context*);
ox::Error init(Context *ctx) noexcept;
ox::Result<Context> init(ox::FileSystem *fs) noexcept;
ox::Error run(Context *ctx) noexcept;

View File

@ -40,11 +40,13 @@ static void initTimer() noexcept {
REG_IE = REG_IE | Int_timer0;
}
ox::Error init(Context *ctx) noexcept {
oxReturnError(initGfx(ctx));
ox::Result<Context> init(ox::FileSystem *fs) noexcept {
Context ctx;
ctx.rom = fs;
oxReturnError(initGfx(&ctx));
initTimer();
initIrq();
return OxError(0);
return ctx;
}
void setEventHandler(Context*, event_handler h) noexcept {

View File

@ -100,6 +100,9 @@ ox::Error shutdownGfx(Context*) noexcept {
return OxError(0);
}
void setWindowTitle(Context*, const char*) noexcept {
}
int getScreenWidth(Context*) noexcept {
return 240;
}

View File

@ -142,13 +142,13 @@ char charMap[128] = {
};
void addCustomDrawer(Context *ctx, Drawer *cd) noexcept {
ctx->drawer.emplace_back(cd);
ctx->drawers.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));
for (auto i = 0u; i < ctx->drawers.size(); ++i) {
if (ctx->drawers[i] == cd) {
oxIgnoreError(ctx->drawers.erase(i));
break;
}
}

View File

@ -80,6 +80,8 @@ void addCustomDrawer(Context *ctx, Drawer *cd) noexcept;
void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept;
void setWindowTitle(Context *ctx, const char *title) noexcept;
[[nodiscard]]
int getScreenWidth(Context *ctx) noexcept;

View File

@ -37,15 +37,17 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int
}
}
ox::Error init(Context *ctx) noexcept {
ox::Result<Context> init(ox::FileSystem *fs) noexcept {
Context ctx;
ctx.rom = fs;
const auto id = new GlfwImplData;
ctx->setWindowerData(id);
ctx.setWindowerData(id);
using namespace std::chrono;
id->startTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
glfwInit();
oxReturnError(initGfx(ctx));
oxReturnError(initGfx(&ctx));
glfwSetKeyCallback(id->window, handleGlfwKeyEvent);
return OxError(0);
return ctx;
}
ox::Error run(Context *ctx) noexcept {

View File

@ -57,6 +57,11 @@ ox::Error shutdownGfx(Context *ctx) noexcept {
return OxError(0);
}
void setWindowTitle(Context *ctx, const char *title) noexcept {
const auto id = ctx->windowerData<GlfwImplData>();
glfwSetWindowTitle(id->window, title);
}
int getScreenWidth(Context *ctx) noexcept {
auto id = ctx->windowerData<GlfwImplData>();
int w = 0, h = 0;

View File

@ -278,7 +278,7 @@ void draw(Context *ctx) noexcept {
renderer::drawBackgrounds(id);
//bool showDemo = true;
//ImGui::ShowDemoWindow(&showDemo);
for (const auto cd : ctx->drawer) {
for (const auto cd : ctx->drawers) {
cd->draw(ctx);
}
if constexpr(config::ImGuiEnabled) {

View File

@ -33,9 +33,7 @@ static int eventHandler(core::Context *ctx) noexcept {
}
ox::Error run(ox::FileSystem *fs) noexcept {
core::Context ctx;
ctx.rom = fs;
oxReturnError(core::init(&ctx));
oxRequireM(ctx, core::init(fs));
constexpr auto TileSheetAddr = "/TileSheets/Charset.ng";
constexpr auto PaletteAddr = "/Palettes/Charset.npal";
oxReturnError(core::loadSpriteTileSheet(&ctx, 0, TileSheetAddr, PaletteAddr));