2024-12-25 23:01:05 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <unordered_map>
|
2024-12-27 05:31:12 +00:00
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
#include <vector>
|
2024-12-27 18:19:20 +00:00
|
|
|
|
#include <variant>
|
|
|
|
|
#include "gcml.h"
|
|
|
|
|
#include "stdexcept"
|
|
|
|
|
#include <iostream>
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// Forward-declare your Shader class
|
|
|
|
|
class Shader;
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// Define types of assets
|
2024-12-25 23:01:05 +00:00
|
|
|
|
enum class AssetType
|
|
|
|
|
{
|
|
|
|
|
TEXTURE,
|
|
|
|
|
SHADER,
|
|
|
|
|
SOUND,
|
2024-12-27 05:31:12 +00:00
|
|
|
|
MODEL,
|
2024-12-25 23:01:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// A simple struct to hold the generic pointer
|
2024-12-25 23:01:05 +00:00
|
|
|
|
struct GenericAsset
|
|
|
|
|
{
|
2024-12-27 18:19:20 +00:00
|
|
|
|
void *data = nullptr;
|
2024-12-25 23:01:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-12-27 18:19:20 +00:00
|
|
|
|
struct Vertex
|
|
|
|
|
{
|
2024-12-27 05:31:12 +00:00
|
|
|
|
float position[3];
|
|
|
|
|
float texCoord[2];
|
|
|
|
|
float normal[3];
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-27 18:19:20 +00:00
|
|
|
|
struct Model
|
|
|
|
|
{
|
2024-12-27 05:31:12 +00:00
|
|
|
|
std::vector<Vertex> vertices;
|
|
|
|
|
std::vector<unsigned int> indices;
|
|
|
|
|
GLuint vao;
|
|
|
|
|
GLuint vbo;
|
|
|
|
|
GLuint ebo;
|
|
|
|
|
GLuint textureID;
|
|
|
|
|
|
|
|
|
|
Model()
|
|
|
|
|
: vao(0), vbo(0), ebo(0), textureID(0) {}
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// The main AssetManager
|
2024-12-25 23:01:05 +00:00
|
|
|
|
class AssetManager
|
|
|
|
|
{
|
|
|
|
|
public:
|
2024-12-27 18:19:20 +00:00
|
|
|
|
AssetManager() = default;
|
2024-12-25 23:01:05 +00:00
|
|
|
|
~AssetManager() = default;
|
|
|
|
|
|
2024-12-27 18:19:20 +00:00
|
|
|
|
using AssetVariant = std::variant<GLuint, Shader *, std::string, Model *>;
|
|
|
|
|
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// Load an asset from disk (texture, shader, etc.)
|
|
|
|
|
// Returns a void* pointer to the loaded resource.
|
2024-12-27 18:19:20 +00:00
|
|
|
|
// - For TEXTURE, cast to (GLuint)
|
|
|
|
|
// - For SHADER, cast to (Shader*)
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// - For SOUND, cast to whatever you store
|
2024-12-27 18:19:20 +00:00
|
|
|
|
|
|
|
|
|
// Template function to load an asset
|
|
|
|
|
// 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 it’s already loaded
|
|
|
|
|
auto it = m_AssetMap.find(key);
|
|
|
|
|
if (it != m_AssetMap.end())
|
|
|
|
|
{
|
|
|
|
|
// Debug: Log the variant type
|
|
|
|
|
std::cout << "[AssetManager] Found asset in map." << std::endl;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
return 0; // For non-pointer types
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4) Store in cache
|
|
|
|
|
m_AssetMap[key] = assetData;
|
|
|
|
|
|
|
|
|
|
DEBUG_PRINT("[AssetManager] Loaded Asset: %s", path.c_str());
|
|
|
|
|
|
|
|
|
|
// 5) Return the loaded asset
|
|
|
|
|
return std::get<T>(assetData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DebugAssetMap();
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// Cache of already loaded assets: key = "type + path"
|
2024-12-27 18:19:20 +00:00
|
|
|
|
std::unordered_map<std::string, AssetVariant> m_AssetMap;
|
2024-12-25 23:01:05 +00:00
|
|
|
|
|
2024-12-27 18:19:20 +00:00
|
|
|
|
AssetVariant loadAssetFromDisk(AssetType type, const std::string &path);
|
2024-12-25 23:35:38 +00:00
|
|
|
|
// Generate the unique key
|
2024-12-27 18:19:20 +00:00
|
|
|
|
std::string generateKey(AssetType type, const std::string &path);
|
2024-12-25 23:01:05 +00:00
|
|
|
|
};
|