[nostalgia] Make almost everyting noexcept

This commit is contained in:
Gary Talent 2021-04-20 01:56:41 -05:00
parent 161780cb91
commit 6ece0b6f9b
22 changed files with 139 additions and 136 deletions

View File

@ -19,16 +19,17 @@ namespace nostalgia::core {
using event_handler = int(*)(Context*); using event_handler = int(*)(Context*);
ox::Error init(Context *ctx); ox::Error init(Context *ctx) noexcept;
ox::Error run(Context *ctx); ox::Error run(Context *ctx) noexcept;
// Sets event handler that sleeps for the time given in the return value. The // Sets event handler that sleeps for the time given in the return value. The
// sleep time is a minimum of ~16 milliseconds. // sleep time is a minimum of ~16 milliseconds.
void setEventHandler(event_handler); void setEventHandler(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]] uint64_t ticksMs(); [[nodiscard]]
uint64_t ticksMs() noexcept;
} }

View File

@ -21,7 +21,7 @@ extern volatile gba_timer_t g_timerMs;
gba_timer_t g_wakeupTime; gba_timer_t g_wakeupTime;
event_handler g_eventHandler = nullptr; event_handler g_eventHandler = nullptr;
ox::Error run(Context *ctx) { ox::Error run(Context *ctx) noexcept {
g_wakeupTime = 0; g_wakeupTime = 0;
while (1) { while (1) {
if (g_wakeupTime <= g_timerMs && g_eventHandler) { if (g_wakeupTime <= g_timerMs && g_eventHandler) {

View File

@ -27,12 +27,12 @@ extern event_handler g_eventHandler;
extern volatile gba_timer_t g_timerMs; extern volatile gba_timer_t g_timerMs;
static void initIrq() { static void initIrq() noexcept {
REG_ISR = isr; REG_ISR = isr;
REG_IME = 1; // enable interrupts REG_IME = 1; // enable interrupts
} }
static void initTimer() { static void initTimer() noexcept {
// make timer0 a ~1 millisecond timer // make timer0 a ~1 millisecond timer
REG_TIMER0 = TicksMs59ns; REG_TIMER0 = TicksMs59ns;
REG_TIMER0CTL = 0b11000000; REG_TIMER0CTL = 0b11000000;
@ -40,22 +40,22 @@ static void initTimer() {
REG_IE = REG_IE | Int_timer0; REG_IE = REG_IE | Int_timer0;
} }
ox::Error init(Context *ctx) { ox::Error init(Context *ctx) noexcept {
oxReturnError(initGfx(ctx)); oxReturnError(initGfx(ctx));
initTimer(); initTimer();
initIrq(); initIrq();
return OxError(0); return OxError(0);
} }
void setEventHandler(event_handler h) { void setEventHandler(event_handler h) noexcept {
g_eventHandler = h; g_eventHandler = h;
} }
uint64_t ticksMs() { uint64_t ticksMs() noexcept {
return g_timerMs; return g_timerMs;
} }
bool buttonDown(Key k) { bool buttonDown(Key k) noexcept {
return !(REG_GAMEPAD & k); return !(REG_GAMEPAD & k);
} }

View File

@ -85,7 +85,7 @@ ox::Error modelRead(T *io, GbaTileMapTarget *t) {
return io->template field<uint8_t, decltype(handleTileMap)>("tileMap", handleTileMap); return io->template field<uint8_t, decltype(handleTileMap)>("tileMap", handleTileMap);
} }
ox::Error initGfx(Context*) { ox::Error initGfx(Context*) noexcept {
REG_DISPCTL = DispCtl_Mode0 REG_DISPCTL = DispCtl_Mode0
| DispCtl_SpriteMap1D | DispCtl_SpriteMap1D
| DispCtl_Obj; | DispCtl_Obj;
@ -96,42 +96,43 @@ ox::Error initGfx(Context*) {
return OxError(0); return OxError(0);
} }
ox::Error shutdownGfx(Context*) { ox::Error shutdownGfx(Context*) noexcept {
return OxError(0); return OxError(0);
} }
int getScreenWidth(Context*) { int getScreenWidth(Context*) noexcept {
return 240; return 240;
} }
int getScreenHeight(Context*) { int getScreenHeight(Context*) noexcept {
return 160; return 160;
} }
common::Size getScreenSize(Context*) { common::Size getScreenSize(Context*) noexcept {
return {240, 160}; return {240, 160};
} }
uint8_t bgStatus(Context*) { uint8_t bgStatus(Context*) noexcept {
return (REG_DISPCTL >> 8) & 0b1111; return (REG_DISPCTL >> 8) & 0b1111;
} }
void setBgStatus(Context*, uint32_t status) { void setBgStatus(Context*, uint32_t status) noexcept {
constexpr auto Bg0Status = 8; constexpr auto Bg0Status = 8;
REG_DISPCTL = (REG_DISPCTL & ~0b111100000000u) | status << Bg0Status; REG_DISPCTL = (REG_DISPCTL & ~0b111100000000u) | status << Bg0Status;
} }
bool bgStatus(Context*, unsigned bg) { bool bgStatus(Context*, unsigned bg) noexcept {
return (REG_DISPCTL >> (8 + bg)) & 1; return (REG_DISPCTL >> (8 + bg)) & 1;
} }
void setBgStatus(Context*, unsigned bg, bool status) { void setBgStatus(Context*, unsigned bg, bool status) noexcept {
constexpr auto Bg0Status = 8; constexpr auto Bg0Status = 8;
const auto mask = static_cast<uint32_t>(status) << (Bg0Status + bg); const auto mask = static_cast<uint32_t>(status) << (Bg0Status + bg);
REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask); REG_DISPCTL = REG_DISPCTL | ((REG_DISPCTL & ~mask) | mask);
} }
[[nodiscard]] constexpr volatile uint32_t &bgCtl(int bg) noexcept { [[nodiscard]]
constexpr volatile uint32_t &bgCtl(int bg) noexcept {
switch (bg) { switch (bg) {
case 0: case 0:
return REG_BG0CTL; return REG_BG0CTL;
@ -148,16 +149,13 @@ void setBgStatus(Context*, unsigned bg, bool status) {
} }
// Do NOT rely on Context in the GBA version of this function. // Do NOT rely on Context in the GBA version of this function.
ox::Error initConsole(Context *ctx) { ox::Error initConsole(Context *ctx) noexcept {
constexpr auto TilesheetAddr = "/TileSheets/Charset.ng"; constexpr auto TilesheetAddr = "/TileSheets/Charset.ng";
constexpr auto PaletteAddr = "/Palettes/Charset.npal"; constexpr auto PaletteAddr = "/Palettes/Charset.npal";
setBgStatus(ctx, 0b0001); setBgStatus(ctx, 0b0001);
if (!ctx) { if (!ctx) {
ctx = new (ox_alloca(sizeof(Context))) Context(); ctx = new (ox_alloca(sizeof(Context))) Context();
auto rom = loadRom(); oxRequire(rom, loadRom());
if (!rom) {
return OxError(1);
}
ox::FileStore32 fs(rom, 32 * ox::units::MB); ox::FileStore32 fs(rom, 32 * ox::units::MB);
ctx->rom = new (ox_alloca(sizeof(ox::FileSystem32))) ox::FileSystem32(fs); ctx->rom = new (ox_alloca(sizeof(ox::FileSystem32))) ox::FileSystem32(fs);
} }
@ -167,7 +165,7 @@ ox::Error initConsole(Context *ctx) {
ox::Error loadBgTileSheet(Context *ctx, ox::Error loadBgTileSheet(Context *ctx,
int section, int section,
ox::FileAddress tilesheetAddr, ox::FileAddress tilesheetAddr,
ox::FileAddress paletteAddr) { ox::FileAddress paletteAddr) noexcept {
oxRequire(tsStat, ctx->rom->stat(tilesheetAddr)); oxRequire(tsStat, ctx->rom->stat(tilesheetAddr));
oxRequire(ts, ctx->rom->directAccess(tilesheetAddr)); oxRequire(ts, ctx->rom->directAccess(tilesheetAddr));
GbaTileMapTarget target; GbaTileMapTarget target;
@ -187,7 +185,7 @@ ox::Error loadBgTileSheet(Context *ctx,
ox::Error loadSpriteTileSheet(Context *ctx, ox::Error loadSpriteTileSheet(Context *ctx,
int section, int section,
ox::FileAddress tilesheetAddr, ox::FileAddress tilesheetAddr,
ox::FileAddress paletteAddr) { ox::FileAddress paletteAddr) noexcept {
oxRequire(tsStat, ctx->rom->stat(tilesheetAddr)); oxRequire(tsStat, ctx->rom->stat(tilesheetAddr));
oxRequire(ts, ctx->rom->directAccess(tilesheetAddr)); oxRequire(ts, ctx->rom->directAccess(tilesheetAddr));
GbaTileMapTarget target; GbaTileMapTarget target;
@ -205,7 +203,7 @@ ox::Error loadSpriteTileSheet(Context *ctx,
return OxError(0); return OxError(0);
} }
ox::Error loadBgPalette(Context *ctx, int section, ox::FileAddress paletteAddr) { ox::Error loadBgPalette(Context *ctx, int section, ox::FileAddress paletteAddr) noexcept {
GbaPaletteTarget target; GbaPaletteTarget target;
target.palette = &MEM_BG_PALETTE[section]; target.palette = &MEM_BG_PALETTE[section];
oxRequire(palStat, ctx->rom->stat(paletteAddr)); oxRequire(palStat, ctx->rom->stat(paletteAddr));
@ -214,7 +212,7 @@ ox::Error loadBgPalette(Context *ctx, int section, ox::FileAddress paletteAddr)
return OxError(0); return OxError(0);
} }
ox::Error loadSpritePalette(Context *ctx, int section, ox::FileAddress paletteAddr) { ox::Error loadSpritePalette(Context *ctx, int section, ox::FileAddress paletteAddr) noexcept {
GbaPaletteTarget target; GbaPaletteTarget target;
target.palette = &MEM_SPRITE_PALETTE[section]; target.palette = &MEM_SPRITE_PALETTE[section];
oxRequire(palStat, ctx->rom->stat(paletteAddr)); oxRequire(palStat, ctx->rom->stat(paletteAddr));
@ -224,23 +222,23 @@ ox::Error loadSpritePalette(Context *ctx, int section, ox::FileAddress paletteAd
} }
// Do NOT use Context in the GBA version of this function. // Do NOT use Context in the GBA version of this function.
void puts(Context *ctx, int column, int row, const char *str) { void puts(Context *ctx, int column, int row, const char *str) noexcept {
for (int i = 0; str[i]; i++) { for (int i = 0; str[i]; i++) {
setTile(ctx, 0, column + i, row, static_cast<uint8_t>(charMap[static_cast<int>(str[i])])); setTile(ctx, 0, column + i, row, static_cast<uint8_t>(charMap[static_cast<int>(str[i])]));
} }
} }
void setTile(Context*, int layer, int column, int row, uint8_t tile) { void setTile(Context*, int layer, int column, int row, uint8_t tile) noexcept {
MEM_BG_MAP[layer][row * GbaTileColumns + column] = tile; MEM_BG_MAP[layer][row * GbaTileColumns + column] = tile;
} }
// Do NOT use Context in the GBA version of this function. // Do NOT use Context in the GBA version of this function.
void clearTileLayer(Context*, int layer) { void clearTileLayer(Context*, int layer) noexcept {
memset(&MEM_BG_MAP[layer], 0, GbaTileRows * GbaTileColumns); memset(&MEM_BG_MAP[layer], 0, GbaTileRows * GbaTileColumns);
} }
[[maybe_unused]] [[maybe_unused]]
void hideSprite(Context*, unsigned idx) { void hideSprite(Context*, unsigned idx) noexcept {
oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow"); oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
GbaSpriteAttrUpdate oa; GbaSpriteAttrUpdate oa;
oa.attr0 = 2 << 8; oa.attr0 = 2 << 8;
@ -269,7 +267,7 @@ void setSprite(Context*,
unsigned tileIdx, unsigned tileIdx,
unsigned spriteShape, unsigned spriteShape,
unsigned spriteSize, unsigned spriteSize,
unsigned flipX) { unsigned flipX) noexcept {
oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow"); oxAssert(g_spriteUpdates < config::GbaSpriteBufferLen, "Sprite update buffer overflow");
GbaSpriteAttrUpdate oa; GbaSpriteAttrUpdate oa;
oa.attr0 = static_cast<uint16_t>(y & ox::onMask<uint8_t>(7)) oa.attr0 = static_cast<uint16_t>(y & ox::onMask<uint8_t>(7))

View File

@ -14,7 +14,7 @@
namespace nostalgia::core { namespace nostalgia::core {
char *loadRom(const char*) { ox::Result<char*> loadRom(const char*) noexcept {
// put the header in the wrong order to prevent mistaking this code for the // put the header in the wrong order to prevent mistaking this code for the
// media section // media section
constexpr auto headerP2 = "_HEADER_________"; constexpr auto headerP2 = "_HEADER_________";
@ -29,10 +29,10 @@ char *loadRom(const char*) {
return current + headerLen; return current + headerLen;
} }
} }
return nullptr; return OxError(1);
} }
void unloadRom(char*) { void unloadRom(char*) noexcept {
} }
} }

View File

@ -13,11 +13,12 @@
namespace ox::heapmgr { namespace ox::heapmgr {
[[nodiscard]] void *malloc(std::size_t allocSize); [[nodiscard]]
void *malloc(std::size_t allocSize) noexcept;
void free(void *ptr); void free(void *ptr) noexcept;
void initHeap(char *heapBegin, char *heapEnd); void initHeap(char *heapBegin, char *heapEnd) noexcept;
} }

View File

@ -141,7 +141,7 @@ char charMap[128] = {
0, // ~ 0, // ~
}; };
void setSprite(Context *c, const Sprite &s) { void setSprite(Context *c, const Sprite &s) noexcept {
setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX); setSprite(c, s.idx, s.x, s.y, s.tileIdx, s.spriteShape, s.spriteSize, s.flipX);
} }

View File

@ -72,50 +72,50 @@ struct Sprite {
unsigned flipX = 0; unsigned flipX = 0;
}; };
ox::Error initGfx(Context *ctx); ox::Error initGfx(Context *ctx) noexcept;
ox::Error shutdownGfx(Context *ctx); ox::Error shutdownGfx(Context *ctx) noexcept;
[[nodiscard]] [[nodiscard]]
int getScreenWidth(Context *ctx); int getScreenWidth(Context *ctx) noexcept;
[[nodiscard]] [[nodiscard]]
int getScreenHeight(Context *ctx); int getScreenHeight(Context *ctx) noexcept;
[[nodiscard]] [[nodiscard]]
common::Size getScreenSize(Context *ctx); common::Size getScreenSize(Context *ctx) noexcept;
[[nodiscard]] [[nodiscard]]
uint8_t bgStatus(Context *ctx); uint8_t bgStatus(Context *ctx) noexcept;
void setBgStatus(Context *ctx, uint32_t status); void setBgStatus(Context *ctx, uint32_t status) noexcept;
bool bgStatus(Context *ctx, unsigned bg); bool bgStatus(Context *ctx, unsigned bg) noexcept;
void setBgStatus(Context *ctx, unsigned bg, bool status); void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept;
ox::Error initConsole(Context *ctx); ox::Error initConsole(Context *ctx) noexcept;
/** /**
* @param section describes which section of the selected TileSheetSpace to use (e.g. MEM_PALLETE_BG[section]) * @param section describes which section of the selected TileSheetSpace to use (e.g. MEM_PALLETE_BG[section])
*/ */
ox::Error loadBgTileSheet(Context *ctx, int section, ox::FileAddress tilesheet, ox::FileAddress palette = nullptr); ox::Error loadBgTileSheet(Context *ctx, int section, ox::FileAddress tilesheet, ox::FileAddress palette = nullptr) noexcept;
ox::Error loadSpriteTileSheet(Context *ctx, ox::Error loadSpriteTileSheet(Context *ctx,
int section, int section,
ox::FileAddress tilesheetAddr, ox::FileAddress tilesheetAddr,
ox::FileAddress paletteAddr); ox::FileAddress paletteAddr) noexcept;
void puts(Context *ctx, int column, int row, const char *str); void puts(Context *ctx, int column, int row, const char *str) noexcept;
void setTile(Context *ctx, int layer, int column, int row, uint8_t tile); void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) noexcept;
void clearTileLayer(Context *ctx, int layer); void clearTileLayer(Context *ctx, int layer) noexcept;
void hideSprite(Context *ctx, unsigned); void hideSprite(Context *ctx, unsigned) noexcept;
void setSprite(Context *ctx, unsigned idx, unsigned x, unsigned y, unsigned tileIdx, unsigned spriteShape = 0, unsigned spriteSize = 0, unsigned flipX = 0); void setSprite(Context *ctx, unsigned idx, unsigned x, unsigned y, unsigned tileIdx, unsigned spriteShape = 0, unsigned spriteSize = 0, unsigned flipX = 0) noexcept;
void setSprite(Context *ctx, const Sprite &s); void setSprite(Context *ctx, const Sprite &s) noexcept;
} }

View File

@ -24,6 +24,7 @@ enum Key {
GamePad_L = 512, GamePad_L = 512,
}; };
[[nodiscard]] bool buttonDown(Key); [[nodiscard]]
bool buttonDown(Key) noexcept;
} }

View File

@ -10,20 +10,17 @@
namespace nostalgia::core { namespace nostalgia::core {
ox::FileSystem *loadRomFs(const char *path) { ox::Result<ox::FileSystem*> loadRomFs(const char *path) noexcept {
const auto lastDot = ox_lastIndexOf(path, '.'); const auto lastDot = ox_lastIndexOf(path, '.');
const auto fsExt = lastDot != -1 ? path + lastDot : ""; const auto fsExt = lastDot != -1 ? path + lastDot : "";
if (ox_strcmp(fsExt, ".oxfs") == 0) { if (ox_strcmp(fsExt, ".oxfs") == 0) {
auto rom = core::loadRom(path); oxRequire(rom, core::loadRom(path));
if (!rom) {
return nullptr;
}
return new ox::FileSystem32(rom, 32 * ox::units::MB, unloadRom); return new ox::FileSystem32(rom, 32 * ox::units::MB, unloadRom);
} else { } else {
#ifdef OX_HAS_PASSTHROUGHFS #ifdef OX_HAS_PASSTHROUGHFS
return new ox::PassThroughFS(path); return new ox::PassThroughFS(path);
#else #else
return nullptr; return OxError(2);
#endif #endif
} }
} }

View File

@ -12,10 +12,10 @@
namespace nostalgia::core { namespace nostalgia::core {
ox::FileSystem *loadRomFs(const char *path); ox::Result<ox::FileSystem*> loadRomFs(const char *path) noexcept;
char *loadRom(const char *path = ""); ox::Result<char*> loadRom(const char *path = "") noexcept;
void unloadRom(char*); void unloadRom(char*) noexcept;
} }

View File

@ -10,35 +10,35 @@
namespace nostalgia::core { namespace nostalgia::core {
ox::Error initGfx(Context*) { ox::Error initGfx(Context*) noexcept {
return OxError(1); return OxError(1);
} }
ox::Error shutdownGfx(Context*) { ox::Error shutdownGfx(Context*) noexcept {
return OxError(1); return OxError(1);
} }
ox::Error initConsole(Context*) { ox::Error initConsole(Context*) noexcept {
return OxError(1); return OxError(1);
} }
ox::Error loadBgTileSheet(Context*, ox::Error loadBgTileSheet(Context*,
int, int,
ox::FileAddress, ox::FileAddress,
ox::FileAddress) { ox::FileAddress) noexcept {
return OxError(1); return OxError(1);
} }
void puts(Context *ctx, int column, int row, const char *str) { void puts(Context *ctx, int column, int row, const char *str) noexcept {
for (int i = 0; str[i]; i++) { for (int i = 0; str[i]; i++) {
setTile(ctx, 0, column + i, row, static_cast<uint8_t>(charMap[static_cast<int>(str[i])])); setTile(ctx, 0, column + i, row, static_cast<uint8_t>(charMap[static_cast<int>(str[i])]));
} }
} }
void setTile(Context*, int, int, int, uint8_t) { void setTile(Context*, int, int, int, uint8_t) noexcept {
} }
void setSprite(Context*, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned) { void setSprite(Context*, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned) noexcept {
} }
} }

View File

@ -22,12 +22,12 @@ static uint64_t g_wakeupTime;
void draw(Context *ctx); void draw(Context *ctx);
ox::Error init(Context *ctx) { ox::Error init(Context *ctx) noexcept {
oxReturnError(initGfx(ctx)); oxReturnError(initGfx(ctx));
return OxError(0); return OxError(0);
} }
ox::Error run(Context *ctx) { ox::Error run(Context *ctx) noexcept {
const auto id = ctx->windowerData<SdlImplData>(); const auto id = ctx->windowerData<SdlImplData>();
// try adaptive vsync // try adaptive vsync
if (SDL_GL_SetSwapInterval(config::SdlVsyncOption) < 0) { if (SDL_GL_SetSwapInterval(config::SdlVsyncOption) < 0) {
@ -64,15 +64,15 @@ ox::Error run(Context *ctx) {
return OxError(0); return OxError(0);
} }
void setEventHandler(event_handler h) { void setEventHandler(event_handler h) noexcept {
g_eventHandler = h; g_eventHandler = h;
} }
uint64_t ticksMs() { uint64_t ticksMs() noexcept {
return SDL_GetTicks(); return SDL_GetTicks();
} }
bool buttonDown(Key) { bool buttonDown(Key) noexcept {
return false; return false;
} }

View File

@ -22,7 +22,7 @@ namespace nostalgia::core {
constexpr auto Scale = 5; constexpr auto Scale = 5;
ox::Error initGfx(Context *ctx) { ox::Error initGfx(Context *ctx) noexcept {
auto id = new SdlImplData; auto id = new SdlImplData;
ctx->setWindowerData(id); ctx->setWindowerData(id);
id->window = SDL_CreateWindow("nostalgia", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, id->window = SDL_CreateWindow("nostalgia", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
@ -42,7 +42,7 @@ ox::Error initGfx(Context *ctx) {
return OxError(0); return OxError(0);
} }
ox::Error shutdownGfx(Context *ctx) { ox::Error shutdownGfx(Context *ctx) noexcept {
oxReturnError(renderer::shutdown(ctx)); oxReturnError(renderer::shutdown(ctx));
auto id = ctx->windowerData<SdlImplData>(); auto id = ctx->windowerData<SdlImplData>();
SDL_GL_DeleteContext(id->renderer); SDL_GL_DeleteContext(id->renderer);
@ -52,21 +52,21 @@ ox::Error shutdownGfx(Context *ctx) {
return OxError(0); return OxError(0);
} }
int getScreenWidth(Context *ctx) { int getScreenWidth(Context *ctx) noexcept {
auto id = ctx->windowerData<SdlImplData>(); auto id = ctx->windowerData<SdlImplData>();
int x = 0, y = 0; int x = 0, y = 0;
SDL_GetWindowSize(id->window, &x, &y); SDL_GetWindowSize(id->window, &x, &y);
return x; return x;
} }
int getScreenHeight(Context *ctx) { int getScreenHeight(Context *ctx) noexcept {
auto id = ctx->windowerData<SdlImplData>(); auto id = ctx->windowerData<SdlImplData>();
int x = 0, y = 0; int x = 0, y = 0;
SDL_GetWindowSize(id->window, &x, &y); SDL_GetWindowSize(id->window, &x, &y);
return y; return y;
} }
common::Size getScreenSize(Context *ctx) { common::Size getScreenSize(Context *ctx) noexcept {
auto id = ctx->windowerData<SdlImplData>(); auto id = ctx->windowerData<SdlImplData>();
int x = 0, y = 0; int x = 0, y = 0;
SDL_GetWindowSize(id->window, &x, &y); SDL_GetWindowSize(id->window, &x, &y);

View File

@ -14,7 +14,7 @@
namespace nostalgia::core { namespace nostalgia::core {
static ox::Result<ox::Vector<char>> readFile(Context *ctx, const ox::FileAddress &file) { static ox::Result<ox::Vector<char>> readFile(Context *ctx, const ox::FileAddress &file) noexcept {
oxRequire(stat, ctx->rom->stat(file)); oxRequire(stat, ctx->rom->stat(file));
ox::Vector<char> buff(stat.size); ox::Vector<char> buff(stat.size);
oxReturnError(ctx->rom->read(file, buff.data(), buff.size())); oxReturnError(ctx->rom->read(file, buff.data(), buff.size()));
@ -22,14 +22,14 @@ static ox::Result<ox::Vector<char>> readFile(Context *ctx, const ox::FileAddress
} }
template<typename T> template<typename T>
ox::Result<T> readObj(Context *ctx, const ox::FileAddress &file) { ox::Result<T> readObj(Context *ctx, const ox::FileAddress &file) noexcept {
oxRequire(buff, readFile(ctx, file)); oxRequire(buff, readFile(ctx, file));
T t; T t;
oxReturnError(ox::readClaw(buff.data(), buff.size(), &t)); oxReturnError(ox::readClaw(buff.data(), buff.size(), &t));
return ox::move(t); return ox::move(t);
} }
ox::Error initConsole(Context *ctx) { ox::Error initConsole(Context *ctx) noexcept {
constexpr auto TilesheetAddr = "/TileSheets/Charset.ng"; constexpr auto TilesheetAddr = "/TileSheets/Charset.ng";
constexpr auto PaletteAddr = "/Palettes/Charset.npal"; constexpr auto PaletteAddr = "/Palettes/Charset.npal";
setBgStatus(ctx, 0b0001); setBgStatus(ctx, 0b0001);
@ -39,14 +39,14 @@ ox::Error initConsole(Context *ctx) {
ox::Error loadSpriteTileSheet(Context*, ox::Error loadSpriteTileSheet(Context*,
int, int,
ox::FileAddress, ox::FileAddress,
ox::FileAddress) { ox::FileAddress) noexcept {
return OxError(0); return OxError(0);
} }
ox::Error loadBgTileSheet(Context *ctx, ox::Error loadBgTileSheet(Context *ctx,
int section, int section,
ox::FileAddress tilesheetPath, ox::FileAddress tilesheetPath,
ox::FileAddress palettePath) { ox::FileAddress palettePath) noexcept {
oxRequire(tilesheet, readObj<NostalgiaGraphic>(ctx, tilesheetPath)); oxRequire(tilesheet, readObj<NostalgiaGraphic>(ctx, tilesheetPath));
if (!palettePath) { if (!palettePath) {
palettePath = tilesheet.defaultPalette; palettePath = tilesheet.defaultPalette;
@ -72,7 +72,7 @@ ox::Error loadBgTileSheet(Context *ctx,
return renderer::loadBgTexture(ctx, section, pixels.data(), width, height); return renderer::loadBgTexture(ctx, section, pixels.data(), width, height);
} }
void puts(Context *ctx, int column, int row, const char *str) { void puts(Context *ctx, int column, int row, const char *str) noexcept {
for (int i = 0; str[i]; ++i) { for (int i = 0; str[i]; ++i) {
setTile(ctx, 0, column + i, row, static_cast<uint8_t>(charMap[static_cast<int>(str[i])])); setTile(ctx, 0, column + i, row, static_cast<uint8_t>(charMap[static_cast<int>(str[i])]));
} }

View File

@ -131,7 +131,7 @@ static Buffer genBuffer() noexcept {
return Buffer(buff); return Buffer(buff);
} }
static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) { static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) noexcept {
// vao // vao
bg->vao = genVertexArrayObject(); bg->vao = genVertexArrayObject();
glBindVertexArray(bg->vao); glBindVertexArray(bg->vao);
@ -151,7 +151,7 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg)
ox::bit_cast<void*>(2 * sizeof(float))); ox::bit_cast<void*>(2 * sizeof(float)));
} }
static Texture loadTexture(GLsizei w, GLsizei h, void *pixels) { static Texture loadTexture(GLsizei w, GLsizei h, void *pixels) noexcept {
GLuint texId = 0; GLuint texId = 0;
glGenTextures(1, &texId); glGenTextures(1, &texId);
Texture tex(texId); Texture tex(texId);
@ -167,7 +167,7 @@ static Texture loadTexture(GLsizei w, GLsizei h, void *pixels) {
return ox::move(tex); return ox::move(tex);
} }
static void tickFps(GlImplData *id) { static void tickFps(GlImplData *id) noexcept {
++id->draws; ++id->draws;
if (id->draws >= 500) { if (id->draws >= 500) {
using namespace std::chrono; using namespace std::chrono;
@ -183,7 +183,7 @@ static void tickFps(GlImplData *id) {
} }
} }
static void drawBackground(Background *bg) { static void drawBackground(Background *bg) noexcept {
if (bg->enabled) { if (bg->enabled) {
glBindVertexArray(bg->vao); glBindVertexArray(bg->vao);
if (bg->updated) { if (bg->updated) {
@ -195,7 +195,7 @@ static void drawBackground(Background *bg) {
} }
} }
static void drawBackgrounds(GlImplData *id) { static void drawBackgrounds(GlImplData *id) noexcept {
// load background shader and its uniforms // load background shader and its uniforms
glUseProgram(id->bgShader); glUseProgram(id->bgShader);
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(id->bgShader, "vTileHeight")); const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(id->bgShader, "vTileHeight"));
@ -205,7 +205,7 @@ static void drawBackgrounds(GlImplData *id) {
} }
} }
ox::Error init(Context *ctx) { ox::Error init(Context *ctx) noexcept {
const auto id = new GlImplData; const auto id = new GlImplData;
ctx->setRendererData(id); ctx->setRendererData(id);
oxReturnError(buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader)); oxReturnError(buildShaderProgram(bgvshad, bgfshad).moveTo(&id->bgShader));
@ -215,14 +215,14 @@ ox::Error init(Context *ctx) {
return OxError(0); return OxError(0);
} }
ox::Error shutdown(Context *ctx) { ox::Error shutdown(Context *ctx) noexcept {
const auto id = ctx->rendererData<GlImplData>(); const auto id = ctx->rendererData<GlImplData>();
ctx->setRendererData(nullptr); ctx->setRendererData(nullptr);
delete id; delete id;
return OxError(0); return OxError(0);
} }
ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) { ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) noexcept {
oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h); oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h);
const auto &id = ctx->rendererData<GlImplData>(); const auto &id = ctx->rendererData<GlImplData>();
auto &tex = id->backgrounds[static_cast<std::size_t>(section)].tex; auto &tex = id->backgrounds[static_cast<std::size_t>(section)].tex;
@ -232,7 +232,7 @@ ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) {
} }
uint8_t bgStatus(Context *ctx) { uint8_t bgStatus(Context *ctx) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto &id = ctx->rendererData<renderer::GlImplData>();
uint8_t out = 0; uint8_t out = 0;
for (unsigned i = 0; i < id->backgrounds.size(); ++i) { for (unsigned i = 0; i < id->backgrounds.size(); ++i) {
@ -241,25 +241,25 @@ uint8_t bgStatus(Context *ctx) {
return out; return out;
} }
void setBgStatus(Context *ctx, uint32_t status) { void setBgStatus(Context *ctx, uint32_t status) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto &id = ctx->rendererData<renderer::GlImplData>();
for (unsigned i = 0; i < id->backgrounds.size(); ++i) { for (unsigned i = 0; i < id->backgrounds.size(); ++i) {
id->backgrounds[i].enabled = (status >> i) & 1; id->backgrounds[i].enabled = (status >> i) & 1;
} }
} }
bool bgStatus(Context *ctx, unsigned bg) { bool bgStatus(Context *ctx, unsigned bg) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto &id = ctx->rendererData<renderer::GlImplData>();
return id->backgrounds[bg].enabled; return id->backgrounds[bg].enabled;
} }
void setBgStatus(Context *ctx, unsigned bg, bool status) { void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept {
const auto &id = ctx->rendererData<renderer::GlImplData>(); const auto &id = ctx->rendererData<renderer::GlImplData>();
id->backgrounds[bg].enabled = status; id->backgrounds[bg].enabled = status;
} }
void draw(Context *ctx) { void draw(Context *ctx) noexcept {
const auto id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
renderer::tickFps(id); renderer::tickFps(id);
// clear screen // clear screen
@ -269,14 +269,14 @@ void draw(Context *ctx) {
renderer::drawBackgrounds(id); renderer::drawBackgrounds(id);
} }
void clearTileLayer(Context *ctx, int layer) { void clearTileLayer(Context *ctx, int layer) noexcept {
const auto id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
auto &bg = id->backgrounds[static_cast<std::size_t>(layer)]; auto &bg = id->backgrounds[static_cast<std::size_t>(layer)];
initBackgroundBufferObjects(ctx, &bg); initBackgroundBufferObjects(ctx, &bg);
bg.updated = true; bg.updated = true;
} }
void hideSprite(Context*, unsigned) { void hideSprite(Context*, unsigned) noexcept {
} }
void setSprite(Context*, void setSprite(Context*,
@ -286,10 +286,10 @@ void setSprite(Context*,
unsigned, unsigned,
unsigned, unsigned,
unsigned, unsigned,
unsigned) { unsigned) noexcept {
} }
void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) { void setTile(Context *ctx, int layer, int column, int row, uint8_t tile) noexcept {
const auto id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
const auto z = static_cast<unsigned>(layer); const auto z = static_cast<unsigned>(layer);
const auto y = static_cast<unsigned>(row); const auto y = static_cast<unsigned>(row);

View File

@ -26,7 +26,7 @@ struct TextureBase {
GLsizei width = 0; GLsizei width = 0;
GLsizei height = 0; GLsizei height = 0;
constexpr TextureBase() = default; constexpr TextureBase() noexcept = default;
constexpr TextureBase(TextureBase &&tb) noexcept { constexpr TextureBase(TextureBase &&tb) noexcept {
width = tb.width; width = tb.width;
@ -51,7 +51,7 @@ struct GLobject: public Base {
GLuint id = 0; GLuint id = 0;
constexpr GLobject() = default; constexpr GLobject() noexcept = default;
explicit constexpr GLobject(GLuint id) { explicit constexpr GLobject(GLuint id) {
this->id = id; this->id = id;

View File

@ -14,21 +14,25 @@
namespace nostalgia::core { namespace nostalgia::core {
char *loadRom(const char *path) { ox::Result<char*> loadRom(const char *path) noexcept {
std::ifstream file(path, std::ios::binary | std::ios::ate); std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.good()) { if (!file.good()) {
oxTrace("nostalgia::core::userland::loadRom") << "Read failed:" << path; oxErrorf("Could not find ROM file: {}", path);
return nullptr; return OxError(1, "Could not find ROM file");
} }
try {
const auto size = file.tellg(); const auto size = file.tellg();
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
auto buff = new char[static_cast<std::size_t>(size)]; auto buff = new char[static_cast<std::size_t>(size)];
file.read(buff, size); file.read(buff, size);
return buff; return buff;
} catch (const std::ios_base::failure &e) {
oxErrorf("Could not read ROM file: {}", e.what());
return OxError(2, "Could not read ROM file");
}
} }
void unloadRom(char *rom) { void unloadRom(char *rom) noexcept {
delete rom; delete rom;
} }

View File

@ -15,11 +15,15 @@ int main(int argc, const char **argv) {
if (argc > 1) { if (argc > 1) {
ox::trace::init(); ox::trace::init();
auto path = argv[1]; auto path = argv[1];
auto fs = nostalgia::core::loadRomFs(path); auto [fs, err] = nostalgia::core::loadRomFs(path);
auto err = run(fs); if (err) {
oxAssert(err, "Something went wrong...");
return static_cast<int>(err);
}
err = run(fs);
oxAssert(err, "Something went wrong..."); oxAssert(err, "Something went wrong...");
delete fs; delete fs;
return err; return static_cast<int>(err);
} }
return 1; return 1;
} }

View File

@ -41,8 +41,7 @@ void Project::mkdir(QString path) const {
} }
ox::FileStat Project::stat(QString path) const { ox::FileStat Project::stat(QString path) const {
auto [s, e] = m_fs.stat(path.toUtf8().data()); oxRequireT(s, m_fs.stat(path.toUtf8().data()));
oxThrowError(e);
return s; return s;
} }

View File

@ -95,22 +95,20 @@ class NOSTALGIASTUDIO_EXPORT Project: public QObject {
template<typename T> template<typename T>
void Project::writeObj(QString path, T *obj) const { void Project::writeObj(QString path, T *obj) const {
// write MetalClaw // write MetalClaw
auto [buff, err] = ox::writeClaw(obj, ox::ClawFormat::Metal); oxRequireMT(buff, ox::writeClaw(obj, ox::ClawFormat::Metal));
oxThrowError(err);
// write to FS // write to FS
writeBuff(path, ox::bit_cast<uint8_t*>(buff.data()), buff.size()); writeBuff(path, ox::bit_cast<uint8_t*>(buff.data()), buff.size());
// write type descriptor // write type descriptor
const auto type = ox::buildTypeDef(obj); const auto type = ox::buildTypeDef(obj);
auto typeOut = ox::writeClaw(type.value, ox::ClawFormat::Organic); oxRequireMT(typeOut, ox::writeClaw(type.value, ox::ClawFormat::Organic));
oxThrowError(typeOut);
// replace garbage last character with new line // replace garbage last character with new line
typeOut.value.back().value = '\n'; typeOut.back().value = '\n';
// write to FS // write to FS
QString descPath = "/.nostalgia/type_descriptors/"; QString descPath = "/.nostalgia/type_descriptors/";
const auto typePath = descPath + type.value->typeName.c_str(); const auto typePath = descPath + type.value->typeName.c_str();
mkdir(descPath); mkdir(descPath);
writeBuff(typePath, ox::bit_cast<uint8_t*>(typeOut.value.data()), typeOut.value.size()); writeBuff(typePath, ox::bit_cast<uint8_t*>(typeOut.data()), typeOut.size());
emit fileUpdated(path); emit fileUpdated(path);
} }

View File

@ -258,7 +258,7 @@ void MainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockWidget)
QAction *MainWindow::addAction(QMenu *menu, QString text, QString toolTip, const QObject *tgt, const char *cb) { QAction *MainWindow::addAction(QMenu *menu, QString text, QString toolTip, const QObject *tgt, const char *cb) {
auto action = menu->addAction(text); auto action = menu->addAction(text);
action->setStatusTip(toolTip); action->setStatusTip(toolTip);
auto conn = connect(action, SIGNAL(triggered()), tgt, cb); connect(action, SIGNAL(triggered()), tgt, cb);
return action; return action;
} }
@ -267,7 +267,7 @@ QAction *MainWindow::addAction(QMenu *menu, QString text, QString toolTip,
auto action = menu->addAction(text); auto action = menu->addAction(text);
action->setShortcuts(key); action->setShortcuts(key);
action->setStatusTip(toolTip); action->setStatusTip(toolTip);
auto conn = connect(action, SIGNAL(triggered()), tgt, cb); connect(action, SIGNAL(triggered()), tgt, cb);
return action; return action;
} }
@ -276,7 +276,7 @@ QAction *MainWindow::addAction(QMenu *menu, QString text, QString toolTip,
auto action = menu->addAction(text); auto action = menu->addAction(text);
action->setShortcuts(key); action->setShortcuts(key);
action->setStatusTip(toolTip); action->setStatusTip(toolTip);
auto conn = connect(action, &QAction::triggered, cb); connect(action, &QAction::triggered, cb);
return action; return action;
} }