[nostalgia/core/userland] Fix Tile row skipping and move some non-SDL code into userland
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
add_library(
|
||||
NostalgiaCore-Userspace OBJECT
|
||||
gfx.cpp
|
||||
gfx_opengl.cpp
|
||||
glutils.cpp
|
||||
media.cpp
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2016 - 2021 gary@drinkingtea.net
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include <ox/claw/claw.hpp>
|
||||
#include <nostalgia/core/gfx.hpp>
|
||||
#include <nostalgia/core/gfx.hpp>
|
||||
|
||||
#include "gfx.hpp"
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
static ox::Result<ox::Vector<char>> readFile(Context *ctx, const ox::FileAddress &file) {
|
||||
oxRequire(stat, ctx->rom->stat(file));
|
||||
ox::Vector<char> buff(stat.size);
|
||||
oxReturnError(ctx->rom->read(file, buff.data(), buff.size()));
|
||||
return buff;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ox::Result<T> readObj(Context *ctx, const ox::FileAddress &file) {
|
||||
oxRequire(buff, readFile(ctx, file));
|
||||
T t;
|
||||
oxReturnError(ox::readClaw(buff.data(), buff.size(), &t));
|
||||
return t;
|
||||
}
|
||||
|
||||
ox::Error initConsole(Context *ctx) {
|
||||
constexpr auto TilesheetAddr = "/TileSheets/Charset.ng";
|
||||
return loadBgTileSheet(ctx, 0, TilesheetAddr);
|
||||
}
|
||||
|
||||
ox::Error loadSpriteTileSheet(Context*,
|
||||
int,
|
||||
ox::FileAddress,
|
||||
ox::FileAddress) {
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
ox::Error loadBgTileSheet(Context *ctx,
|
||||
int section,
|
||||
ox::FileAddress tilesheetPath,
|
||||
ox::FileAddress palettePath) {
|
||||
oxRequire(tilesheet, readObj<NostalgiaGraphic>(ctx, tilesheetPath));
|
||||
if (!palettePath) {
|
||||
palettePath = tilesheet.defaultPalette;
|
||||
}
|
||||
oxRequire(palette, readObj<NostalgiaPalette>(ctx, palettePath));
|
||||
const unsigned bytesPerTile = tilesheet.bpp == 8 ? 64 : 32;
|
||||
const auto tiles = tilesheet.pixels.size() / bytesPerTile;
|
||||
constexpr int width = 8;
|
||||
const int height = 8 * tiles;
|
||||
ox::Vector<uint32_t> pixels;
|
||||
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]]);
|
||||
}
|
||||
} 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]);
|
||||
}
|
||||
}
|
||||
return renderer::loadBgTexture(ctx, section, pixels.data(), width, height);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,15 +22,16 @@ using TileMap = std::array<std::array<int, 128>, 128>;
|
||||
|
||||
namespace renderer {
|
||||
|
||||
constexpr auto TileRows = 16;
|
||||
constexpr auto TileColumns = 16;
|
||||
constexpr auto TileRows = 128;
|
||||
constexpr auto TileColumns = 128;
|
||||
constexpr auto TileCount = TileRows * TileColumns;
|
||||
constexpr auto BgVertexVboRows = 4;
|
||||
constexpr auto BgVertexVboLength = 16;
|
||||
constexpr auto BgVertexEboLength = 6;
|
||||
|
||||
struct BackgroundBufferset: public Bufferset {
|
||||
std::array<float, TileCount * BgVertexVboLength * 4> bgVertices;
|
||||
std::array<GLuint, TileCount * BgVertexEboLength * 4> bgEbos;
|
||||
std::array<float, TileCount * BgVertexVboLength> bgVertices;
|
||||
std::array<GLuint, TileCount * BgVertexEboLength> bgEbos;
|
||||
};
|
||||
|
||||
struct GlImplData {
|
||||
@@ -64,16 +65,17 @@ constexpr const GLchar *bgfshad = R"(
|
||||
void initTileBufferObjects(unsigned vi, float x, float y, float *vbo, GLuint *ebo) {
|
||||
// don't worry, this gets optimized to something much more ideal
|
||||
constexpr float xmod = 0.06f;
|
||||
constexpr float ymod = 0.1f;
|
||||
x *= xmod;
|
||||
y *= 0.1f;
|
||||
const float vertices[BgVertexVboLength] = {
|
||||
y *= ymod;
|
||||
const float vertices[] = {
|
||||
x, y, 0, 0.04, // bottom left
|
||||
x + xmod, y, 1, 0.04, // bottom right
|
||||
x + xmod, y + 0.1f, 1, 0, // top right
|
||||
x, y + 0.1f, 0, 0, // top left
|
||||
x + xmod, y + ymod, 1, 0.02, // top right
|
||||
x, y + ymod, 0, 0.02, // top left
|
||||
};
|
||||
memcpy(vbo, vertices, sizeof(vertices));
|
||||
const GLuint elms[BgVertexEboLength] = {
|
||||
const GLuint elms[] = {
|
||||
vi + 0, vi + 1, vi + 2,
|
||||
vi + 2, vi + 3, vi + 0,
|
||||
};
|
||||
@@ -84,10 +86,9 @@ void initBackgroundBufferObjects(GLuint shader, BackgroundBufferset *bs) {
|
||||
auto i = 0u;
|
||||
for (auto x = 0u; x < TileColumns; ++x) {
|
||||
for (auto y = 0u; y < TileRows; ++y) {
|
||||
const auto vi = i * BgVertexVboLength;
|
||||
auto vbo = &bs->bgVertices[vi];
|
||||
auto vbo = &bs->bgVertices[i * BgVertexVboLength];
|
||||
auto ebo = &bs->bgEbos[i * BgVertexEboLength];
|
||||
initTileBufferObjects(vi, x, y, vbo, ebo);
|
||||
initTileBufferObjects(i * BgVertexVboRows, x, y, vbo, ebo);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user