[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(
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

View File

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

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 "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<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:
ox::UniquePtr<ox::FileSystem> rom;
ox::Vector<Drawer*, 5> drawers;
@ -49,16 +86,17 @@ class Context {
return static_cast<T*>(m_customData);
}
constexpr void setWindowerData(void *windowerData) noexcept {
m_windowerData = windowerData;
}
protected:
template<typename T>
[[nodiscard]]
constexpr T *windowerData() noexcept {
return static_cast<T*>(m_windowerData);
}
constexpr void setWindowerData(void *windowerData) noexcept {
m_windowerData = windowerData;
}
constexpr void setRendererData(void *rendererData) noexcept {
m_rendererData = rendererData;
}

View File

@ -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<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> 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;
}

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
#include <ox/std/types.hpp>
#include <ox/model/def.hpp>
#include <nostalgia/common/size.hpp>
#include "color.hpp"
@ -37,24 +38,18 @@ struct NostalgiaGraphic {
ox::Vector<uint8_t> pixels;
};
template<typename T>
constexpr ox::Error model(T *io, NostalgiaPalette *pal) noexcept {
io->template setTypeInfo<NostalgiaPalette>();
oxReturnError(io->field("colors", &pal->colors));
return OxError(0);
}
oxModelBegin(NostalgiaPalette)
oxModelField(colors)
oxModelEnd()
template<typename T>
constexpr ox::Error model(T *io, NostalgiaGraphic *ng) noexcept {
io->template setTypeInfo<NostalgiaGraphic>();
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;

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;
}
void shutdown(Context *ctx) noexcept {
ox::Error shutdown(Context *ctx) noexcept {
const auto id = ctx->windowerData<GlfwImplData>();
glfwSetWindowShouldClose(id->window, true);
return OxError(0);
}

View File

@ -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<GlfwImplData>();
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(
NostalgiaCore-Studio PUBLIC
NostalgiaStudio
NostalgiaCore-GLFW
NostalgiaCore
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 {
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/gfx.hpp>
#include "nostalgia/core/context.hpp"
namespace nostalgia::core {

View File

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

View File

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

View File

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

View File

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