-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureManager.cpp
37 lines (32 loc) · 1.15 KB
/
TextureManager.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
#include "TextureManager.h"
void TextureManager::LoadTexture(const std::string &name, const std::string &filename)
{
sf::Texture *pTex = new sf::Texture();
pTex->loadFromFile(filename);
m_Textures[name] = pTex;
}
void TextureManager::LoadSheet(const std::string &name, const std::string &filename, int n_cols, int sprite_count, float single_width, float single_height)
{
try
{
for (size_t iTexture = 0; iTexture < sprite_count; iTexture++)
{
sf::Texture *pTex = new sf::Texture();
// Calculate top left of the texture, width and height are the same for all
int left = iTexture * single_width;
int top = iTexture * single_height;
pTex->loadFromFile(filename, sf::IntRect(left, top, single_width, single_height));
// Pattern is name_0, name_1, etc.
m_Textures[name + "_" + std::to_string(iTexture)] = pTex;
}
}
catch (const std::exception &e)
{
// TODO: Handle possible memory leaks
std::cerr << e.what() << '\n';
}
}
sf::Texture *TextureManager::GetTexture(const std::string &name)
{
return m_Textures.at(name);
}