aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/ResourceManager.h
blob: 7a454939c7388f1abd39dc1774e3a2a1128121f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

#include <any>
#include <memory>
#include <string>
#include <unordered_map>

namespace crepe {

/**
 * \brief The ResourceManager is responsible for storing and managing assets over
 * multiple scenes.
 * 
 * The ResourceManager ensures that assets are loaded once and can be accessed
 * across different scenes. It caches assets to avoid reloading them every time
 * a scene is loaded. Assets are retained in memory until the ResourceManager is
 * destroyed, at which point the cached assets are cleared.
 */
class ResourceManager {

private:
	//! A cache that holds all the assets, accessible by their file path, over multiple scenes.
	std::unordered_map<std::string, std::any> asset_cache;

private:
	ResourceManager();
	virtual ~ResourceManager();

public:
	ResourceManager(const ResourceManager &) = delete;
	ResourceManager(ResourceManager &&) = delete;
	ResourceManager & operator=(const ResourceManager &) = delete;
	ResourceManager & operator=(ResourceManager &&) = delete;

	/**
	 * \brief Retrieves the singleton instance of the ResourceManager.
	 *
	 * \return A reference to the single instance of the ResourceManager.
	 */
	static ResourceManager & get_instance();

public:
	/**
	 * \brief Caches an asset by loading it from the given file path.
	 *
	 * \param file_path The path to the asset file to load.
	 * \param reload If true, the asset will be reloaded from the file, even if
	 * it is already cached.
	 * \tparam T The type of asset to cache (e.g., texture, sound, etc.).
	 * 
	 * \return A shared pointer to the cached asset.
	 * 
	 * This template function caches the asset at the given file path. If the
	 * asset is already cached and `reload` is false, the existing cached version
	 * will be returned. Otherwise, the asset will be reloaded and added to the
	 * cache.
	 */
	template <typename T>
	std::shared_ptr<T> cache(const std::string & file_path,
							 bool reload = false);
};

} // namespace crepe

#include "ResourceManager.hpp"