Files
simian/include/AssetManager.h
nick 756b818f46
Sync Docs to Gitea Wiki / Sync docs to Gitea wiki (push) Successful in 26s
CI / build-and-test (push) Failing after 2m36s
feat: asset management, editor camera
2026-03-09 21:31:51 +13:00

145 lines
5.8 KiB
C++

#pragma once
#include "raylib.h"
#include <string>
#include <unordered_map>
#include <vector>
class ModelManager;
class ShaderManager;
class MaterialManager;
class TextureManager;
struct AssetEntry {
unsigned int id = 0;
std::string source;
};
struct TextureDefinition {
std::string path;
};
struct ShaderDefinition {
std::string vsPath;
std::string fsPath;
};
struct ModelDefinition {
enum class Type {
Path,
Primitive,
Heightmap,
Cubicmap,
Mesh
};
Type type = Type::Path;
std::string path;
std::string primitive;
std::vector<float> params;
};
struct MaterialDefinition {
std::string shaderKey;
unsigned int shaderId = 0;
Color albedo = {255, 255, 255, 255};
bool hasShaderKey = false;
bool hasShaderId = false;
};
class AssetManager
{
public:
AssetManager();
~AssetManager();
void SetManagers(ModelManager* modelMgr, ShaderManager* shaderMgr,
MaterialManager* materialMgr, TextureManager* textureMgr);
// Register an existing asset ID under a key (no loading performed).
bool RegisterTexture(const std::string& key, unsigned int id, const std::string& source = std::string());
bool RegisterShader(const std::string& key, unsigned int id, const std::string& source = std::string());
bool RegisterModel(const std::string& key, unsigned int id, const std::string& source = std::string());
bool RegisterMaterial(const std::string& key, unsigned int id, const std::string& source = std::string());
// Load helpers that also register by key.
unsigned int LoadTexture(const std::string& key, const std::string& path);
unsigned int LoadShader(const std::string& key, const std::string& vsPath, const std::string& fsPath);
unsigned int LoadModel(const std::string& key, const std::string& path);
unsigned int LoadCube(const std::string& key, float width, float height, float length);
unsigned int LoadPoly(const std::string& key, int sides, float radius);
unsigned int LoadSphere(const std::string& key, float radius, int rings, int slices);
unsigned int LoadHemiSphere(const std::string& key, float radius, int rings, int slices);
unsigned int LoadPlane(const std::string& key, float width, float length, int resX, int resZ);
unsigned int LoadCylinder(const std::string& key, float radius, float height, int slices);
unsigned int LoadCone(const std::string& key, float radius, float height, int slices);
unsigned int LoadTorus(const std::string& key, float radius, float size, int radSeg, int sides);
unsigned int LoadKnot(const std::string& key, float radius, float size, int radSeg, int sides);
unsigned int LoadHeightmap(const std::string& key, const std::string& heightmapPath, float sizeX, float sizeY, float sizeZ);
unsigned int LoadCubicmap(const std::string& key, const std::string& cubicmapPath, float sizeX, float sizeY, float sizeZ);
unsigned int LoadFromMesh(const std::string& key, Mesh mesh, const std::string& source = std::string());
unsigned int CreateMaterial(const std::string& key, unsigned int shaderId, Color albedo);
bool ApplyMeshTangents(const std::string& key, int meshIndex = 0);
// Unload by key (removes registry entries).
bool UnloadTexture(const std::string& key);
bool UnloadShader(const std::string& key);
bool UnloadModel(const std::string& key);
bool UnloadMaterial(const std::string& key);
// Query by key.
unsigned int GetTextureId(const std::string& key) const;
unsigned int GetShaderId(const std::string& key) const;
unsigned int GetModelId(const std::string& key) const;
unsigned int GetMaterialId(const std::string& key) const;
// Query by id.
const std::string& GetTextureKey(unsigned int id) const;
const std::string& GetShaderKey(unsigned int id) const;
const std::string& GetModelKey(unsigned int id) const;
const std::string& GetMaterialKey(unsigned int id) const;
// Asset listing for editor UI.
std::vector<std::string> ListTextureKeys() const;
std::vector<std::string> ListShaderKeys() const;
std::vector<std::string> ListModelKeys() const;
std::vector<std::string> ListMaterialKeys() const;
// Source info (path or descriptive string).
std::string GetTextureSource(const std::string& key) const;
std::string GetShaderSource(const std::string& key) const;
std::string GetModelSource(const std::string& key) const;
std::string GetMaterialSource(const std::string& key) const;
// Asset registry persistence.
bool SaveRegistry(const std::string& path) const;
bool LoadRegistry(const std::string& path);
// Clear registry entries (does not unload underlying resources).
void Clear();
private:
ModelManager* modelManager = nullptr;
ShaderManager* shaderManager = nullptr;
MaterialManager* materialManager = nullptr;
TextureManager* textureManager = nullptr;
std::unordered_map<std::string, AssetEntry> textureAssets;
std::unordered_map<std::string, AssetEntry> shaderAssets;
std::unordered_map<std::string, AssetEntry> modelAssets;
std::unordered_map<std::string, AssetEntry> materialAssets;
std::unordered_map<std::string, TextureDefinition> textureDefs;
std::unordered_map<std::string, ShaderDefinition> shaderDefs;
std::unordered_map<std::string, ModelDefinition> modelDefs;
std::unordered_map<std::string, MaterialDefinition> materialDefs;
std::unordered_map<unsigned int, std::string> textureIds;
std::unordered_map<unsigned int, std::string> shaderIds;
std::unordered_map<unsigned int, std::string> modelIds;
std::unordered_map<unsigned int, std::string> materialIds;
const std::string& GetKeyForId(const std::unordered_map<unsigned int, std::string>& map, unsigned int id) const;
std::vector<std::string> ListKeys(const std::unordered_map<std::string, AssetEntry>& map) const;
};