Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
ResourceManager.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Singleton.hpp"
4 #include "Resource.h"
5 #include "FlingTypes.h" // Guid
6 
7 #include <fstream>
8 #include <vector>
9 #include <map>
10 
11 namespace Fling
12 {
23  class ResourceManager : public Singleton<ResourceManager>
24  {
25  public:
26 
27  virtual void Init() override;
28 
29  virtual void Shutdown() override;
30 
31  template<class T, class ...ARGS>
32  static std::shared_ptr<T> LoadResource(Guid t_ID, ARGS&& ... args)
33  {
34  return ResourceManager::Get().LoadResourceImpl<T>(t_ID, std::forward<ARGS>(args)...);
35  }
36 
37  template <class T>
38  std::shared_ptr<T> GetResourceOfType(Guid_Handle t_ID) const;
39 
46  std::shared_ptr<Resource> GetResource(Guid_Handle t_ID) const;
47 
52  bool IsLoaded(Guid_Handle t_ID) const;
53 
54  private:
55 
56  template<class T, class ...ARGS>
57  std::shared_ptr<T> LoadResourceImpl(Guid t_ID, ARGS&& ... args);
58 
59  typedef std::map<Fling::Guid_Handle, std::shared_ptr<Resource>>::iterator ResourceMapIt;
60  typedef std::map<Fling::Guid_Handle, std::shared_ptr<Resource>>::const_iterator ResourceMapConstIt;
61 
63  std::map<Fling::Guid_Handle, std::shared_ptr<Resource>> m_ResourceMap;
64  };
65 
66 
67  template<class T, class ...ARGS>
68  inline std::shared_ptr<T> ResourceManager::LoadResourceImpl(Guid t_ID, ARGS&& ... args)
69  {
70  // If this resource exists already then just return that
71  if (std::shared_ptr<T> Existing = GetResourceOfType<T>(t_ID))
72  {
73  return Existing;
74  }
75 
76  // Create a new resource of type T and return it
77  // Every resource type has an explict CTOR whose first arg has to be an ID
78  std::shared_ptr<Resource> NewResource = std::make_shared<T>(t_ID, std::forward<ARGS>(args)...);
79 
80  // Keep track of this resource in the map
81  m_ResourceMap[t_ID] = NewResource;
82  return std::static_pointer_cast<T>( NewResource );
83  }
84 
85  template<class T>
86  inline std::shared_ptr<T> ResourceManager::GetResourceOfType(Guid_Handle t_ID) const
87  {
88  if (std::shared_ptr<Resource> Res = GetResource(t_ID))
89  {
90  return std::static_pointer_cast<T>(Res);
91  }
92  return nullptr;
93  }
94 } // namespace Fling
std::map< Fling::Guid_Handle, std::shared_ptr< Resource > > m_ResourceMap
Map of currently loaded resources.
Definition: ResourceManager.h:63
std::shared_ptr< T > LoadResourceImpl(Guid t_ID, ARGS &&... args)
Definition: ResourceManager.h:68
std::shared_ptr< T > GetResourceOfType(Guid_Handle t_ID) const
Definition: ResourceManager.h:86
static std::shared_ptr< T > LoadResource(Guid t_ID, ARGS &&... args)
Definition: ResourceManager.h:32
entt::hashed_string::hash_type Guid_Handle
Definition: FlingTypes.h:22
Class that can have only one instance.
Definition: Singleton.hpp:11
std::map< Fling::Guid_Handle, std::shared_ptr< Resource > >::const_iterator ResourceMapConstIt
Definition: ResourceManager.h:60
std::shared_ptr< Resource > GetResource(Guid_Handle t_ID) const
Get the already loaded resouce with this Guid.
Definition: ResourceManager.cpp:33
std::map< Fling::Guid_Handle, std::shared_ptr< Resource > >::iterator ResourceMapIt
Definition: ResourceManager.h:59
entt::hashed_string Guid
Definition: FlingTypes.h:21
The resource manager handles loading of files off disk.
Definition: ResourceManager.h:23
static ResourceManager & Get()
Definition: Singleton.hpp:36
virtual void Init() override
Definition: ResourceManager.cpp:6
Definition: Engine.h:13
bool IsLoaded(Guid_Handle t_ID) const
Check if there is a resource with this ID loaded or not.
Definition: ResourceManager.cpp:44
virtual void Shutdown() override
Definition: ResourceManager.cpp:26