From 396f732bc58080430562ea60fd345bf2e51cfc0b Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Sun, 17 Sep 2023 23:55:59 -0600 Subject: Refactored to use a better file format --- src/factories/TextureFactory.cpp | 77 +++++++++++++--------------------------- 1 file changed, 24 insertions(+), 53 deletions(-) (limited to 'src/factories/TextureFactory.cpp') diff --git a/src/factories/TextureFactory.cpp b/src/factories/TextureFactory.cpp index 4c37fd5..12d8540 100644 --- a/src/factories/TextureFactory.cpp +++ b/src/factories/TextureFactory.cpp @@ -1,77 +1,48 @@ #include "TextureFactory.h" -#include -#include #include "Companion.h" #include "utils/MIODecoder.h" namespace fs = std::filesystem; -enum class TextureType { - Error, - RGBA32bpp, - RGBA16bpp, - Palette4bpp, - Palette8bpp, - Grayscale4bpp, - Grayscale8bpp, - GrayscaleAlpha4bpp, - GrayscaleAlpha8bpp, - GrayscaleAlpha16bpp, -}; - static const std::unordered_map gTextureTypes = { - { ".rgba16", TextureType::RGBA16bpp }, - { ".rgba32", TextureType::RGBA32bpp }, - { ".ia1", TextureType::GrayscaleAlpha4bpp }, - { ".ia4", TextureType::GrayscaleAlpha4bpp }, - { ".ia8", TextureType::GrayscaleAlpha8bpp }, - { ".ia16", TextureType::GrayscaleAlpha16bpp }, + { "RGBA16", TextureType::RGBA16bpp }, + { "RGBA32", TextureType::RGBA32bpp }, + { "IA1", TextureType::GrayscaleAlpha4bpp }, + { "IA4", TextureType::GrayscaleAlpha4bpp }, + { "IA8", TextureType::GrayscaleAlpha8bpp }, + { "IA16", TextureType::GrayscaleAlpha16bpp }, }; -bool TextureFactory::process(LUS::BinaryWriter* writer, nlohmann::json& data, std::vector& buffer) { - std::string path = data["path"]; - std::string ext = fs::path(path).extension().string(); +bool TextureFactory::process(LUS::BinaryWriter* writer, YAML::Node& data, std::vector& buffer) { - bool isTileTexture = path.find("cake") != std::string::npos || path.find("skyboxes") != std::string::npos; + auto format = data["format"].as(); + auto width = data["width"].as(); + auto height = data["height"].as(); + auto size = data["size"].as(); + auto offset = data["offset"].as(); - if(isTileTexture) { + if(!gTextureTypes.contains(format)) { return false; } - if(!gTextureTypes.contains(ext)) { - return false; - } - - auto metadata = data["offsets"]; - if(!metadata[3].contains("us")){ - return false; - } - - // Path: [Width, Height, Size, { Country : [Rom Offset, MIO0 Size] }], - - TextureType type = isTileTexture ? TextureType::RGBA32bpp : gTextureTypes.at(ext); + TextureType type = gTextureTypes.at(format); WRITE_HEADER(LUS::ResourceType::Texture, 0); - WRITE_U32(type); // Texture Type - WRITE_U32(metadata[0]); // Width - WRITE_U32(metadata[1]); // Height - size_t size = metadata[2]; - auto offsets = metadata[3]["us"]; + WRITE_U32(type); + WRITE_U32(width); + WRITE_U32(height); - auto* texture = new uint8_t[size]; - - if(offsets.size() > 1){ - auto mio0 = MIO0Decoder::Decode(buffer, offsets[0]); - memcpy(texture, mio0.data() + (size_t)offsets[1], size); + if(data["mio0"]){ + auto mio0 = data["mio0"].as(); + auto decoded = MIO0Decoder::Decode(buffer, offset); + WRITE_U32(size); + WRITE_ARRAY(decoded.data() + mio0, size); } else { - memcpy(texture, buffer.data() + (size_t)offsets[0], size); + WRITE_U32(size); + WRITE_ARRAY(buffer.data() + offset, size); } - WRITE_U32(size); // Texture Data Size - WRITE_ARRAY(texture, size); // Texture Data - - delete[] texture; return true; } -- cgit v1.2.3