Introduce AssetManager

This commit is contained in:
2025-04-20 19:36:17 +02:00
parent d643288e56
commit b78c214229
5 changed files with 81 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ find_package(fx-gltf REQUIRED)
add_subdirectory(${PROJECT_SOURCE_DIR}/lib) add_subdirectory(${PROJECT_SOURCE_DIR}/lib)
add_library(fever_core add_library(fever_core
src/asset/asset_manager.cpp
src/components/transform.cpp src/components/transform.cpp
# src/core/application.cpp # src/core/application.cpp
src/core/camera.cpp src/core/camera.cpp

View File

@@ -1,4 +1,4 @@
A work in progress game engine written in C++ using OpenGL and [entt](https://github.com/skypjack/entt). A work in progress game engine written in C++ using OpenGL and [flecs](https://flecs.dev).
![A screenshot](screenshot.png) ![A screenshot](screenshot.png)

View File

@@ -1,3 +1,4 @@
#include <asset/asset_manager.h>
#include <flecs.h> #include <flecs.h>
#include <input/input.h> #include <input/input.h>
#include <log/log.h> #include <log/log.h>
@@ -14,6 +15,13 @@ int main()
world.import <Window::WindowModule>(); world.import <Window::WindowModule>();
world.import <Input::InputModule>(); world.import <Input::InputModule>();
world.import <Asset::AssetModule>();
Asset::AssetManager manager;
manager.register_asset_type<int>(world);
auto handle0 = manager.load<int>(world, "hi");
auto handle1 = manager.load<int>(world, "hi");
auto handle2 = manager.load<int>(world, "hi2");
#ifndef NDEBUG #ifndef NDEBUG
world.import <flecs::stats>(); world.import <flecs::stats>();

View File

@@ -0,0 +1,10 @@
#include "asset_manager.h"
namespace Asset {
AssetModule::AssetModule(flecs::world& world)
{
auto asset_manager = world.component<AssetManager>();
}
} // namespace Asset

61
src/asset/asset_manager.h Normal file
View File

@@ -0,0 +1,61 @@
#pragma once
#include <flecs.h>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
namespace Asset {
struct AssetModule
{
AssetModule(flecs::world& world);
};
template <typename T> struct AssetCache
{
std::unordered_map<std::string, std::shared_ptr<T>> cache;
};
template <typename T> struct AssetLoader
{
template <typename Args> std::shared_ptr<T> operator()(Args&&...)
{
static_assert(sizeof(T) == 0, "AssetLoader not implemented for this type.");
}
};
template <> struct AssetLoader<int>
{
std::shared_ptr<int> operator()(std::string_view path)
{
static int i = 0;
return std::make_shared<int>(i++);
}
};
struct AssetManager
{
template <typename T> void register_asset_type(flecs::world& world)
{
world.set<AssetCache<T>>({});
}
template <typename T> std::shared_ptr<T> load(flecs::world& world, std::string asset_path)
{
auto asset_cache = world.get_mut<AssetCache<T>>();
auto asset_it = asset_cache->cache.find(asset_path);
if (asset_it != asset_cache->cache.end())
return asset_it->second;
AssetLoader<T> asset_loader;
auto loaded_asset = asset_loader(asset_path);
asset_cache->cache.emplace(asset_path, loaded_asset);
return loaded_asset;
}
};
} // namespace Asset