Tesseract-Engine/src/Engine/AssetManager.h

117 lines
2.6 KiB
C
Raw Normal View History

#pragma once
#include <string>
#include <unordered_map>
#include <GL/glew.h>
#include <vector>
#include <variant>
#include "gcml.h"
#include "stdexcept"
#include <iostream>
// Forward-declare your Shader class
class Shader;
// Define types of assets
enum class AssetType
{
TEXTURE,
SHADER,
SOUND,
MODEL,
};
// A simple struct to hold the generic pointer
struct GenericAsset
{
void *data = nullptr;
};
struct Vertex
{
float position[3];
float texCoord[2];
float normal[3];
};
// Define a Texture structure
struct Texture {
GLuint id;
std::string type;
std::string path;
};
struct Model {
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<Texture> textures;
GLuint vao, vbo, ebo;
};
// The main AssetManager
class AssetManager
{
public:
AssetManager() = default;
~AssetManager() = default;
using AssetVariant = std::variant<GLuint, Shader *, std::string, Model *>;
// Template function to load an asset
template <typename T>
T loadAsset(AssetType type, const std::string &path)
{
// 1) Create a unique key for cache lookup
std::string key = generateKey(type, path);
// 2) Check if its already loaded
auto it = m_AssetMap.find(key);
if (it != m_AssetMap.end())
{
// Debug: Log the variant type
if (std::holds_alternative<T>(it->second))
{
return std::get<T>(it->second);
}
else
{
throw std::runtime_error("Asset type mismatch for key: " + key);
}
}
// 3) Not loaded yet, load from disk
AssetVariant assetData = loadAssetFromDisk(type, path);
if (assetData.index() == std::variant_npos)
{
DEBUG_PRINT("[AssetManager] Failed to load asset: %s", path.c_str());
// Replace NULL with 0 for non-pointer types
if constexpr (std::is_pointer_v<T>)
{
return nullptr; // For pointers
}
else
{
2024-12-28 19:06:42 +00:00
return 0; // For non-pointer
}
}
// 4) Store in cache
m_AssetMap[key] = assetData;
// 5) Return the loaded asset
return std::get<T>(assetData);
}
void DebugAssetMap();
private:
// Cache of already loaded assets: key = "type + path"
std::unordered_map<std::string, AssetVariant> m_AssetMap;
AssetVariant loadAssetFromDisk(AssetType type, const std::string &path);
// Generate the unique key
std::string generateKey(AssetType type, const std::string &path);
};