70 lines
1.5 KiB
C++
70 lines
1.5 KiB
C++
#include "ResourceManager.h"
|
|
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
|
|
#include "texture.h"
|
|
#include "font.h"
|
|
|
|
ResourceManager::~ResourceManager()
|
|
{
|
|
for (auto& [path, texture] : textures) {
|
|
if (!texture) continue;
|
|
texture->Delete();
|
|
delete texture;
|
|
}
|
|
for (Texture* texture : createdTextures) {
|
|
texture->Delete();
|
|
delete texture;
|
|
}
|
|
for (auto& [path, font] : fonts)
|
|
delete font;
|
|
}
|
|
|
|
Texture* ResourceManager::CreateTexture(int width, int height)
|
|
{
|
|
createdTextures.push_back(new Texture(width, height));
|
|
return createdTextures.back();
|
|
}
|
|
|
|
Texture* ResourceManager::CopyTexture(const Texture& source)
|
|
{
|
|
createdTextures.push_back(new Texture(source));
|
|
return createdTextures.back();
|
|
}
|
|
|
|
// Failed loads are cached as nullptr too, so a missing file only gets reported once
|
|
Texture* ResourceManager::GetTexture(const std::string& path)
|
|
{
|
|
auto existing = textures.find(path);
|
|
if (existing != textures.end())
|
|
return existing->second;
|
|
|
|
Texture* texture = nullptr;
|
|
try {
|
|
texture = new Texture(path.c_str());
|
|
}
|
|
catch (const std::exception& error) {
|
|
std::cout << "RESOURCE MANAGER: " << error.what() << "\n";
|
|
}
|
|
textures[path] = texture;
|
|
return texture;
|
|
}
|
|
|
|
Font* ResourceManager::GetFont(const std::string& path)
|
|
{
|
|
auto existing = fonts.find(path);
|
|
if (existing != fonts.end())
|
|
return existing->second;
|
|
|
|
Font* font = nullptr;
|
|
try {
|
|
font = new Font(path.c_str());
|
|
}
|
|
catch (const std::exception& error) {
|
|
std::cout << "RESOURCE MANAGER: " << error.what() << ": " << path << "\n";
|
|
}
|
|
fonts[path] = font;
|
|
return font;
|
|
}
|