From 285a0f9b24d1b9c4c6c5b1c4a06fb1cae6145129 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 29 May 2022 01:27:35 -0500 Subject: [PATCH] [nostalgia/core] Add custom TypeStore with type desc loader --- src/nostalgia/core/CMakeLists.txt | 2 ++ .../core/studio/tilesheeteditor-imgui.cpp | 21 ++++++++---- src/nostalgia/core/typestore.cpp | 5 +++ src/nostalgia/core/typestore.hpp | 33 +++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/nostalgia/core/typestore.cpp create mode 100644 src/nostalgia/core/typestore.hpp diff --git a/src/nostalgia/core/CMakeLists.txt b/src/nostalgia/core/CMakeLists.txt index 9f273c2a..884de654 100644 --- a/src/nostalgia/core/CMakeLists.txt +++ b/src/nostalgia/core/CMakeLists.txt @@ -48,6 +48,7 @@ add_library( gfx.cpp media.cpp typeconv.cpp + typestore.cpp ) add_library( @@ -101,6 +102,7 @@ install( input.hpp media.hpp typeconv.hpp + typestore.hpp DESTINATION include/nostalgia/core ) diff --git a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp index 9af5b022..04444404 100644 --- a/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/core/studio/tilesheeteditor-imgui.cpp @@ -12,25 +12,32 @@ namespace nostalgia::core { -static ox::Error toPngFile(const ox::String &path, const TileSheet::SubSheet &s, const Palette &pal, int8_t bpp) noexcept { +template +ox::Error toPngFile(const ox::String &path, const TileSheet::SubSheet &s, const Palette &pal, int8_t bpp) noexcept { ox::Vector pixels; s.readPixelsTo(&pixels, bpp); const unsigned rows = s.rows == -1 ? pixels.size() / PixelsPerTile : static_cast(s.rows); const unsigned cols = s.columns == -1 ? 1 : static_cast(s.columns); const auto width = cols * TileWidth; const auto height = rows * TileHeight; - ox::Vector outData(pixels.size() * 3); + constexpr auto bytesPerPixel = alpha ? 4 : 3; + ox::Vector outData(pixels.size() * bytesPerPixel); for (auto idx = 0; const auto colorIdx : pixels) { const auto pt = idxToPt(idx, static_cast(cols)); - const auto i = static_cast(pt.y * static_cast(width) + pt.x) * 3; + const auto i = static_cast(pt.y * static_cast(width) + pt.x) * bytesPerPixel; const auto c = pal.colors[colorIdx]; - outData[i + 0] = (red32(c)); - outData[i + 1] = (green32(c)); - outData[i + 2] = (blue32(c)); + outData[i + 0] = red32(c); + outData[i + 1] = green32(c); + outData[i + 2] = blue32(c); + if constexpr(alpha) { + outData[i + 3] = colorIdx ? 255 : 0; + } ++idx; } - return OxError(lodepng_encode24_file(path.c_str(), outData.data(), width, height)); + constexpr auto fmt = alpha ? LCT_RGBA : LCT_RGB; + return OxError(lodepng_encode_file(path.c_str(), outData.data(), width, height, fmt, 8)); } + TileSheetEditorImGui::TileSheetEditorImGui(Context *ctx, const ox::String &path): m_tileSheetEditor(ctx, path) { m_ctx = ctx; m_itemPath = path; diff --git a/src/nostalgia/core/typestore.cpp b/src/nostalgia/core/typestore.cpp new file mode 100644 index 00000000..21a9329e --- /dev/null +++ b/src/nostalgia/core/typestore.cpp @@ -0,0 +1,5 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#include "typestore.hpp" diff --git a/src/nostalgia/core/typestore.hpp b/src/nostalgia/core/typestore.hpp new file mode 100644 index 00000000..07b299fc --- /dev/null +++ b/src/nostalgia/core/typestore.hpp @@ -0,0 +1,33 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#pragma once + +#include +#include + +#include "context.hpp" + +namespace nostalgia::core { + +class TypeStore: public ox::TypeStore { + private: + ox::FileSystem *m_fs = nullptr; + + public: + constexpr explicit TypeStore(ox::FileSystem *fs) noexcept: m_fs(fs) { + } + + protected: + ox::Result> loadDescriptor(const ox::String &name) noexcept override { + constexpr auto descPath = "/.nostalgia/type_descriptors"; + auto path = ox::sfmt("{}/{}", descPath, name); + oxRequire(buff, m_fs->read(path)); + auto dt = ox::make_unique(); + oxReturnError(ox::readClaw(buff, dt.get())); + return dt; + } +}; + +}