#include "BetterTextureFactory.h" #include "fast/resource/type/Texture.h" #include "spdlog/spdlog.h" #include #include #include "ship/resource/archive/ArchiveManager.h" #include "ship/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::GetRawInstance()->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::GetRawInstance()->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