From 0eb0ff49e0afaf62be2c70a173a122ac58bbabfb Mon Sep 17 00:00:00 2001 From: coco875 <59367621+coco875@users.noreply.github.com> Date: Sun, 22 Jun 2025 12:49:15 +0000 Subject: [PATCH] allow to replace img with png or jpg or bmp (#272) * allow to replace img with png or jpg or bmp * revert experimental change --- CMakeLists.txt | 3 + src/port/Engine.cpp | 10 ++- .../importers/BetterTextureFactory.cpp | 89 +++++++++++++++++++ .../resource/importers/BetterTextureFactory.h | 18 ++++ 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/port/resource/importers/BetterTextureFactory.cpp create mode 100644 src/port/resource/importers/BetterTextureFactory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f77ecf401..b5429241b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -287,6 +287,9 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(dr_libs) +#=================== STB =================== +include_directories(${STB_DIR}) + #==============================================================================# # Libultraship Integration # #==============================================================================# diff --git a/src/port/Engine.cpp b/src/port/Engine.cpp index 8d66a0211..5ca0b22f6 100644 --- a/src/port/Engine.cpp +++ b/src/port/Engine.cpp @@ -20,6 +20,7 @@ #include "resource/importers/UnkActorSpawnDataFactory.h" #include "resource/importers/ArrayFactory.h" #include "resource/importers/MinimapFactory.h" +#include "resource/importers/BetterTextureFactory.h" #include #include "window/gui/resource/Font.h" #include "window/gui/resource/FontFactory.h" @@ -202,11 +203,14 @@ GameEngine::GameEngine() { loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "GenericArray", static_cast(SF64::ResourceType::GenericArray), 0); - loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + // loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + // "Texture", static_cast(Fast::ResourceType::Texture), 0); + // loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + // "Texture", static_cast(Fast::ResourceType::Texture), 1); + loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Texture", static_cast(Fast::ResourceType::Texture), 0); - loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, + loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Texture", static_cast(Fast::ResourceType::Texture), 1); - loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_BINARY, "Vertex", static_cast(Fast::ResourceType::Vertex), 0); loader->RegisterResourceFactory(std::make_shared(), RESOURCE_FORMAT_XML, "Vertex", diff --git a/src/port/resource/importers/BetterTextureFactory.cpp b/src/port/resource/importers/BetterTextureFactory.cpp new file mode 100644 index 000000000..40fe92eef --- /dev/null +++ b/src/port/resource/importers/BetterTextureFactory.cpp @@ -0,0 +1,89 @@ +#include "BetterTextureFactory.h" +#include "resource/type/Texture.h" +#include "spdlog/spdlog.h" +#include +#include +#include "resource/archive/ArchiveManager.h" +#include "resource/ResourceManager.h" + +namespace MK64 { + +std::shared_ptr loadPngTexture(std::shared_ptr filePng, std::shared_ptr initData) { + auto texture = std::make_shared(initData); + + int height, width = 0; + texture->ImageData = stbi_load_from_memory((const stbi_uc*)filePng->Buffer.get()->data(), + filePng->Buffer.get()->size(), &width, &height, nullptr, 4); + texture->Width = width; + texture->Height = height; + texture->Type = Fast::TextureType::RGBA32bpp; + texture->ImageDataSize = texture->Width * texture->Height * 4; + texture->Flags = TEX_FLAG_LOAD_AS_IMG; + return texture; +} + +std::vector extension = {".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".bmp", ".BMP"}; + +std::shared_ptr +ResourceFactoryBinaryTextureV0::ReadResource(std::shared_ptr file, + std::shared_ptr initData) { + if (!FileHasValidFormatAndReader(file, initData)) { + return nullptr; + } + + for (const auto& ext : extension) { + auto filePng = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess( + initData->Path + ext); + + if (filePng != nullptr) { + return loadPngTexture(filePng, initData); + } + } + + auto texture = std::make_shared(initData); + auto reader = std::get>(file->Reader); + + texture->Type = (Fast::TextureType)reader->ReadUInt32(); + texture->Width = reader->ReadUInt32(); + texture->Height = reader->ReadUInt32(); + texture->ImageDataSize = reader->ReadUInt32(); + texture->ImageData = new uint8_t[texture->ImageDataSize]; + + reader->Read((char*)texture->ImageData, texture->ImageDataSize); + + return texture; +} + +std::shared_ptr +ResourceFactoryBinaryTextureV1::ReadResource(std::shared_ptr file, + std::shared_ptr initData) { + if (!FileHasValidFormatAndReader(file, initData)) { + return nullptr; + } + + for (const auto& ext : extension) { + auto filePng = Ship::Context::GetInstance()->GetResourceManager()->LoadFileProcess( + initData->Path + ext); + + if (filePng != nullptr) { + return loadPngTexture(filePng, initData); + } + } + + auto texture = std::make_shared(initData); + auto reader = std::get>(file->Reader); + + texture->Type = (Fast::TextureType)reader->ReadUInt32(); + texture->Width = reader->ReadUInt32(); + texture->Height = reader->ReadUInt32(); + texture->Flags = reader->ReadUInt32(); + texture->HByteScale = reader->ReadFloat(); + texture->VPixelScale = reader->ReadFloat(); + texture->ImageDataSize = reader->ReadUInt32(); + texture->ImageData = new uint8_t[texture->ImageDataSize]; + + reader->Read((char*)texture->ImageData, texture->ImageDataSize); + + return texture; +} +} // namespace Fast diff --git a/src/port/resource/importers/BetterTextureFactory.h b/src/port/resource/importers/BetterTextureFactory.h new file mode 100644 index 000000000..68e7e2d20 --- /dev/null +++ b/src/port/resource/importers/BetterTextureFactory.h @@ -0,0 +1,18 @@ +#pragma once + +#include "resource/Resource.h" +#include "resource/ResourceFactoryBinary.h" + +namespace MK64 { +class ResourceFactoryBinaryTextureV0 final : public Ship::ResourceFactoryBinary { + public: + std::shared_ptr ReadResource(std::shared_ptr file, + std::shared_ptr initData) override; +}; + +class ResourceFactoryBinaryTextureV1 final : public Ship::ResourceFactoryBinary { + public: + std::shared_ptr ReadResource(std::shared_ptr file, + std::shared_ptr initData) override; +}; +} // namespace MK64