Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResourcesManager.cxx
Go to the documentation of this file.
1 #include "ResourcesManager.hxx"
2 
3 #include "WindowManager.hxx"
4 #include "TileManager.hxx"
5 #include "Settings.hxx"
6 #include "LOG.hxx"
7 #include "Exception.hxx"
8 #include "Filesystem.hxx"
9 
10 #include <SDL_image.h>
11 
12 #include "json.hxx"
13 
15 
17 
19 
20 void ResourcesManager::loadTexture(const std::string &id, const std::string &fileName)
21 {
22  m_surfaceMap[id] = createSurfaceFromFile(fileName);
24 }
25 
27 {
28  std::string jsonFileContent = fs::readFileAsString(Settings::instance().uiDataJSONFile.get());
29  const json uiDataJSON = json::parse(jsonFileContent, nullptr, false);
30 
31  // check if json file can be parsed
32  if (uiDataJSON.is_discarded())
33  throw ConfigurationError(TRACE_INFO "Error parsing JSON File " + Settings::instance().uiDataJSONFile.get());
34 
35  for (const auto &tileID : uiDataJSON.items())
36  {
37  for (auto it = uiDataJSON[tileID.key()].begin(); it != uiDataJSON[tileID.key()].end(); ++it)
38  {
39  m_uiTextureMap[tileID.key()][it.key()] = createTextureFromSurface(createSurfaceFromFile(it.value()));
40  }
41  }
42 }
43 
44 SDL_Texture *ResourcesManager::getUITexture(const std::string &uiElement)
45 {
46  std::string texture = "Texture_Default";
47  if (m_uiTextureMap[uiElement].find(texture) != m_uiTextureMap[uiElement].end())
48  {
49  return m_uiTextureMap[uiElement].at(texture);
50  }
51  if (m_uiTextureMap[uiElement].find("Texture_Default") != m_uiTextureMap[uiElement].end())
52  {
53  // If no texture is found, check if there's a default texture
54  return m_uiTextureMap[uiElement].at("Texture_Default");
55  }
56  throw UIError(TRACE_INFO "No texture found for " + uiElement);
57 }
58 
60 {
61  if (m_tileTextureMap.find(id) != m_tileTextureMap.end())
62  {
63  return m_tileTextureMap.at(id);
64  }
65  throw UIError(TRACE_INFO "No texture found for " + id);
66 }
67 
69 {
70  if (m_surfaceMap.find(id) != m_surfaceMap.end())
71  {
72  return m_surfaceMap.at(id);
73  }
74  throw UIError(TRACE_INFO "No surface found for " + id);
75 }
76 
78 {
79  string fName = fs::getBasePath() + fileName;
80 
81  if (!fs::fileExists(fName))
82  throw ConfigurationError(TRACE_INFO "File " + fName + " doesn't exist");
83 
84  SDL_Surface *surface = IMG_Load(fName.c_str());
85 
86  if (surface)
87  return surface;
88 
89  throw ConfigurationError(TRACE_INFO "Could not load Texture from file " + fName + ": " + IMG_GetError());
90 }
91 
92 SDL_Texture *ResourcesManager::createTextureFromSurface(SDL_Surface *surface)
93 {
94  SDL_Texture *texture = SDL_CreateTextureFromSurface(WindowManager::instance().getRenderer(), surface);
95 
96  if (texture)
97  return texture;
98 
99  throw UIError(TRACE_INFO "Texture could not be created! SDL Error: " + string{SDL_GetError()});
100 }
101 
103 {
104  for (const auto &it : m_surfaceMap)
105  {
106  SDL_FreeSurface(it.second);
107  }
108  m_surfaceMap.clear();
109 
110  for (const auto &it : m_tileTextureMap)
111  {
112  SDL_DestroyTexture(it.second);
113  }
114  m_tileTextureMap.clear();
115 
116  for (const auto &it : m_uiTextureMap)
117  {
118  for (const auto &ita : it.second)
119  {
120  SDL_DestroyTexture(ita.second);
121  }
122  }
123  m_uiTextureMap.clear();
124 }
125 
126 SDL_Color ResourcesManager::getColorOfPixelInSurface(const std::string &tileID, int x, int y)
127 {
128  SDL_Color Color{0, 0, 0, SDL_ALPHA_TRANSPARENT};
129  // create and initialize a variable within the condition
130  if (SDL_Surface *surface = getTileSurface(tileID); surface)
131  {
132  const int bpp = surface->format->BytesPerPixel;
133  Uint8 *p = &static_cast<Uint8 *>(surface->pixels)[y * surface->pitch + x * bpp];
134  const Uint32 pixel = *reinterpret_cast<Uint32 *>(p);
135 
136  SDL_GetRGBA(pixel, surface->format, &Color.r, &Color.g, &Color.b, &Color.a);
137  }
138 
139  return Color;
140 }
TRACE_INFO
#define TRACE_INFO
Definition: Exception.hxx:12
ConfigurationError
A configuration error.
Definition: Exception.hxx:36
ResourcesManager::getTileSurface
SDL_Surface * getTileSurface(const std::string &id)
Definition: ResourcesManager.cxx:68
UIError
A UI-related error occured.
Definition: Exception.hxx:76
ResourcesManager::ResourcesManager
ResourcesManager()
Definition: ResourcesManager.cxx:16
fileExists
bool fileExists(const std::string &filePath)
Check if a file (or folder) exists.
Definition: Filesystem.cxx:62
TileManager.hxx
ResourcesManager::getTileTexture
SDL_Texture * getTileTexture(const std::string &id)
Definition: ResourcesManager.cxx:59
LOG.hxx
ResourcesManager::m_tileTextureMap
std::unordered_map< std::string, SDL_Texture * > m_tileTextureMap
Definition: ResourcesManager.hxx:59
ResourcesManager::getUITexture
SDL_Texture * getUITexture(const std::string &uiElement)
retrieves texture for a tileID
Definition: ResourcesManager.cxx:44
ResourcesManager::loadUITexture
void loadUITexture()
Load Texture in to Texture Map, if an entry for this tile ID does not yet exist.
Definition: ResourcesManager.cxx:26
ResourcesManager::m_surfaceMap
std::unordered_map< std::string, SDL_Surface * > m_surfaceMap
Definition: ResourcesManager.hxx:60
readFileAsString
std::string readFileAsString(const std::string &fileName, bool binaryMode)
Read contents from a file as string.
Definition: Filesystem.cxx:12
WindowManager.hxx
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
ResourcesManager.hxx
Filesystem.hxx
Settings.hxx
ResourcesManager::getColorOfPixelInSurface
SDL_Color getColorOfPixelInSurface(const std::string &tileID, int x, int y)
Get the Color Of Pixel In Surface object at a given coordinate.
Definition: ResourcesManager.cxx:126
ResourcesManager::flush
void flush()
Delete everything. Should be called from the destuctor only.
Definition: ResourcesManager.cxx:102
ResourcesManager::createTextureFromSurface
SDL_Texture * createTextureFromSurface(SDL_Surface *surface)
Definition: ResourcesManager.cxx:92
ResourcesManager::loadTexture
void loadTexture(const std::string &id, const std::string &fileName)
Definition: ResourcesManager.cxx:20
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
json
nlohmann::json json
Definition: ResourcesManager.cxx:14
Exception.hxx
ResourcesManager::m_uiTextureMap
std::unordered_map< std::string, std::unordered_map< std::string, SDL_Texture * > > m_uiTextureMap
Definition: ResourcesManager.hxx:57
json
nlohmann::json json
Definition: Settings.hxx:12
string
std::string string
Definition: AudioConfig.hxx:14
ResourcesManager::createSurfaceFromFile
SDL_Surface * createSurfaceFromFile(const std::string &fileName)
Definition: ResourcesManager.cxx:77
ResourcesManager::~ResourcesManager
~ResourcesManager()
Definition: ResourcesManager.cxx:18