From 71b38b243e7b425abb63771ae29f8277935f12dc Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 9 Nov 2019 17:56:50 -0600 Subject: [PATCH] [nostalgia/core] Replace puts loc param with column and row params --- src/nostalgia/core/gba/CMakeLists.txt | 1 + src/nostalgia/core/gba/gfx.cpp | 6 +++- src/nostalgia/core/gba/panic.cpp | 8 ++--- src/nostalgia/core/gfx.cpp | 4 +-- src/nostalgia/core/gfx.hpp | 2 +- src/nostalgia/core/sdl/gfx.cpp | 42 +++++++++++++++------------ src/nostalgia/core/types.hpp | 2 -- src/nostalgia/player/main.cpp | 2 +- 8 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt index 7072a6cb..e5a80d68 100644 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ b/src/nostalgia/core/gba/CMakeLists.txt @@ -9,6 +9,7 @@ add_library( target_link_libraries( NostalgiaCore-GBA PUBLIC + NostalgiaCore GbaStartup OxFS OxStd diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index 6e78e7f2..49ed24a4 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -112,7 +112,11 @@ ox::Error initConsole(Context *ctx) { constexpr auto PaletteAddr = "/Palettes/Charset.npal"; if (!ctx) { ctx = new (ox_alloca(sizeof(Context))) Context(); - ox::FileStore32 fs(loadRom(), 32 * ox::units::MB); + auto rom = loadRom(); + if (!rom) { + return OxError(1); + } + ox::FileStore32 fs(rom, 32 * ox::units::MB); ctx->rom = new (ox_alloca(sizeof(ox::FileSystem32))) ox::FileSystem32(fs); } return loadTileSheet(ctx, TileSheetSpace::Background, 0, TilesheetAddr, PaletteAddr); diff --git a/src/nostalgia/core/gba/panic.cpp b/src/nostalgia/core/gba/panic.cpp index 5b72101a..9f6b2d07 100644 --- a/src/nostalgia/core/gba/panic.cpp +++ b/src/nostalgia/core/gba/panic.cpp @@ -13,10 +13,10 @@ namespace nostalgia::core { void panic(const char *msg) { oxIgnoreError(initConsole(nullptr)); - puts(nullptr, 1 * 32 + 0, "SADNESS..."); - puts(nullptr, 4 * 32 + 0, "UNEXPECTED STATE:"); - puts(nullptr, 6 * 32 + 2, msg); - puts(nullptr, 10 * 32 + 0, "PLEASE RESTART THE SYSTEM"); + puts(nullptr, 32 + 0, 1, "SADNESS..."); + puts(nullptr, 32 + 0, 4, "UNEXPECTED STATE:"); + puts(nullptr, 32 + 2, 6, msg); + puts(nullptr, 32 + 0, 10, "PLEASE RESTART THE SYSTEM"); // TODO: properly end program execution, this wastes power while (1); } diff --git a/src/nostalgia/core/gfx.cpp b/src/nostalgia/core/gfx.cpp index 2abffce5..f484eafc 100644 --- a/src/nostalgia/core/gfx.cpp +++ b/src/nostalgia/core/gfx.cpp @@ -141,9 +141,9 @@ char charMap[128] = { 0, // ~ }; -void puts(Context *ctx, int loc, const char *str) { +void puts(Context *ctx, int column, int row, const char *str) { for (int i = 0; str[i]; i++) { - setTile(ctx, 0, loc + i, 0, charMap[static_cast(str[i])]); + setTile(ctx, 0, column + i, row, charMap[static_cast(str[i])]); } } diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index 460a6789..d1b10e3b 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -65,7 +65,7 @@ ox::Error model(T *io, NostalgiaGraphic *ng) { ox::Error loadTileSheet(Context *ctx, ox::FileAddress file); -void puts(Context *ctx, int loc, const char *str); +void puts(Context *ctx, int column, int row, const char *str); void setTile(Context *ctx, int layer, int column, int row, uint8_t tile); diff --git a/src/nostalgia/core/sdl/gfx.cpp b/src/nostalgia/core/sdl/gfx.cpp index b2dfcfa5..fb88a148 100644 --- a/src/nostalgia/core/sdl/gfx.cpp +++ b/src/nostalgia/core/sdl/gfx.cpp @@ -21,7 +21,8 @@ static SDL_Window *window = nullptr; static SDL_Renderer *renderer = nullptr; static std::array bgTextures; -static std::vector> bgTileMaps(128, std::vector(128, 0)); +using TileMap = std::array, 128>; +static std::array bgTileMaps; [[nodiscard]] static ox::ValErr> readFile(Context *ctx, const ox::FileAddress &file) { auto [stat, err] = ctx->rom->stat(file); @@ -124,39 +125,42 @@ ox::Error loadTileSheet(Context *ctx, return OxError(0); } -void drawBackground(SDL_Texture *tex) { - constexpr auto DstSize = 32; - //oxTrace("nostalgia::core::drawBackground") << "Drawing background"; - SDL_Rect src = {}, dst = {}; - src.x = 0; - src.w = 8; - src.h = 8; - dst.x = 0; - dst.y = 0; - dst.w = DstSize; - dst.h = DstSize; +void drawBackground(const TileMap &tm, SDL_Texture *tex) { if (tex) { - for (auto &m : bgTileMaps) { + constexpr auto DstSize = 32; + //oxTrace("nostalgia::core::drawBackground") << "Drawing background"; + SDL_Rect src = {}, dst = {}; + src.x = 0; + src.w = 8; + src.h = 8; + dst.x = 0; + dst.y = 0; + dst.w = DstSize; + dst.h = DstSize; + for (auto &m : tm) { for (auto t : m) { src.y = t * 8; SDL_RenderCopy(renderer, tex, &src, &dst); - dst.y += DstSize; + dst.x += DstSize; } - dst.x += DstSize; - dst.y = 0; + dst.x = 0; + dst.y += DstSize; } } } void draw() { SDL_RenderClear(renderer); - for (auto tex : bgTextures) { - drawBackground(tex); + for (std::size_t i = 0; i < bgTileMaps.size(); i++) { + auto tex = bgTextures[i]; + auto &tm = bgTileMaps[i]; + drawBackground(tm, tex); } SDL_RenderPresent(renderer); } -void setTile(Context*, int, int, int, uint8_t) { +void setTile(Context*, int layer, int column, int row, uint8_t tile) { + bgTileMaps[layer][row][column] = tile; } } diff --git a/src/nostalgia/core/types.hpp b/src/nostalgia/core/types.hpp index d8336d12..ab49a7eb 100644 --- a/src/nostalgia/core/types.hpp +++ b/src/nostalgia/core/types.hpp @@ -12,6 +12,4 @@ namespace nostalgia::core { -typedef ox::FileStore32::InodeId_t InodeId_t; - } diff --git a/src/nostalgia/player/main.cpp b/src/nostalgia/player/main.cpp index df6b6be6..0fa6a75c 100644 --- a/src/nostalgia/player/main.cpp +++ b/src/nostalgia/player/main.cpp @@ -22,7 +22,7 @@ ox::Error run(ox::FileSystem *fs) { //oxReturnError(zone.init(&ctx, Bounds{0, 0, 40, 40}, "/TileSheets/Charset.ng", "/Palettes/Charset.npal")); //zone.draw(&ctx); oxReturnError(initConsole(&ctx)); - puts(&ctx, 9 * 32 + 10, "DOPENESS!!!"); + puts(&ctx, 10, 9, "DOPENESS!!!"); oxReturnError(run()); oxReturnError(shutdownGfx()); return OxError(0);