SDL Breakout Engine  0.1.0
An implementation of a Breakout game using sdl, implemented with efficient resource management.
ResourceManager.hpp
Go to the documentation of this file.
1 #ifndef RESOURCE_MANAGER_HPP
2 #define RESOURCE_MANAGER_HPP
3 
4 #include <SDL2/SDL_ttf.h>
5 #include <SDL2/SDL_image.h>
6 #include <map>
7 #include <string>
8 #include <memory>
9 #include <iterator>
10 #include <iostream>
11 
15 struct Sprite
16 {
17  std::shared_ptr<SDL_Surface> spriteSheet;
18  std::shared_ptr<SDL_Texture> texture;
19 
24  {
25  SDL_Log("Sprite being freed");
26  }
27 };
28 
33 {
39  TextureWrapper(const std::shared_ptr<SDL_Texture> &texture)
40  {
41  mTexture = texture;
42  }
43 
47  SDL_Texture* get() const
48  {
49  return mTexture.get();
50  }
51 
52 private:
53  std::shared_ptr<SDL_Texture> mTexture;
54 };
55 
56 template <typename Derived, class T>
57 
63 {
64 public:
65 
69  int shutDown()
70  {
71  while (resourceMap.begin() != resourceMap.end())
72  {
73  resourceMap.erase(resourceMap.begin());
74  }
75 
76  while (fileMap.begin() != fileMap.end())
77  {
78  fileMap.erase(fileMap.begin());
79  }
80 
81  return 1;
82  }
83 
89  std::shared_ptr<T> GetResource(const std::string &resourceName)
90  {
91  auto res = Find(resourceName);
92 
93  if (res)
94  return res->first;
95  else
96  return NULL;
97  }
98 
104  std::shared_ptr<T> Load(const std::string &path)
105  {
106  return static_cast<Derived *>(this)->Load(path);
107  }
108 
115  bool AddResource(const std::string &resourceName)
116  {
117  auto res = Find(resourceName);
118 
119  if (res)
120  {
121  ++res->second;
122  return true;
123  }
124 
125  auto filePath = fileMap.find(resourceName);
126  if (filePath == fileMap.end())
127  return false;
128 
129  std::shared_ptr<T> resource = Load(filePath->second);
130 
131  if (!resource)
132  return false;
133 
134  resourceMap.emplace(resourceName, std::make_pair(resource, 1));
135 
136  return true;
137  }
138 
143  {
144  for (auto const &[key, val] : fileMap)
145  {
146  printf("loading resource: %s, %s\n", key.c_str(), val.c_str());
147  if (!AddResource(key))
148  return false;
149  }
150  return true;
151  }
152 
153 protected:
154 
155  std::map<std::string, std::pair<std::shared_ptr<T>, int>> resourceMap;
156  std::map<std::string, std::string> fileMap;
157 
160  void operator=(ResourceManager const &);
161 
162 private:
163  std::pair<std::shared_ptr<T>, int> *Find(const std::string &resourceName)
164  {
165  auto itr = resourceMap.find(resourceName);
166 
167  if (itr != resourceMap.end())
168  {
169  return &itr->second;
170  }
171  else
172  {
173  return NULL;
174  }
175  }
176 };
177 
178 #endif
void operator=(ResourceManager const &)
Allow subclasses to assign to a resource manager, but subclasses must ensure singleton functionality...
bool AddAllResource()
Definition: ResourceManager.hpp:142
bool AddResource(const std::string &resourceName)
Definition: ResourceManager.hpp:115
std::shared_ptr< T > Load(const std::string &path)
Definition: ResourceManager.hpp:104
std::shared_ptr< T > GetResource(const std::string &resourceName)
Definition: ResourceManager.hpp:89
std::map< std::string, std::string > fileMap
stores location of resources
Definition: ResourceManager.hpp:156
std::map< std::string, std::pair< std::shared_ptr< T >, int > > resourceMap
Internal resource mapping inherited by manager implementations.
Definition: ResourceManager.hpp:155
int shutDown()
Definition: ResourceManager.hpp:69
Definition: ResourceManager.hpp:62
SDL_Texture * get() const
Definition: ResourceManager.hpp:47
TextureWrapper(const std::shared_ptr< SDL_Texture > &texture)
Definition: ResourceManager.hpp:39
Definition: ResourceManager.hpp:32
~Sprite()
Definition: ResourceManager.hpp:23
std::shared_ptr< SDL_Texture > texture
the full texture of the sprite bitmap.
Definition: ResourceManager.hpp:18
ResourceManager()
Definition: ResourceManager.hpp:158
std::shared_ptr< SDL_Surface > spriteSheet
the spritesheet to create the texture from.
Definition: ResourceManager.hpp:17
Definition: ResourceManager.hpp:15