CSC3224_Computer_Games_Development / GameEngine / Engine_Resource_Management / ResourceManagement.cpp
ResourceManagement.cpp
Raw
#include "ResourceManagement.h"
ResourceManagement::ResourceManagement() : textures(), sounds()
{
	textures.reserve(MAX_NO_OF_TEXTURES);
	sounds.reserve(MAX_NO_OF_SOUNDS);
}
ResourceManagement::~ResourceManagement()
{
	for (auto deleteTexture : textures)
	{
		delete deleteTexture.second;
	}
	for (auto deleteSound : sounds)
	{
		delete deleteSound.second;
	}
}
// Get Texture by Name, iterate through the map
sf::Texture* ResourceManagement::getTexture(std::string textureName)
{
	auto texture = textures.find(textureName);
	if (texture != textures.end())
	{
		return texture->second;
	}
	throw "No Texture";
}
// Get sound by Name, iterate through the map
sf::SoundBuffer* ResourceManagement::getSound(std::string soundName)
{
	auto sound = sounds.find(soundName);
	if (sound != sounds.end())
	{
		return sound->second;
	}
	throw "No Sound";
}
// Assign a Texture Name (for accessing via get) and path (to load from)
sf::Texture* ResourceManagement::loadTexture(std::string textureName, std::string texturePath)
{ 
	auto texture = textures.find(textureName);
	if (texture != textures.end())
	{
		return texture->second;
	}
// Create Texture
	auto newTexture = new sf::Texture();
	if (newTexture->loadFromFile(texturePath))
	{
		std::cout << "Texture Loaded" << std::endl;
		textures.emplace(textureName, newTexture);
		return newTexture;
	}

// No file
	delete newTexture;
	throw "Error - No Texture File";
}
// Assign a Sound Name (for accessing via get) and path (to load from)
sf::SoundBuffer* ResourceManagement::loadSound(std::string soundName, std::string soundPath)
{
	auto sound = sounds.find(soundName);
	if (sound != sounds.end())
	{
		return sound->second;
	}
// Create Sound
	auto newSound = new sf::SoundBuffer();
	if (newSound->loadFromFile(soundPath))
	{
		std::cout << "Sound Loaded" << std::endl;
		sounds.emplace(soundName, newSound);
		return newSound;
	}
// No file
	delete newSound;
	throw "Error - No Sound File";
}