-
Notifications
You must be signed in to change notification settings - Fork 0
/
frameFactory.cpp
89 lines (82 loc) · 2.66 KB
/
frameFactory.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
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
#include "frameFactory.h"
#include "extractSurface.h"
#include "ioManager.h"
#include "vector2f.h"
FrameFactory::~FrameFactory() {
std::cout << "Deleting FrameFactory" << std::endl;
std::map<std::string, SDL_Surface*>::iterator itSurf = surfaces.begin();
while ( itSurf != surfaces.end() ) {
SDL_FreeSurface( itSurf->second );
++itSurf;
}
std::map<std::string, std::vector<SDL_Surface*> >::iterator
surfaces = multiSurfaces.begin();
while ( surfaces != multiSurfaces.end() ) {
for (unsigned int i = 0; i < surfaces->second.size(); ++i) {
SDL_FreeSurface( surfaces->second[i] );
}
++surfaces;
}
std::map<std::string, Frame*>::iterator frame = frames.begin();
while ( frame != frames.end() ) {
delete frame->second;
++frame;
}
std::map<std::string, std::vector<Frame*> >::iterator
frames = multiFrames.begin();
while ( frames != multiFrames.end() ) {
for (unsigned int i = 0; i < frames->second.size(); ++i) {
delete frames->second[i];
}
++frames;
}
}
FrameFactory& FrameFactory::getInstance() {
static FrameFactory factory;
return factory;
}
Frame* FrameFactory::getFrame(const std::string& name) {
std::map<std::string, Frame*>::const_iterator pos = frames.find(name);
if ( pos == frames.end() ) {
SDL_Surface * const surface =
IOManager::getInstance().loadAndSet(
gdata.getXmlStr(name+"/file"),
gdata.getXmlBool(name+"/transparency"));
surfaces[name] = surface;
Frame * const frame =new Frame(surface);
frames[name] = frame;
return frame;
}
else {
return pos->second;
}
}
std::vector<Frame*> FrameFactory::getFrames(const std::string& name) {
// First search map to see if we've already made it:
std::map<std::string, std::vector<Frame*> >::const_iterator
pos = multiFrames.find(name);
if ( pos != multiFrames.end() ) {
return pos->second;
}
// It wasn't in the map, so we have to make the vector of Frames:
SDL_Surface* surface = IOManager::
getInstance().loadAndSet(gdata.getXmlStr(name+"/file"), true);
unsigned numberOfFrames = gdata.getXmlInt(name+"/frames");
std::vector<Frame*> frames;
std::vector<SDL_Surface*> surfaces;
frames.reserve(numberOfFrames);
Uint16 width = surface->w/numberOfFrames;
Uint16 height = surface->h;
SDL_Surface* surf;
for (unsigned i = 0; i < numberOfFrames; ++i) {
unsigned frameX = i * width;
surf = ExtractSurface::getInstance().
get(surface, width, height, frameX, 0);
surfaces.push_back( surf );
frames.push_back( new Frame(surf) );
}
SDL_FreeSurface(surface);
multiSurfaces[name] = surfaces;
multiFrames[name] = frames;
return frames;
}