]>
Dogcows Code - chaz/yoink/blob - src/Animation.cc
b449a0fbd97a500e36cd10e987831117fee4bf08
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
32 #include <Moof/Log.hh>
33 #include <Moof/Mippleton.hh>
34 #include <Moof/Script.hh>
36 #include "Animation.hh"
40 * The collection of nested animation classes. The animation implementation
41 * consists of an Impl class which is allocated and initialized with the
42 * interface object. This class contains the specific fields which are required
43 * to run a single instance of an animation. The sequence data is loaded in a
44 * different class which can be shared amongst multiple animation implementation
50 friend class Animation
;
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.
58 class Data
: public Mf::Mippleton
<Data
>
61 friend class Mf::Mippleton
<Data
>;
64 * A frame of an animation sequence. A frame is merely an index which
65 * presumably represents a "slide" or tile which should be displayed,
66 * and the duration that is how long the slide will be shown.
71 unsigned index
; ///< Frame index.
72 Mf::Scalar duration
; ///< Frame duration.
75 * Construction is initialization. The frame data is loaded from a
76 * frame map which is probably loaded within an animation file.
79 Frame(Mf::Script
& script
, Mf::Script::Value table
) :
83 table
.pushField("index");
84 script
[-1].get(index
);
87 table
.pushField("duration");
88 script
[-1].get(duration
);
95 * A sequence is just a few attributes and a list of frames in the order
96 * that they should be played.
101 std::vector
<Frame
> frames
; ///< List of frames.
102 Mf::Scalar delay
; ///< Scale frame durations.
103 bool loop
; ///< Does the sequence repeat?
104 std::string next
; ///< Next sequence name.
107 * Construction is initialization. The constructor loads sequence
108 * data from the sequence map, presumably loaded from an animation
109 * file. The rest of the loading takes place in the frame's
110 * constructor which loads each individual frame.
113 Sequence(Mf::Script
& script
, Mf::Script::Value table
) :
117 table
.pushField("delay");
118 script
[-1].get(delay
);
121 table
.pushField("loop");
122 script
[-1].get(loop
);
125 table
.pushField("next");
126 script
[-1].get(next
);
129 // TODO - sequence class/type not yet implemented
131 table
.pushField("frames");
132 Mf::Script::Value frameTable
= script
.getTop();
133 if (frameTable
.isTable())
135 Mf::Script::Value top
= script
[-1];
141 frameTable
.pushField();
143 if (top
.isTable()) frames
.push_back(Frame(script
, top
));
155 * Starts loading a file with animation data. Such a file is formatted
156 * as a map of named sequences. The sequence constructor loads each
157 * individual sequence.
163 std::string filePath
= Animation::getPath(getName());
165 script
.importStandardLibraries();
166 importLogScript(script
);
167 importAnimationBindings(script
);
169 if (script
.doFile(filePath
) != Mf::Script::SUCCESS
)
173 Mf::logScript("%s", str
.c_str());
177 int defineSequence(Mf::Script
& script
)
179 Mf::Script::Value name
= script
[1].requireString();
180 Mf::Script::Value table
= script
[2].requireTable();
185 sequences
.insert(std::pair
<std::string
,Sequence
>(nameStr
,
186 Sequence(script
, table
)));
192 void importAnimationBindings(Mf::Script
& script
)
194 script
.importFunction("DefineSequence",
195 boost::bind(&Data::defineSequence
, this, _1
));
197 script
.push(1); script
.set("ATTACK");
198 script
.push(2); script
.set("CHARGE");
199 script
.push(3); script
.set("FLY");
200 script
.push(4); script
.set("HIT");
201 script
.push(5); script
.set("JUMP");
202 script
.push(6); script
.set("RUN");
203 script
.push(7); script
.set("STAND");
208 * Construction is initialization. The animation class data container
209 * registers itself as a mippleton and then loads the animation data.
212 explicit Data(const std::string
& name
) :
213 Mf::Mippleton
<Data
>(name
)
218 std::map
<std::string
,Sequence
> sequences
; ///< List of sequences.
223 * Construction is intialization.
226 Impl(const std::string
& name
) :
227 data(Data::getInstance(name
)),
236 * Sets up the animation classes to "play" a named sequence. If another
237 * sequence was active, it will be replaced. Future updates will progress
241 void startSequence(const std::string
& name
)
243 std::map
<std::string
,Data::Sequence
>::iterator it
;
245 it
= data
->sequences
.find(name
);
247 if (it
!= data
->sequences
.end())
249 currentSequence
= &(*it
).second
;
251 frameIndex
= currentSequence
->frames
[0].index
;
253 frameDuration
= currentSequence
->delay
*
254 currentSequence
->frames
[0].duration
;
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
268 void update(Mf::Scalar t
, Mf::Scalar dt
)
274 if (timeAccum
>= frameDuration
)
276 if (++frameCounter
>= currentSequence
->frames
.size())
278 if (!currentSequence
->next
.empty())
280 startSequence(currentSequence
->next
);
282 else if (currentSequence
->loop
)
293 frameIndex
= currentSequence
->frames
[frameCounter
].index
;
294 timeAccum
= frameDuration
- timeAccum
;
295 frameDuration
= currentSequence
->delay
*
296 currentSequence
->frames
[frameCounter
].duration
;
301 boost::shared_ptr
<Data
> data
; ///< Internal data.
303 Data::Sequence
* currentSequence
; ///< Active sequence.
304 unsigned frameCounter
; ///< Current frame.
305 unsigned frameIndex
; ///< Index of current frame.
306 Mf::Scalar timeAccum
; ///< Time accumulation.
307 Mf::Scalar frameDuration
; ///< Scaled frame duration.
311 Animation::Animation(const std::string
& name
) :
313 impl_(new Animation::Impl(name
)) {}
316 void Animation::startSequence(const std::string
& name
)
319 impl_
->startSequence(name
);
322 void Animation::update(Mf::Scalar t
, Mf::Scalar dt
)
325 impl_
->update(t
, dt
);
330 * Gets the index for the current frame. This is presumably called by some
331 * drawing code which will draw the correct current frame.
334 unsigned Animation::getFrame() const
336 return impl_
->frameIndex
;
341 * Specialized search location for animation files. They can be found in the
342 * "animations" subdirectory of any of the searched directories.
345 std::string
Animation::getPath(const std::string
& name
)
347 return Mf::Resource::getPath("animations/" + name
+ ".lua");
351 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.053062 seconds and 4 git commands to generate.