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