Repository navigation
Expand file tree
/
Copy pathAssetLoader.cpp
More file actions
96 lines (80 loc) · 2.07 KB
/
Copy pathAssetLoader.cpp
File metadata and controls
96 lines (80 loc) · 2.07 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
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
#include "AssetLoader.h"
namespace fs = std::filesystem;
namespace AssetLoader
{
namespace
{
std::unordered_map<std::string ,std::shared_ptr<sf::Texture>> texturesMap;
std::unordered_map<std::string, std::shared_ptr<sf::Font>> fontsMap;
enum extensionType
{
SPRITE,
FONT,
INVALID
};
int determineExtension(std::string extension)
{
if (extension == ".png" || extension == ".jpg" || extension == ".bmp" || extension == ".webp" || extension == ".jpeg")
return extensionType::SPRITE;
else if (extension == ".ttf")
return extensionType::FONT;
std::cout << "File extension " << extension << " is not supported.";
throw("File extension isn't supported.");
return extensionType::INVALID;
}
void LoadFontFromDir(std::filesystem::path fontPath)
{
std::shared_ptr<sf::Font> tempFont = std::make_unique<sf::Font>();
tempFont->loadFromFile(fontPath.string());
fontsMap[fontPath.filename().string()] = tempFont;
}
void LoadSpriteFromDir(std::filesystem::path spritePath)
{
std::shared_ptr<sf::Texture> tempTexture = std::make_unique<sf::Texture>();
tempTexture->loadFromFile(spritePath.string());
texturesMap[spritePath.filename().string()] = tempTexture;
}
}
void AssetLoader::LoadFromDir(std::string dir)
{
for (const auto& entry : fs::recursive_directory_iterator(dir))
{
if (entry.is_regular_file())
{
switch (determineExtension(entry.path().extension().string()))
{
case extensionType::FONT:
LoadFontFromDir(entry.path());
break;
case extensionType::SPRITE:
LoadSpriteFromDir(entry.path());
break;
default:
break;
}
}
}
}
std::shared_ptr<sf::Texture> GetTexture(std::string imageName)
{
if (texturesMap.find(imageName) == texturesMap.end())
{
throw("Image name not found.");
}
else
{
return texturesMap[imageName];
}
}
std::shared_ptr<sf::Font> GetFont(std::string fontName)
{
if (fontsMap.find(fontName) == fontsMap.end())
{
throw("Image name not found.");
}
else
{
return fontsMap[fontName];
}
}
}