-
Notifications
You must be signed in to change notification settings - Fork 1
/
resourcemanager.cpp
112 lines (85 loc) · 2.46 KB
/
resourcemanager.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "resourcemanager.hpp"
#include "config.hpp"
std::unique_ptr<ResourceManager> g_resourceManager = nullptr;
const std::string resourcePath = "./resources/";
ResourceManager::ResourceManager()
{
}
ResourceManager::~ResourceManager()
{
}
sf::Texture* ResourceManager::getTexture(const std::string& _fileName)
{
for (auto& texture : m_textures)
{
if (texture.name == _fileName) return texture.data.get();
}
sf::Texture* texture = new sf::Texture();
if (!texture->loadFromFile(resourcePath + _fileName))
{
return nullptr;
}
texture->setSmooth(true);
m_textures.emplace_back(_fileName, texture);
return m_textures.back().data.get();
}
sf::Font* ResourceManager::getFont(const std::string& _fileName)
{
for (auto& font : m_fonts)
{
if (font.name == _fileName) return font.data.get();
}
sf::Font* font = new sf::Font();
if (!font->loadFromFile(resourcePath + _fileName + ".ttf"))
{
return nullptr;
}
m_fonts.emplace_back(_fileName, font);
return m_fonts.back().data.get();
}
sf::Music* ResourceManager::getTrack(const std::string& _fileName)
{
for (auto& track : m_tracks)
{
if (track.name == _fileName) return track.data.get();
}
sf::Music* track = new sf::Music();
if (!track->openFromFile(resourcePath + _fileName + ".ogg"))
{
return nullptr;
}
m_tracks.emplace_back(_fileName, track);
return m_tracks.back().data.get();
}
sf::SoundBuffer* ResourceManager::getSound(const std::string& _fileName)
{
for (auto& soundBuffer : m_soundBuffers)
{
if (soundBuffer.name == _fileName) return soundBuffer.data.get();
}
sf::SoundBuffer* soundBuffer = new sf::SoundBuffer();
if (!soundBuffer->loadFromFile(resourcePath + _fileName + ".ogg"))
return nullptr;
m_soundBuffers.emplace_back(_fileName, soundBuffer);
return m_soundBuffers.back().data.get();
}
sf::Shader* ResourceManager::getShader(const std::string& _fileName, bool _geometry)
{
for (auto& shader : m_shaders)
{
if (shader.name == _fileName) return shader.data.get();
}
sf::Shader* shader = new sf::Shader();
static std::string path = resourcePath + "shader/";
bool b = _geometry ? shader->loadFromFile(resourcePath + "shader/" + _fileName + ".vs",
resourcePath + "shader/" + _fileName + ".gs",
resourcePath + "shader/" + _fileName + ".ps")
: shader->loadFromFile(resourcePath + "shader/" + _fileName + ".vs",
resourcePath+"shader/" + _fileName + ".ps");
if (!b)
{
return nullptr;
}
m_shaders.emplace_back(_fileName, shader);
return m_shaders.back().data.get();
}