]> Dogcows Code - chaz/yoink/blob - src/animation.cc
beginnings of scene rendering
[chaz/yoink] / src / animation.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <map>
30 #include <vector>
31
32 #include "serializable.hh"
33 #include "mippleton.hh"
34 #include "animation.hh"
35
36
37 namespace dc {
38
39
40 /**
41 * The collection of nested animation classes. The animation implementation
42 * consists of an animation_impl classes which is allocated and initialized with
43 * the interface object. This class contains the specific fields which are
44 * required to run a single instance of an animation. The sequence data is
45 * loaded in a difference class which can be shared amongst multiple animation
46 * implementation instances.
47 */
48
49 struct animation::animation_impl
50 {
51
52 /**
53 * Contains "global" animation data for the various animations which get
54 * loaded. This is a mippleton, so it will be shared amongst any animation
55 * which wants to use these loaded sequences.
56 */
57
58 struct animation_data : public mippleton<animation_data>
59 {
60 /**
61 * A frame of an animation sequence. A frame is merely an index which
62 * presumably represents a "slide" or tile which should be displayed,
63 * and the duration that is how long the slide will be shown.
64 */
65
66 struct frame
67 {
68 unsigned index; ///< Frame index.
69 scalar duration; ///< Frame duration.
70
71 /**
72 * Construction is initialization. The frame data is loaded from a
73 * frame map which is probably loaded within an animation file.
74 */
75
76 frame(serializable_ptr root) :
77 index(0),
78 duration(1.0)
79 {
80 std::map<std::string,serializable_ptr> rootObj;
81
82 if (root->get(rootObj))
83 {
84 std::map<std::string,serializable_ptr>::iterator i;
85 for (i = rootObj.begin(); i != rootObj.end(); i++)
86 {
87 std::string key = (*i).first;
88 if (key == "index")
89 {
90 long value = 0;
91 (*i).second->get(value);
92 index = unsigned(value);
93 }
94 else if (key == "duration")
95 {
96 double value = 0.0;
97 (*i).second->getNumber(value);
98 duration = scalar(value);
99 }
100 }
101 }
102 }
103 };
104
105
106 /**
107 * A sequence is just a few attributes and a list of frames in the order
108 * that they should be played.
109 */
110
111 struct sequence
112 {
113 std::vector<frame> frames; ///< List of frames.
114 scalar delay; ///< Scale frame durations.
115 bool loop; ///< Does the sequence repeat?
116 std::string next; ///< Next sequence name.
117
118 /**
119 * Construction is initialization. The constructor loads sequence
120 * data from the sequence map, presumably loaded from an animation
121 * file. The rest of the loading takes place in the frame's
122 * constructor which loads each individual frame.
123 */
124
125 sequence(serializable_ptr root) :
126 delay(0.0),
127 loop(true)
128 {
129 std::map<std::string,serializable_ptr> rootObj;
130
131 if (root->get(rootObj))
132 {
133 std::map<std::string,serializable_ptr>::iterator i;
134 for (i = rootObj.begin(); i != rootObj.end(); i++)
135 {
136 std::string key = (*i).first;
137
138 if (key == "frames")
139 {
140 std::vector<serializable_ptr> framesObj;
141
142 if ((*i).second->get(framesObj))
143 {
144 std::vector<serializable_ptr>::iterator j;
145
146 for (j = framesObj.begin();
147 j != framesObj.end(); j++)
148 {
149 if (*j)
150 {
151 frames.push_back(frame(*j));
152 }
153 }
154 }
155 }
156 else if (key == "delay")
157 {
158 double value;
159 (*i).second->getNumber(value);
160 delay = scalar(value);
161 }
162 else if (key == "loop")
163 {
164 (*i).second->get(loop);
165 }
166 else if (key == "next")
167 {
168 (*i).second->get(next);
169 }
170 }
171 }
172 }
173 };
174
175
176 /**
177 * Starts loading a file with animation data. Such a file is formatted
178 * as a map of named sequences. The sequence constructor loads each
179 * individual sequence.
180 */
181
182 void loadFromFile()
183 {
184 std::string filePath = animation::getPathToResource(getName());
185
186 deserializer in(filePath);
187
188 serializable_ptr root = in.deserialize();
189
190 if (root)
191 {
192 std::map<std::string,serializable_ptr> rootObj;
193
194 if (root->get(rootObj))
195 {
196 std::map<std::string,serializable_ptr>::iterator i;
197
198 for (i = rootObj.begin(); i != rootObj.end(); i++)
199 {
200 sequences.insert(std::pair<std::string,sequence>((*i).first,
201 sequence((*i).second)));
202 }
203 }
204 }
205 }
206
207 /**
208 * Construction is initialization. The animation class data container
209 * registers itself as a mippleton and then loads the animation data.
210 */
211
212 explicit animation_data(const std::string& name) :
213 mippleton<animation_data>(name)
214 {
215 loadFromFile();
216 }
217
218 std::map<std::string,sequence> sequences; ///< List of sequences.
219 };
220
221
222 /**
223 * Construction is intialization.
224 */
225
226 animation_impl(const std::string& name) :
227 data(animation_data::retain(name), &animation_data::release),
228 currentSequence(0),
229 frameCounter(0),
230 frameIndex(0),
231 timeAccum(0),
232 frameDuration(0) {}
233
234
235 /**
236 * Sets up the animation classes to "play" a named sequence. If another
237 * sequence was active, it will be replaced as the current sequence. Future
238 * updates will progress the new sequence.
239 */
240
241 void startSequence(const std::string& sequenceName)
242 {
243 std::map<std::string,animation_data::sequence>::iterator i;
244
245 i = data->sequences.find(sequenceName);
246
247 if (i != data->sequences.end())
248 {
249 currentSequence = &(*i).second;
250 frameCounter = 0;
251 frameIndex = currentSequence->frames[0].index;
252 timeAccum = 0.0;
253 frameDuration = currentSequence->delay *
254 currentSequence->frames[0].duration;
255 }
256 }
257
258 /**
259 * Updates or progresses the animation sequence. If the time interval
260 * surpasses the duration of the current frame, a new frame becomes the
261 * current frame. If the last frame of a sequence expires, the active
262 * sequence will switch automatically to the designated "next" sequence, or
263 * if none is specified but the sequence is set to loop, the first frame of
264 * the sequence will become the current frame, and the animation essentially
265 * starts over again.
266 */
267
268 void update(scalar t, scalar dt)
269 {
270 if (currentSequence)
271 {
272 timeAccum += dt;
273
274 if (timeAccum >= frameDuration)
275 {
276 if (++frameCounter >= currentSequence->frames.size())
277 {
278 if (!currentSequence->next.empty())
279 {
280 startSequence(currentSequence->next);
281 }
282 else if (currentSequence->loop)
283 {
284 frameCounter = 0;
285 }
286 else
287 {
288 frameCounter--;
289 currentSequence = 0;
290 }
291 }
292
293 frameIndex = currentSequence->frames[frameCounter].index;
294 timeAccum = frameDuration - timeAccum;
295 frameDuration = currentSequence->delay *
296 currentSequence->frames[frameCounter].duration;
297 }
298 }
299 }
300
301 boost::shared_ptr<animation_data> data; ///< Internal data.
302
303 animation_data::sequence* currentSequence; ///< Active sequence.
304 unsigned frameCounter; ///< Current frame.
305 unsigned frameIndex; ///< Index of current frame.
306 scalar timeAccum; ///< Time accumulation.
307 scalar frameDuration; ///< Scaled frame duration.
308 };
309
310
311 animation::animation(const std::string& name) :
312 // pass through
313 impl(new animation::animation_impl(name)) {}
314
315
316 void animation::startSequence(const std::string& sequenceName)
317 {
318 // pass through
319 impl->startSequence(sequenceName);
320 }
321
322 void animation::update(scalar t, scalar dt)
323 {
324 // pass through
325 impl->update(t, dt);
326 }
327
328
329 /**
330 * Gets the index for the current frame. This is presumably called by some
331 * drawing code which will draw the correct current frame.
332 */
333
334 unsigned animation::getFrame() const
335 {
336 return impl->frameIndex;
337 }
338
339
340 /**
341 * Specialized search location for animation files. They can be found in the
342 * "animations" subdirectory of any of the searched directories.
343 */
344
345 std::string animation::getPathToResource(const std::string& name)
346 {
347 return resource::getPathToResource("animations/" + name + ".json");
348 }
349
350
351 } // namespace dc
352
353 /** vim: set ts=4 sw=4 tw=80: *************************************************/
354
This page took 0.046399 seconds and 4 git commands to generate.