-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageCache.cpp
More file actions
61 lines (49 loc) · 1.88 KB
/
ImageCache.cpp
File metadata and controls
61 lines (49 loc) · 1.88 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
#include "stdafx.h"
#include "ImageCache.h"
ImageCache::ImageCache(void) {}
//Free memory
ImageCache::~ImageCache(void)
{
std::for_each(mImages.begin(), mImages.end(), Deallocator<sf::Image*>());
for (std::map<sf::Color, sf::Image*, CompareColor>::iterator itr = mColors.begin(); itr != mColors.end(); ++itr)
{
delete itr->second;
}
}
//If sound exists already, return it; else load and then return it
sf::Image* ImageCache::GetImage(std::string imageName) const
{
std::map<std::string, sf::Image*>::iterator itr = mImages.find(imageName);
if (itr == mImages.end())
{
sf::Image *image = new sf::Image();
if (!image->LoadFromFile(imageName))
{
delete image;
throw ImageNotFoundException(imageName + " was not found in call to ImageCache::GetSound");
}
std::map<std::string, sf::Image*>::iterator res = mImages.insert(std::pair<std::string, sf::Image*>(imageName, image)).first;
return image;
}
else
{
return itr->second;
}
throw ImageNotFoundException(imageName + " was not found in call to ImageCache::GetSound");
}
sf::Image* ImageCache::GetColor(int width, int height, sf::Color color) const
{
std::map<sf::Color, sf::Image*, CompareColor>::iterator itr;
for (itr = mColors.begin(); itr != mColors.end(); ++itr)
{
if (itr->first == color)
if (itr->second->GetWidth() == width && itr->second->GetHeight() == height)
return itr->second;
}
sf::Image *image = new sf::Image(width, height, color);
std::map<sf::Color, sf::Image*, CompareColor>::iterator res = mColors.insert(std::pair<sf::Color, sf::Image*>(color, image)).first;
return image;
}
//std::map for containing sounds and their name
std::map<std::string, sf::Image*> ImageCache::mImages;
std::map<sf::Color, sf::Image*, CompareColor> ImageCache::mColors;