Debian Codegraph @

Oodle/src/CAnimation.cpp

raw
1 #include "oodle.h"
2 #include <set>
3 using namespace std;
4
5 CAnimation::CAnimation(const string &fileName,
6 int width, int height, int padding, int speed, bool alpha, bool nopath)
7 : width(width), height(height), isClone(false), animating(true),
8 timer(speed), done(false), loop(true)
9 {
10 frames = CImgLoader::loadBigassBitmap(fileName, width, height, padding, alpha, nopath);
11 currentFrame = frames.begin();
12 }
13
14 CAnimation::CAnimation(const string &fileName,
15 int width, int height, int padding, int speed, bool loop, bool alpha, bool nopath)
16 : width(width), height(height), isClone(false), animating(true),
17 timer(speed), done(false), loop(loop)
18 {
19 frames = CImgLoader::loadBigassBitmap(fileName, width, height, padding, alpha, nopath);
20 currentFrame = frames.begin();
21 }
22
23
24 CAnimation::CAnimation(const string &fileName, bool alpha, bool nopath)
25 : isClone(false), animating(false), timer(0),
26 done(false), loop(true)
27 {
28 frames.push_back(CImgLoader::loadImage(fileName, alpha, nopath));
29 currentFrame = frames.begin();
30 width = (*currentFrame)->getWidth();
31 height = (*currentFrame)->getHeight();
32 }
33
34 CAnimation::CAnimation(CSurface* s)
35 : isClone(false), animating(false), timer(0),
36 done(false), loop(true)
37 {
38 if (!s)
39 {
40 _THROWEX(ex_illegalArguments, "Surface 's' is a NULL pointer", "CAnimation", "CAnimation", "s = {" << s << "}");
41 }
42
43 frames.push_back(s);
44 currentFrame = frames.begin();
45 width = (*currentFrame)->getWidth();
46 height = (*currentFrame)->getHeight();
47 }
48
49 CAnimation::CAnimation(const list<CSurface*> &s, int period, bool loop)
50 : isClone(false), animating(true), timer(period),
51 done(false), loop(loop)
52 {
53 for(list<CSurface*>::const_iterator i = s.begin(); i != s.end(); ++i)
54 {
55 if(*i == NULL)
56 {
57 _THROWEX(ex_illegalArguments, "Given list of Surfaces contains NULL ptr", "CAnimation", "CAnimation", "s = {" << &s << "}");
58 }
59
60 frames.push_back(*i);
61 currentFrame = frames.begin();
62 width = (*currentFrame)->getWidth();
63 height = (*currentFrame)->getHeight();
64 }
65 }
66
67 const CAnimation& CAnimation::operator=(const CAnimation &a)
68 {
69 if (&a != this) // don't allow assignment to self
70 {
71 // free old memory
72 list<CSurface*>::iterator i;
73 for(i = frames.begin(); i != frames.end(); ++i)
74 {
75 delete *i;
76 }
77 frames.clear();
78
79 for(i = frames.begin(); i != frames.end(); ++i) {
80 frames.push_back( new CSurface(**i) );
81 }
82
83 currentFrame = frames.begin(); // not sure how to preserve the currentFrame so do this
84
85 animating = a.animating;
86 isClone = a.isClone;
87 timer = a.timer;
88 loop = a.loop;
89 done = a.done;
90 }
91 return *this;
92 }
93
94 CAnimation::~CAnimation()
95 {
96 if ((isClone == false))
97 {
98 set<CSurface*> s;
99 s.insert(frames.begin(), frames.end());
100 set<CSurface*>::iterator i;
101 for(i = s.begin(); i != s.end(); ++i) {
102 delete (*i);
103 }
104 frames.clear();
105 }
106 }
107
108 // copy constructor
109 CAnimation::CAnimation(const CAnimation& obj)
110 : width(obj.width), height(obj.height), animating(obj.animating),
111 isClone(obj.isClone), timer(obj.timer), done(obj.done), loop(obj.loop)
112 {
113 list<CSurface*>::iterator i;
114 for(i = frames.begin(); i != frames.end(); ++i) {
115 frames.push_back( new CSurface(**i) );
116 }
117
118 currentFrame = frames.begin(); // not sure how to preserve the currentFrame so do this
119
120 }
121
122 CAnimation* CAnimation::getClone()
123 {
124 CAnimation* clone = new CAnimation(*this, true);
125 return clone;
126 }
127
128 CAnimation::CAnimation(const CAnimation& obj, bool clone)
129 : width(obj.width), height(obj.height), animating(obj.animating),
130 frames(obj.frames), isClone(clone), timer(obj.timer), done(obj.done), loop(obj.loop)
131 {
132 if (!clone)
133 {
134 _THROWEX(ex_illegalArguments, "Clone constructor called without intent to clone", "CAnimation", "CAnimation", "sprite = {" << &obj << "}, clone = " << clone);
135 }
136
137 currentFrame = frames.begin();
138 timer= obj.timer;
139 }
140
141 void CAnimation::setAlpha(Uint32 flags, Uint8 alpha)
142 {
143 list<CSurface*>::iterator i;
144 for(i = frames.begin(); i != frames.end(); ++i) {
145 (*i)->setAlpha(flags, alpha);
146 }
147 }
148
149 void CAnimation::update()
150 {
151 if (timer.isRinging() && animating) { // if speed seconds have been spent on current frame
152
153 if(++currentFrame == frames.end()) // make sure we are not overstepping list
154 {
155 if (loop)
156 {
157 currentFrame = frames.begin();
158 }
159 else
160 {
161 currentFrame = frames.end();
162 animating = false;
163 done = true;
164 }
165 }
166 }
167 }
168
169 int CAnimation::getFrames()
170 {
171 /*int n = 0;
172 for (list<CSurface*>::iterator i = frames.begin(); i != frames.end(); ++i)
173 {
174 n++;
175 }
176 return n;*/
177 return frames.size();
178 }
179
180 void CAnimation::setFrame(int n)
181 {
182 int i2 = 0;
183 list<CSurface*>::iterator i;
184
185 for (i = frames.begin(); i != frames.end(); ++i)
186 {if (i2 >= n) break; i2++;}
187 currentFrame = i;
188 }
189
190 int CAnimation::getFrameIndex()
191 {
192 int i2 = 0;
193 for (list<CSurface*>::iterator i = frames.begin(); i != frames.end(); ++i)
194 {
195 if (i == currentFrame) break; i2++;
196 }
197 return i2;
198 }