[nostalgia] Cleanup

This commit is contained in:
Gary Talent 2023-11-05 10:19:27 -06:00
parent 4ae6fd7c17
commit 868adae053
10 changed files with 139 additions and 124 deletions

View File

@ -5,15 +5,11 @@
#pragma once #pragma once
#include <ox/std/array.hpp> #include <ox/std/array.hpp>
#include <ox/std/point.hpp>
#include <ox/std/size.hpp> #include <ox/std/size.hpp>
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
#include <ox/model/def.hpp> #include <ox/model/def.hpp>
#include "color.hpp"
#include "context.hpp" #include "context.hpp"
#include "ptidxconv.hpp"
#include "tilesheet.hpp"
namespace nostalgia::core { namespace nostalgia::core {
@ -82,6 +78,6 @@ void setSprite(Context *ctx, Sprite const&s) noexcept;
} }
namespace nostalgia::core::gl { namespace nostalgia::core::gl {
void drawMainView(core::Context*, ox::Size const&) noexcept; void drawMainView(core::Context&, ox::Size const&) noexcept;
} }

View File

@ -13,8 +13,10 @@
#include <keel/media.hpp> #include <keel/media.hpp>
#include <turbine/turbine.hpp> #include <turbine/turbine.hpp>
#include <nostalgia/core/color.hpp>
#include <nostalgia/core/context.hpp> #include <nostalgia/core/context.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/tilesheet.hpp>
#include "context.hpp" #include "context.hpp"

View File

@ -12,17 +12,27 @@
#include <nostalgia/core/context.hpp> #include <nostalgia/core/context.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/tilesheet.hpp>
#include "context.hpp" #include "context.hpp"
namespace nostalgia::core { namespace nostalgia::core {
[[nodiscard]]
inline GlContext &glctx(Context &ctx) noexcept {
if constexpr(ox::defines::Debug) {
return dynamic_cast<GlContext&>(ctx);
} else {
return static_cast<GlContext&>(ctx);
}
}
namespace renderer { namespace renderer {
Drawer::Drawer(Context &ctx) noexcept: m_ctx(ctx) {} Drawer::Drawer(Context &ctx) noexcept: m_ctx(ctx) {}
void Drawer::draw(turbine::Context &tctx) noexcept { void Drawer::draw(turbine::Context &tctx) noexcept {
core::gl::drawMainView(&m_ctx, turbine::getScreenSize(tctx)); core::gl::drawMainView(m_ctx, turbine::getScreenSize(tctx));
} }
constexpr ox::StringView bgvshadTmpl = R"( constexpr ox::StringView bgvshadTmpl = R"(
@ -71,17 +81,17 @@ constexpr ox::StringView spritevshadTmpl = R"(
constexpr ox::StringView spritefshadTmpl = bgfshadTmpl; constexpr ox::StringView spritefshadTmpl = bgfshadTmpl;
[[nodiscard]] [[nodiscard]]
static constexpr auto bgVertexRow(unsigned x, unsigned y) noexcept { static constexpr auto bgVertexRow(uint_t x, uint_t y) noexcept {
return y * TileRows + x; return y * TileRows + x;
} }
static void setSpriteBufferObject( static void setSpriteBufferObject(
unsigned vi, uint_t vi,
float enabled, float enabled,
float x, float x,
float y, float y,
unsigned textureRow, uint_t textureRow,
unsigned flipX, uint_t flipX,
float *vbo, float *vbo,
GLuint *ebo) noexcept { GLuint *ebo) noexcept {
// don't worry, this memcpy gets optimized to something much more ideal // don't worry, this memcpy gets optimized to something much more ideal
@ -109,10 +119,10 @@ static void setSpriteBufferObject(
} }
void setTileBufferObject( void setTileBufferObject(
unsigned vi, uint_t vi,
float x, float x,
float y, float y,
unsigned textureRow, uint_t textureRow,
float *vbo, float *vbo,
GLuint *ebo) noexcept { GLuint *ebo) noexcept {
// don't worry, this memcpy gets optimized to something much more ideal // don't worry, this memcpy gets optimized to something much more ideal
@ -137,22 +147,22 @@ void setTileBufferObject(
memcpy(ebo, elms.data(), sizeof(elms)); memcpy(ebo, elms.data(), sizeof(elms));
} }
static void initSpriteBufferObjects(glutils::BufferSet *bs) noexcept { static void initSpriteBufferObjects(glutils::BufferSet &bs) noexcept {
for (auto i = 0u; i < SpriteCount; ++i) { for (auto i = 0u; i < SpriteCount; ++i) {
auto vbo = &bs->vertices[i * static_cast<std::size_t>(SpriteVertexVboLength)]; auto vbo = &bs.vertices[i * static_cast<std::size_t>(SpriteVertexVboLength)];
auto ebo = &bs->elements[i * static_cast<std::size_t>(SpriteVertexEboLength)]; auto ebo = &bs.elements[i * static_cast<std::size_t>(SpriteVertexEboLength)];
setSpriteBufferObject(i * SpriteVertexVboRows, 0, 0, 0, 0, false, vbo, ebo); setSpriteBufferObject(i * SpriteVertexVboRows, 0, 0, 0, 0, false, vbo, ebo);
} }
} }
static void initBackgroundBufferObjects(glutils::BufferSet *bg) noexcept { static void initBackgroundBufferObjects(glutils::BufferSet &bg) noexcept {
for (auto x = 0u; x < TileColumns; ++x) { for (auto x = 0u; x < TileColumns; ++x) {
for (auto y = 0u; y < TileRows; ++y) { for (auto y = 0u; y < TileRows; ++y) {
const auto i = bgVertexRow(x, y); const auto i = bgVertexRow(x, y);
auto vbo = &bg->vertices[i * static_cast<std::size_t>(BgVertexVboLength)]; auto vbo = &bg.vertices[i * static_cast<std::size_t>(BgVertexVboLength)];
auto ebo = &bg->elements[i * static_cast<std::size_t>(BgVertexEboLength)]; auto ebo = &bg.elements[i * static_cast<std::size_t>(BgVertexEboLength)];
setTileBufferObject( setTileBufferObject(
static_cast<unsigned>(i * BgVertexVboRows), static_cast<uint_t>(i * BgVertexVboRows),
static_cast<float>(x), static_cast<float>(x),
static_cast<float>(y), static_cast<float>(y),
0, 0,
@ -162,25 +172,25 @@ static void initBackgroundBufferObjects(glutils::BufferSet *bg) noexcept {
} }
} }
static void initSpritesBufferset(GLuint shader, glutils::BufferSet *bs) noexcept { static void initSpritesBufferset(GLuint shader, glutils::BufferSet &bs) noexcept {
// vao // vao
bs->vao = glutils::generateVertexArrayObject(); bs.vao = glutils::generateVertexArrayObject();
glBindVertexArray(bs->vao); glBindVertexArray(bs.vao);
// vbo & ebo // vbo & ebo
bs->vbo = glutils::generateBuffer(); bs.vbo = glutils::generateBuffer();
bs->ebo = glutils::generateBuffer(); bs.ebo = glutils::generateBuffer();
initSpriteBufferObjects(bs); initSpriteBufferObjects(bs);
glutils::sendVbo(*bs); glutils::sendVbo(bs);
glutils::sendEbo(*bs); glutils::sendEbo(bs);
// vbo layout // vbo layout
auto enabledAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vEnabled")); auto const enabledAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vEnabled"));
glEnableVertexAttribArray(enabledAttr); glEnableVertexAttribArray(enabledAttr);
glVertexAttribPointer(enabledAttr, 1, GL_FLOAT, GL_FALSE, SpriteVertexVboRowLength * sizeof(float), nullptr); glVertexAttribPointer(enabledAttr, 1, GL_FLOAT, GL_FALSE, SpriteVertexVboRowLength * sizeof(float), nullptr);
auto posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition")); auto const posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition"));
glEnableVertexAttribArray(posAttr); glEnableVertexAttribArray(posAttr);
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, SpriteVertexVboRowLength * sizeof(float), glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, SpriteVertexVboRowLength * sizeof(float),
reinterpret_cast<void*>(1 * sizeof(float))); reinterpret_cast<void*>(1 * sizeof(float)));
auto texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord")); auto const texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord"));
glEnableVertexAttribArray(texCoordAttr); glEnableVertexAttribArray(texCoordAttr);
glVertexAttribPointer(texCoordAttr, 2, GL_FLOAT, GL_FALSE, SpriteVertexVboRowLength * sizeof(float), glVertexAttribPointer(texCoordAttr, 2, GL_FLOAT, GL_FALSE, SpriteVertexVboRowLength * sizeof(float),
reinterpret_cast<void*>(3 * sizeof(float))); reinterpret_cast<void*>(3 * sizeof(float)));
@ -188,25 +198,25 @@ static void initSpritesBufferset(GLuint shader, glutils::BufferSet *bs) noexcept
static void initBackgroundBufferset( static void initBackgroundBufferset(
GLuint shader, GLuint shader,
glutils::BufferSet *bg) noexcept { glutils::BufferSet &bg) noexcept {
// vao // vao
bg->vao = glutils::generateVertexArrayObject(); bg.vao = glutils::generateVertexArrayObject();
glBindVertexArray(bg->vao); glBindVertexArray(bg.vao);
// vbo & ebo // vbo & ebo
bg->vbo = glutils::generateBuffer(); bg.vbo = glutils::generateBuffer();
bg->ebo = glutils::generateBuffer(); bg.ebo = glutils::generateBuffer();
initBackgroundBufferObjects(bg); initBackgroundBufferObjects(bg);
glutils::sendVbo(*bg); glutils::sendVbo(bg);
glutils::sendEbo(*bg); glutils::sendEbo(bg);
// vbo layout // vbo layout
auto posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition")); auto const posAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vPosition"));
glEnableVertexAttribArray(posAttr); glEnableVertexAttribArray(posAttr);
glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr); glVertexAttribPointer(posAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(float), nullptr);
auto texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord")); auto const texCoordAttr = static_cast<GLuint>(glGetAttribLocation(shader, "vTexCoord"));
glEnableVertexAttribArray(texCoordAttr); glEnableVertexAttribArray(texCoordAttr);
glVertexAttribPointer( glVertexAttribPointer(
texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(bg->vertices[0]), texCoordAttr, 2, GL_FLOAT, GL_FALSE, BgVertexVboRowLength * sizeof(bg.vertices[0]),
reinterpret_cast<void*>(2 * sizeof(bg->vertices[0]))); reinterpret_cast<void*>(2 * sizeof(bg.vertices[0])));
} }
static glutils::GLTexture loadTexture( static glutils::GLTexture loadTexture(
@ -228,42 +238,42 @@ static glutils::GLTexture loadTexture(
return tex; return tex;
} }
static void drawBackground(CBB *cbb) noexcept { static void drawBackground(CBB &cbb) noexcept {
glBindVertexArray(cbb->vao); glBindVertexArray(cbb.vao);
if (cbb->updated) { if (cbb.updated) {
cbb->updated = false; cbb.updated = false;
glutils::sendVbo(*cbb); glutils::sendVbo(cbb);
} }
glBindTexture(GL_TEXTURE_2D, cbb->tex); glBindTexture(GL_TEXTURE_2D, cbb.tex);
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(cbb->elements.size()), GL_UNSIGNED_INT, nullptr); glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(cbb.elements.size()), GL_UNSIGNED_INT, nullptr);
} }
static void drawBackgrounds( static void drawBackgrounds(
GlContext *gctx, GlContext &gctx,
ox::Size const&renderSz) noexcept { ox::Size const&renderSz) noexcept {
// load background shader and its uniforms // load background shader and its uniforms
glUseProgram(gctx->bgShader); glUseProgram(gctx.bgShader);
const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(gctx->bgShader, "vXScale")); const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(gctx.bgShader, "vXScale"));
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(gctx->bgShader, "vTileHeight")); const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(gctx.bgShader, "vTileHeight"));
const auto [wi, hi] = renderSz; const auto [wi, hi] = renderSz;
const auto wf = static_cast<float>(wi); const auto wf = static_cast<float>(wi);
const auto hf = static_cast<float>(hi); const auto hf = static_cast<float>(hi);
glUniform1f(uniformXScale, hf / wf); glUniform1f(uniformXScale, hf / wf);
for (const auto &bg : gctx->backgrounds) { for (const auto &bg : gctx.backgrounds) {
if (bg.enabled) { if (bg.enabled) {
auto &cbb = gctx->cbbs[bg.cbbIdx]; auto &cbb = gctx.cbbs[bg.cbbIdx];
const auto tileRows = cbb.tex.height / TileHeight; const auto tileRows = cbb.tex.height / TileHeight;
glUniform1f(uniformTileHeight, 1.0f / static_cast<float>(tileRows)); glUniform1f(uniformTileHeight, 1.0f / static_cast<float>(tileRows));
drawBackground(&cbb); drawBackground(cbb);
} }
} }
} }
static void drawSprites(GlContext *gctx, ox::Size const&renderSz) noexcept { static void drawSprites(GlContext &gctx, ox::Size const&renderSz) noexcept {
glUseProgram(gctx->spriteShader); glUseProgram(gctx.spriteShader);
auto &sb = gctx->spriteBlocks; auto &sb = gctx.spriteBlocks;
const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(gctx->bgShader, "vXScale")); const auto uniformXScale = static_cast<GLint>(glGetUniformLocation(gctx.bgShader, "vXScale"));
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(gctx->spriteShader, "vTileHeight")); const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(gctx.spriteShader, "vTileHeight"));
const auto [wi, hi] = renderSz; const auto [wi, hi] = renderSz;
const auto wf = static_cast<float>(wi); const auto wf = static_cast<float>(wi);
const auto hf = static_cast<float>(hi); const auto hf = static_cast<float>(hi);
@ -302,31 +312,31 @@ static void loadPalette(
glUniform4fv(uniformPalette, ColorCnt, palette.data()); glUniform4fv(uniformPalette, ColorCnt, palette.data());
} }
static void loadBgPalette(GlContext *gctx, Palette const&pal) noexcept { static void loadBgPalette(GlContext &gctx, Palette const&pal) noexcept {
loadPalette(gctx->bgShader, pal); loadPalette(gctx.bgShader, pal);
} }
static void loadSpritePalette(GlContext *gctx, Palette const&pal) noexcept { static void loadSpritePalette(GlContext &gctx, Palette const&pal) noexcept {
loadPalette(gctx->spriteShader, pal, true); loadPalette(gctx.spriteShader, pal, true);
} }
static void loadBgTexture( static void loadBgTexture(
GlContext *gctx, GlContext &gctx,
unsigned cbbIdx, uint_t cbbIdx,
const void *pixels, const void *pixels,
int w, int w,
int h) noexcept { int h) noexcept {
oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h); oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h);
gctx->cbbs[cbbIdx].tex = loadTexture(w, h, pixels); gctx.cbbs[cbbIdx].tex = loadTexture(w, h, pixels);
} }
static void loadSpriteTexture( static void loadSpriteTexture(
GlContext *gctx, GlContext &gctx,
const void *pixels, const void *pixels,
int w, int w,
int h) noexcept { int h) noexcept {
oxTracef("nostalgia::core::gfx::gl", "loadSpriteTexture: { w: {}, h: {} }", w, h); oxTracef("nostalgia::core::gfx::gl", "loadSpriteTexture: { w: {}, h: {} }", w, h);
gctx->spriteBlocks.tex = loadTexture(w, h, pixels); gctx.spriteBlocks.tex = loadTexture(w, h, pixels);
} }
} }
@ -340,22 +350,22 @@ ox::Error initGfx(
const auto bgFshad = ox::sfmt(renderer::bgfshadTmpl, glutils::GlslVersion); const auto bgFshad = ox::sfmt(renderer::bgfshadTmpl, glutils::GlslVersion);
const auto spriteVshad = ox::sfmt(renderer::spritevshadTmpl, glutils::GlslVersion); const auto spriteVshad = ox::sfmt(renderer::spritevshadTmpl, glutils::GlslVersion);
const auto spriteFshad = ox::sfmt(renderer::spritefshadTmpl, glutils::GlslVersion); const auto spriteFshad = ox::sfmt(renderer::spritefshadTmpl, glutils::GlslVersion);
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
oxReturnError(glutils::buildShaderProgram(bgVshad.c_str(), bgFshad.c_str()).moveTo(&gctx.bgShader)); oxReturnError(glutils::buildShaderProgram(bgVshad.c_str(), bgFshad.c_str()).moveTo(&gctx.bgShader));
oxReturnError( oxReturnError(
glutils::buildShaderProgram(spriteVshad.c_str(), spriteFshad.c_str()).moveTo(&gctx.spriteShader)); glutils::buildShaderProgram(spriteVshad.c_str(), spriteFshad.c_str()).moveTo(&gctx.spriteShader));
for (auto &bg : gctx.cbbs) { for (auto &bg : gctx.cbbs) {
initBackgroundBufferset(gctx.bgShader, &bg); initBackgroundBufferset(gctx.bgShader, bg);
} }
if (initParams.glInstallDrawer) { if (initParams.glInstallDrawer) {
turbine::gl::addDrawer(gctx.turbineCtx, &gctx.drawer); turbine::gl::addDrawer(gctx.turbineCtx, &gctx.drawer);
initSpritesBufferset(gctx.spriteShader, &gctx.spriteBlocks); initSpritesBufferset(gctx.spriteShader, gctx.spriteBlocks);
} }
return {}; return {};
} }
void shutdownGfx(Context &ctx) noexcept { void shutdownGfx(Context &ctx) noexcept {
auto &gctx = static_cast<GlContext&>(ctx); auto &gctx = glctx(ctx);
turbine::gl::removeDrawer(gctx.turbineCtx, &gctx.drawer); turbine::gl::removeDrawer(gctx.turbineCtx, &gctx.drawer);
} }
@ -366,9 +376,9 @@ struct TileSheetData {
}; };
static ox::Result<TileSheetData> loadTileSheet( static ox::Result<TileSheetData> loadTileSheet(
Context *ctx, CompactTileSheet const&tilesheet) noexcept { Context &ctx, CompactTileSheet const&tilesheet) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(ctx);
const unsigned bytesPerTile = tilesheet.bpp == 8 ? PixelsPerTile : PixelsPerTile / 2; const uint_t bytesPerTile = tilesheet.bpp == 8 ? PixelsPerTile : PixelsPerTile / 2;
const auto tiles = tilesheet.pixels.size() / bytesPerTile; const auto tiles = tilesheet.pixels.size() / bytesPerTile;
constexpr int width = 8; constexpr int width = 8;
const int height = 8 * static_cast<int>(tiles); const int height = 8 * static_cast<int>(tiles);
@ -385,22 +395,22 @@ static ox::Result<TileSheetData> loadTileSheet(
pixels[i * 2 + 1] = tilesheet.pixels[i] >> 4; pixels[i * 2 + 1] = tilesheet.pixels[i] >> 4;
} }
} }
renderer::loadSpriteTexture(&gctx, pixels.data(), width, height); renderer::loadSpriteTexture(gctx, pixels.data(), width, height);
return TileSheetData{std::move(pixels), width, height}; return TileSheetData{std::move(pixels), width, height};
} }
ox::Error loadBgTileSheet( ox::Error loadBgTileSheet(
Context *ctx, Context *ctx,
unsigned cbb, uint_t cbb,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept { ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
auto &kctx = gctx.turbineCtx.keelCtx; auto &kctx = gctx.turbineCtx.keelCtx;
oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr)); oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr));
oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette)); oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
oxRequire(tsd, loadTileSheet(ctx, *tilesheet)); oxRequire(tsd, loadTileSheet(*ctx, *tilesheet));
renderer::loadBgTexture(&gctx, cbb, tsd.pixels.data(), tsd.width, tsd.height); renderer::loadBgTexture(gctx, cbb, tsd.pixels.data(), tsd.width, tsd.height);
renderer::loadBgPalette(&gctx, *palette); renderer::loadBgPalette(gctx, *palette);
return {}; return {};
} }
@ -408,13 +418,13 @@ ox::Error loadSpriteTileSheet(
Context *ctx, Context *ctx,
ox::FileAddress const&tilesheetAddr, ox::FileAddress const&tilesheetAddr,
ox::FileAddress const&paletteAddr) noexcept { ox::FileAddress const&paletteAddr) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
auto &kctx = gctx.turbineCtx.keelCtx; auto &kctx = gctx.turbineCtx.keelCtx;
oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr)); oxRequire(tilesheet, readObj<CompactTileSheet>(&kctx, tilesheetAddr));
oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette)); oxRequire(palette, readObj<Palette>(&kctx, paletteAddr ? paletteAddr : tilesheet->defaultPalette));
oxRequire(tsd, loadTileSheet(ctx, *tilesheet)); oxRequire(tsd, loadTileSheet(*ctx, *tilesheet));
renderer::loadSpriteTexture(&gctx, tsd.pixels.data(), tsd.width, tsd.height); renderer::loadSpriteTexture(gctx, tsd.pixels.data(), tsd.width, tsd.height);
renderer::loadSpritePalette(&gctx, *palette); renderer::loadSpritePalette(gctx, *palette);
return {}; return {};
} }
@ -427,7 +437,7 @@ ox::Error initConsole(Context *ctx) noexcept {
} }
void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept { void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
const auto col = static_cast<unsigned>(column); const auto col = static_cast<uint_t>(column);
for (auto i = 0u; i < str.bytes(); ++i) { for (auto i = 0u; i < str.bytes(); ++i) {
setTile( setTile(
ctx, ctx,
@ -438,48 +448,48 @@ void puts(Context *ctx, int column, int row, ox::CRStringView str) noexcept {
} }
} }
void setBgCbb(Context *ctx, unsigned bgIdx, unsigned cbbIdx) noexcept { void setBgCbb(Context *ctx, uint_t bgIdx, uint_t cbbIdx) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
auto &bg = gctx.backgrounds[bgIdx]; auto &bg = gctx.backgrounds[bgIdx];
bg.cbbIdx = cbbIdx; bg.cbbIdx = cbbIdx;
} }
uint8_t bgStatus(Context *ctx) noexcept { uint8_t bgStatus(Context *ctx) noexcept {
const auto &gctx = static_cast<GlContext&>(*ctx); const auto &gctx = glctx(*ctx);
uint8_t out = 0; uint8_t out = 0;
for (unsigned i = 0; i < gctx.cbbs.size(); ++i) { for (uint_t i = 0; i < gctx.cbbs.size(); ++i) {
out |= static_cast<uint8_t>(static_cast<unsigned>(gctx.backgrounds[i].enabled) << i); out |= static_cast<uint8_t>(static_cast<uint_t>(gctx.backgrounds[i].enabled) << i);
} }
return out; return out;
} }
void setBgStatus(Context *ctx, uint32_t status) noexcept { void setBgStatus(Context *ctx, uint32_t status) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
for (unsigned i = 0; i < gctx.cbbs.size(); ++i) { for (uint_t i = 0; i < gctx.cbbs.size(); ++i) {
gctx.backgrounds[i].enabled = (status >> i) & 1; gctx.backgrounds[i].enabled = (status >> i) & 1;
} }
} }
bool bgStatus(Context *ctx, unsigned bg) noexcept { bool bgStatus(Context *ctx, uint_t bg) noexcept {
const auto &gctx = static_cast<GlContext&>(*ctx); const auto &gctx = glctx(*ctx);
return gctx.backgrounds[bg].enabled; return gctx.backgrounds[bg].enabled;
} }
void setBgStatus(Context *ctx, unsigned bg, bool status) noexcept { void setBgStatus(Context *ctx, uint_t bg, bool status) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
gctx.backgrounds[bg].enabled = status; gctx.backgrounds[bg].enabled = status;
} }
void clearTileLayer(Context *ctx, unsigned bgIdx) noexcept { void clearTileLayer(Context *ctx, uint_t bgIdx) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
auto &bg = gctx.cbbs[static_cast<std::size_t>(bgIdx)]; auto &bg = gctx.cbbs[static_cast<std::size_t>(bgIdx)];
initBackgroundBufferObjects(&bg); initBackgroundBufferObjects(bg);
bg.updated = true; bg.updated = true;
} }
void hideSprite(Context *ctx, unsigned idx) noexcept { void hideSprite(Context *ctx, uint_t idx) noexcept {
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
auto vbo = &gctx.spriteBlocks.vertices[idx * renderer::SpriteVertexVboLength]; auto vbo = &gctx.spriteBlocks.vertices[idx * renderer::SpriteVertexVboLength];
auto ebo = &gctx.spriteBlocks.elements[idx * renderer::SpriteVertexEboLength]; auto ebo = &gctx.spriteBlocks.elements[idx * renderer::SpriteVertexEboLength];
renderer::setSpriteBufferObject(idx * renderer::SpriteVertexVboRows, 0, renderer::setSpriteBufferObject(idx * renderer::SpriteVertexVboRows, 0,
@ -489,17 +499,17 @@ void hideSprite(Context *ctx, unsigned idx) noexcept {
void setSprite( void setSprite(
Context *ctx, Context *ctx,
unsigned idx, uint_t idx,
int x, int x,
int y, int y,
unsigned tileIdx, uint_t tileIdx,
unsigned spriteShape, uint_t spriteShape,
unsigned spriteSize, uint_t spriteSize,
unsigned flipX) noexcept { uint_t flipX) noexcept {
//oxTracef("nostalgia::core::gfx::gl", "setSprite(ctx, {}, {}, {}, {}, {}, {}, {})", //oxTracef("nostalgia::core::gfx::gl", "setSprite(ctx, {}, {}, {}, {}, {}, {}, {})",
// idx, x, y, tileIdx, spriteShape, spriteSize, flipX); // idx, x, y, tileIdx, spriteShape, spriteSize, flipX);
// Tonc Table 8.4 // Tonc Table 8.4
static constexpr ox::Array<ox::Vec<unsigned>, 12> dimensions{ static constexpr ox::Array<ox::Vec<uint_t>, 12> dimensions{
// col 0 // col 0
{1, 1}, // 0, 0 {1, 1}, // 0, 0
{2, 2}, // 0, 1 {2, 2}, // 0, 1
@ -519,7 +529,7 @@ void setSprite(
const auto dim = dimensions[(spriteShape << 2) | spriteSize]; const auto dim = dimensions[(spriteShape << 2) | spriteSize];
const auto uX = static_cast<int>(x) % 255; const auto uX = static_cast<int>(x) % 255;
const auto uY = static_cast<int>(y + 8) % 255 - 8; const auto uY = static_cast<int>(y + 8) % 255 - 8;
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
auto i = 0u; auto i = 0u;
const auto set = [&](int xIt, int yIt) { const auto set = [&](int xIt, int yIt) {
const auto fX = static_cast<float>(uX + xIt * 8) / 8; const auto fX = static_cast<float>(uX + xIt * 8) / 8;
@ -549,7 +559,7 @@ void setSprite(
void setTile( void setTile(
Context *ctx, Context *ctx,
unsigned bgIdx, uint_t bgIdx,
int column, int column,
int row, int row,
uint8_t tile) noexcept { uint8_t tile) noexcept {
@ -557,16 +567,16 @@ void setTile(
"nostalgia::core::gfx::setTile", "nostalgia::core::gfx::setTile",
"bgIdx: {}, column: {}, row: {}, tile: {}", "bgIdx: {}, column: {}, row: {}, tile: {}",
bgIdx, column, row, tile); bgIdx, column, row, tile);
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(*ctx);
const auto z = static_cast<unsigned>(bgIdx); const auto z = static_cast<uint_t>(bgIdx);
const auto y = static_cast<unsigned>(row); const auto y = static_cast<uint_t>(row);
const auto x = static_cast<unsigned>(column); const auto x = static_cast<uint_t>(column);
const auto i = renderer::bgVertexRow(x, y); const auto i = renderer::bgVertexRow(x, y);
auto &bg = gctx.cbbs[z]; auto &bg = gctx.cbbs[z];
auto vbo = &bg.vertices[i * renderer::BgVertexVboLength]; auto vbo = &bg.vertices[i * renderer::BgVertexVboLength];
auto ebo = &bg.elements[i * renderer::BgVertexEboLength]; auto ebo = &bg.elements[i * renderer::BgVertexEboLength];
renderer::setTileBufferObject( renderer::setTileBufferObject(
static_cast<unsigned>(i * renderer::BgVertexVboRows), static_cast<uint_t>(i * renderer::BgVertexVboRows),
static_cast<float>(x), static_cast<float>(x),
static_cast<float>(y), static_cast<float>(y),
tile, tile,
@ -577,13 +587,13 @@ void setTile(
namespace gl { namespace gl {
void drawMainView(core::Context *ctx, ox::Size const&renderSz) noexcept { void drawMainView(core::Context &ctx, ox::Size const&renderSz) noexcept {
glViewport(0, 0, renderSz.width, renderSz.height); glViewport(0, 0, renderSz.width, renderSz.height);
glutils::clearScreen(); glutils::clearScreen();
auto &gctx = static_cast<GlContext&>(*ctx); auto &gctx = glctx(ctx);
renderer::drawBackgrounds(&gctx, renderSz); renderer::drawBackgrounds(gctx, renderSz);
if (gctx.spriteBlocks.tex) { if (gctx.spriteBlocks.tex) {
renderer::drawSprites(&gctx, renderSz); renderer::drawSprites(gctx, renderSz);
} }
} }

View File

@ -7,6 +7,7 @@
#include <studio/studio.hpp> #include <studio/studio.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core { namespace nostalgia::core {

View File

@ -6,7 +6,9 @@
#include <studio/studio.hpp> #include <studio/studio.hpp>
#include <nostalgia/core/color.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/palette.hpp>
namespace nostalgia::core { namespace nostalgia::core {
@ -105,4 +107,4 @@ class MoveColorCommand: public studio::UndoCommand {
void moveColor(int idx, int offset) noexcept; void moveColor(int idx, int offset) noexcept;
}; };
} }

View File

@ -12,6 +12,7 @@
#include <studio/studio.hpp> #include <studio/studio.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/tilesheet.hpp>
namespace nostalgia::core { namespace nostalgia::core {

View File

@ -8,6 +8,7 @@
#include <studio/studio.hpp> #include <studio/studio.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/tilesheet.hpp>
namespace nostalgia::core { namespace nostalgia::core {

View File

@ -9,6 +9,7 @@
#include <glutils/glutils.hpp> #include <glutils/glutils.hpp>
#include <studio/studio.hpp> #include <studio/studio.hpp>
#include <nostalgia/core/color.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
namespace nostalgia::core { namespace nostalgia::core {

View File

@ -58,9 +58,10 @@ struct TileDoc {
}; };
oxModelBegin(TileDoc) oxModelBegin(TileDoc)
oxModelFieldRename(subsheet_id, subsheetId); oxModelFieldRename(subsheet_id, subsheetId)
oxModelFieldRename(subsheet_path, subsheetPath); oxModelFieldRename(subsheet_path, subsheetPath)
oxModelField(type); oxModelField(type)
oxModelFieldRename(layer_attachments, layerAttachments)
oxModelEnd() oxModelEnd()
struct SceneDoc { struct SceneDoc {

View File

@ -23,7 +23,7 @@ void SceneEditorView::draw(int width, int height) noexcept {
glutils::resizeInitFrameBuffer(&m_frameBuffer, width, height); glutils::resizeInitFrameBuffer(&m_frameBuffer, width, height);
} }
const glutils::FrameBufferBind frameBufferBind(m_frameBuffer); const glutils::FrameBufferBind frameBufferBind(m_frameBuffer);
core::gl::drawMainView(m_cctx.get(), {width, height}); core::gl::drawMainView(*m_cctx, {width, height});
} }
const glutils::FrameBuffer &SceneEditorView::framebuffer() const noexcept { const glutils::FrameBuffer &SceneEditorView::framebuffer() const noexcept {