Arcade Shooter
ResourceManager.h
1 
2 /* ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
3  ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
4  █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
5  ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
6  ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
7  ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
8  /vvvvvvvvvvvvvvvvvvv \=========================================,
9  `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
10  Katana Engine \/ © 2012 - Shuriken Studios LLC */
11 
12 #pragma once
13 
14 namespace KatanaEngine
15 {
18  {
19 
20  public:
21 
22  ResourceManager() { m_nextResourceID = 0; }
23 
26  void SetContentPath(const std::string &path) { m_contentPath = path; }
27 
30  {
31  std::map<std::string, Resource *>::iterator it;
32  for (it = m_resources.begin(); it != m_resources.end(); ++it)
33  {
34  Resource *resource = it->second;
35  delete resource;
36  }
37  m_resources.clear();
38 
39  std::vector<Resource *>::iterator cloneIt;
40  for (cloneIt = m_clones.begin(); cloneIt != m_clones.end(); ++cloneIt)
41  {
42  delete *cloneIt;
43  }
44  m_clones.clear();
45  }
46 
54  template <typename T>
55  T *Load(const std::string &path, const bool cache = true, const bool appendContentPath = true)
56  {
57  if (m_resources.find(path) != m_resources.end())
58  {
59  T *pResource = dynamic_cast<T *>(m_resources[path]);
60 
61  if (pResource->IsCloneable())
62  {
63  T *pClone = dynamic_cast<T*>(pResource->Clone());
64 
65  pClone->m_id = m_nextResourceID;
66  m_nextResourceID++;
67 
68  m_clones.push_back(pClone);
69 
70  return pClone;
71  }
72 
73  return pResource;
74  }
75 
76  T *pT = new T;
77 
78  pT->m_pResourceManager = this;
79 
80  std::string fullPath;
81 
82  if (appendContentPath)
83  {
84  fullPath = m_contentPath;
85  fullPath.append(path);
86  }
87  else fullPath = path;
88 
89  if (pT->Load(fullPath, this))
90  {
91  if (cache) m_resources[path] = pT;
92 
93  pT->m_id = m_nextResourceID;
94  m_nextResourceID++;
95 
96  return pT;
97  }
98 
99  delete pT;
100  return nullptr;
101  }
102 
103  private:
104 
105  std::map<std::string, Resource *> m_resources;
106 
107  std::vector<Resource *> m_clones;
108 
109  std::string m_contentPath;
110 
111  unsigned short m_nextResourceID;
112 
113  };
114 }
void SetContentPath(const std::string &path)
Sets the location of the folder where game resources are stored.
Definition: ResourceManager.h:26
void UnloadAllResources()
Unloads all game resources.
Definition: ResourceManager.h:29
Base class for all resource types to be managed by the ResourceManager class.
Definition: Resource.h:19
T * Load(const std::string &path, const bool cache=true, const bool appendContentPath=true)
Load and manage a resource.
Definition: ResourceManager.h:55
Loads and manages the lifespan of objects from external files.
Definition: ResourceManager.h:17
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition: Animation.cpp:14