From 134f5593e222a89330a5c8768b10be0974e19001 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 31 Mar 2021 00:33:31 -0500 Subject: [PATCH] [nostalgia/player] Split player application logic into separate files --- src/nostalgia/player/CMakeLists.txt | 1 + src/nostalgia/player/app.cpp | 50 +++++++++++++++++++++++++++++ src/nostalgia/player/app.hpp | 11 +++++++ src/nostalgia/player/main.cpp | 44 ++----------------------- 4 files changed, 64 insertions(+), 42 deletions(-) create mode 100644 src/nostalgia/player/app.cpp create mode 100644 src/nostalgia/player/app.hpp diff --git a/src/nostalgia/player/CMakeLists.txt b/src/nostalgia/player/CMakeLists.txt index d14c3729..faff4d19 100644 --- a/src/nostalgia/player/CMakeLists.txt +++ b/src/nostalgia/player/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable( nostalgia + app.cpp main.cpp ) diff --git a/src/nostalgia/player/app.cpp b/src/nostalgia/player/app.cpp new file mode 100644 index 00000000..d0d57c3c --- /dev/null +++ b/src/nostalgia/player/app.cpp @@ -0,0 +1,50 @@ +/* + * Copyright 2016 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +using namespace nostalgia; + +static unsigned spriteX = 72; +static unsigned spriteY = 64; + +static int eventHandler(core::Context *ctx) { + if (core::buttonDown(core::GamePad_Right)) { + spriteX += 2; + } else if (core::buttonDown(core::GamePad_Left)) { + spriteX -= 2; + } + if (core::buttonDown(core::GamePad_Down)) { + spriteY += 2; + } else if (core::buttonDown(core::GamePad_Up)) { + spriteY -= 2; + } + constexpr auto s = "nostalgia"; + for (unsigned i = 0; s[i]; ++i) { + const auto c = static_cast(s[i] - ('a' - 1)); + core::setSprite(ctx, i, spriteX + 8 * (i + 1), spriteY, c); + } + return 16; +} + +ox::Error run(ox::FileSystem *fs) { + core::Context ctx; + ctx.rom = fs; + oxReturnError(core::init(&ctx)); + constexpr auto TileSheetAddr = "/TileSheets/Charset.ng"; + constexpr auto PaletteAddr = "/Palettes/Charset.npal"; + oxReturnError(core::loadSpriteTileSheet(&ctx, 0, TileSheetAddr, PaletteAddr)); + oxReturnError(core::initConsole(&ctx)); + core::puts(&ctx, 10, 9, "DOPENESS!!!"); + core::setEventHandler(eventHandler); + oxReturnError(core::run(&ctx)); + oxReturnError(core::shutdownGfx(&ctx)); + return OxError(0); +} + diff --git a/src/nostalgia/player/app.hpp b/src/nostalgia/player/app.hpp new file mode 100644 index 00000000..fc65b56c --- /dev/null +++ b/src/nostalgia/player/app.hpp @@ -0,0 +1,11 @@ +/* + * Copyright 2016 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +ox::Error run(ox::FileSystem *fs); diff --git a/src/nostalgia/player/main.cpp b/src/nostalgia/player/main.cpp index b32805af..d0fdb32c 100644 --- a/src/nostalgia/player/main.cpp +++ b/src/nostalgia/player/main.cpp @@ -6,53 +6,13 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include #include #include -#include + +#include "app.hpp" using namespace nostalgia; -static unsigned spriteX = 72; -static unsigned spriteY = 64; - -static int eventHandler(core::Context *ctx) { - if (core::buttonDown(core::GamePad_Right)) { - spriteX += 2; - } else if (core::buttonDown(core::GamePad_Left)) { - spriteX -= 2; - } - if (core::buttonDown(core::GamePad_Down)) { - spriteY += 2; - } else if (core::buttonDown(core::GamePad_Up)) { - spriteY -= 2; - } - constexpr auto s = "nostalgia"; - for (unsigned i = 0; s[i]; ++i) { - const auto c = static_cast(s[i] - ('a' - 1)); - core::setSprite(ctx, i, spriteX + 8 * (i + 1), spriteY, c); - } - return 16; -} - -static ox::Error run(ox::FileSystem *fs) { - core::Context ctx; - ctx.rom = fs; - oxReturnError(core::init(&ctx)); - //Zone zone; - //oxReturnError(zone.init(&ctx, Bounds{0, 0, 40, 40}, "/TileSheets/Charset.ng", "/Palettes/Charset.npal")); - //zone.draw(&ctx); - constexpr auto TileSheetAddr = "/TileSheets/Charset.ng"; - constexpr auto PaletteAddr = "/Palettes/Charset.npal"; - oxReturnError(core::loadSpriteTileSheet(&ctx, 0, TileSheetAddr, PaletteAddr)); - oxReturnError(core::initConsole(&ctx)); - core::puts(&ctx, 10, 9, "DOPENESS!!!"); - core::setEventHandler(eventHandler); - oxReturnError(core::run(&ctx)); - oxReturnError(core::shutdownGfx(&ctx)); - return OxError(0); -} - int main(int argc, const char **argv) { if (argc > 1) { ox::trace::init();