From 8174d04b0654e1f6a43afbaa158d7d1ce13a2cb1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 3 Feb 2022 19:57:43 -0600 Subject: [PATCH] [nostalgia] Collapse NostalgiaCore down to a single library and cleanup impl data access --- src/nostalgia/core/CMakeLists.txt | 57 +++++++++++++++++++--- src/nostalgia/core/color.hpp | 1 - src/nostalgia/core/context.cpp | 9 ++++ src/nostalgia/core/context.hpp | 46 +++++++++++++++-- src/nostalgia/core/core.hpp | 11 ++--- src/nostalgia/core/event.hpp | 17 +++++++ src/nostalgia/core/gba/CMakeLists.txt | 41 ---------------- src/nostalgia/core/gfx.hpp | 29 +++++------ src/nostalgia/core/glfw/CMakeLists.txt | 33 ------------- src/nostalgia/core/glfw/core.cpp | 3 +- src/nostalgia/core/glfw/gfx.cpp | 12 ++--- src/nostalgia/core/sdl/CMakeLists.txt | 26 ---------- src/nostalgia/core/studio/CMakeLists.txt | 2 +- src/nostalgia/core/userland/CMakeLists.txt | 29 ----------- src/nostalgia/core/userland/gfx.hpp | 6 +-- src/nostalgia/core/userland/gfx_opengl.cpp | 2 + src/nostalgia/player/CMakeLists.txt | 3 +- src/nostalgia/studio/CMakeLists.txt | 2 +- src/nostalgia/studio/lib/CMakeLists.txt | 3 +- src/nostalgia/studio/studioapp.cpp | 2 +- 20 files changed, 152 insertions(+), 182 deletions(-) create mode 100644 src/nostalgia/core/context.cpp create mode 100644 src/nostalgia/core/event.hpp delete mode 100644 src/nostalgia/core/gba/CMakeLists.txt delete mode 100644 src/nostalgia/core/glfw/CMakeLists.txt delete mode 100644 src/nostalgia/core/sdl/CMakeLists.txt delete mode 100644 src/nostalgia/core/userland/CMakeLists.txt diff --git a/src/nostalgia/core/CMakeLists.txt b/src/nostalgia/core/CMakeLists.txt index 96d3c861..d5c65486 100644 --- a/src/nostalgia/core/CMakeLists.txt +++ b/src/nostalgia/core/CMakeLists.txt @@ -1,7 +1,56 @@ +if(NOT NOSTALGIA_BUILD_TYPE STREQUAL "GBA") + find_package(glfw3 REQUIRED) + find_package(imgui REQUIRED) + if(APPLE) + find_package(OpenGL REQUIRED) + else() + set(OPENGL_gl_LIBRARY GL) + endif() + set( + NOSTALGIA_CORE_IMPL_SRC + glfw/clipboard.cpp + glfw/core.cpp + glfw/gfx.cpp + userland/gfx.cpp + userland/gfx_opengl.cpp + userland/media.cpp + ) + set( + NOSTALGIA_CORE_IMPL_LIBS + ${OPENGL_gl_LIBRARY} + glfw::glfw + imgui::imgui + imgui-glfw + NostalgiaGlUtils + ) +else() + enable_language(C ASM) + set_source_files_properties(core.arm.cpp irq.arm.cpp PROPERTIES COMPILE_FLAGS -marm) + set( + NOSTALGIA_CORE_IMPL_SRC + gba/bios.s + gba/core.arm.cpp + gba/core.cpp + gba/gfx.cpp + gba/irq.arm.cpp + gba/irq.s + gba/media.cpp + gba/panic.cpp + ) + set( + NOSTALGIA_CORE_IMPL_LIBS + GbaStartup + OxFS + OxStd + ) +endif() + add_library( NostalgiaCore + context.cpp gfx.cpp media.cpp + ${NOSTALGIA_CORE_IMPL_SRC} ) if(NOT MSVC) @@ -12,14 +61,9 @@ target_link_libraries( NostalgiaCore PUBLIC OxClaw OxFS + ${NOSTALGIA_CORE_IMPL_LIBS} ) -add_subdirectory(gba) -if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") - add_subdirectory(glfw) - #add_subdirectory(sdl) - add_subdirectory(userland) -endif() if(NOSTALGIA_BUILD_STUDIO) add_subdirectory(studio) endif() @@ -32,6 +76,7 @@ install( consts.hpp context.hpp core.hpp + event.hpp gfx.hpp input.hpp media.hpp diff --git a/src/nostalgia/core/color.hpp b/src/nostalgia/core/color.hpp index 6f6c6d16..63d87db7 100644 --- a/src/nostalgia/core/color.hpp +++ b/src/nostalgia/core/color.hpp @@ -17,7 +17,6 @@ using Color16 = uint16_t; * Nostalgia Core logically uses 16 bit colors, but must translate that to 32 * bit colors in some implementations. */ - using Color32 = uint32_t; [[nodiscard]] diff --git a/src/nostalgia/core/context.cpp b/src/nostalgia/core/context.cpp new file mode 100644 index 00000000..352da561 --- /dev/null +++ b/src/nostalgia/core/context.cpp @@ -0,0 +1,9 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#include "context.hpp" + +namespace nostalgia::core { + +} \ No newline at end of file diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp index 6f190d5e..38321790 100644 --- a/src/nostalgia/core/context.hpp +++ b/src/nostalgia/core/context.hpp @@ -7,6 +7,11 @@ #include #include "assetmanager.hpp" +#include "event.hpp" + +namespace nostalgia::common { +class Size; +} namespace nostalgia::core { @@ -18,8 +23,40 @@ class Drawer { virtual void draw(Context*) noexcept = 0; }; +namespace renderer { +ox::Error init(Context *ctx) noexcept; +ox::Error shutdown(Context *ctx) noexcept; +ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept; +} + // User Input Output class Context { + friend bool bgStatus(Context *ctx, unsigned bg) noexcept; + friend common::Size getScreenSize(Context *ctx) noexcept; + friend int getScreenHeight(Context *ctx) noexcept; + friend int getScreenWidth(Context *ctx) noexcept; + friend ox::Error initGfx(Context *ctx) noexcept; + friend ox::Error renderer::init(Context *ctx) noexcept; + friend ox::Error renderer::loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept; + friend ox::Error renderer::shutdown(Context *ctx) noexcept; + friend ox::Error run(Context *ctx) noexcept; + friend ox::Error shutdown(Context *ctx) noexcept; + friend ox::Result> init(ox::UniquePtr fs, const char *appName) noexcept; + friend ox::String getClipboardText(Context *ctx) noexcept; + friend uint64_t ticksMs(Context *ctx) noexcept; + friend uint8_t bgStatus(Context *ctx) noexcept; + friend void clearTileLayer(Context *ctx, int layer) noexcept; + friend void draw(Context *ctx) noexcept; + friend void focusWindow(Context *ctx) noexcept; + friend void setBgStatus(Context *ctx, uint32_t status) noexcept; + friend void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept; + friend void setClipboardText(Context *ctx, const ox::String &text) noexcept; + friend void setEventHandler(Context *ctx, event_handler h) noexcept; + friend void setRendererData(Context *ctx, void *rendererData) noexcept; + friend void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) noexcept; + friend void setWindowTitle(Context *ctx, const char *title) noexcept; + friend void setWindowerData(Context *ctx, void *windowerData) noexcept; + public: ox::UniquePtr rom; ox::Vector drawers; @@ -49,16 +86,17 @@ class Context { return static_cast(m_customData); } - constexpr void setWindowerData(void *windowerData) noexcept { - m_windowerData = windowerData; - } - + protected: template [[nodiscard]] constexpr T *windowerData() noexcept { return static_cast(m_windowerData); } + constexpr void setWindowerData(void *windowerData) noexcept { + m_windowerData = windowerData; + } + constexpr void setRendererData(void *rendererData) noexcept { m_rendererData = rendererData; } diff --git a/src/nostalgia/core/core.hpp b/src/nostalgia/core/core.hpp index 13337dc0..c7af77ea 100644 --- a/src/nostalgia/core/core.hpp +++ b/src/nostalgia/core/core.hpp @@ -9,27 +9,22 @@ #include "assetmanager.hpp" #include "clipboard.hpp" #include "consts.hpp" +#include "event.hpp" #include "gfx.hpp" #include "input.hpp" #include "media.hpp" namespace nostalgia::core { -using event_handler = int(*)(Context*); - ox::Result> init(ox::UniquePtr fs, const char *appName = "Nostalgia") noexcept; ox::Error run(Context *ctx) noexcept; -// Sets event handler that sleeps for the time given in the return value. The -// sleep time is a minimum of ~16 milliseconds. -void setEventHandler(Context *ctx, event_handler) noexcept; - // Returns the number of milliseconds that have passed since the start of the -// program. +// program. [[nodiscard]] uint64_t ticksMs(Context *ctx) noexcept; -void shutdown(Context *ctx) noexcept; +ox::Error shutdown(Context *ctx) noexcept; } diff --git a/src/nostalgia/core/event.hpp b/src/nostalgia/core/event.hpp new file mode 100644 index 00000000..a93a2f79 --- /dev/null +++ b/src/nostalgia/core/event.hpp @@ -0,0 +1,17 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#pragma once + +namespace nostalgia::core { + +class Context; + +using event_handler = int(*)(Context*); + +// Sets event handler that sleeps for the time given in the return value. The +// sleep time is a minimum of ~16 milliseconds. +void setEventHandler(Context *ctx, event_handler) noexcept; + +} diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt deleted file mode 100644 index 30329095..00000000 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ /dev/null @@ -1,41 +0,0 @@ -if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") - enable_language(C ASM) - - add_library( - NostalgiaCore-GBA - bios.s - core.arm.cpp - core.cpp - gfx.cpp - irq.arm.cpp - irq.s - media.cpp - panic.cpp - ) - - set_source_files_properties(core.arm.cpp irq.arm.cpp PROPERTIES COMPILE_FLAGS -marm) - - target_link_libraries( - NostalgiaCore-GBA PUBLIC - NostalgiaCore - GbaStartup - OxFS - OxStd - ) - - install( - TARGETS - NostalgiaCore-GBA - DESTINATION - LIBRARY DESTINATION lib/nostalgia - ARCHIVE DESTINATION lib/nostalgia - ) -endif() - -# tests -if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") - add_executable(NostalgiaCore-GBA_Test - tests.cpp - ) - target_link_libraries(NostalgiaCore-GBA_Test NostalgiaCore) -endif() diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 2f86f0f5..71b415d3 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include "color.hpp" @@ -37,24 +38,18 @@ struct NostalgiaGraphic { ox::Vector pixels; }; -template -constexpr ox::Error model(T *io, NostalgiaPalette *pal) noexcept { - io->template setTypeInfo(); - oxReturnError(io->field("colors", &pal->colors)); - return OxError(0); -} +oxModelBegin(NostalgiaPalette) + oxModelField(colors) +oxModelEnd() -template -constexpr ox::Error model(T *io, NostalgiaGraphic *ng) noexcept { - io->template setTypeInfo(); - oxReturnError(io->field("bpp", &ng->bpp)); - oxReturnError(io->field("rows", &ng->rows)); - oxReturnError(io->field("columns", &ng->columns)); - oxReturnError(io->field("defaultPalette", &ng->defaultPalette)); - oxReturnError(io->field("pal", &ng->pal)); - oxReturnError(io->field("pixels", &ng->pixels)); - return OxError(0); -} +oxModelBegin(NostalgiaGraphic) + oxModelField(bpp) + oxModelField(rows) + oxModelField(columns) + oxModelField(defaultPalette) + oxModelField(pal) + oxModelField(pixels) +oxModelEnd() struct Sprite { unsigned idx = 0; diff --git a/src/nostalgia/core/glfw/CMakeLists.txt b/src/nostalgia/core/glfw/CMakeLists.txt deleted file mode 100644 index cb88996f..00000000 --- a/src/nostalgia/core/glfw/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -add_library( - NostalgiaCore-GLFW - clipboard.cpp - core.cpp - gfx.cpp -) - -find_package(glfw3 REQUIRED) -find_package(imgui REQUIRED) - -if(APPLE) - find_package(OpenGL REQUIRED) -else() - set(OPENGL_gl_LIBRARY GL) -endif() - -target_link_libraries( - NostalgiaCore-GLFW PUBLIC - ${OPENGL_gl_LIBRARY} - glfw::glfw - imgui::imgui - imgui-glfw - NostalgiaGlUtils - NostalgiaCore-Userspace -) - -install( - TARGETS - NostalgiaCore-GLFW - DESTINATION - LIBRARY DESTINATION lib/nostalgia - ARCHIVE DESTINATION lib/nostalgia -) diff --git a/src/nostalgia/core/glfw/core.cpp b/src/nostalgia/core/glfw/core.cpp index 952b1652..d754233e 100644 --- a/src/nostalgia/core/glfw/core.cpp +++ b/src/nostalgia/core/glfw/core.cpp @@ -68,9 +68,10 @@ bool buttonDown(Key) noexcept { return false; } -void shutdown(Context *ctx) noexcept { +ox::Error shutdown(Context *ctx) noexcept { const auto id = ctx->windowerData(); glfwSetWindowShouldClose(id->window, true); + return OxError(0); } diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp index aa6aa189..61885e35 100644 --- a/src/nostalgia/core/glfw/gfx.cpp +++ b/src/nostalgia/core/glfw/gfx.cpp @@ -16,18 +16,14 @@ namespace nostalgia::core { constexpr auto Scale = 5; static void handleGlfwError(int err, const char *desc) noexcept { - oxErrf("GLFW error ({}): {}\n", err, desc); -} - -void ImGui_Impl_NewFrame() noexcept { - ImGui_ImplGlfw_NewFrame(); + oxErrf("GLFW error ({}): {}\n", err, desc); } static void handleKeyPress(Context *ctx, int key) noexcept { switch (key) { case GLFW_KEY_ESCAPE: case GLFW_KEY_Q: - shutdown(ctx); + oxIgnoreError(shutdown(ctx)); break; default: break; @@ -41,6 +37,10 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int } } +void ImGui_Impl_NewFrame() noexcept { + ImGui_ImplGlfw_NewFrame(); +} + ox::Error initGfx(Context *ctx) noexcept { auto id = ctx->windowerData(); glfwSetErrorCallback(handleGlfwError); diff --git a/src/nostalgia/core/sdl/CMakeLists.txt b/src/nostalgia/core/sdl/CMakeLists.txt deleted file mode 100644 index 1c47d389..00000000 --- a/src/nostalgia/core/sdl/CMakeLists.txt +++ /dev/null @@ -1,26 +0,0 @@ -find_package(SDL2 REQUIRED) - -if(SDL2_FOUND) - - add_library( - NostalgiaCore-SDL - core.cpp - gfx.cpp - ) - - target_link_libraries( - NostalgiaCore-SDL PUBLIC - SDL2::SDL2 - NostalgiaGlUtils - NostalgiaCore-Userspace - ) - - install( - TARGETS - NostalgiaCore-SDL - DESTINATION - LIBRARY DESTINATION lib/nostalgia - ARCHIVE DESTINATION lib/nostalgia - ) - -endif() \ No newline at end of file diff --git a/src/nostalgia/core/studio/CMakeLists.txt b/src/nostalgia/core/studio/CMakeLists.txt index bf7ef63d..7bf32750 100644 --- a/src/nostalgia/core/studio/CMakeLists.txt +++ b/src/nostalgia/core/studio/CMakeLists.txt @@ -20,7 +20,7 @@ endif() target_link_libraries( NostalgiaCore-Studio PUBLIC NostalgiaStudio - NostalgiaCore-GLFW + NostalgiaCore NostalgiaGlUtils ) diff --git a/src/nostalgia/core/userland/CMakeLists.txt b/src/nostalgia/core/userland/CMakeLists.txt deleted file mode 100644 index 74688d02..00000000 --- a/src/nostalgia/core/userland/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ -add_library( - NostalgiaCore-Userspace OBJECT - gfx.cpp - gfx_opengl.cpp - media.cpp -) - -if(NOT MSVC) - target_compile_options(NostalgiaCore-Userspace PRIVATE -Wsign-conversion) -endif() - -find_package(imgui REQUIRED) - -target_link_libraries( - NostalgiaCore-Userspace PUBLIC - imgui::imgui - OxClaw - OxFS - OxStd - NostalgiaCore - NostalgiaGlUtils -) - -install( - TARGETS - NostalgiaCore-Userspace - DESTINATION - include/nostalgia/core -) diff --git a/src/nostalgia/core/userland/gfx.hpp b/src/nostalgia/core/userland/gfx.hpp index 3d1ceb4a..dfc83ae1 100644 --- a/src/nostalgia/core/userland/gfx.hpp +++ b/src/nostalgia/core/userland/gfx.hpp @@ -10,10 +10,10 @@ namespace nostalgia::core::renderer { -ox::Error init(Context *ctx); +ox::Error init(Context *ctx) noexcept; -ox::Error shutdown(Context *ctx); +ox::Error shutdown(Context *ctx) noexcept; -ox::Error loadBgTexture(Context *ctx, int section, void *bytes, int w, int h); +ox::Error loadBgTexture(Context *ctx, int section, void *bytes, int w, int h) noexcept; } diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 2ecd82e3..75dacb7f 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -12,6 +12,8 @@ #include #include +#include "nostalgia/core/context.hpp" + namespace nostalgia::core { diff --git a/src/nostalgia/player/CMakeLists.txt b/src/nostalgia/player/CMakeLists.txt index f5676528..92639d3e 100644 --- a/src/nostalgia/player/CMakeLists.txt +++ b/src/nostalgia/player/CMakeLists.txt @@ -21,8 +21,7 @@ endif() target_link_libraries( nostalgia NostalgiaWorld - $<$:NostalgiaCore-GBA> - $<$:NostalgiaCore-GLFW> + NostalgiaCore ) install( diff --git a/src/nostalgia/studio/CMakeLists.txt b/src/nostalgia/studio/CMakeLists.txt index e71f9530..598d1391 100644 --- a/src/nostalgia/studio/CMakeLists.txt +++ b/src/nostalgia/studio/CMakeLists.txt @@ -14,7 +14,7 @@ target_link_libraries( OxClArgs OxFS NostalgiaCore-Studio - NostalgiaCore-Userspace + NostalgiaCore NostalgiaStudio NostalgiaPack ) diff --git a/src/nostalgia/studio/lib/CMakeLists.txt b/src/nostalgia/studio/lib/CMakeLists.txt index cb889eeb..22ebecad 100644 --- a/src/nostalgia/studio/lib/CMakeLists.txt +++ b/src/nostalgia/studio/lib/CMakeLists.txt @@ -45,8 +45,7 @@ target_link_libraries( OxEvent OxFS OxClaw - NostalgiaCore-Userspace - NostalgiaCore-GLFW + NostalgiaCore ) install( diff --git a/src/nostalgia/studio/studioapp.cpp b/src/nostalgia/studio/studioapp.cpp index ab9f3def..5c1e19dc 100644 --- a/src/nostalgia/studio/studioapp.cpp +++ b/src/nostalgia/studio/studioapp.cpp @@ -83,7 +83,7 @@ void StudioUI::drawMenu() noexcept { if (ImGui::MenuItem("Save", "Ctrl+S", false, m_saveEnabled)) { } if (ImGui::MenuItem("Quit", "Ctrl+Q")) { - core::shutdown(m_ctx); + oxIgnoreError(core::shutdown(m_ctx)); } ImGui::EndMenu(); }