[nostalgia/core/studio] Fix subsheet editor to allocate correct number of pixels

This commit is contained in:
Gary Talent 2022-03-05 15:51:39 -06:00
parent 2f7c62f2ef
commit 20a61de9fd
7 changed files with 33 additions and 19 deletions

View File

@ -198,6 +198,19 @@ struct TileSheet {
setPixel(bpp, idx, palIdx);
}
constexpr auto setPixelCount(int8_t bpp, std::size_t cnt) noexcept {
switch (bpp) {
case 4:
pixels.resize(cnt * 2);
return OxError(0);
case 8:
pixels.resize(cnt);
return OxError(0);
default:
return OxError(1, "Invalid bpp used for TileSheet::SubSheet::setPixelCount");
}
}
/**
* Gets a count of the pixels in this sheet, and not that of its children.
* @param bpp bits per pixel, need for knowing how to count the pixels

View File

@ -102,7 +102,6 @@ void TileSheetEditorImGui::draw(core::Context*) noexcept {
}
ImGui::EndChild();
m_subsheetEditor.draw();
ImGui::ShowDemoWindow();
}
void TileSheetEditorImGui::drawSubsheetSelector(TileSheet::SubSheet *subsheet, TileSheet::SubSheetIdx *path) noexcept {
@ -241,10 +240,11 @@ ox::Error TileSheetEditorImGui::markUnsavedChanges(int) noexcept {
void TileSheetEditorImGui::SubSheetEditor::draw() noexcept {
constexpr auto modalFlags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
constexpr auto popupName = "Edit SubSheet";
if (m_show) {
ImGui::OpenPopup(popupName);
ImGui::SetNextWindowSize(ImVec2(235, 125));
if (!m_show) {
return;
}
ImGui::OpenPopup(popupName);
ImGui::SetNextWindowSize(ImVec2(235, 125));
if (ImGui::BeginPopupModal(popupName, &m_show, modalFlags)) {
ImGui::InputText("Name", m_name.data(), m_name.cap());
if (m_cols != -1) {

View File

@ -206,7 +206,7 @@ struct UpdateSubSheetCommand: public studio::UndoCommand {
sheet.name = m_newName;
sheet.columns = m_newCols;
sheet.rows = m_newRows;
sheet.pixels.resize(static_cast<std::size_t>(m_newCols * m_newRows * PixelsPerTile));
oxLogError(sheet.setPixelCount(m_img->bpp, static_cast<std::size_t>(m_newCols * m_newRows)));
}
void undo() noexcept final {

View File

@ -36,7 +36,7 @@ void TileSheetGrid::initBufferSet(const geo::Vec2 &paneSize, const TileSheet::Su
glBindVertexArray(m_bufferSet.vao);
// vbo
m_bufferSet.vbo = glutils::generateBuffer();
setBufferObjects(paneSize, subsheet, &m_bufferSet);
setBufferObjects(paneSize, subsheet);
glutils::sendVbo(m_bufferSet);
// vbo layout
const auto pt1Attr = static_cast<GLuint>(glGetAttribLocation(m_shader, "vPt1"));
@ -62,10 +62,10 @@ void TileSheetGrid::setBufferObject(geo::Point pt1, geo::Point pt2, Color32 c, f
memcpy(vbo, vertices, sizeof(vertices));
}
void TileSheetGrid::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet, glutils::BufferSet *bg) noexcept {
void TileSheetGrid::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet) noexcept {
const auto pixSize = pixelSize(paneSize);
const auto set = [&bg, &pixSize](unsigned i, geo::Point pt1, geo::Point pt2, Color32 c) {
const auto vbo = &bg->vertices[i * VertexVboLength];
const auto set = [&](unsigned i, geo::Point pt1, geo::Point pt2, Color32 c) {
const auto vbo = &m_bufferSet.vertices[i * VertexVboLength];
setBufferObject(pt1, pt2, c, vbo, pixSize);
};
// set buffer length

View File

@ -74,7 +74,7 @@ class TileSheetGrid {
private:
static void setBufferObject(geo::Point pt1, geo::Point pt2, Color32 c, float *vbo, const geo::Vec2 &pixSize) noexcept;
void setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet, glutils::BufferSet *bg) noexcept;
void setBufferObjects(const geo::Vec2 &paneSize, const TileSheet::SubSheet &subsheet) noexcept;
[[nodiscard]]
geo::Vec2 pixelSize(const geo::Vec2 &paneSize) const noexcept;

View File

@ -36,7 +36,7 @@ void TileSheetPixels::initBufferSet(const geo::Vec2 &paneSize, const TileSheet &
// vbo & ebo
m_bufferSet.vbo = glutils::generateBuffer();
m_bufferSet.ebo = glutils::generateBuffer();
setBufferObjects(paneSize, img, subSheet, pal, &m_bufferSet);
setBufferObjects(paneSize, img, subSheet, pal);
glutils::sendVbo(m_bufferSet);
glutils::sendEbo(m_bufferSet);
// vbo layout
@ -78,22 +78,23 @@ 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 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) {
void TileSheetPixels::setBufferObjects(const geo::Vec2 &paneSize, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal) noexcept {
const auto setPixel = [&](std::size_t i, uint8_t p) {
const auto color = pal.colors[p];
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];
const auto ebo = &bg->elements[i * VertexEboLength];
const auto vbo = &m_bufferSet.vertices[i * VertexVboLength];
const auto ebo = &m_bufferSet.elements[i * VertexEboLength];
setPixelBufferObject(paneSize, i * VertexVboRows, fx, fy, color, vbo, ebo);
};
// set buffer lengths
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);
oxDebugf("rows: {}", subSheet.rows);
const auto pixels = static_cast<unsigned>(width * height);
m_bufferSet.vertices.resize(pixels * VertexVboLength);
m_bufferSet.elements.resize(pixels * VertexEboLength);
// set pixels
if (img.bpp == 4) {
for (std::size_t i = 0; i < subSheet.pixels.size(); ++i) {

View File

@ -56,7 +56,7 @@ class TileSheetPixels {
private:
void setPixelBufferObject(const geo::Vec2 &paneS, unsigned vertexRow, float x, float y, Color16 color, float *vbo, GLuint *ebo) const noexcept;
void setBufferObjects(const geo::Vec2 &paneS, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal, glutils::BufferSet *bg) noexcept;
void setBufferObjects(const geo::Vec2 &paneS, const TileSheet &img, const TileSheet::SubSheet &subSheet, const Palette &pal) noexcept;
};