summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcoco875 <59367621+coco875@users.noreply.github.com>2025-06-22 12:49:15 +0000
committerGitHub <noreply@github.com>2025-06-22 06:49:15 -0600
commit0eb0ff49e0afaf62be2c70a173a122ac58bbabfb (patch)
tree2dbdf1ffd0d1352876bbbf88801505a0a4d22a26
parente73d3f4fa366f8b984ca3568015fa8f6f3f79e62 (diff)
allow to replace img with png or jpg or bmp (#272)
* allow to replace img with png or jpg or bmp * revert experimental change
-rw-r--r--CMakeLists.txt3
-rw-r--r--src/port/Engine.cpp10
-rw-r--r--src/port/resource/importers/BetterTextureFactory.cpp89
-rw-r--r--src/port/resource/importers/BetterTextureFactory.h18
4 files changed, 117 insertions, 3 deletions
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 <Fonts.h>
#include "window/gui/resource/Font.h"
#include "window/gui/resource/FontFactory.h"
@@ -202,11 +203,14 @@ GameEngine::GameEngine() {
loader->RegisterResourceFactory(std::make_shared<SF64::ResourceFactoryBinaryGenericArrayV0>(),
RESOURCE_FORMAT_BINARY, "GenericArray",
static_cast<uint32_t>(SF64::ResourceType::GenericArray), 0);
- loader->RegisterResourceFactory(std::make_shared<Fast::ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY,
+ // loader->RegisterResourceFactory(std::make_shared<Fast::ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY,
+ // "Texture", static_cast<uint32_t>(Fast::ResourceType::Texture), 0);
+ // loader->RegisterResourceFactory(std::make_shared<Fast::ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY,
+ // "Texture", static_cast<uint32_t>(Fast::ResourceType::Texture), 1);
+ loader->RegisterResourceFactory(std::make_shared<MK64::ResourceFactoryBinaryTextureV0>(), RESOURCE_FORMAT_BINARY,
"Texture", static_cast<uint32_t>(Fast::ResourceType::Texture), 0);
- loader->RegisterResourceFactory(std::make_shared<Fast::ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY,
+ loader->RegisterResourceFactory(std::make_shared<MK64::ResourceFactoryBinaryTextureV1>(), RESOURCE_FORMAT_BINARY,
"Texture", static_cast<uint32_t>(Fast::ResourceType::Texture), 1);
-
loader->RegisterResourceFactory(std::make_shared<Fast::ResourceFactoryBinaryVertexV0>(), RESOURCE_FORMAT_BINARY,
"Vertex", static_cast<uint32_t>(Fast::ResourceType::Vertex), 0);
loader->RegisterResourceFactory(std::make_shared<Fast::ResourceFactoryXMLVertexV0>(), 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 <stb_image.h>
+#include <Context.h>
+#include "resource/archive/ArchiveManager.h"
+#include "resource/ResourceManager.h"
+
+namespace MK64 {
+
+std::shared_ptr<Ship::IResource> loadPngTexture(std::shared_ptr<Ship::File> filePng, std::shared_ptr<Ship::ResourceInitData> initData) {
+ auto texture = std::make_shared<Fast::Texture>(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<std::string> extension = {".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".bmp", ".BMP"};
+
+std::shared_ptr<Ship::IResource>
+ResourceFactoryBinaryTextureV0::ReadResource(std::shared_ptr<Ship::File> file,
+ std::shared_ptr<Ship::ResourceInitData> 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<Fast::Texture>(initData);
+ auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(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<Ship::IResource>
+ResourceFactoryBinaryTextureV1::ReadResource(std::shared_ptr<Ship::File> file,
+ std::shared_ptr<Ship::ResourceInitData> 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<Fast::Texture>(initData);
+ auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(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<Ship::IResource> ReadResource(std::shared_ptr<Ship::File> file,
+ std::shared_ptr<Ship::ResourceInitData> initData) override;
+};
+
+class ResourceFactoryBinaryTextureV1 final : public Ship::ResourceFactoryBinary {
+ public:
+ std::shared_ptr<Ship::IResource> ReadResource(std::shared_ptr<Ship::File> file,
+ std::shared_ptr<Ship::ResourceInitData> initData) override;
+};
+} // namespace MK64