diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp index 1c8e446a..2dccf38c 100644 --- a/src/nostalgia/core/context.hpp +++ b/src/nostalgia/core/context.hpp @@ -13,8 +13,22 @@ namespace nostalgia::core { // User Input Output -struct Context { - ox::FileSystem *rom = nullptr; +class Context { + public: + ox::FileSystem *rom = nullptr; + private: + void *m_implData = nullptr; + + public: + constexpr void setImplData(void *implData) noexcept { + m_implData = implData; + } + + template + constexpr T *implData() noexcept { + return static_cast(m_implData); + } + }; } diff --git a/src/nostalgia/core/core.hpp b/src/nostalgia/core/core.hpp index 7eb2ee2f..e26200f0 100644 --- a/src/nostalgia/core/core.hpp +++ b/src/nostalgia/core/core.hpp @@ -19,6 +19,6 @@ namespace nostalgia::core { [[nodiscard]] ox::Error init(Context *ctx); -[[nodiscard]] ox::Error run(); +[[nodiscard]] ox::Error run(Context *ctx); } diff --git a/src/nostalgia/core/gba/core.cpp b/src/nostalgia/core/gba/core.cpp index 7d565127..f611f4be 100644 --- a/src/nostalgia/core/gba/core.cpp +++ b/src/nostalgia/core/gba/core.cpp @@ -10,7 +10,7 @@ namespace nostalgia::core { -ox::Error run() { +ox::Error run(Context*) { while (1) { } return OxError(0); diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index 5755623e..4b36bc49 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -89,7 +89,7 @@ ox::Error initGfx(Context*) { return OxError(0); } -ox::Error shutdownGfx() { +ox::Error shutdownGfx(Context*) { return OxError(0); } diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 3a295e76..e703dff5 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -65,7 +65,7 @@ ox::Error model(T *io, NostalgiaGraphic *ng) { [[nodiscard]] ox::Error initGfx(Context *ctx); -[[nodiscard]] ox::Error shutdownGfx(); +[[nodiscard]] ox::Error shutdownGfx(Context*); [[nodiscard]] ox::Error initConsole(Context *ctx); diff --git a/src/nostalgia/core/qt/gfx.cpp b/src/nostalgia/core/qt/gfx.cpp index 90b9707a..66ccf928 100644 --- a/src/nostalgia/core/qt/gfx.cpp +++ b/src/nostalgia/core/qt/gfx.cpp @@ -14,6 +14,9 @@ ox::Error initGfx(Context*) { return OxError(1); } +ox::Error shutdownGfx(Context*) { +} + ox::Error initConsole(Context*) { return OxError(1); } diff --git a/src/nostalgia/core/sdl/core.cpp b/src/nostalgia/core/sdl/core.cpp index 46f247ec..76a87530 100644 --- a/src/nostalgia/core/sdl/core.cpp +++ b/src/nostalgia/core/sdl/core.cpp @@ -12,9 +12,9 @@ namespace nostalgia::core { -void draw(); +void draw(Context *ctx); -ox::Error run() { +ox::Error run(Context *ctx) { for (auto running = true; running;) { SDL_Event event; while (SDL_PollEvent(&event)) { @@ -30,7 +30,7 @@ ox::Error run() { } } } - draw(); + draw(ctx); } return OxError(0); } diff --git a/src/nostalgia/core/sdl/gfx.cpp b/src/nostalgia/core/sdl/gfx.cpp index 828f750a..52231c7e 100644 --- a/src/nostalgia/core/sdl/gfx.cpp +++ b/src/nostalgia/core/sdl/gfx.cpp @@ -6,6 +6,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#define NOST_FPS_PRINT + #include #include #ifdef NOST_FPS_PRINT @@ -20,12 +22,19 @@ namespace nostalgia::core { -static SDL_Window *window = nullptr; -static SDL_Renderer *renderer = nullptr; - -static std::array bgTextures; using TileMap = std::array, 128>; -static std::array bgTileMaps; + +struct SdlImplData { + SDL_Window *window = nullptr; + SDL_Renderer *renderer = nullptr; + std::array bgTextures; + std::array bgTileMaps; +#ifdef NOST_FPS_PRINT + uint64_t prevFpsCheckTime = 0; + uint64_t draws = 0; +#endif + +}; [[nodiscard]] static ox::ValErr> readFile(Context *ctx, const ox::FileAddress &file) { auto [stat, err] = ctx->rom->stat(file); @@ -44,19 +53,23 @@ template return t; } -ox::Error initGfx(Context*) { - window = SDL_CreateWindow("nostalgia", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, - SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); - renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - return OxError(window == nullptr); +ox::Error initGfx(Context *ctx) { + auto id = new SdlImplData; + ctx->setImplData(id); + id->window = SDL_CreateWindow("nostalgia", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, + SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); + id->renderer = SDL_CreateRenderer(id->window, -1, SDL_RENDERER_ACCELERATED); + return OxError(id->window == nullptr); } -ox::Error shutdownGfx() { - for (auto tex : bgTextures) { +ox::Error shutdownGfx(Context *ctx) { + auto id = ctx->implData(); + for (auto tex : id->bgTextures) { SDL_DestroyTexture(tex); } - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); + SDL_DestroyRenderer(id->renderer); + SDL_DestroyWindow(id->window); + delete id; return OxError(0); } @@ -89,6 +102,7 @@ ox::Error loadTileSheet(Context *ctx, int section, ox::FileAddress tilesheetPath, ox::FileAddress palettePath) { + auto id = ctx->implData(); auto [tilesheet, tserr] = readMC(ctx, tilesheetPath); oxReturnError(tserr); NostalgiaPalette palette; @@ -110,28 +124,29 @@ ox::Error loadTileSheet(Context *ctx, SDL_memcpy(surface->pixels, tilesheet.tiles.data(), bytesPerTile * tiles); } else { for (std::size_t i = 0; i < tilesheet.tiles.size(); ++i) { - static_cast(surface->pixels)[i * 2 + 0] = tilesheet.tiles[i] & 0xF; + static_cast(surface->pixels)[i * 2 + 0] = tilesheet.tiles[i] & 0xF; static_cast(surface->pixels)[i * 2 + 1] = tilesheet.tiles[i] >> 4; } } - auto texture = SDL_CreateTextureFromSurface(renderer, surface); + auto texture = SDL_CreateTextureFromSurface(id->renderer, surface); SDL_FreeSurface(surface); SDL_FreePalette(sdlPalette); if (tss == TileSheetSpace::Background) { - if (bgTextures[section]) { - SDL_DestroyTexture(bgTextures[section]); + if (id->bgTextures[section]) { + SDL_DestroyTexture(id->bgTextures[section]); } - bgTextures[section] = texture; + id->bgTextures[section] = texture; } return OxError(0); } -void drawBackground(const TileMap &tm, SDL_Texture *tex) { +void drawBackground(Context *ctx, const TileMap &tm, SDL_Texture *tex) { if (tex) { constexpr auto DstSize = 32; + auto id = ctx->implData(); //oxTrace("nostalgia::core::drawBackground") << "Drawing background"; SDL_Rect src = {}, dst = {}; src.x = 0; @@ -144,7 +159,7 @@ void drawBackground(const TileMap &tm, SDL_Texture *tex) { for (auto &m : tm) { for (auto t : m) { src.y = t * 8; - SDL_RenderCopy(renderer, tex, &src, &dst); + SDL_RenderCopy(id->renderer, tex, &src, &dst); dst.x += DstSize; } dst.x = 0; @@ -153,34 +168,31 @@ void drawBackground(const TileMap &tm, SDL_Texture *tex) { } } +void draw(Context *ctx) { + auto id = ctx->implData(); #ifdef NOST_FPS_PRINT -static uint64_t prevFpsCheckTime = 0; -static uint64_t draws = 0; -#endif - -void draw() { -#ifdef NOST_FPS_PRINT - ++draws; - if (draws >= 5000) { + ++id->draws; + if (id->draws >= 5000) { using namespace std::chrono; auto now = duration_cast(system_clock::now().time_since_epoch()).count(); - auto duration = static_cast(now - prevFpsCheckTime) / 1000.0; - std::cout << "FPS: " << static_cast(static_cast(draws) / duration) << '\n'; - prevFpsCheckTime = now; - draws = 0; + auto duration = static_cast(now - id->prevFpsCheckTime) / 1000.0; + std::cout << "FPS: " << static_cast(static_cast(id->draws) / duration) << '\n'; + id->prevFpsCheckTime = now; + id->draws = 0; } #endif - SDL_RenderClear(renderer); - for (std::size_t i = 0; i < bgTileMaps.size(); i++) { - auto tex = bgTextures[i]; - auto &tm = bgTileMaps[i]; - drawBackground(tm, tex); + SDL_RenderClear(id->renderer); + for (std::size_t i = 0; i < id->bgTileMaps.size(); i++) { + auto tex = id->bgTextures[i]; + auto &tm = id->bgTileMaps[i]; + drawBackground(ctx, tm, tex); } - SDL_RenderPresent(renderer); + SDL_RenderPresent(id->renderer); } -void setTile(Context*, int layer, int column, int row, uint8_t tile) { - bgTileMaps[layer][row][column] = tile; +void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) { + auto id = ctx->implData(); + id->bgTileMaps[layer][row][column] = tile; } } diff --git a/src/nostalgia/player/main.cpp b/src/nostalgia/player/main.cpp index cb29cdea..aa7fee79 100644 --- a/src/nostalgia/player/main.cpp +++ b/src/nostalgia/player/main.cpp @@ -22,8 +22,8 @@ ox::Error run(ox::FileSystem *fs) { //zone.draw(&ctx); oxReturnError(core::initConsole(&ctx)); core::puts(&ctx, 10, 9, "DOPENESS!!!"); - oxReturnError(core::run()); - oxReturnError(core::shutdownGfx()); + oxReturnError(core::run(&ctx)); + oxReturnError(core::shutdownGfx(&ctx)); return OxError(0); }