Repository navigation
Expand file tree
/
Copy pathResourceManager.cpp
More file actions
98 lines (81 loc) · 2.6 KB
/
Copy pathResourceManager.cpp
File metadata and controls
98 lines (81 loc) · 2.6 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
92
93
94
95
96
97
98
//
// Created by Neirokan on 09.05.2020
//
#include "ResourceManager.h"
#include <map>
#include <memory>
#include <iostream>
namespace ResourceManager
{
namespace
{
std::map<std::string, std::shared_ptr<sf::Texture>> _textures;
std::map<std::string, std::shared_ptr<sf::Font>> _fonts;
std::map<std::string, std::shared_ptr<sf::SoundBuffer>> _soundBuffers;
}
void unloadTextures()
{
for (auto it = _textures.begin(); it != _textures.end(); it++)
it->second.reset();
_textures.clear();
}
void unloadSoundBuffers()
{
for (auto it = _soundBuffers.begin(); it != _soundBuffers.end(); it++)
it->second.reset();
_soundBuffers.clear();
}
void unloadFonts() {
for (auto it = _fonts.begin(); it != _fonts.end(); it++)
it->second.reset();
_fonts.clear();
}
void unloadAllResources()
{
unloadTextures();
unloadSoundBuffers();
unloadFonts();
}
sf::Texture* loadTexture(const std::string& filename)
{
// If texture is already loaded - return pointer to it
auto it = _textures.find(filename);
if (it != _textures.end())
return it->second.get();
// Otherwise - try to load it. If failure - return zero
std::shared_ptr<sf::Texture> texture(new sf::Texture);
if (!texture->loadFromFile(filename))
return nullptr;
// If success - remember and return texture pointer
texture->setRepeated(true);
_textures.emplace(filename, texture);
return texture.get();
}
sf::SoundBuffer* loadSoundBuffer(const std::string& filename)
{
// If sound buffer is already loaded - return pointer to it
auto it = _soundBuffers.find(filename);
if (it != _soundBuffers.end())
return it->second.get();
// Otherwise - try to load it. If failure - return zero
std::shared_ptr<sf::SoundBuffer> soundBuffer(new sf::SoundBuffer);
if (!soundBuffer->loadFromFile(filename))
return nullptr;
// If success - remember and return texture pointer
_soundBuffers.emplace(filename, soundBuffer);
return soundBuffer.get();
}
sf::Font* loadFont(const std::string& filename) {
// If font is already loaded - return pointer to it
auto it = _fonts.find(filename);
if (it != _fonts.end())
return it->second.get();
// Otherwise - try to load it. If failure - return zero
std::shared_ptr<sf::Font> font(new sf::Font);
if (!font->loadFromFile(filename))
return nullptr;
// If success - remember and return texture pointer
_fonts.emplace(filename, font);
return font.get();
}
}