jubilant-funicular
ContextData.cpp
1 #include "nta/ContextData.h"
2 #include "nta/ResourceManager.h"
3 #include "nta/Logger.h"
4 
5 namespace nta {
8  std::string full_path = (m_glsl_folder + progPath).to_string();
9  if (m_glslMap.find(full_path) == m_glslMap.end()) {
10  m_glslMap[full_path].compileShaders(full_path);
11  }
12  return &m_glslMap[full_path];
13  }
14  GLSLProgram* ContextData::getGLSLProgram(crstring name, const utils::Path& vert, const utils::Path& frag) {
15  std::string full_vert_path = (m_glsl_folder + vert).to_string(),
16  full_frag_path = (m_glsl_folder + frag).to_string();
17  if (m_glslMap.find(name) == m_glslMap.end()) {
18  m_glslMap[name].compileShaders(full_vert_path, full_frag_path);
19  }
20  return &m_glslMap[name];
21  }
22  Result<GLTexture> ContextData::getTexture(const utils::Path& path, crvec2 dimensions) {
23  utils::Path full_path = m_texture_folder + path;
24  if (m_textureMap.find(full_path) == m_textureMap.end()) {
25  std::string path_str = full_path.to_string();
26  auto raw = ResourceManager::getTexture(path_str, dimensions);
27  // I really like this bit
28  return raw.map<GLTexture>([&](const RawTexture& raw) {
29  Logger::writeToLog("RawTexture \"" + path_str + "\" doesn't have a GLTexture in this context.");
30  return m_textureMap[full_path] = GLTexture(raw);
31  });
32  }
33  return Result<GLTexture>::new_ok(m_textureMap[full_path]);
34  }
36  if (m_textureFiles.find(tex) == m_textureFiles.end()) {
37  auto err = Logger::writeErrorToLog("tex not found in ContextData::m_textureFiles", MISSING_RESOURCE);
38  return Result<utils::Path>::new_err(err);
39  }
41  }
43  std::pair<utils::Path, int> key = std::make_pair(m_font_folder + fontPath, fontSize);
44  if (m_fontMap.find(key) == m_fontMap.end()) {
45  std::string path_str = key.first.to_string();
46  auto ttf_font = ResourceManager::getFont(path_str, fontSize);
47  if (ttf_font.is_err()) return ttf_font.convert_error<SpriteFont*>();
48  Logger::writeToLog("TTF_Font \"" + path_str + "\" (size: " + utils::to_string(fontSize) + ") doesn't have a SpriteFont in this context.");
49  m_fontMap[key].init(ttf_font.unwrap());
50  }
52  }
54  Logger::writeToLog("Reloading shaders...");
56  for (auto& pair : m_glslMap) {
57  pair.second.reload();
58  }
60  Logger::writeToLog("Reloaded shaders");
61  }
64  }
66  Logger::writeToLog("Destroying ContextData...");
68  for (auto& pair : m_glslMap) {
69  Logger::writeToLog("Deleting GLSLProgram \"" + pair.first + "\"...");
70  pair.second.destroy();
71  Logger::writeToLog("Deleted Program");
72  }
73  for (auto& pair : m_textureMap) {
74  Logger::writeToLog("Deleting GLTexture \"" + pair.first.to_string() + "\"...");
75  pair.second.destroy();
76  Logger::writeToLog("Deleted GLTexture");
77  }
78  for (auto& pair : m_fontMap) {
79  Logger::writeToLog("Deleting SpriteFont \"" + pair.first.first.to_string() + "\"...");
80  pair.second.destroy();
81  Logger::writeToLog("Deleted SpriteFont");
82  }
83  m_glslMap.clear();
84  m_textureMap.clear();
85  m_fontMap.clear();
87  Logger::writeToLog("Destroyed ContextData");
88  }
89 }
nta::GLSLProgram
represents a program written in GLSL comprised of a vertex shader and a fragment shader
Definition: GLSLProgram.h:14
nta::ContextData::destroy
void destroy()
Deletes all data (does not reset folder paths)
Definition: ContextData.cpp:65
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
utils::Path
A (case-sensitive) Path in a file system.
Definition: Path.h:16
nta::ContextData::reloadShaders
void reloadShaders()
Reloads all known shaders.
Definition: ContextData.cpp:53
nta::Logger::unindent
static void unindent(size_t tab_size=TAB_SIZE)
unindents entries
Definition: Logger.cpp:46
nta::ContextData::m_textureMap
std::map< utils::Path, GLTexture > m_textureMap
a map file names to textures
Definition: ContextData.h:22
nta::Result
Definition: Errors.h:74
nta::ContextData::m_texture_folder
utils::Path m_texture_folder
Folder containing textures.
Definition: ContextData.h:28
nta
Definition: Animation2D.h:6
nta::Result::new_err
static Result new_err(const Error &err)
Definition: Errors.h:110
nta::ContextData::getTextureFile
Result< utils::Path > getTextureFile(GLTexture tex)
Gets the name of the file used the create tex.
Definition: ContextData.cpp:35
nta::Result::new_ok
static Result new_ok(const T &data)
Definition: Errors.h:101
nta::ContextData::m_glslMap
std::map< std::string, GLSLProgram > m_glslMap
Collection of GLSLProgram.
Definition: ContextData.h:20
nta::GLTexture
represents a texture (tied to a specific GL context)
Definition: GLTexture.h:24
nta::ContextData::getTexture
Result< GLTexture > getTexture(const utils::Path &path, crvec2 dimensions=glm::vec2(0))
Gets a GLTexture representing the image at the given path.
Definition: ContextData.cpp:22
nta::ContextData::m_textureFiles
std::map< GLTexture, utils::Path > m_textureFiles
Inverse map to m_textureMap.
Definition: ContextData.h:24
nta::ContextData::getSpriteFont
Result< SpriteFont * > getSpriteFont(const utils::Path &fontPath, int fontSize=32)
Gets SpriteFont created from the given font with the given size.
Definition: ContextData.cpp:42
nta::ContextData::m_glsl_folder
utils::Path m_glsl_folder
Folder containing shaders.
Definition: ContextData.h:30
nta::ContextData::getGLSLProgram
GLSLProgram * getGLSLProgram(const utils::Path &progPath)
Definition: ContextData.cpp:6
nta::ContextData::m_font_folder
utils::Path m_font_folder
Folder containing fonts.
Definition: ContextData.h:32
nta::utils::to_string
std::string to_string(const T &input, std::size_t precision=0)
converts input to a std::string
Definition: utils.h:36
nta::Logger::writeErrorToLog
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
Definition: Logger.cpp:31
nta::SpriteFont
Loads in a .ttf file, creates a font texture from it which is then used to render text.
Definition: SpriteFont.h:55
nta::RawTexture
Definition: GLTexture.h:13
nta::Logger::indent
static void indent(size_t tab_size=TAB_SIZE)
indents entries
Definition: Logger.cpp:43
nta::ContextData::m_fontMap
std::map< std::pair< utils::Path, int >, SpriteFont > m_fontMap
Map front (font name, font size) -> SpriteFont.
Definition: ContextData.h:26
nta::ContextData::reloadTextures
void reloadTextures()
Reloads all known texture.
Definition: ContextData.cpp:62