-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetManager.cpp
108 lines (94 loc) · 2.09 KB
/
AssetManager.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
#include "AssetManager.h"
AssetManager* AssetManager::assetManager;
AssetManager::AssetManager()
{
}
AssetManager::~AssetManager()
{
}
AssetManager* AssetManager::getInstance()
{
if (assetManager == nullptr)
{
assetManager = new AssetManager();
}
return assetManager;
}
fs::path AssetManager::getImageFilePathExtension(fs::path filePathWithNoExtension)
{
fs::path jpgPath = filePathWithNoExtension;
fs::path pngPath = filePathWithNoExtension;
jpgPath.replace_extension("jpg");
pngPath.replace_extension("png");
if (std::filesystem::exists(jpgPath))
{
return jpgPath;
}
else if (std::filesystem::exists(pngPath))
{
return pngPath;
}
else
{
throw std::exception("No match image file path.");
}
}
fs::path AssetManager::getAudioFilePathExtension(fs::path filePathWithNoExtention)
{
fs::path wavPath = filePathWithNoExtention;
fs::path oggPath = filePathWithNoExtention;
wavPath.replace_extension("wav");
oggPath.replace_extension("ogg");
if (std::filesystem::exists(wavPath))
{
return wavPath;
}
else if (std::filesystem::exists(oggPath))
{
return oggPath;
}
else
{
throw "No match audio file path.";
}
}
void AssetManager::loadTexture(std::string name, std::string filename)
{
this->textureFiles[name] = filename;
}
sf::Texture& AssetManager::getTexture(std::string name)
{
if (this->textures.find(name) == this->textures.end())
{
sf::Texture texture;
if (texture.loadFromFile(this->textureFiles.at(name)))
{
this->textures[name] = texture;
}
}
return this->textures.at(name);
}
void AssetManager::loadFont(std::string name, std::string filename)
{
this->fontFiles[name] = filename;
}
sf::Font& AssetManager::getFont(std::string name)
{
if (this->fonts.find(name) == this->fonts.end())
{
sf::Font font;
if (font.loadFromFile(this->fontFiles.at(name)))
{
this->fonts[name] = font;
}
}
return this->fonts.at(name);
}
void AssetManager::loadDrawable(std::string name, std::unique_ptr<sf::Drawable> drawable)
{
this->drawables[name] = std::move(drawable);
}
sf::Drawable& AssetManager::getDrawable(std::string name)
{
return *this->drawables.at(name);
}