BARE2D
BARE2D::ResourceManager Class Reference

The resource manager manages resources. Groundbreaking news, I know. In short, the resource manager loads and manages Textures, Sounds, Scripts, Fonts, etc. More...

#include <ResourceManager.hpp>

+ Collaboration diagram for BARE2D::ResourceManager:

Static Public Member Functions

static ShaderProgram loadShaders (std::string &vertShaderPath, std::string &fragShaderPath)
 Loads some shaders. Combines both to give a full shader program. Does not cache. More...
 
static ShaderProgram loadShadersFromSource (std::string &vertShaderSource, std::string &fragShaderSource)
 Loads some shaders from their source. Does not cache. More...
 
static Texture loadTexture (std::string &texturePath)
 Loads a texture if it isn't already in the cache. More...
 
static MutableTexturecreateMutableTexture (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. More...
 
static MutableTextureloadMutableTexture (std::string &textureName)
 Gets a texture from the cache, or creates a new, empty texture. More...
 
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. More...
 
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 it. More...
 
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 cache. More...
 
static LuaScript loadScriptFromSource (std::string &scriptSource, std::string name)
 Creates and caches a script from the given source code. More...
 
static Font loadFont (std::string &fontPath, int size)
 Loads a font to the cache. More...
 
static void clearCaches ()
 Clears the various caches. This is useful for debugging. More...
 
static void setAssetsPathPrefix (std::string prefix)
 Changes the prefix that will be prepended to all paths when resources are loaded. More...
 
static std::string getAssetsPathPrefix ()
 Returns the assets path prefix. Pretty simple. More...
 
static void setTexturePathPrefix (std::string prefix)
 Changes the prefix that will be prepended to all texture paths when they're loaded. More...
 

Static Private Attributes

static std::string m_assetsPathPrefix = ""
 
static std::string m_texturePathPrefix = ""
 
static Cache< std::string, Texture > * m_textures = new Cache<std::string, Texture>()
 
static Cache< std::string, MutableTexture > * m_mutableTextures = new Cache<std::string, MutableTexture>()
 
static Cache< std::string, Sound > * m_sounds = new Cache<std::string, Sound>()
 
static Cache< std::string, Music > * m_music = new Cache<std::string, Music>()
 
static Cache< std::string, LuaScript > * m_scripts = new Cache<std::string, LuaScript>()
 
static Cache< std::string, Font > * m_fonts = new Cache<std::string, Font>()
 

Detailed Description

The resource manager manages resources. Groundbreaking news, I know. In short, the resource manager loads and manages Textures, Sounds, Scripts, Fonts, etc.

Definition at line 20 of file ResourceManager.hpp.

Member Function Documentation

◆ clearCaches()

void BARE2D::ResourceManager::clearCaches ( )
static

Clears the various caches. This is useful for debugging.

Definition at line 283 of file ResourceManager.cpp.

References BARE2D::Logger::getInstance(), BARE2D::Logger::log(), m_fonts, m_mutableTextures, m_scripts, m_sounds, and m_textures.

+ Here is the call graph for this function:

◆ createMutableTexture()

MutableTexture * BARE2D::ResourceManager::createMutableTexture ( std::string &  textureName,
unsigned int  width,
unsigned int  height,
GLenum  minMagFilter = GL_LINEAR,
unsigned int  channels = 4,
GLenum  format = GL_RGBA 
)
static

Creates a new mutable texture, or replaces one that exists with a new texture.

Parameters
widthThe width of the texture
heightThe height of the texture.
textureNameThe identifier for the cache
minMagFilterThe GLenum which corresponds to how minimizing and magnifying is going to be handled. Generally, GL_LINEAR or GL_NEAREST. Defaults to GL_LINEAR
channelsThe number of colour channels. For RGBA, it's 4. For just red, it's one. etc. etc. Defaults to 4.
formatThe format of texture data. Generally GL_RGBA, GL_RED, etc. Defaults to GL_RGBA.
Returns
A pointer to the mutable texture.

Definition at line 110 of file ResourceManager.cpp.

References BARE2D::GLContext::bindTexture(), BARE2D::MutableTexture::channels, BARE2D::Texture::filepath, BARE2D::MutableTexture::format, BARE2D::GLContextManager::getContext(), BARE2D::Texture::height, BARE2D::Texture::id, loadMutableTexture(), m_mutableTextures, and BARE2D::Texture::width.

+ Here is the call graph for this function:

◆ getAssetsPathPrefix()

std::string BARE2D::ResourceManager::getAssetsPathPrefix ( )
static

Returns the assets path prefix. Pretty simple.

Definition at line 299 of file ResourceManager.cpp.

References m_assetsPathPrefix.

Referenced by BARE2D::ShaderProgram::compileShaders().

+ Here is the caller graph for this function:

◆ loadFont()

Font BARE2D::ResourceManager::loadFont ( std::string &  fontPath,
int  size 
)
static

Loads a font to the cache.

Parameters
fontPathThe path to load the font from
sizeThe size of the font to load it as.
Returns
An instance of the font.

Definition at line 263 of file ResourceManager.cpp.

References BARE2D::Font::init(), m_assetsPathPrefix, and m_fonts.

+ Here is the call graph for this function:

◆ loadMusic()

Music BARE2D::ResourceManager::loadMusic ( std::string &  musicPath)
static

Loads some music from the filepath given from the cache or from the file if the cache doesn't contain it.

Parameters
musicPathThe path to load the music from
Returns
An instance of the music.

Definition at line 186 of file ResourceManager.cpp.

References m_assetsPathPrefix, m_music, BARE2D::Music::music, BARE2D::SDL_MIXER_LOAD_FAILURE, and BARE2D::throwFatalError().

+ Here is the call graph for this function:

◆ loadMutableTexture()

MutableTexture * BARE2D::ResourceManager::loadMutableTexture ( std::string &  textureName)
static

Gets a texture from the cache, or creates a new, empty texture.

Parameters
textureNameThe "filepath" of the texture, this is used to identify the texture in the cache.
Returns
A pointer to the mutable texture. Nullptr if none can be found. Must be set first.

Definition at line 153 of file ResourceManager.cpp.

References m_mutableTextures.

Referenced by createMutableTexture().

+ Here is the caller graph for this function:

◆ loadScript()

LuaScript BARE2D::ResourceManager::loadScript ( std::string &  scriptPath)
static

Loads a script from the filepath given from the cache or from the file if it's not already in the cache.

Parameters
scriptPathThe path to load the script from.
Returns
An instance of the script.

Definition at line 211 of file ResourceManager.cpp.

References BARE2D::LuaScript::inited, m_assetsPathPrefix, BARE2D::LuaScript::m_path, BARE2D::LuaScript::m_script, m_scripts, and BARE2D::IOManager::readFileToBuffer().

Referenced by BARE2D::XMLData::read().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ loadScriptFromSource()

LuaScript BARE2D::ResourceManager::loadScriptFromSource ( std::string &  scriptSource,
std::string  name 
)
static

Creates and caches a script from the given source code.

Parameters
scriptSourceThe actual source code (Lua) of the script
nameThe name of the script
Returns
The name of the script, so that it can be loaded from the regular cache.

Definition at line 244 of file ResourceManager.cpp.

References BARE2D::LuaScript::inited, BARE2D::LuaScript::m_path, BARE2D::LuaScript::m_script, and m_scripts.

◆ loadShaders()

ShaderProgram BARE2D::ResourceManager::loadShaders ( std::string &  vertShaderPath,
std::string &  fragShaderPath 
)
static

Loads some shaders. Combines both to give a full shader program. Does not cache.

Parameters
vertShaderPathThe path to the vertex shader
fragShaderPathThe path to the fragment shader
Returns
A GLSL program of type ShaderProgram instance which is the compiled version of vertShader + fragShader.

Definition at line 22 of file ResourceManager.cpp.

References BARE2D::ShaderProgram::compileShaders(), fragShaderSource, m_assetsPathPrefix, and vertShaderSource.

+ Here is the call graph for this function:

◆ loadShadersFromSource()

ShaderProgram BARE2D::ResourceManager::loadShadersFromSource ( std::string &  vertShaderSource,
std::string &  fragShaderSource 
)
static

Loads some shaders from their source. Does not cache.

Parameters
vertShaderSourceThe source to compile from.
fragShaderSourceThe source to compile from.
Returns

Definition at line 32 of file ResourceManager.cpp.

References BARE2D::ShaderProgram::compileShadersFromSource(), fragShaderSource, and vertShaderSource.

+ Here is the call graph for this function:

◆ loadSound()

Sound BARE2D::ResourceManager::loadSound ( std::string &  soundPath)
static

Loads a sound from the filepath given from the cache or from the file if the cache doesn't contain it.

Parameters
soundPathThe path to load the sound from.
Returns
An instance of the sound.

Definition at line 161 of file ResourceManager.cpp.

References BARE2D::Sound::chunk, m_assetsPathPrefix, m_sounds, BARE2D::SDL_MIXER_LOAD_FAILURE, and BARE2D::throwFatalError().

+ Here is the call graph for this function:

◆ loadTexture()

Texture BARE2D::ResourceManager::loadTexture ( std::string &  texturePath)
static

Loads a texture if it isn't already in the cache.

Parameters
texturePathA path to the texture to be loaded, including the actual name of the texture ("assets/texture.png" for example)
Returns
A copy of the texture, whether it was just loaded or it was taken from the cache.

Definition at line 41 of file ResourceManager.cpp.

References BARE2D::GLContext::bindTexture(), BARE2D::decodePNG(), BARE2D::Texture::filepath, BARE2D::GLContextManager::getContext(), BARE2D::Texture::height, BARE2D::Texture::id, m_assetsPathPrefix, m_texturePathPrefix, m_textures, BARE2D::IOManager::readFileToBuffer(), BARE2D::TEXTURE_FAILURE, BARE2D::throwFatalError(), and BARE2D::Texture::width.

Referenced by BARE2D::XMLData::read().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAssetsPathPrefix()

void BARE2D::ResourceManager::setAssetsPathPrefix ( std::string  prefix)
static

Changes the prefix that will be prepended to all paths when resources are loaded.

Parameters
prefixThe path to prepend on any load.

Definition at line 294 of file ResourceManager.cpp.

References m_assetsPathPrefix.

◆ setTexturePathPrefix()

void BARE2D::ResourceManager::setTexturePathPrefix ( std::string  prefix)
static

Changes the prefix that will be prepended to all texture paths when they're loaded.

Parameters
prefixThe path to prepend on texture loads.

Definition at line 304 of file ResourceManager.cpp.

References m_texturePathPrefix.

Field Documentation

◆ m_assetsPathPrefix

std::string BARE2D::ResourceManager::m_assetsPathPrefix = ""
staticprivate

◆ m_fonts

Cache< std::string, Font > * BARE2D::ResourceManager::m_fonts = new Cache<std::string, Font>()
staticprivate

Definition at line 138 of file ResourceManager.hpp.

Referenced by clearCaches(), and loadFont().

◆ m_music

Cache< std::string, Music > * BARE2D::ResourceManager::m_music = new Cache<std::string, Music>()
staticprivate

Definition at line 136 of file ResourceManager.hpp.

Referenced by loadMusic().

◆ m_mutableTextures

Cache< std::string, MutableTexture > * BARE2D::ResourceManager::m_mutableTextures = new Cache<std::string, MutableTexture>()
staticprivate

Definition at line 134 of file ResourceManager.hpp.

Referenced by clearCaches(), createMutableTexture(), and loadMutableTexture().

◆ m_scripts

Cache< std::string, LuaScript > * BARE2D::ResourceManager::m_scripts = new Cache<std::string, LuaScript>()
staticprivate

Definition at line 137 of file ResourceManager.hpp.

Referenced by clearCaches(), loadScript(), and loadScriptFromSource().

◆ m_sounds

Cache< std::string, Sound > * BARE2D::ResourceManager::m_sounds = new Cache<std::string, Sound>()
staticprivate

Definition at line 135 of file ResourceManager.hpp.

Referenced by clearCaches(), and loadSound().

◆ m_texturePathPrefix

std::string BARE2D::ResourceManager::m_texturePathPrefix = ""
staticprivate

Definition at line 131 of file ResourceManager.hpp.

Referenced by loadTexture(), and setTexturePathPrefix().

◆ m_textures

Cache< std::string, Texture > * BARE2D::ResourceManager::m_textures = new Cache<std::string, Texture>()
staticprivate

Definition at line 133 of file ResourceManager.hpp.

Referenced by clearCaches(), and loadTexture().


The documentation for this class was generated from the following files: