allow to replace img with png or jpg or bmp (#272)

* allow to replace img with png or jpg or bmp

* revert experimental change
This commit is contained in:
coco875
2025-06-22 12:49:15 +00:00
committed by GitHub
parent e73d3f4fa3
commit 0eb0ff49e0
4 changed files with 117 additions and 3 deletions
+3
View File
@@ -287,6 +287,9 @@ FetchContent_Declare(
)
FetchContent_MakeAvailable(dr_libs)
#=================== STB ===================
include_directories(${STB_DIR})
#==============================================================================#
# Libultraship Integration #
#==============================================================================#
+7 -3
View File
@@ -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",
@@ -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
@@ -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