Impl Import Minimap

This commit is contained in:
MegaMech
2025-04-27 17:29:22 -06:00
parent ade89f6b8b
commit 950ef8ea57
9 changed files with 152 additions and 8 deletions
+49 -8
View File
@@ -9,7 +9,9 @@
#include <nlohmann/json.hpp>
#include "port/Engine.h"
#include <libultraship/src/resource/type/Json.h>
#include "port/resource/type/Minimap.h"
#include <libultraship/src/resource/File.h>
#include "port/resource/type/ResourceType.h"
namespace Editor {
@@ -24,12 +26,26 @@ namespace Editor {
data["Props"] = props.to_json();
nlohmann::json actors;
nlohmann::json staticMesh;
// for (const auto& actor : gWorldInstance.StaticMeshActors) {
for (const auto& mesh : gWorldInstance.StaticMeshActors) {
staticMesh.push_back(mesh->to_json());
}
data["StaticMeshActors"] = staticMesh;
// nlohmann::json actors;
// for (const auto& actor : gWorldInstance.Actors) {
// actors.push_back(actor->to_json());
// }
// data["Actors"] = actors;
// }
// data["Actors"] = actors;
// nlohmann::json objects;
// for (const auto& object : gWorldInstance.Objects) {
// objects.push_back(object->to_json());
// }
// data["Objects"] = objects;
auto dat = data.dump();
std::vector<uint8_t> stringify;
@@ -73,10 +89,10 @@ namespace Editor {
} else {
std::cerr << "Props data not found in the JSON file!" << std::endl;
}
// Load the Actors (deserialize them)
if (data.contains("Actors")) {
auto& actorsJson = data["Actors"];
if (data.contains("StaticMeshActors")) {
auto& actorsJson = data["StaticMeshActors"];
gWorldInstance.StaticMeshActors.clear(); // Clear existing actors, if any
for (const auto& actorJson : actorsJson) {
@@ -87,7 +103,7 @@ namespace Editor {
}
}
}
void Load_AddStaticMeshActor(const nlohmann::json& actorJson) {
gWorldInstance.StaticMeshActors.push_back(new StaticMeshActor("", FVector(0, 0, 0), IRotator(0, 0, 0), FVector(1, 1, 1), "", nullptr));
auto actor = gWorldInstance.StaticMeshActors.back();
@@ -103,4 +119,29 @@ namespace Editor {
CurrentArchive = archive;
SceneFile = sceneFile;
}
void LoadMinimap(std::shared_ptr<Ship::Archive> archive, Course* course, std::string filePath) {
printf("LOADING MINIMAP %s\n", filePath.c_str());
if (archive) {
auto initData = std::make_shared<Ship::ResourceInitData>();
initData->Parent = archive;
initData->Format = RESOURCE_FORMAT_BINARY;
initData->ByteOrder = Ship::Endianness::Little;
initData->Type = static_cast<uint32_t>(MK64::ResourceType::Minimap);
initData->ResourceVersion = 0;
std::shared_ptr<MK64::Minimap> ptr = std::static_pointer_cast<MK64::Minimap>(
GameEngine::Instance->context->GetResourceManager()->LoadResource(filePath, true, initData));
if (ptr) {
MK64::MinimapTexture texture = ptr->Texture;
printf("LOADED MINIMAP!\n");
course->Props.MinimapTexture = (const char*)texture.Data;
course->Props.MinimapDimensions = IVector2D(texture.Width, texture.Height);
} else {
course->Props.MinimapTexture = gTextureCourseOutlineMarioRaceway;
course->Props.MinimapDimensions = IVector2D(ResourceGetTexWidthByName(course->Props.MinimapTexture), ResourceGetTexHeightByName(course->Props.MinimapTexture));
}
}
}
}
+1
View File
@@ -6,6 +6,7 @@ namespace Editor {
void LoadLevel(std::shared_ptr<Ship::Archive> archive, Course* course, std::string sceneFile);
void Load_AddStaticMeshActor(const nlohmann::json& actorJson);
void SetSceneFile(std::shared_ptr<Ship::Archive> archive, std::string sceneFile);
void LoadMinimap(std::shared_ptr<Ship::Archive> archive, Course* course, std::string filePath);
extern std::shared_ptr<Ship::Archive> CurrentArchive; // This is used to retrieve and write the scene data file
extern std::string SceneFile;
+4
View File
@@ -18,6 +18,7 @@
#include "resource/importers/ActorSpawnDataFactory.h"
#include "resource/importers/UnkActorSpawnDataFactory.h"
#include "resource/importers/ArrayFactory.h"
#include "resource/importers/MinimapFactory.h"
#include <Fast3D/Fast3dWindow.h>
#include <Fonts.h>
#include "window/gui/resource/Font.h"
@@ -170,6 +171,9 @@ GameEngine::GameEngine() {
loader->RegisterResourceFactory(std::make_shared<MK64::ResourceFactoryBinaryUnkActorSpawnDataV0>(),
RESOURCE_FORMAT_BINARY, "UnkSpawnData",
static_cast<uint32_t>(MK64::ResourceType::UnkSpawnData), 0);
loader->RegisterResourceFactory(std::make_shared<MK64::ResourceFactoryBinaryMinimapV0>(),
RESOURCE_FORMAT_BINARY, "Minimap",
static_cast<uint32_t>(MK64::ResourceType::Minimap), 0);
fontMono = CreateFontWithSize(16.0f, "fonts/Inconsolata-Regular.ttf");
fontMonoLarger = CreateFontWithSize(20.0f, "fonts/Inconsolata-Regular.ttf");
@@ -0,0 +1,36 @@
#include "MinimapFactory.h"
#include "port/resource/type/Minimap.h"
#include "spdlog/spdlog.h"
#include "stb_image.h"
namespace MK64 {
std::shared_ptr<Ship::IResource>
ResourceFactoryBinaryMinimapV0::ReadResource(std::shared_ptr<Ship::File> file,
std::shared_ptr<Ship::ResourceInitData> initData) {
if (!FileHasValidFormatAndReader(file, initData)) {
return nullptr;
}
auto texture = std::make_shared<MK64::Minimap>(initData);
auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);
texture->Texture.Size = file->Buffer->size();
texture->Texture.Width = 0;
texture->Texture.Height = 0;
texture->Texture.Channels = 4; // Always force 4 channels (RGBA)
texture->Texture.Data = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(file->Buffer->data()), texture->Texture.Size,
&texture->Texture.Width, &texture->Texture.Height, nullptr, 4);
if (nullptr == texture->Texture.Data) {
SPDLOG_ERROR("MinimapFactory.cpp: Error loading minimap texture {}", stbi_failure_reason());
return nullptr;
}
// stbi_image_free(imageData);
return texture;
}
}
@@ -0,0 +1,14 @@
#pragma once
#include "resource/Resource.h"
#include "resource/ResourceFactoryBinary.h"
#include "resource/ResourceFactoryXML.h"
namespace MK64 {
class ResourceFactoryBinaryMinimapV0 : 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
+16
View File
@@ -0,0 +1,16 @@
#include "Minimap.h"
#include "libultraship/libultra/gbi.h"
namespace MK64 {
Minimap::Minimap() : Resource(std::shared_ptr<Ship::ResourceInitData>()) {
}
MinimapTexture* Minimap::GetPointer() {
return &Texture;
}
size_t Minimap::GetPointerSize() {
return sizeof(Texture);
}
} // namespace MK64
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include "resource/Resource.h"
#include <vector>
#include <cstdint>
namespace MK64 {
struct MinimapTexture {
int32_t Width;
int32_t Height;
int32_t Channels;
size_t Size;
uint8_t* Data;
};
class Minimap : public Ship::Resource<MinimapTexture> {
public:
using Resource::Resource;
Minimap();
MinimapTexture* GetPointer() override;
size_t GetPointerSize() override;
MinimapTexture Texture;
};
} // namespace MK64
+1
View File
@@ -34,5 +34,6 @@ enum class ResourceType {
SpawnData = 0x53444154, // SDAT
UnkSpawnData = 0x55534454, // USDT
KartAI = 0x44424856, // DBHV
Minimap = 0x4D4D4150, // MMAP
};
}
+2
View File
@@ -231,6 +231,7 @@ namespace Editor {
for (const std::string& dir : dirs) {
std::string name = dir.substr(dir.find_last_of('/') + 1);
std::string sceneFile = dir + "/scene.json";
std::string minimapFile = dir + "/minimap";
if (manager->HasFile(sceneFile)) {
auto archive = manager->GetArchiveFromFile(sceneFile);
@@ -238,6 +239,7 @@ namespace Editor {
course->LoadO2R(dir);
gWorldInstance.Courses.push_back(course);
LoadLevel(archive, course, sceneFile);
LoadMinimap(archive, course, minimapFile);
Tracks.push_back({course, sceneFile, name, dir, archive});
} else {
const std::string file = dir + "/data_track_sections";