jubilant-funicular
ResourceManager.cpp
1 #include "nta/ResourceManager.h"
2 #include "nta/Logger.h"
3 
4 namespace nta {
5  std::map<std::string, RawTexture> ResourceManager::m_textureMap;
6  std::map<std::pair<std::string, int>, TTF_Font*> ResourceManager::m_fontMap;
8  crvec2 dimensions) {
9  if (m_textureMap.find(imagePath) == m_textureMap.end()) {
10  Result<RawTexture> res = ImageLoader::readImage(imagePath, dimensions);
11  if (res.is_ok()) m_textureMap[imagePath] = res.get_data();
12  return res;
13  }
14  return Result<RawTexture>::new_ok(m_textureMap[imagePath]);
15  }
16  Result<TTF_Font*> ResourceManager::getFont(crstring fontPath, int fontSize) {
17  std::pair<std::string, int> key = std::make_pair(fontPath, fontSize);
18  if (m_fontMap.find(key) == m_fontMap.end()) {
19  Logger::writeToLog("Loading ttf font: " + fontPath + "...");
20  auto font = TTF_OpenFont(fontPath.c_str(), fontSize);
21  if (!font) {
22  auto err_str = "SDL_TTF failed to load font \"" + fontPath +
23  "\" with error: " + TTF_GetError();
24  auto err = Logger::writeErrorToLog(err_str, SDL_FAILURE);
25  return Result<TTF_Font*>::new_err(err);
26  }
27  Logger::writeToLog("Loaded font");
28  m_fontMap[key] = font;
29  }
30  return Result<TTF_Font*>::new_ok(m_fontMap[key]);
31  }
32  void ResourceManager::destroy() {
33  Logger::writeToLog("Destroying ResourceManager...");
35  for (auto& pair : m_textureMap) {
36  Logger::writeToLog("Deleting RawTexture: " + pair.first + "...");
37  delete[] pair.second.data;
38  Logger::writeToLog("Deleted RawTexture");
39  }
40  for (auto& pair : m_fontMap) {
41  Logger::writeToLog("Deleting TTF_Font*: " + pair.first.first + "...");
42  TTF_CloseFont(pair.second);
43  Logger::writeToLog("Deleted TTF_Font*");
44  }
45  m_textureMap.clear();
46  m_fontMap.clear();
48  Logger::writeToLog("Destroyed ResourceManager");
49  }
50 }
nta::ResourceManager::m_textureMap
static std::map< std::string, RawTexture > m_textureMap
a map for associating a texture with the name of its file
Definition: ResourceManager.h:19
nta::ImageLoader::readImage
static Result< GLTexture > readImage(crstring filePath, GLint minFilt, GLint magFilt, crvec2 dimensions)
loads in any image file
Definition: GLTexture.cpp:183
nta::Logger::writeToLog
static void writeToLog(crstring entry)
writes an entry in the log
Definition: Logger.cpp:17
nta::ResourceManager::getTexture
static Result< RawTexture > getTexture(crstring imagePath, crvec2 dimensions)
returns the resource with the given path, loading it if need be
Definition: ResourceManager.cpp:7
nta::Logger::unindent
static void unindent(size_t tab_size=TAB_SIZE)
unindents entries
Definition: Logger.cpp:46
nta::Result
Definition: Errors.h:74
nta::Result::is_ok
bool is_ok() const
Is this normal data.
Definition: Errors.h:119
nta
Definition: Animation2D.h:6
nta::Result::new_err
static Result new_err(const Error &err)
Definition: Errors.h:110
nta::Result::new_ok
static Result new_ok(const T &data)
Definition: Errors.h:101
nta::Result::get_data
T get_data() const
Get the data (only use if is_ok() returns true)
Definition: Errors.h:121
nta::Logger::writeErrorToLog
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
Definition: Logger.cpp:31
nta::Logger::indent
static void indent(size_t tab_size=TAB_SIZE)
indents entries
Definition: Logger.cpp:43