[nostalgia] Collapse NostalgiaCore down to a single library and cleanup impl data access

This commit is contained in:
Gary Talent 2022-02-03 19:57:43 -06:00
parent d4e903b593
commit 8174d04b06
20 changed files with 152 additions and 182 deletions

View File

@ -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( add_library(
NostalgiaCore NostalgiaCore
context.cpp
gfx.cpp gfx.cpp
media.cpp media.cpp
${NOSTALGIA_CORE_IMPL_SRC}
) )
if(NOT MSVC) if(NOT MSVC)
@ -12,14 +61,9 @@ target_link_libraries(
NostalgiaCore PUBLIC NostalgiaCore PUBLIC
OxClaw OxClaw
OxFS 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) if(NOSTALGIA_BUILD_STUDIO)
add_subdirectory(studio) add_subdirectory(studio)
endif() endif()
@ -32,6 +76,7 @@ install(
consts.hpp consts.hpp
context.hpp context.hpp
core.hpp core.hpp
event.hpp
gfx.hpp gfx.hpp
input.hpp input.hpp
media.hpp media.hpp

View File

@ -17,7 +17,6 @@ using Color16 = uint16_t;
* Nostalgia Core logically uses 16 bit colors, but must translate that to 32 * Nostalgia Core logically uses 16 bit colors, but must translate that to 32
* bit colors in some implementations. * bit colors in some implementations.
*/ */
using Color32 = uint32_t; using Color32 = uint32_t;
[[nodiscard]] [[nodiscard]]

View File

@ -0,0 +1,9 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "context.hpp"
namespace nostalgia::core {
}

View File

@ -7,6 +7,11 @@
#include <ox/fs/fs.hpp> #include <ox/fs/fs.hpp>
#include "assetmanager.hpp" #include "assetmanager.hpp"
#include "event.hpp"
namespace nostalgia::common {
class Size;
}
namespace nostalgia::core { namespace nostalgia::core {
@ -18,8 +23,40 @@ class Drawer {
virtual void draw(Context*) noexcept = 0; 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 // User Input Output
class Context { 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<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> 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: public:
ox::UniquePtr<ox::FileSystem> rom; ox::UniquePtr<ox::FileSystem> rom;
ox::Vector<Drawer*, 5> drawers; ox::Vector<Drawer*, 5> drawers;
@ -49,16 +86,17 @@ class Context {
return static_cast<T*>(m_customData); return static_cast<T*>(m_customData);
} }
constexpr void setWindowerData(void *windowerData) noexcept { protected:
m_windowerData = windowerData;
}
template<typename T> template<typename T>
[[nodiscard]] [[nodiscard]]
constexpr T *windowerData() noexcept { constexpr T *windowerData() noexcept {
return static_cast<T*>(m_windowerData); return static_cast<T*>(m_windowerData);
} }
constexpr void setWindowerData(void *windowerData) noexcept {
m_windowerData = windowerData;
}
constexpr void setRendererData(void *rendererData) noexcept { constexpr void setRendererData(void *rendererData) noexcept {
m_rendererData = rendererData; m_rendererData = rendererData;
} }

View File

@ -9,27 +9,22 @@
#include "assetmanager.hpp" #include "assetmanager.hpp"
#include "clipboard.hpp" #include "clipboard.hpp"
#include "consts.hpp" #include "consts.hpp"
#include "event.hpp"
#include "gfx.hpp" #include "gfx.hpp"
#include "input.hpp" #include "input.hpp"
#include "media.hpp" #include "media.hpp"
namespace nostalgia::core { namespace nostalgia::core {
using event_handler = int(*)(Context*);
ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const char *appName = "Nostalgia") noexcept; ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const char *appName = "Nostalgia") noexcept;
ox::Error run(Context *ctx) 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 // Returns the number of milliseconds that have passed since the start of the
// program. // program.
[[nodiscard]] [[nodiscard]]
uint64_t ticksMs(Context *ctx) noexcept; uint64_t ticksMs(Context *ctx) noexcept;
void shutdown(Context *ctx) noexcept; ox::Error shutdown(Context *ctx) noexcept;
} }

View File

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

View File

@ -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()

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
#include <ox/model/def.hpp>
#include <nostalgia/common/size.hpp> #include <nostalgia/common/size.hpp>
#include "color.hpp" #include "color.hpp"
@ -37,24 +38,18 @@ struct NostalgiaGraphic {
ox::Vector<uint8_t> pixels; ox::Vector<uint8_t> pixels;
}; };
template<typename T> oxModelBegin(NostalgiaPalette)
constexpr ox::Error model(T *io, NostalgiaPalette *pal) noexcept { oxModelField(colors)
io->template setTypeInfo<NostalgiaPalette>(); oxModelEnd()
oxReturnError(io->field("colors", &pal->colors));
return OxError(0);
}
template<typename T> oxModelBegin(NostalgiaGraphic)
constexpr ox::Error model(T *io, NostalgiaGraphic *ng) noexcept { oxModelField(bpp)
io->template setTypeInfo<NostalgiaGraphic>(); oxModelField(rows)
oxReturnError(io->field("bpp", &ng->bpp)); oxModelField(columns)
oxReturnError(io->field("rows", &ng->rows)); oxModelField(defaultPalette)
oxReturnError(io->field("columns", &ng->columns)); oxModelField(pal)
oxReturnError(io->field("defaultPalette", &ng->defaultPalette)); oxModelField(pixels)
oxReturnError(io->field("pal", &ng->pal)); oxModelEnd()
oxReturnError(io->field("pixels", &ng->pixels));
return OxError(0);
}
struct Sprite { struct Sprite {
unsigned idx = 0; unsigned idx = 0;

View File

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

View File

@ -68,9 +68,10 @@ bool buttonDown(Key) noexcept {
return false; return false;
} }
void shutdown(Context *ctx) noexcept { ox::Error shutdown(Context *ctx) noexcept {
const auto id = ctx->windowerData<GlfwImplData>(); const auto id = ctx->windowerData<GlfwImplData>();
glfwSetWindowShouldClose(id->window, true); glfwSetWindowShouldClose(id->window, true);
return OxError(0);
} }

View File

@ -19,15 +19,11 @@ static void handleGlfwError(int err, const char *desc) noexcept {
oxErrf("GLFW error ({}): {}\n", err, desc); oxErrf("GLFW error ({}): {}\n", err, desc);
} }
void ImGui_Impl_NewFrame() noexcept {
ImGui_ImplGlfw_NewFrame();
}
static void handleKeyPress(Context *ctx, int key) noexcept { static void handleKeyPress(Context *ctx, int key) noexcept {
switch (key) { switch (key) {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
case GLFW_KEY_Q: case GLFW_KEY_Q:
shutdown(ctx); oxIgnoreError(shutdown(ctx));
break; break;
default: default:
break; 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 { ox::Error initGfx(Context *ctx) noexcept {
auto id = ctx->windowerData<GlfwImplData>(); auto id = ctx->windowerData<GlfwImplData>();
glfwSetErrorCallback(handleGlfwError); glfwSetErrorCallback(handleGlfwError);

View File

@ -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()

View File

@ -20,7 +20,7 @@ endif()
target_link_libraries( target_link_libraries(
NostalgiaCore-Studio PUBLIC NostalgiaCore-Studio PUBLIC
NostalgiaStudio NostalgiaStudio
NostalgiaCore-GLFW NostalgiaCore
NostalgiaGlUtils NostalgiaGlUtils
) )

View File

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

View File

@ -10,10 +10,10 @@
namespace nostalgia::core::renderer { 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;
} }

View File

@ -12,6 +12,8 @@
#include <nostalgia/core/config.hpp> #include <nostalgia/core/config.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include "nostalgia/core/context.hpp"
namespace nostalgia::core { namespace nostalgia::core {

View File

@ -21,8 +21,7 @@ endif()
target_link_libraries( target_link_libraries(
nostalgia nostalgia
NostalgiaWorld NostalgiaWorld
$<$<STREQUAL:${NOSTALGIA_BUILD_TYPE},GBA>:NostalgiaCore-GBA> NostalgiaCore
$<$<STREQUAL:${NOSTALGIA_BUILD_TYPE},Native>:NostalgiaCore-GLFW>
) )
install( install(

View File

@ -14,7 +14,7 @@ target_link_libraries(
OxClArgs OxClArgs
OxFS OxFS
NostalgiaCore-Studio NostalgiaCore-Studio
NostalgiaCore-Userspace NostalgiaCore
NostalgiaStudio NostalgiaStudio
NostalgiaPack NostalgiaPack
) )

View File

@ -45,8 +45,7 @@ target_link_libraries(
OxEvent OxEvent
OxFS OxFS
OxClaw OxClaw
NostalgiaCore-Userspace NostalgiaCore
NostalgiaCore-GLFW
) )
install( install(

View File

@ -83,7 +83,7 @@ void StudioUI::drawMenu() noexcept {
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_saveEnabled)) { if (ImGui::MenuItem("Save", "Ctrl+S", false, m_saveEnabled)) {
} }
if (ImGui::MenuItem("Quit", "Ctrl+Q")) { if (ImGui::MenuItem("Quit", "Ctrl+Q")) {
core::shutdown(m_ctx); oxIgnoreError(core::shutdown(m_ctx));
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }