-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageFactory.cpp
More file actions
119 lines (106 loc) · 3.61 KB
/
imageFactory.cpp
File metadata and controls
119 lines (106 loc) · 3.61 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "ioMod.h"
#include "vector2f.h"
#include "renderContext.h"
ImageFactory& ImageFactory::getInstance() {
static ImageFactory factory;
return factory;
}
ImageFactory::~ImageFactory() {
std::cout << "Deleting images in Factory" << std::endl;
map<std::string, SDL_Surface*>::iterator s = surfaces.begin();
map<std::string, SDL_Texture*>::iterator t = textures.begin();
map<std::string, Image*>::iterator i = images.begin();
while (s != surfaces.end()) {
SDL_FreeSurface( s->second );
++s;
}
while (t != textures.end()) {
SDL_DestroyTexture( t->second );
++t;
}
while (i != images.end()) {
delete i->second;
++i;
}
// Free multi-image containers
for ( auto& surfaces : multiSurfaces ) {
for (unsigned int i = 0; i < surfaces.second.size(); ++i) {
SDL_FreeSurface( surfaces.second[i] );
}
}
for ( auto& textures : multiTextures ) {
for (unsigned int i = 0; i < textures.second.size(); ++i) {
SDL_DestroyTexture( textures.second[i] );
}
}
for ( auto& images : multiImages ) {
std::cout << "deleting " << images.first << std::endl;
for (unsigned int i = 0; i < images.second.size(); ++i) {
delete images.second[i];
}
}
}
Image* ImageFactory::getImage(const std::string& name) {
std::map<std::string, Image*>::const_iterator it = images.find(name);
if ( it == images.end() ) {
SDL_Surface * const surface =
IoMod::getInstance().readSurface( gdata.getXmlStr(name+"/file"));
bool transparency = gdata.getXmlBool(name+"/transparency");
if ( transparency ) {
int keyColor = SDL_MapRGBA(surface->format, 255, 0, 255, 255);
SDL_SetColorKey(surface, SDL_TRUE, keyColor);
}
surfaces[name] = surface;
Image * const image =new Image(surface);
images[name] = image;
return image;
}
else {
return it->second;
}
}
std::vector<Image*> ImageFactory::getImages(const std::string& name) {
// First search map to see if we've already made it:
std::map<std::string, std::vector<Image*> >::const_iterator
pos = multiImages.find(name);
if ( pos != multiImages.end() ) {
return pos->second;
}
IoMod& iomod = IoMod::getInstance();
RenderContext* renderContext = RenderContext::getInstance();
std::string sheetPath = gdata.getXmlStr(name+"/file");
SDL_Surface* spriteSurface = iomod.readSurface(sheetPath);
bool transparency = gdata.getXmlBool(name+"/transparency");
// It wasn't in the map, so we have to make the vector of Images:
unsigned numberOfFrames = gdata.getXmlInt(name+"/frames");
std::vector<Image*> images;
std::vector<SDL_Surface*> surfaces;
std::vector<SDL_Texture*> textures;
images.reserve(numberOfFrames);
surfaces.reserve(numberOfFrames);
textures.reserve(numberOfFrames);
int width = spriteSurface->w/numberOfFrames;
int height = spriteSurface->h;
if( gdata.checkTag(name+"/imageWidth")
&& gdata.checkTag(name+"/imageHeight") ){
width = gdata.getXmlInt(name+"/imageWidth");
height = gdata.getXmlInt(name+"/imageHeight");
}
SpriteSheet sheet(spriteSurface,width,height);
for (unsigned i = 0; i < numberOfFrames; ++i) {
SDL_Surface* surface = sheet[i];
if ( transparency ) {
int keyColor = SDL_MapRGBA(spriteSurface->format, 255, 0, 255, 255);
SDL_SetColorKey(surface, SDL_TRUE, keyColor);
}
SDL_Texture* texture =
SDL_CreateTextureFromSurface(renderContext->getRenderer(),surface);
surfaces.push_back( surface );
textures.push_back( texture );
images.push_back( new Image(surface) );
}
multiSurfaces[name] = surfaces;
multiTextures[name] = textures;
multiImages[name] = images;
return images;
}