/
factory.cpp
84 lines (74 loc) · 2.8 KB
/
factory.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
#include "factory.h"
#include <iostream>
Frame* Factory::getFrame(const string& s) {
std::map<const string, Frame *>::iterator ptr;
ptr = frameMap.find(s);
return ptr->second;
}
Frame* Factory::makeFrame(const string& s) {
std::map<const string, SDL_Surface *>::iterator it = surfaceMap.find(s);
SDL_Surface *tempSurface;
if(it == surfaceMap.end()) {
tempSurface = makeSurface(s);
}
else {
tempSurface = getSurface(s);
}
if(frameMap.count(s) == 0)
frameMap.insert(std::map<const string, Frame *>::value_type(s, new Frame(tempSurface, io->getScreen(), tempSurface->w, tempSurface->h, 0, 0)));
return(getFrame(s));
}
SDL_Surface* Factory::getSurface(const string& s) {
std::map<const string, SDL_Surface *>::iterator ptr;
ptr = surfaceMap.find(s);
return ptr->second;
}
SDL_Surface* Factory::makeSurface(const string& s) {
map<const string, SDL_Surface *>::iterator it = surfaceMap.find(s);
if(it == surfaceMap.end()) {
surfaceMap.insert(std::map<const string, SDL_Surface *>::value_type(s, io->loadAndSet(s, true)));
}
return getSurface(s);
}
void Factory::makeSprite(const SpriteData& s) {
map<const string, SDL_Surface *>::iterator it = surfaceMap.find(s.getFname());
if(it == surfaceMap.end())
surfaceMap.insert(std::map<const string, SDL_Surface *>::value_type(s.getFname(), io->loadAndSet(s.getFname(), s.getColorKey())));
map<const string, Frame *>::iterator it2 = frameMap.find(s.getFname());
if(it2 == frameMap.end())
frameMap.insert(std::map<const string, Frame *>::value_type(s.getFname(), new Frame(getSurface(s.getFname()), io->getScreen(), s.getWidth(), s.getHeight(), s.getSpriteSrcX(), s.getSpriteSrcY())));
}
Factory* Factory::getInstance() {
if(!instance) instance = new Factory();
return(instance);
}
Factory::~Factory() {
for(map<const string, Frame *>::iterator it = frameMap.begin(); it != frameMap.end(); ++it)
delete (*it).second;
frameMap.clear();
for(map<const string, SDL_Surface *>::iterator it = surfaceMap.begin(); it != surfaceMap.end(); ++it)
SDL_FreeSurface((*it).second);
surfaceMap.clear();
}
void Factory::printFrameMap() {
for(map<const string, Frame *>::iterator it = frameMap.begin(); it != frameMap.end(); ++it)
cout<<it->first<<" "<<it->second->getWidth()<<" "<<it->second->getHeight()<<" "<<it->second->getSourceX()<<" "<<it->second->getSourceY()<<endl;
}
string Factory::getSurfaceKey(const SDL_Surface* s) {
std::map<const string, SDL_Surface *>::iterator it = surfaceMap.begin();
while(it != surfaceMap.end()) {
if(it->second == s)
return (it->first);
++it;
}
return NULL;
}
string Factory::getFrameKey(const Frame *s) {
std::map<const string, Frame *>::iterator it = frameMap.begin();
while(it != frameMap.end()) {
if(it->second == s)
return (it->first);
++it;
}
return NULL;
}