-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceManager.cpp
More file actions
50 lines (42 loc) · 1.54 KB
/
ResourceManager.cpp
File metadata and controls
50 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "ResourceManager.h"
#include <stdexcept>
ResourceManager& ResourceManager::get() {
static ResourceManager instance;
return instance;
}
void ResourceManager::loadFont(const std::string& id, const std::string& path) {
sf::Font font;
if (!font.openFromFile(path))
throw std::runtime_error("Failed to load font: " + path);
mFonts[id] = std::move(font);
}
void ResourceManager::loadTexture(const std::string& id, const std::string& path) {
sf::Texture texture;
if (!texture.loadFromFile(path))
throw std::runtime_error("Failed to load texture: " + path);
mTextures[id] = std::move(texture);
}
void ResourceManager::loadSound(const std::string& id, const std::string& path) {
sf::SoundBuffer buffer;
if (!buffer.loadFromFile(path))
throw std::runtime_error("Failed to load texture: " + path);
mSounds[id] = std::move(buffer);
}
const sf::Font& ResourceManager::getFont(const std::string& id) const {
auto it = mFonts.find(id);
if (it == mFonts.end())
throw std::runtime_error("Font not found: " + id);
return it->second;
}
const sf::Texture& ResourceManager::getTexture(const std::string& id) const {
auto it = mTextures.find(id);
if (it == mTextures.end())
throw std::runtime_error("Texture not found: " + id);
return it->second;
}
const sf::SoundBuffer& ResourceManager::getSound(const std::string& id) const {
auto it = mSounds.find(id);
if (it == mSounds.end())
throw std::runtime_error("Sound not found: " + id);
return it->second;
}