diff --git a/src/nostalgia/modules/core/include/nostalgia/core/initparams.hpp b/src/nostalgia/modules/core/include/nostalgia/core/initparams.hpp index a43ea28c..70138553 100644 --- a/src/nostalgia/modules/core/include/nostalgia/core/initparams.hpp +++ b/src/nostalgia/modules/core/include/nostalgia/core/initparams.hpp @@ -8,6 +8,8 @@ namespace nostalgia::core { struct InitParams { bool glInstallDrawer = true; + uint_t glSpriteCount = 128; + uint_t glBlocksPerSprite = 64; }; } \ No newline at end of file diff --git a/src/nostalgia/modules/core/src/opengl/context.cpp b/src/nostalgia/modules/core/src/opengl/context.cpp index f9916ed3..ecf20530 100644 --- a/src/nostalgia/modules/core/src/opengl/context.cpp +++ b/src/nostalgia/modules/core/src/opengl/context.cpp @@ -11,9 +11,12 @@ void ContextDeleter::operator()(Context *p) noexcept { ox::safeDelete(p); } -Context::Context(turbine::Context &tctx) noexcept: +Context::Context(turbine::Context &tctx, InitParams const¶ms) noexcept: turbineCtx(tctx), - drawer(*this) { + spriteBlocks(params.glSpriteCount, params.glBlocksPerSprite), + drawer(*this), + spriteCount(params.glSpriteCount), + blocksPerSprite(params.glBlocksPerSprite) { } Context::~Context() noexcept { @@ -21,7 +24,7 @@ Context::~Context() noexcept { } ox::Result init(turbine::Context &tctx, InitParams const¶ms) noexcept { - auto ctx = ox::make_unique(tctx); + auto ctx = ox::make_unique(tctx, params); oxReturnError(initGfx(*ctx, params)); return ContextUPtr(ctx.release()); } diff --git a/src/nostalgia/modules/core/src/opengl/context.hpp b/src/nostalgia/modules/core/src/opengl/context.hpp index 4a7163e1..f100a23a 100644 --- a/src/nostalgia/modules/core/src/opengl/context.hpp +++ b/src/nostalgia/modules/core/src/opengl/context.hpp @@ -26,7 +26,9 @@ class Context { ox::Array spriteStates; ox::Array backgrounds; renderer::Drawer drawer; - explicit Context(turbine::Context &tctx) noexcept; + uint_t spriteCount = 0; + uint_t blocksPerSprite = 0; + explicit Context(turbine::Context &tctx, InitParams const¶ms) noexcept; Context(Context const&) = delete; Context(Context&&) = delete; Context &operator=(Context const&) = delete; diff --git a/src/nostalgia/modules/core/src/opengl/gfx.cpp b/src/nostalgia/modules/core/src/opengl/gfx.cpp index dfd44156..cffb3469 100644 --- a/src/nostalgia/modules/core/src/opengl/gfx.cpp +++ b/src/nostalgia/modules/core/src/opengl/gfx.cpp @@ -171,15 +171,15 @@ static void setTileBufferObject( memcpy(ebo, elms.data(), sizeof(elms)); } -static void initSpriteBufferObjects(glutils::BufferSet &bs) noexcept { - for (auto i = 0u; i < SpriteCount; ++i) { +static void initSpriteBufferObjects(Context &ctx, glutils::BufferSet &bs) noexcept { + for (auto i = 0u; i < ctx.spriteCount; ++i) { auto vbo = &bs.vertices[i * static_cast(SpriteVertexVboLength)]; auto ebo = &bs.elements[i * static_cast(SpriteVertexEboLength)]; - setSpriteBufferObject(i * SpriteVertexVboRows * BlocksPerSprite, 0, 0, 0, 0, false, 0, vbo, ebo); + setSpriteBufferObject(i * static_cast(SpriteVertexVboRows) * ctx.blocksPerSprite, 0, 0, 0, 0, false, 0, vbo, ebo); } } -static void initBackgroundBufferObjects(glutils::BufferSet &bs, float priority) noexcept { +static void initBackgroundBufferObjects(glutils::BufferSet &bs) noexcept { for (auto x = 0u; x < TileColumns; ++x) { for (auto y = 0u; y < TileRows; ++y) { const auto i = bgVertexRow(x, y); @@ -190,7 +190,7 @@ static void initBackgroundBufferObjects(glutils::BufferSet &bs, float priority) static_cast(x), static_cast(y), 0, - priority, + 0, vbo, ebo); } @@ -206,7 +206,7 @@ static void initSpritesBufferset(Context &ctx) noexcept { // vbo & ebo bs.vbo = glutils::generateBuffer(); bs.ebo = glutils::generateBuffer(); - initSpriteBufferObjects(bs); + initSpriteBufferObjects(ctx, bs); glutils::sendVbo(bs); glutils::sendEbo(bs); // vbo layout @@ -228,15 +228,14 @@ static void initSpritesBufferset(Context &ctx) noexcept { static void initBackgroundBufferset( GLuint shader, - glutils::BufferSet &bs, - float priority) noexcept { + glutils::BufferSet &bs) noexcept { // vao bs.vao = glutils::generateVertexArrayObject(); glBindVertexArray(bs.vao); // vbo & ebo bs.vbo = glutils::generateBuffer(); bs.ebo = glutils::generateBuffer(); - initBackgroundBufferObjects(bs, priority); + initBackgroundBufferObjects(bs); glutils::sendVbo(bs); glutils::sendEbo(bs); // vbo layout @@ -412,8 +411,8 @@ static void setSprite( auto const uY = static_cast(s.y + 8) % 255 - 8; oxAssert(1 < ctx.spriteBlocks.vertices.size(), "vbo overflow"); oxAssert(1 < ctx.spriteBlocks.elements.size(), "ebo overflow"); - constexpr auto spriteVboSz = renderer::BlocksPerSprite * renderer::SpriteVertexVboLength; - constexpr auto spriteEboSz = renderer::BlocksPerSprite * renderer::SpriteVertexEboLength; + const auto spriteVboSz = ctx.blocksPerSprite * renderer::SpriteVertexVboLength; + const auto spriteEboSz = ctx.blocksPerSprite * renderer::SpriteVertexEboLength; auto const vboBase = spriteVboSz * idx; auto const eboBase = spriteEboSz * idx; auto i = 0u; @@ -452,7 +451,7 @@ static void setSprite( } } // clear remaining blocks in the sprite - for (; i < BlocksPerSprite; ++i) { + for (; i < ctx.blocksPerSprite; ++i) { set(0, 0, false); } ctx.spriteBlocks.updated = true; @@ -471,7 +470,7 @@ ox::Error initGfx( oxReturnError( glutils::buildShaderProgram(spriteVshad, spriteFshad).moveTo(ctx.spriteShader)); for (auto &cbb : ctx.cbbs) { - initBackgroundBufferset(ctx.bgShader, cbb, 0); + initBackgroundBufferset(ctx.bgShader, cbb); } renderer::initSpritesBufferset(ctx); if (initParams.glInstallDrawer) { @@ -606,7 +605,7 @@ void setBgStatus(Context &ctx, uint_t bg, bool status) noexcept { void clearTileLayer(Context &ctx, uint_t bgIdx) noexcept { auto &cbb = ctx.cbbs[static_cast(bgIdx)]; - initBackgroundBufferObjects(cbb, 0); + initBackgroundBufferObjects(cbb); cbb.updated = true; auto &bg = ctx.backgrounds[static_cast(bgIdx)]; bg.priority = 0; diff --git a/src/nostalgia/modules/core/src/opengl/gfx.hpp b/src/nostalgia/modules/core/src/opengl/gfx.hpp index 48a4c6d4..03649ac1 100644 --- a/src/nostalgia/modules/core/src/opengl/gfx.hpp +++ b/src/nostalgia/modules/core/src/opengl/gfx.hpp @@ -17,7 +17,6 @@ namespace nostalgia::core::renderer { constexpr uint64_t TileRows = 128; constexpr uint64_t TileColumns = 128; constexpr uint64_t TileCount = TileRows * TileColumns; -constexpr uint64_t SpriteCount = 128; constexpr uint64_t BgVertexVboRows = 4; constexpr uint64_t BgVertexVboRowLength = 6; constexpr uint64_t BgVertexVboLength = BgVertexVboRows * BgVertexVboRowLength; @@ -26,7 +25,6 @@ constexpr uint64_t SpriteVertexVboRows = 4; constexpr uint64_t SpriteVertexVboRowLength = 6; constexpr uint64_t SpriteVertexVboLength = SpriteVertexVboRows * SpriteVertexVboRowLength; constexpr uint64_t SpriteVertexEboLength = 6; -constexpr uint64_t BlocksPerSprite = 64; struct CBB: public glutils::BufferSet { bool updated = false; @@ -38,9 +36,9 @@ struct CBB: public glutils::BufferSet { struct SpriteBlockset: public glutils::BufferSet { bool updated = false; - constexpr SpriteBlockset() noexcept { - vertices.resize(SpriteCount * SpriteVertexVboLength * BlocksPerSprite); - elements.resize(SpriteCount * SpriteVertexEboLength * BlocksPerSprite); + constexpr SpriteBlockset(uint64_t spriteCount, uint64_t blocksPerSprite) noexcept { + vertices.resize(spriteCount * SpriteVertexVboLength * blocksPerSprite); + elements.resize(spriteCount * SpriteVertexEboLength * blocksPerSprite); } };