Mountain  1.0.0
Simple C++ 2D Game Framework
resource_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_map>
4 
5 #include "Mountain/file/file.hpp"
10 
13 
14 namespace Mountain
15 {
20  class ResourceManager final
21  {
22  STATIC_CLASS(ResourceManager)
23 
24  public:
26  template <Concepts::LoadableResourceT T>
27  static Pointer<T> Add(const std::string& name);
28 
30  template <Concepts::LoadableResourceT T>
31  static Pointer<T> Add(const Pointer<File>& file);
32 
34  template <Concepts::LoadableResourceT T>
35  static Pointer<T> Load(const Pointer<File>& file, bool_t loadInInterface = true);
36 
38  MOUNTAIN_API static Pointer<Font> LoadFont(const Pointer<File>& file, uint32_t size);
39 
41  MOUNTAIN_API static Pointer<Font> LoadFont(const std::string& name, uint32_t size);
42 
44  MOUNTAIN_API static void LoadAll();
45 
48  MOUNTAIN_API static void LoadAllBinaries();
49 
51  [[nodiscard]]
52  MOUNTAIN_API static bool_t Contains(const std::string& name);
53 
55  [[nodiscard]]
56  MOUNTAIN_API static bool_t Contains(const Pointer<File>& file);
57 
60  [[nodiscard]]
61  MOUNTAIN_API static bool_t IsBinary(const std::string& name);
62 
64  template <Concepts::ResourceT T = Resource>
65  [[nodiscard]]
66  static Pointer<T> Get(const std::string& name);
67 
69  template <Concepts::ResourceT T = Resource>
70  [[nodiscard]]
71  static Pointer<T> Get(const Pointer<File>& file);
72 
74  [[nodiscard]]
75  MOUNTAIN_API static Pointer<Font> GetFont(const std::string& name, uint32_t size);
76 
78  [[nodiscard]]
79  MOUNTAIN_API static Pointer<Font> GetFont(const Pointer<File>& file, uint32_t size);
80 
82  template <Concepts::ResourceT T = Resource>
83  [[nodiscard]]
84  static Pointer<T> Get(const Guid& guid);
85 
90  MOUNTAIN_API static void Rename(const std::string& name, const std::string& newName);
91 
96  MOUNTAIN_API static void Rename(const Pointer<Resource>& resource, const std::string& newName);
97 
101  template <Concepts::ResourceT T>
102  [[nodiscard]]
103  static List<Pointer<T>> FindAll();
104 
106  template <Concepts::ResourceT T>
107  static void FindAll(List<Pointer<T>>* result);
108 
115  template <Concepts::ResourceT T = Resource>
116  [[nodiscard]]
117  static Pointer<T> Find(const std::function<bool_t(Pointer<T>)>& predicate);
118 
125  template <Concepts::ResourceT T = Resource>
126  [[nodiscard]]
127  static List<Pointer<T>> FindAll(const std::function<bool_t(Pointer<T>)>& predicate);
128 
130  template <Concepts::ResourceT T>
131  static void FindAll(const std::function<bool_t(Pointer<T>)>& predicate, List<Pointer<T>>* result);
132 
134  template <Concepts::ResourceT T>
135  [[nodiscard]]
136  static bool_t IsResourceOfType(const std::string& name);
137 
139  MOUNTAIN_API static void Unload(const std::string& name);
140 
142  template <Concepts::ResourceT T>
143  static void Unload(const Pointer<T>& resource);
144 
146  MOUNTAIN_API static void UnloadAll();
147 
148  private:
149  MOUNTAIN_API static inline std::unordered_map<std::string, Pointer<Resource>> m_Resources;
150  MOUNTAIN_API static inline std::mutex m_ResourcesMutex;
151  MOUNTAIN_API static inline std::unordered_map<Guid, std::string> m_GuidMap;
152 
153  template <Concepts::ResourceT T>
154  static Pointer<T> AddNoCheck(std::string name);
155 
156  template <Concepts::ResourceT T>
157  static Pointer<T> LoadNoCheck(Pointer<File> file, bool_t loadInRhi = true);
158 
159  template <Concepts::ResourceT T>
160  [[nodiscard]]
161  static Pointer<T> GetNoCheck(const std::string& name);
162  };
163 }
164 
165 #include "Mountain/resource/resource_manager.inl"
static MOUNTAIN_API void LoadAll()
Creates one Resource for each FileManager entry.
Defines the Mountain::List class.
static MOUNTAIN_API void LoadAllBinaries()
Loads all embedded binary Resources created using std::embed.
Defines the Mountain::File class.
static MOUNTAIN_API Pointer< Font > LoadFont(const Pointer< File > &file, uint32_t size)
Creates the Font corresponding to the given file and loads it with the given size.
static MOUNTAIN_API void Rename(const std::string &name, const std::string &newName)
Renames the Resource with the given name to newName.
Defines the Mountain::Logger static class.
A dynamic array implementation. Wrapper around the std::vector class.
Definition: list.hpp:24
static MOUNTAIN_API void UnloadAll()
Unloads all stored Resources.
static List< Pointer< T > > FindAll()
Finds all Resource of type T.
Defines the Mountain::Resource class.
Static class used to add, load, get, or unload Resources.
static bool_t IsResourceOfType(const std::string &name)
Checks whether the given name corresponds to a Resource of type T.
Custom Mountain smart pointer. Represents both a std::shared_ptr and a std::weak_ptr.
static Pointer< T > Get(const std::string &name)
Returns the Resource that was either added or loaded using the given name.
static MOUNTAIN_API bool_t Contains(const std::string &name)
Checks whether the ResourceManager contains the specified Resource name.
static Pointer< T > Add(const std::string &name)
Creates the Resource corresponding to the given name without loading it.
static MOUNTAIN_API Pointer< Font > GetFont(const std::string &name, uint32_t size)
Returns the Font that was loaded using the given name and size.
static MOUNTAIN_API void Unload(const std::string &name)
Unloads the Resource with the given name.
Defines the Mountain::Pointer class.
static Pointer< T > Find(const std::function< bool_t(Pointer< T >)> &predicate)
Finds a specific Resource based on a predicate.
static Pointer< T > Load(const Pointer< File > &file, bool_t loadInInterface=true)
Creates the Resource corresponding to the given file and loads it.
Stands for Global Unique Identifier, it represents a unique ID that&#39;s used to link pointers during se...
Definition: guid.hpp:17
static MOUNTAIN_API bool_t IsBinary(const std::string &name)
Checks whether the given Resource name is an embedded binary Resource.
Contains all declarations of the Mountain Framework.
Definition: audio.hpp:22