[nostalgia/core/userland] Make palettes swappable in GL renderer

This commit is contained in:
Gary Talent 2022-12-11 08:38:38 -06:00
parent 461e3d61ef
commit 79d255b63f
3 changed files with 29 additions and 6 deletions

View File

@ -38,16 +38,18 @@ ox::Error loadBgTileSheet(Context *ctx,
if (bytesPerTile == 64) { // 8 BPP
pixels.resize(tilesheet->pixels.size());
for (std::size_t i = 0; i < tilesheet->pixels.size(); ++i) {
pixels[i] = toColor32(palette->colors[tilesheet->pixels[i]]);
pixels[i] = tilesheet->pixels[i];
}
} else { // 4 BPP
pixels.resize(tilesheet->pixels.size() * 2);
for (std::size_t i = 0; i < tilesheet->pixels.size(); ++i) {
pixels[i * 2 + 0] = toColor32(palette->colors[tilesheet->pixels[i] & 0xF]);
pixels[i * 2 + 1] = toColor32(palette->colors[tilesheet->pixels[i] >> 4]);
pixels[i * 2 + 0] = tilesheet->pixels[i] & 0xF;
pixels[i * 2 + 1] = tilesheet->pixels[i] >> 4;
}
}
renderer::loadBgTexture(ctx->rendererData<void>(), cbb, pixels.data(), width, height);
const auto rd = ctx->rendererData<void>();
renderer::loadBgTexture(rd, cbb, pixels.data(), width, height);
renderer::loadBgPalette(rd, *palette);
return OxError(0);
}

View File

@ -8,12 +8,18 @@
#include <nostalgia/core/context.hpp>
namespace {
class Palette;
}
namespace nostalgia::core::renderer {
ox::Error init(Context *ctx, void **rendererData) noexcept;
void shutdown(Context *ctx, void *rendererData) noexcept;
void loadBgPalette(void *rendererData, const Palette &pal) noexcept;
void loadBgTexture(void *rendererData, unsigned cbb, void *pixels, int w, int h) noexcept;
}

View File

@ -47,6 +47,8 @@ struct GlImplData {
uint64_t draws = 0;
ox::Array<CBB, 4> cbbs;
ox::Array<Background, 4> backgrounds;
static constexpr std::size_t ColorCnt = 256;
ox::Array<GLfloat, ColorCnt * 3> palette{};
};
constexpr const GLchar *bgvshad = R"(
@ -65,9 +67,11 @@ constexpr const GLchar *bgfshad = R"(
out vec4 outColor;
in vec2 fTexCoord;
uniform sampler2D image;
uniform vec3 fPalette[256];
void main() {
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
outColor = texture(image, fTexCoord);
int idx = int(texture(image, fTexCoord).rgb.r * 256);
outColor = vec4(fPalette[idx], 1.0);
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
})";
[[nodiscard]]
@ -175,7 +179,9 @@ static void drawBackground(CBB *cbb) noexcept {
static void drawBackgrounds(GlImplData *id) noexcept {
// load background shader and its uniforms
glUseProgram(id->bgShader);
const auto uniformPalette = static_cast<GLint>(glGetUniformLocation(id->bgShader, "fPalette"));
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(id->bgShader, "vTileHeight"));
glUniform3fv(uniformPalette, GlImplData::ColorCnt, id->palette.data());
for (auto &bg : id->backgrounds) {
if (bg.enabled) {
auto &cbb = id->cbbs[bg.cbbIdx];
@ -205,6 +211,15 @@ void shutdown(Context*, void *rendererData) noexcept {
ox::safeDelete(id);
}
void loadBgPalette(void *rendererData, const Palette &pal) noexcept {
const auto id = static_cast<GlImplData*>(rendererData);
for (auto i = 0u; const auto c : pal.colors) {
id->palette[i++] = redf(c);
id->palette[i++] = greenf(c);
id->palette[i++] = bluef(c);
}
}
void loadBgTexture(void *rendererData, unsigned cbbIdx, void *pixels, int w, int h) noexcept {
oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h);
const auto id = static_cast<GlImplData*>(rendererData);