BARE2D
ResourceManager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Cache.hpp"
4 
5 #include "Font.hpp"
6 #include "LuaScript.hpp"
7 #include "Music.hpp"
8 #include "MutableTexture.hpp"
9 #include "ShaderProgram.hpp"
10 #include "Sound.hpp"
11 #include "Texture.hpp"
12 
13 namespace BARE2D {
14 
15  /**
16  * @class ResourceManager
17  * @brief The resource manager manages resources. Groundbreaking news, I know. In short, the resource manager loads and
18  * manages Textures, Sounds, Scripts, Fonts, etc.
19  */
21  public:
22  /**
23  * @brief Loads some shaders. Combines both to give a full shader program. Does not cache.
24  * @param vertShaderPath The path to the vertex shader
25  * @param fragShaderPath The path to the fragment shader
26  * @return A GLSL program of type ShaderProgram instance which is the compiled version of vertShader + fragShader.
27  */
28  static ShaderProgram loadShaders(std::string& vertShaderPath, std::string& fragShaderPath);
29 
30  /**
31  * @brief Loads some shaders from their source. Does not cache
32  * @param vertShaderSource The source to compile from.
33  * @param fragShaderSource The source to compile from.
34  * @return
35  */
36  static ShaderProgram loadShadersFromSource(std::string& vertShaderSource, std::string& fragShaderSource);
37 
38  /**
39  * @brief Loads a texture if it isn't already in the cache.
40  * @param texturePath A path to the texture to be loaded, including the actual name of the texture
41  * ("assets/texture.png" for example)
42  * @return A copy of the texture, whether it was just loaded or it was taken from the cache.
43  */
44  static Texture loadTexture(std::string& texturePath);
45 
46  /**
47  * @brief Creates a new mutable texture, or replaces one that exists with a new texture.
48  * @param width The width of the texture
49  * @param height The height of the texture.
50  * @param textureName The identifier for the cache
51  * @param minMagFilter The GLenum which corresponds to how minimizing and magnifying is going to be handled. Generally, GL_LINEAR or GL_NEAREST. Defaults to GL_LINEAR
52  * @param channels The number of colour channels. For RGBA, it's 4. For just red, it's one. etc. etc. Defaults to 4.
53  * @param format The format of texture data. Generally GL_RGBA, GL_RED, etc. Defaults to GL_RGBA.
54  * @return A pointer to the mutable texture.
55  */
56  static MutableTexture* createMutableTexture(std::string& textureName,
57  unsigned int width,
58  unsigned int height,
59  GLenum minMagFilter = GL_LINEAR,
60  unsigned int channels = 4,
61  GLenum format = GL_RGBA);
62 
63  /**
64  * @brief Gets a texture from the cache, or creates a new, empty texture
65  * @param textureName The "filepath" of the texture, this is used to identify the texture in the cache.
66  * @return A pointer to the mutable texture. Nullptr if none can be found. Must be set first.
67  */
68  static MutableTexture* loadMutableTexture(std::string& textureName);
69 
70  /**
71  * @brief Loads a sound from the filepath given from the cache or from the file if the cache doesn't contain it.
72  * @param soundPath The path to load the sound from.
73  * @return An instance of the sound.
74  */
75  static Sound loadSound(std::string& soundPath);
76 
77  /**
78  * @brief Loads some music from the filepath given from the cache or from the file if the cache doesn't contain it.
79  * @param musicPath The path to load the music from
80  * @return An instance of the music.
81  */
82  static Music loadMusic(std::string& musicPath);
83 
84  /**
85  * @brief Loads a script from the filepath given from the cache or from the file if it's not already in the cache.
86  * @param scriptPath The path to load the script from.
87  * @return An instance of the script.
88  */
89  static LuaScript loadScript(std::string& scriptPath);
90 
91  /**
92  * @brief Creates and caches a script from the given source code.
93  * @param scriptSource The actual source code (Lua) of the script
94  * @param name The name of the script
95  * @return The name of the script, so that it can be loaded from the regular cache.
96  */
97  static LuaScript loadScriptFromSource(std::string& scriptSource, std::string name);
98 
99  /**
100  * @brief Loads a font to the cache.
101  * @param fontPath The path to load the font from
102  * @param size The size of the font to load it as.
103  * @return An instance of the font.
104  */
105  static Font loadFont(std::string& fontPath, int size);
106 
107  /**
108  * @brief Clears the various caches. This is useful for debugging.
109  */
110  static void clearCaches();
111 
112  /**
113  * @brief Changes the prefix that will be prepended to all paths when resources are loaded.
114  * @param prefix The path to prepend on any load.
115  */
116  static void setAssetsPathPrefix(std::string prefix);
117 
118  /**
119  * @brief Returns the assets path prefix. Pretty simple.
120  */
121  static std::string getAssetsPathPrefix();
122 
123  /**
124  * @brief Changes the prefix that will be prepended to all texture paths when they're loaded.
125  * @param prefix The path to prepend on texture loads.
126  */
127  static void setTexturePathPrefix(std::string prefix);
128 
129  private:
130  static std::string m_assetsPathPrefix;
131  static std::string m_texturePathPrefix;
132 
139  };
140 
141 } // namespace BARE2D
BARE2D::ResourceManager::setAssetsPathPrefix
static void setAssetsPathPrefix(std::string prefix)
Changes the prefix that will be prepended to all paths when resources are loaded.
Definition: ResourceManager.cpp:294
BARE2D::Font
Essentially just a wrapper for the SDL TTF_Font type.
Definition: Font.hpp:29
BARE2D::Sound
Definition: Sound.hpp:7
BARE2D::ResourceManager::m_scripts
static Cache< std::string, LuaScript > * m_scripts
Definition: ResourceManager.hpp:137
BARE2D
Definition: App.cpp:13
BARE2D::ResourceManager::m_sounds
static Cache< std::string, Sound > * m_sounds
Definition: ResourceManager.hpp:135
vertShaderSource
const char * vertShaderSource
Definition: DebugRenderer.cpp:16
BARE2D::ResourceManager::m_textures
static Cache< std::string, Texture > * m_textures
Definition: ResourceManager.hpp:133
BARE2D::ResourceManager::m_assetsPathPrefix
static std::string m_assetsPathPrefix
Definition: ResourceManager.hpp:130
BARE2D::Music
Definition: Music.hpp:7
BARE2D::Texture
The texture struct holds very basic stuff - the filepath, width, height, and ID,.
Definition: Texture.hpp:13
BARE2D::ResourceManager::loadSound
static Sound loadSound(std::string &soundPath)
Loads a sound from the filepath given from the cache or from the file if the cache doesn't contain it...
Definition: ResourceManager.cpp:161
BARE2D::ResourceManager::createMutableTexture
static MutableTexture * createMutableTexture(std::string &textureName, unsigned int width, unsigned int height, GLenum minMagFilter=GL_LINEAR, unsigned int channels=4, GLenum format=GL_RGBA)
Creates a new mutable texture, or replaces one that exists with a new texture.
Definition: ResourceManager.cpp:110
BARE2D::ResourceManager::loadShadersFromSource
static ShaderProgram loadShadersFromSource(std::string &vertShaderSource, std::string &fragShaderSource)
Loads some shaders from their source. Does not cache.
Definition: ResourceManager.cpp:32
Cache.hpp
BARE2D::ResourceManager::clearCaches
static void clearCaches()
Clears the various caches. This is useful for debugging.
Definition: ResourceManager.cpp:283
BARE2D::LuaScript
Definition: LuaScript.hpp:14
BARE2D::ResourceManager::loadScript
static LuaScript loadScript(std::string &scriptPath)
Loads a script from the filepath given from the cache or from the file if it's not already in the cac...
Definition: ResourceManager.cpp:211
BARE2D::MutableTexture
A child of Texture which allows (and gives helpful functions for) mutation.
Definition: MutableTexture.hpp:13
Music.hpp
fragShaderSource
const char * fragShaderSource
Definition: DebugRenderer.cpp:7
BARE2D::ResourceManager::loadMutableTexture
static MutableTexture * loadMutableTexture(std::string &textureName)
Gets a texture from the cache, or creates a new, empty texture.
Definition: ResourceManager.cpp:153
BARE2D::Cache
This is a skeleton cache class. This can only be used by the ResourceManager or other classes who act...
Definition: Cache.hpp:15
BARE2D::ResourceManager::m_mutableTextures
static Cache< std::string, MutableTexture > * m_mutableTextures
Definition: ResourceManager.hpp:134
Font.hpp
ShaderProgram.hpp
BARE2D::ResourceManager::loadShaders
static ShaderProgram loadShaders(std::string &vertShaderPath, std::string &fragShaderPath)
Loads some shaders. Combines both to give a full shader program. Does not cache.
Definition: ResourceManager.cpp:22
BARE2D::ResourceManager::m_music
static Cache< std::string, Music > * m_music
Definition: ResourceManager.hpp:136
BARE2D::ResourceManager::loadTexture
static Texture loadTexture(std::string &texturePath)
Loads a texture if it isn't already in the cache.
Definition: ResourceManager.cpp:41
MutableTexture.hpp
Sound.hpp
BARE2D::ShaderProgram
The ShaderProgram is a GLSL program which combines two shaders - the vertex shader and the fragment s...
Definition: ShaderProgram.hpp:14
BARE2D::ResourceManager::getAssetsPathPrefix
static std::string getAssetsPathPrefix()
Returns the assets path prefix. Pretty simple.
Definition: ResourceManager.cpp:299
BARE2D::ResourceManager::loadScriptFromSource
static LuaScript loadScriptFromSource(std::string &scriptSource, std::string name)
Creates and caches a script from the given source code.
Definition: ResourceManager.cpp:244
BARE2D::ResourceManager::loadMusic
static Music loadMusic(std::string &musicPath)
Loads some music from the filepath given from the cache or from the file if the cache doesn't contain...
Definition: ResourceManager.cpp:186
BARE2D::ResourceManager
The resource manager manages resources. Groundbreaking news, I know. In short, the resource manager l...
Definition: ResourceManager.hpp:20
BARE2D::ResourceManager::setTexturePathPrefix
static void setTexturePathPrefix(std::string prefix)
Changes the prefix that will be prepended to all texture paths when they're loaded.
Definition: ResourceManager.cpp:304
BARE2D::ResourceManager::m_texturePathPrefix
static std::string m_texturePathPrefix
Definition: ResourceManager.hpp:131
BARE2D::ResourceManager::loadFont
static Font loadFont(std::string &fontPath, int size)
Loads a font to the cache.
Definition: ResourceManager.cpp:263
Texture.hpp
BARE2D::ResourceManager::m_fonts
static Cache< std::string, Font > * m_fonts
Definition: ResourceManager.hpp:138
LuaScript.hpp
This is the basic Lua script wrapper - it is what the end-user will create and add to the queue....