[nostalgia/core] Add custom TypeStore with type desc loader
This commit is contained in:
parent
ab1dc83630
commit
285a0f9b24
@ -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
|
||||
)
|
||||
|
@ -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<bool alpha = false>
|
||||
ox::Error toPngFile(const ox::String &path, const TileSheet::SubSheet &s, const Palette &pal, int8_t bpp) noexcept {
|
||||
ox::Vector<uint8_t> pixels;
|
||||
s.readPixelsTo(&pixels, bpp);
|
||||
const unsigned rows = s.rows == -1 ? pixels.size() / PixelsPerTile : static_cast<unsigned>(s.rows);
|
||||
const unsigned cols = s.columns == -1 ? 1 : static_cast<unsigned>(s.columns);
|
||||
const auto width = cols * TileWidth;
|
||||
const auto height = rows * TileHeight;
|
||||
ox::Vector<unsigned char> outData(pixels.size() * 3);
|
||||
constexpr auto bytesPerPixel = alpha ? 4 : 3;
|
||||
ox::Vector<unsigned char> outData(pixels.size() * bytesPerPixel);
|
||||
for (auto idx = 0; const auto colorIdx : pixels) {
|
||||
const auto pt = idxToPt(idx, static_cast<int>(cols));
|
||||
const auto i = static_cast<unsigned>(pt.y * static_cast<int>(width) + pt.x) * 3;
|
||||
const auto i = static_cast<unsigned>(pt.y * static_cast<int>(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;
|
||||
|
5
src/nostalgia/core/typestore.cpp
Normal file
5
src/nostalgia/core/typestore.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
||||
*/
|
||||
|
||||
#include "typestore.hpp"
|
33
src/nostalgia/core/typestore.hpp
Normal file
33
src/nostalgia/core/typestore.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ox/claw/claw.hpp>
|
||||
#include <ox/model/typestore.hpp>
|
||||
|
||||
#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<ox::UniquePtr<ox::DescriptorType>> 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<ox::DescriptorType>();
|
||||
oxReturnError(ox::readClaw<ox::DescriptorType>(buff, dt.get()));
|
||||
return dt;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user