-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManager.hpp
More file actions
81 lines (68 loc) · 2.1 KB
/
ImageManager.hpp
File metadata and controls
81 lines (68 loc) · 2.1 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
#pragma once
#include "stdafx.h"
/*!
* \brief Manages images loading
* \details This class is used to manage the memory taken by images and to return textures from these images
* \author Vincent Studer
* \date 2013
* \pre None
* \bug None
* \warning A wrong filepath will give you an empty image.
* \copyright GNU Public License.
*/
class ImageManager
{
public:
ImageManager();
~ImageManager();
//! Return the image located at "filename"
const sf::Image& getImage(const std::string& filename);
//! Return a reference to a texture of the full image located at "filename"
sf::Texture& getTexture(const std::string& filename);
//! Return a reference to a texture of the image located at "filename" cut in the rectangle
sf::Texture& getTexture(const std::string& filename, sf::Rect<int> rect);
private:
//! Images list with their corresponding names
std::map< std::string, sf::Image > m_images;
//! Textures list
std::vector<sf::Texture> m_textures;
};
ImageManager::ImageManager() :
m_images()
{
}
ImageManager::~ImageManager()
{
m_images.clear();
}
sf::Texture& ImageManager::getTexture( const std::string& filename )
{
sf::Texture texture;
texture.loadFromImage(getImage(filename));
m_textures.push_back(texture);
return m_textures.back();
}
sf::Texture& ImageManager::getTexture(const std::string& filename, sf::Rect<int> rect)
{
sf::Texture texture;
texture.loadFromImage(getImage(filename), rect);
m_textures.push_back(texture);
return m_textures.back();
}
const sf::Image& ImageManager::getImage( const std::string& filename )
{
//Check weither the image is already loaded or not
for( std::map<std::string, sf::Image>::const_iterator it = m_images.begin(); it != m_images.end(); ++it)
if( filename == it->first )
return it->second;
//The image does not exist in memory, we need to load it
sf::Image image;
if( image.loadFromFile(filename) )
{
m_images[filename] = image;
return m_images[filename];
}
else
std::cout << "Image '" << filename <<"' was not found. It is filled with an empty image";
return image;
}