Gary Talent 4ccdfc3a6e Squashed 'deps/nostalgia/' changes from 3c7652ef..161640fa
161640fa [nostalgia] Cleanup
e42126c9 [nostalgia/core] Improve TileSheet validation, add repair
36942cca [nostalgia,olympic] Replace SpanView with Span<const T>
b14f1d50 [ox] Replace SpanView with Span<const T>
1bf4f246 [applib] Make run take args as a SpanView
edda8e01 [ox/clargs] Add constructor that takes a SpanView
3308b4dd [ox/std] Add missing + and += operators to Span
27f4703a [teagba] Suppress warnings for unsafe buffers
6af00d9a [nostalgia] Enable warnings for unsafe buffers
86b9f931 [olympic] Enable warnings for unsafe buffers
a0ed1b3f [ox/std] Fix Span raw array constructor
8dad624b [studio/applib] Cleanup
dc6605fd [keel] Add missing error checking to pack
c78d3cf6 [ox] Add more unsafe buffer exceptions
cee4f65d [ox/std] Replace an unsafe buffer
cd3eeeef [ox/fs] Suppress unsafe buffer warnings
287d42f2 [ox/clargs] Cleanup
dbbaaa46 [ox/clargs] Enable unsafe buffer warnings
9b8a8c4e [ox/std] Enable unsafe buffer warnings
e44fa288 [cityhash] Add pragmas to ignore unsafe buffer warnings
e13c6e81 [ox/std] Remove raw char* CharBufferWriter constructor
cb55b31a [ox/std] Cleanup
ab3f9e16 [ox/std] Make Span access check message consistent with other messages
8f25ef96 [ox/std] Make CharBufferWriter constructor take a Span
e13eebaf [ox/std] Cleanup an unsafe buffer
114f5c66 [ox/std] Add overflow checking to SpanIterator
df44fe23 [keel] Cleanup
72f4db3d [nostalgia/core/studio] Fix paste command to never paste beyond target dimensions
8a9ff971 [nostalgia/core] Fix resizeSubsheet to work for both growing and shrinking
5a8da59d [keel] Fix readAsset to actually return asset
afa3a13d [keel] Cleanup
6522cf8a [keel] Add ensureValid call to readAsset
f772e48b [ox] Add Vector/Array/Span overflow checking
13bfe881 [nostalgia/core] Fix resizeSubsheet array overflow
50254754 Merge commit '9e11019b87ba27d1dac9e097dc212a126e404218'
bfe890ae [ox] Fix typo in docs
ab5bc1ad [ox/std] Remove oxRequireT and oxRequireMT
abf7548a [nostalgia/core] Add missing include
e2682b5e [studio/modlib] Add missing include
792ad414 [nostalgia] Remove .vs dir

git-subtree-dir: deps/nostalgia
git-subtree-split: 161640fa11986677dc2e1da6ffd4575e38ab31ad
2024-12-04 19:55:17 -06:00

176 lines
5.5 KiB
C++

/*
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <keel/media.hpp>
#include <turbine/turbine.hpp>
#include <nostalgia/core/core.hpp>
#include <nostalgia/scene/scene.hpp>
using namespace nostalgia;
static int spriteX{};
static int spriteY{};
static bool s_paused = false;
[[maybe_unused]]
static int testUpdateHandler(turbine::Context &tctx) noexcept {
auto &cctx = *turbine::applicationData<core::Context>(tctx);
constexpr auto sleepTime = 16;
if (s_paused) {
return sleepTime;
}
int xmod = 0;
int ymod = 0;
if (buttonDown(tctx, turbine::Alpha_D) || buttonDown(tctx, turbine::GamePad_Right)) {
xmod = 2;
} else if (buttonDown(tctx, turbine::Alpha_A) || buttonDown(tctx, turbine::GamePad_Left)) {
xmod = -2;
}
if (buttonDown(tctx, turbine::Alpha_S) || buttonDown(tctx, turbine::GamePad_Down)) {
ymod = 2;
} else if (buttonDown(tctx, turbine::Alpha_W) || buttonDown(tctx, turbine::GamePad_Up)) {
ymod = -2;
}
if (!xmod && !ymod) {
spriteX += 1;
}
spriteX += xmod;
spriteY += ymod;
constexpr ox::StringView sprites = "nostalgia";
for (unsigned i = 0; i < sprites.len(); ++i) {
auto const c = static_cast<unsigned>(sprites[i] - ('a' - 1));
core::setSprite(cctx, i, {
.enabled = true,
.x = spriteX + 8 * (static_cast<int>(i) + 1),
.y = spriteY,
.tileIdx = c,
.priority = 1,
});
}
return sleepTime;
}
[[maybe_unused]]
static void testKeyEventHandler(turbine::Context &tctx, turbine::Key key, bool down) noexcept {
if (down) {
if (key == turbine::Key::Alpha_Q) {
turbine::requestShutdown(tctx);
} else if (key == turbine::Key::Alpha_P) {
s_paused = !s_paused;
}
}
}
[[maybe_unused]]
static ox::Error runTest(turbine::Context &tctx) {
constexpr ox::StringView TileSheetAddr{"/TileSheets/Charset.ng"};
constexpr ox::StringView PaletteAddr{"/Palettes/Chester.npal"};
oxRequireM(cctx, core::init(tctx));
turbine::setApplicationData(tctx, cctx.get());
oxRequire(tsStat, turbine::rom(tctx)->stat(PaletteAddr));
oxReturnError(core::loadSpriteTileSheet(*cctx, TileSheetAddr));
oxReturnError(core::loadSpritePalette(*cctx, PaletteAddr));
oxReturnError(core::initConsole(*cctx));
core::puts(*cctx, 10, 9, "DOPENESS!!!");
turbine::setUpdateHandler(tctx, testUpdateHandler);
turbine::setKeyEventHandler(tctx, testKeyEventHandler);
return turbine::run(tctx);
}
[[maybe_unused]]
static ox::Error runTileSheetSetTest(turbine::Context &tctx) {
// this should make the screen display 'ABCDB', with the A being upside down
// and the first B being backwards
constexpr ox::StringView PaletteAddr{"/Palettes/Charset.npal"};
oxRequireM(cctx, core::init(tctx));
turbine::setApplicationData(tctx, cctx.get());
oxRequire(tsStat, turbine::rom(tctx)->stat(PaletteAddr));
core::TileSheetSet const set{
.bpp = 4,
.entries = {
{ .tilesheet = ox::StringLiteral{"/TileSheets/Chester.ng"}, .sections{{.begin = 0, .tiles = 1}} },
{ .tilesheet = ox::StringLiteral{"/TileSheets/AB.ng"}, .sections{{.begin = 0, .tiles = 2}} },
{ .tilesheet = ox::StringLiteral{"/TileSheets/CD.ng"}, .sections{{.begin = 0, .tiles = 2}} },
{ .tilesheet = ox::StringLiteral{"/TileSheets/AB.ng"}, .sections{{.begin = 1, .tiles = 1}} },
},
};
constexpr auto bgPalBank = 1;
oxReturnError(core::loadBgTileSheet(*cctx, 0, set));
oxReturnError(core::loadSpriteTileSheet(*cctx, set));
oxReturnError(core::loadBgPalette(*cctx, bgPalBank, PaletteAddr));
oxReturnError(core::loadBgPalette(*cctx, 0, PaletteAddr));
oxReturnError(core::loadSpritePalette(*cctx, PaletteAddr));
core::setBgStatus(*cctx, 0, true);
core::setBgTile(*cctx, 0, 10, 9, { .tileIdx = 1, .palBank = bgPalBank, .flipX = 0, .flipY = 1 });
core::setBgTile(*cctx, 0, 11, 9, { .tileIdx = 2, .palBank = bgPalBank, .flipX = 1, .flipY = 0 });
core::setBgTile(*cctx, 0, 13, 9, { .tileIdx = 4, .palBank = bgPalBank, .flipX = 0, .flipY = 0 });
core::setSprite(*cctx, 16, {
.enabled = true,
.x = 12 * 8,
.y = 9 * 8,
.tileIdx = 3,
.bpp = static_cast<unsigned>(set.bpp),
});
core::setSprite(*cctx, 17, {
.enabled = true,
.x = 14 * 8,
.y = 9 * 8,
.tileIdx = 5,
.bpp = static_cast<unsigned>(set.bpp),
});
turbine::setKeyEventHandler(tctx, testKeyEventHandler);
return turbine::run(tctx);
}
static int sceneUpdateHandler(turbine::Context&) noexcept {
constexpr auto sleepTime = 16;
if (s_paused) {
return sleepTime;
}
// do stuff
return sleepTime;
}
static void sceneKeyEventHandler(turbine::Context &tctx, turbine::Key key, bool down) noexcept {
if (down) {
if (key == turbine::Key::Alpha_Q) {
turbine::requestShutdown(tctx);
} else if (key == turbine::Key::Alpha_P) {
s_paused = !s_paused;
}
}
}
[[maybe_unused]]
static ox::Error runScene(turbine::Context &tctx) {
constexpr ox::StringView SceneAddr{"/Scenes/Chester.nscn"};
oxRequireM(cctx, core::init(tctx));
oxRequire(scn, keel::readObj<scene::SceneStatic>(keelCtx(tctx), SceneAddr));
turbine::setUpdateHandler(tctx, sceneUpdateHandler);
turbine::setKeyEventHandler(tctx, sceneKeyEventHandler);
scene::Scene const scene(*scn);
oxReturnError(scene.setupDisplay(*cctx));
return turbine::run(tctx);
}
namespace olympic {
ox::Error run(
[[maybe_unused]] ox::StringView project,
[[maybe_unused]] ox::StringView appName,
[[maybe_unused]] ox::StringView projectDataDir,
ox::SpanView<char const*> args) noexcept {
if (args.size() < 2) {
return OxError(1, "Please provide path to project directory or OxFS file.");
}
auto const path = args[1];
oxRequireM(fs, keel::loadRomFs(path));
oxRequireM(tctx, turbine::init(std::move(fs), project));
return runTileSheetSetTest(*tctx);
}
}