[keel,nostalgia,studio,turbine] Make turbine::Context have a keel::Context instead of being one

This commit is contained in:
2023-06-20 00:56:55 -05:00
parent d4eaade326
commit c5233e0d1d
24 changed files with 134 additions and 79 deletions

View File

@@ -16,7 +16,7 @@ struct GbaContext: public core::Context {
[[nodiscard]]
const auto &rom() const noexcept {
return *turbineCtx->rom;
return *turbine::rom(*turbineCtx);
}
};

View File

@@ -133,18 +133,20 @@ static ox::Error loadBgTileSheet(
return {};
}
ox::Error loadBgTileSheet(Context *ctx,
unsigned cbb,
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
ox::Error loadBgTileSheet(
Context *ctx,
unsigned cbb,
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx);
auto &rom = static_cast<const ox::MemFS&>(gctx.rom());
return loadBgTileSheet(rom, cbb, tilesheetAddr, paletteAddr);
}
ox::Error loadSpriteTileSheet(Context *ctx,
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
ox::Error loadSpriteTileSheet(
Context *ctx,
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
auto &gctx = static_cast<GbaContext&>(*ctx);
oxRequire(tsStat, gctx.rom().stat(tilesheetAddr));
oxRequire(ts, static_cast<const ox::MemFS&>(gctx.rom()).directAccess(tilesheetAddr));

View File

@@ -76,7 +76,6 @@ static constexpr auto bgVertexRow(unsigned x, unsigned y) noexcept {
}
static void setSpriteBufferObject(
Context*,
unsigned vi,
float enabled,
float x,
@@ -110,7 +109,6 @@ static void setSpriteBufferObject(
}
static void setTileBufferObject(
Context*,
unsigned vi,
float x,
float y,
@@ -139,22 +137,21 @@ static void setTileBufferObject(
memcpy(ebo, elms.data(), sizeof(elms));
}
static void initSpriteBufferObjects(Context *ctx, glutils::BufferSet *bs) noexcept {
static void initSpriteBufferObjects(glutils::BufferSet *bs) noexcept {
for (auto i = 0u; i < SpriteCount; ++i) {
auto vbo = &bs->vertices[i * static_cast<std::size_t>(SpriteVertexVboLength)];
auto ebo = &bs->elements[i * static_cast<std::size_t>(SpriteVertexEboLength)];
setSpriteBufferObject(ctx, i * SpriteVertexVboRows, 0, 0, 0, 0, false, vbo, ebo);
setSpriteBufferObject(i * SpriteVertexVboRows, 0, 0, 0, 0, false, vbo, ebo);
}
}
static void initBackgroundBufferObjects(Context *ctx, glutils::BufferSet *bg) noexcept {
static void initBackgroundBufferObjects(glutils::BufferSet *bg) noexcept {
for (auto x = 0u; x < TileColumns; ++x) {
for (auto y = 0u; y < TileRows; ++y) {
const auto i = bgVertexRow(x, y);
auto vbo = &bg->vertices[i * static_cast<std::size_t>(BgVertexVboLength)];
auto ebo = &bg->elements[i * static_cast<std::size_t>(BgVertexEboLength)];
setTileBufferObject(
ctx,
static_cast<unsigned>(i * BgVertexVboRows),
static_cast<float>(x),
static_cast<float>(y),
@@ -165,15 +162,14 @@ static void initBackgroundBufferObjects(Context *ctx, glutils::BufferSet *bg) no
}
}
static void initSpritesBufferset(
Context *ctx, GLuint shader, glutils::BufferSet *bs) noexcept {
static void initSpritesBufferset(GLuint shader, glutils::BufferSet *bs) noexcept {
// vao
bs->vao = glutils::generateVertexArrayObject();
glBindVertexArray(bs->vao);
// vbo & ebo
bs->vbo = glutils::generateBuffer();
bs->ebo = glutils::generateBuffer();
initSpriteBufferObjects(ctx, bs);
initSpriteBufferObjects(bs);
glutils::sendVbo(*bs);
glutils::sendEbo(*bs);
// vbo layout
@@ -191,7 +187,6 @@ static void initSpritesBufferset(
}
static void initBackgroundBufferset(
Context *ctx,
GLuint shader,
glutils::BufferSet *bg) noexcept {
// vao
@@ -200,7 +195,7 @@ static void initBackgroundBufferset(
// vbo & ebo
bg->vbo = glutils::generateBuffer();
bg->ebo = glutils::generateBuffer();
initBackgroundBufferObjects(ctx, bg);
initBackgroundBufferObjects(bg);
glutils::sendVbo(*bg);
glutils::sendEbo(*bg);
// vbo layout
@@ -349,11 +344,11 @@ ox::Error initGfx(
oxReturnError(
glutils::buildShaderProgram(spriteVshad.c_str(), spriteFshad.c_str()).moveTo(&gctx.spriteShader));
for (auto &bg : gctx.cbbs) {
initBackgroundBufferset(ctx, gctx.bgShader, &bg);
initBackgroundBufferset(gctx.bgShader, &bg);
}
if (initParams.glInstallDrawer) {
turbine::gl::addDrawer(gctx.turbineCtx, &gctx.drawer);
initSpritesBufferset(ctx, gctx.spriteShader, &gctx.spriteBlocks);
initSpritesBufferset(gctx.spriteShader, &gctx.spriteBlocks);
}
return {};
}
@@ -399,7 +394,7 @@ ox::Error loadBgTileSheet(
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx);
auto &kctx = gctx.turbineCtx;
auto &kctx = gctx.turbineCtx.keelCtx;
oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr));
oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
oxRequire(tsd, loadTileSheet(ctx, *tilesheet));
@@ -413,7 +408,7 @@ ox::Error loadSpriteTileSheet(
const ox::FileAddress &tilesheetAddr,
const ox::FileAddress &paletteAddr) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx);
auto &kctx = gctx.turbineCtx;
auto &kctx = gctx.turbineCtx.keelCtx;
oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr));
oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
oxRequire(tsd, loadTileSheet(ctx, *tilesheet));
@@ -473,7 +468,7 @@ void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept {
void clearTileLayer(Context *ctx, unsigned bgIdx) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx);
auto &bg = gctx.cbbs[static_cast<std::size_t>(bgIdx)];
initBackgroundBufferObjects(&gctx, &bg);
initBackgroundBufferObjects(&bg);
bg.updated = true;
}
@@ -481,7 +476,7 @@ void hideSprite(Context *ctx, unsigned idx) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx);
auto vbo = &gctx.spriteBlocks.vertices[idx * renderer::SpriteVertexVboLength];
auto ebo = &gctx.spriteBlocks.elements[idx * renderer::SpriteVertexEboLength];
renderer::setSpriteBufferObject(ctx, idx * renderer::SpriteVertexVboRows, 0,
renderer::setSpriteBufferObject(idx * renderer::SpriteVertexVboRows, 0,
0, 0, 0, false, vbo, ebo);
gctx.spriteBlocks.updated = true;
}
@@ -526,7 +521,7 @@ void setSprite(
const auto cidx = idx + i;
auto vbo = &gctx.spriteBlocks.vertices[cidx * renderer::SpriteVertexVboLength];
auto ebo = &gctx.spriteBlocks.elements[cidx * renderer::SpriteVertexEboLength];
renderer::setSpriteBufferObject(ctx, cidx * renderer::SpriteVertexVboRows, 1,
renderer::setSpriteBufferObject(cidx * renderer::SpriteVertexVboRows, 1,
fX, fY, tileIdx + i, flipX, vbo, ebo);
++i;
};
@@ -565,7 +560,6 @@ void setTile(
auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
renderer::setTileBufferObject(
ctx,
static_cast<unsigned>(i * renderer::BgVertexVboRows),
static_cast<float>(x),
static_cast<float>(y),

View File

@@ -26,7 +26,7 @@ ox::Result<PaletteEditorImGui*> PaletteEditorImGui::make(turbine::Context *ctx,
out->m_itemPath = path;
const auto lastSlash = std::find(out->m_itemPath.rbegin(), out->m_itemPath.rend(), '/').offset();
out->m_itemName = out->m_itemPath.substr(lastSlash + 1);
oxRequire(pal, keel::readObj<Palette>(out->m_ctx, ox::FileAddress(out->m_itemPath.c_str())));
oxRequire(pal, keel::readObj<Palette>(&out->m_ctx->keelCtx, ox::FileAddress(out->m_itemPath.c_str())));
out->m_pal = *pal;
return out.release();
}
@@ -148,7 +148,7 @@ void PaletteEditorImGui::draw(turbine::Context*) noexcept {
ox::Error PaletteEditorImGui::saveItem() noexcept {
const auto sctx = applicationData<studio::StudioContext>(*m_ctx);
oxReturnError(sctx->project->writeObj(m_itemPath, &m_pal));
oxReturnError(m_ctx->assetManager.setAsset(m_itemPath, m_pal));
oxReturnError(m_ctx->keelCtx.assetManager.setAsset(m_itemPath, m_pal));
return {};
}

View File

@@ -566,10 +566,10 @@ class PaletteChangeCommand: public TileSheetCommand {
TileSheetEditorModel::TileSheetEditorModel(turbine::Context *ctx, ox::String path):
m_ctx(ctx),
m_path(std::move(path)) {
oxRequireT(img, readObj<TileSheet>(ctx, m_path));
oxRequireT(img, readObj<TileSheet>(&ctx->keelCtx, m_path));
m_img = *img;
if (m_img.defaultPalette) {
oxThrowError(readObj<Palette>(ctx, m_img.defaultPalette).moveTo(&m_pal));
oxThrowError(readObj<Palette>(&ctx->keelCtx, m_img.defaultPalette).moveTo(&m_pal));
}
m_pal.updated.connect(this, &TileSheetEditorModel::markUpdated);
m_undoStack.changeTriggered.connect(this, &TileSheetEditorModel::markUpdatedCmdId);
@@ -633,7 +633,7 @@ ox::StringView TileSheetEditorModel::palPath() const noexcept {
constexpr ox::StringView uuidPrefix = "uuid://";
if (ox::beginsWith(path, uuidPrefix)) {
auto uuid = ox::StringView(path + uuidPrefix.bytes(), ox_strlen(path) - uuidPrefix.bytes());
auto out = m_ctx->uuidToPath.at(uuid);
auto out = m_ctx->keelCtx.uuidToPath.at(uuid);
if (out.error) {
return {};
}
@@ -644,7 +644,7 @@ ox::StringView TileSheetEditorModel::palPath() const noexcept {
}
ox::Error TileSheetEditorModel::setPalette(const ox::String &path) noexcept {
oxRequire(uuid, m_ctx->pathToUuid.at(path));
oxRequire(uuid, m_ctx->keelCtx.pathToUuid.at(path));
pushCommand(ox::make<PaletteChangeCommand>(activeSubSheetIdx(), m_img, uuid->toString()));
return {};
}
@@ -752,7 +752,7 @@ ox::Error TileSheetEditorModel::markUpdatedCmdId(const studio::UndoCommand *cmd)
m_updated = true;
const auto cmdId = cmd->commandId();
if (static_cast<CommandId>(cmdId) == CommandId::PaletteChange) {
oxReturnError(readObj<Palette>(m_ctx, ox::StringView(m_img.defaultPalette.getPath().value)).moveTo(&m_pal));
oxReturnError(readObj<Palette>(&m_ctx->keelCtx, ox::StringView(m_img.defaultPalette.getPath().value)).moveTo(&m_pal));
}
auto tsCmd = dynamic_cast<const TileSheetCommand*>(cmd);
auto idx = m_img.validateSubSheetIdx(tsCmd->subsheetIdx());
@@ -774,7 +774,7 @@ void TileSheetEditorModel::ackUpdate() noexcept {
ox::Error TileSheetEditorModel::saveFile() noexcept {
const auto sctx = applicationData<studio::StudioContext>(*m_ctx);
oxReturnError(sctx->project->writeObj(m_path, &m_img));
return m_ctx->assetManager.setAsset(m_path, m_img).error;
return m_ctx->keelCtx.assetManager.setAsset(m_path, m_img).error;
}
bool TileSheetEditorModel::pixelSelected(std::size_t idx) const noexcept {

View File

@@ -50,7 +50,7 @@ void SceneEditorImGui::onActivated() noexcept {
ox::Error SceneEditorImGui::saveItem() noexcept {
const auto sctx = applicationData<studio::StudioContext>(*m_ctx);
oxReturnError(sctx->project->writeObj(m_itemPath, &m_editor.scene()));
oxReturnError(m_ctx->assetManager.setAsset(m_itemPath, m_editor.scene()));
oxReturnError(m_ctx->keelCtx.assetManager.setAsset(m_itemPath, m_editor.scene()));
return {};
}

View File

@@ -10,7 +10,7 @@ namespace nostalgia::scene {
SceneEditor::SceneEditor(turbine::Context *ctx, ox::CRStringView path) {
m_ctx = ctx;
oxRequireT(scn, keel::readObj<SceneStatic>(m_ctx, path));
oxRequireT(scn, keel::readObj<SceneStatic>(&m_ctx->keelCtx, path));
m_scene = *scn;
}

View File

@@ -32,12 +32,12 @@ static void keyEventHandler(turbine::Context &tctx, turbine::Key key, bool down)
}
}
ox::Error run(ox::UniquePtr<ox::FileSystem> fs) noexcept {
ox::Error run(ox::UniquePtr<ox::FileSystem> &&fs) noexcept {
oxTraceInitHook();
oxRequireM(tctx, turbine::init(std::move(fs), "Nostalgia"));
oxRequireM(cctx, core::init(tctx.get()));
constexpr ox::FileAddress SceneAddr("/Scenes/Chester.nscn");
oxRequire(scn, keel::readObj<scene::SceneStatic>(tctx.get(), SceneAddr));
oxRequire(scn, keel::readObj<scene::SceneStatic>(&tctx->keelCtx, SceneAddr));
turbine::setUpdateHandler(*tctx, updateHandler);
turbine::setKeyEventHandler(*tctx, keyEventHandler);
s_scene.emplace(*scn);

View File

@@ -3,5 +3,6 @@
*/
#include <ox/std/memory.hpp>
#include <ox/fs/fs.hpp>
typename ox::Error run(ox::UniquePtr<class ox::FileSystem> fs) noexcept;
typename ox::Error run(ox::UniquePtr<ox::FileSystem> &&fs) noexcept;