-
Notifications
You must be signed in to change notification settings - Fork 1
/
frameFactory.cpp
99 lines (93 loc) · 3.37 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
90
91
92
93
94
95
96
97
98
#include <sstream>
#include "frameFactory.h"
#include "ioManager.h"
#include "vector2f.h"
#include "SDL/SDL_rotozoom.h"
FrameFactory::~FrameFactory() {
// std::cout << "The FrameFactory is down sizing!" << 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, Frame*>::iterator itFrame = frames.begin();
while ( itFrame != frames.end() ) {
delete itFrame->second;
++itFrame;
}
std::map<std::string, std::vector<Frame*> >::iterator itFrameVect = frameVectors.begin();
while ( itFrameVect != frameVectors.end() ) {
for(unsigned i = 0; i<itFrameVect->second.size(); i++) {
delete itFrameVect->second[i];
}
++itFrameVect;
}
}
FrameFactory& FrameFactory::getInstance() {
static FrameFactory factory;
return factory;
}
std::vector<Frame*> FrameFactory::getFrameVector(const std::string& name, float * const scale) {
std::map<std::string, std::vector<Frame*> >::const_iterator pos = frameVectors.find(name);
if ( pos == frameVectors.end() ) {
SDL_Surface * const surface =
IOManager::getInstance().loadAndSet(
gdata->getXmlStr(name+"File"),
gdata->getXmlBool(name+"Transparency"));
surfaces[name+"_multiframe"] = surface;
*scale = getRand(Gamedata::getInstance()->getXmlFloat(name+"ScaleMin"),
Gamedata::getInstance()->getXmlFloat(name+"ScaleMax"));
unsigned numberOfFrames = gdata->getXmlInt(name+"Frames");
Uint16 pwidth = gdata->getXmlInt(name+"Width");
Uint16 pheight = gdata->getXmlInt(name+"Height");
Uint16 srcX = gdata->getXmlInt(name+"SrcX");
Uint16 srcY = gdata->getXmlInt(name+"SrcY");
std::vector<Frame*> framesTemp;
framesTemp.reserve((surface->w - srcX)/pwidth);
for (unsigned i = 0; i < numberOfFrames; ++i) {
unsigned frameX = i * pwidth + srcX;
framesTemp.push_back(
new Frame(surface, pwidth, pheight, frameX, srcY) );
}
frameVectors[name] = framesTemp;
return framesTemp;
} else {
return pos->second;
}
}
Frame* FrameFactory::getFrame(const std::string& name, float * const scale, bool ignorePrevious = false) {
std::map<std::string, Frame*>::const_iterator pos = frames.find(name);
if ( pos == frames.end() || ignorePrevious) {
SDL_Surface * const surface =
IOManager::getInstance().loadAndSet(
gdata->getXmlStr(name+"File"),
gdata->getXmlBool(name+"Transparency"));
*scale = getRand(Gamedata::getInstance()->getXmlFloat(name+"ScaleMin"),
Gamedata::getInstance()->getXmlFloat(name+"ScaleMax"));
std::ostringstream temp;
temp << *scale;
std::string scaleString = temp.str();
SDL_Surface* tmp =
rotozoomSurface(surface, 0.0, static_cast<double>(*scale), SMOOTHING_ON);
SDL_FreeSurface(surface);
if(!ignorePrevious) {
surfaces[name] = tmp;
} else {
surfaces[name + scaleString] = tmp;
}
Frame * const frame =new Frame(tmp,
gdata->getXmlInt(name+"Width")* *scale,
gdata->getXmlInt(name+"Height")* *scale,
gdata->getXmlInt(name+"SrcX"),
gdata->getXmlInt(name+"SrcY"));
if(!ignorePrevious) {
frames[name] = frame;
} else {
frames[name + scaleString] = frame;
}
return frame;
}
else {
return pos->second;
}
}