[nostalgia] Add basic support for subsheets

This commit is contained in:
2022-02-26 22:48:18 -06:00
parent 329ecb3266
commit e8a046c2dc
20 changed files with 630 additions and 238 deletions
+12 -12
View File
@@ -29,14 +29,14 @@ void TileSheetPixels::draw(bool update, const geo::Vec2 &scroll) noexcept {
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(m_bufferSet.elements.size()), GL_UNSIGNED_INT, nullptr);
}
void TileSheetPixels::initBufferSet(const geo::Vec2 &paneSize, const TileSheet &img, const Palette &pal) noexcept {
void TileSheetPixels::initBufferSet(const geo::Vec2 &paneSize, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal) noexcept {
// vao
m_bufferSet.vao = glutils::generateVertexArrayObject();
glBindVertexArray(m_bufferSet.vao);
// vbo & ebo
m_bufferSet.vbo = glutils::generateBuffer();
m_bufferSet.ebo = glutils::generateBuffer();
setBufferObjects(paneSize, img, pal, &m_bufferSet);
setBufferObjects(paneSize, img, subSheet, pal, &m_bufferSet);
glutils::sendVbo(m_bufferSet);
glutils::sendEbo(m_bufferSet);
// vbo layout
@@ -78,10 +78,10 @@ void TileSheetPixels::setPixelBufferObject(const geo::Vec2 &paneSize, unsigned v
memcpy(ebo, elms, sizeof(elms));
}
void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const Palette &pal, glutils::BufferSet *bg) noexcept {
const auto setPixel = [this, paneSize, bg, img, pal](std::size_t i, uint8_t p) {
void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal, glutils::BufferSet *bg) noexcept {
const auto setPixel = [this, paneSize, bg, subSheet, pal](std::size_t i, uint8_t p) {
const auto color = pal.colors[p];
const auto pt = idxToPt(static_cast<int>(i), img.columns());
const auto pt = idxToPt(static_cast<int>(i), subSheet.columns);
const auto fx = static_cast<float>(pt.x);
const auto fy = static_cast<float>(pt.y);
const auto vbo = &bg->vertices[i * VertexVboLength];
@@ -89,22 +89,22 @@ void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileShee
setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo);
};
// set buffer lengths
const auto width = img.columns() * TileWidth;
const auto height = img.rows() * TileHeight;
const auto width = subSheet.columns * TileWidth;
const auto height = subSheet.rows * TileHeight;
const auto tiles = static_cast<unsigned>(width * height);
m_bufferSet.vertices.resize(tiles * VertexVboLength);
m_bufferSet.elements.resize(tiles * VertexEboLength);
// set pixels
if (img.bpp == 4) {
for (std::size_t i = 0; i < img.pixels.size(); ++i) {
const auto colorIdx1 = img.pixels[i] & 0xF;
const auto colorIdx2 = img.pixels[i] >> 4;
for (std::size_t i = 0; i < subSheet.pixels.size(); ++i) {
const auto colorIdx1 = subSheet.pixels[i] & 0xF;
const auto colorIdx2 = subSheet.pixels[i] >> 4;
setPixel(i * 2 + 0, colorIdx1);
setPixel(i * 2 + 1, colorIdx2);
}
} else {
for (std::size_t i = 0; i < img.pixels.size(); ++i) {
const auto p = img.pixels[i];
for (std::size_t i = 0; i < subSheet.pixels.size(); ++i) {
const auto p = subSheet.pixels[i];
setPixel(i, p);
}
}