]>
Dogcows Code - chaz/yoink/blob - Animation.cc
ab178df6f9850a27b0d94e36aad786fc40f3c439
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/Library.hh>
33 #include <Moof/Log.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::Library
<Data
>
61 friend class Mf::Library
<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.
73 unsigned mIndex
; ///< Frame index.
74 Mf::Scalar mDuration
; ///< Frame duration.
77 * Construction is initialization. The frame data is loaded from a
78 * frame map which is probably loaded within an animation file.
81 Frame(Mf::Script
& script
, Mf::Script::Value table
) :
85 table
.pushField("index");
86 script
[-1].get(mIndex
);
89 table
.pushField("duration");
90 script
[-1].get(mDuration
);
97 * A sequence is just a few attributes and a list of frames in the order
98 * that they should be played.
105 std::vector
<Frame
> mFrames
; ///< List of frames.
106 Mf::Scalar mDelay
; ///< Scale frame durations.
107 bool mLoop
; ///< Does the sequence repeat?
108 std::string mNext
; ///< Next sequence name.
111 * Construction is initialization. The constructor loads sequence
112 * data from the sequence map, presumably loaded from an animation
113 * file. The rest of the loading takes place in the frame's
114 * constructor which loads each individual frame.
117 Sequence(Mf::Script
& script
, Mf::Script::Value table
) :
121 table
.pushField("delay");
122 script
[-1].get(mDelay
);
125 table
.pushField("loop");
126 script
[-1].get(mLoop
);
129 table
.pushField("next");
130 script
[-1].get(mNext
);
133 // TODO - sequence class/type not yet implemented
135 table
.pushField("frames");
136 Mf::Script::Value frameTable
= script
.getTop();
137 if (frameTable
.isTable())
139 Mf::Script::Value top
= script
[-1];
145 frameTable
.pushField();
147 if (top
.isTable()) mFrames
.push_back(Frame(script
, top
));
159 * Starts loading a file with animation data. Such a file is formatted
160 * as a map of named sequences. The sequence constructor loads each
161 * individual sequence.
167 std::string filePath
= Animation::getPath(getName());
169 script
.importBaseLibrary();
170 importLogPrintFunction(script
);
171 importAnimationBindings(script
);
173 if (script
.doFile(filePath
) != Mf::Script::SUCCESS
)
177 Mf::logScript("%s", str
.c_str());
181 int defineSequence(Mf::Script
& script
)
183 Mf::Script::Value name
= script
[1].requireString();
184 Mf::Script::Value table
= script
[2].requireTable();
189 sequences
.insert(std::pair
<std::string
,Sequence
>(nameStr
,
190 Sequence(script
, table
)));
196 void importAnimationBindings(Mf::Script
& script
)
198 script
.importFunction("DefineSequence",
199 boost::bind(&Data::defineSequence
, this, _1
));
201 script
.push(1); script
.set("ATTACK");
202 script
.push(2); script
.set("CHARGE");
203 script
.push(3); script
.set("FLY");
204 script
.push(4); script
.set("HIT");
205 script
.push(5); script
.set("JUMP");
206 script
.push(6); script
.set("RUN");
207 script
.push(7); script
.set("STAND");
212 * Construction is initialization. The animation class data container
213 * registers itself as a mippleton and then loads the animation data.
216 explicit Data(const std::string
& name
) :
217 Mf::Library
<Data
>(name
)
222 std::map
<std::string
,Sequence
> sequences
; ///< List of sequences.
227 * Construction is intialization.
230 Impl(const std::string
& name
) :
231 mData(Data::getInstance(name
)),
240 * Sets up the animation classes to "play" a named sequence. If another
241 * sequence was active, it will be replaced. Future updates will progress
245 void startSequence(const std::string
& name
)
247 std::map
<std::string
,Data::Sequence
>::iterator it
;
249 it
= mData
->sequences
.find(name
);
251 if (it
!= mData
->sequences
.end())
253 mCurrentSequence
= &(*it
).second
;
255 mFrameIndex
= mCurrentSequence
->mFrames
[0].mIndex
;
257 mFrameDuration
= mCurrentSequence
->mDelay
*
258 mCurrentSequence
->mFrames
[0].mDuration
;
263 * Updates or progresses the animation sequence. If the time interval
264 * surpasses the duration of the current frame, a new frame becomes the
265 * current frame. If the last frame of a sequence expires, the active
266 * sequence will switch automatically to the designated "next" sequence, or
267 * if none is specified but the sequence is set to loop, the first frame of
268 * the sequence will become the current frame, and the animation essentially
272 void update(Mf::Scalar t
, Mf::Scalar dt
)
274 if (mCurrentSequence
)
278 if (mTimeAccum
>= mFrameDuration
)
280 if (++mFrameCounter
>= mCurrentSequence
->mFrames
.size())
282 if (!mCurrentSequence
->mNext
.empty())
284 startSequence(mCurrentSequence
->mNext
);
286 else if (mCurrentSequence
->mLoop
)
293 mCurrentSequence
= 0;
297 mFrameIndex
= mCurrentSequence
->mFrames
[mFrameCounter
].mIndex
;
298 mTimeAccum
= mFrameDuration
- mTimeAccum
;
299 mFrameDuration
= mCurrentSequence
->mDelay
*
300 mCurrentSequence
->mFrames
[mFrameCounter
].mDuration
;
305 boost::shared_ptr
<Data
> mData
; ///< Internal data.
307 Data::Sequence
* mCurrentSequence
; ///< Active sequence.
308 unsigned mFrameCounter
; ///< Current frame.
309 unsigned mFrameIndex
; ///< Index of current frame.
310 Mf::Scalar mTimeAccum
; ///< Time accumulation.
311 Mf::Scalar mFrameDuration
; ///< Scaled frame duration.
315 Animation::Animation(const std::string
& name
) :
317 mImpl(new Animation::Impl(name
)) {}
320 void Animation::startSequence(const std::string
& name
)
323 mImpl
->startSequence(name
);
326 void Animation::update(Mf::Scalar t
, Mf::Scalar dt
)
329 mImpl
->update(t
, dt
);
334 * Gets the index for the current frame. This is presumably called by some
335 * drawing code which will draw the correct current frame.
338 unsigned Animation::getFrame() const
340 return mImpl
->mFrameIndex
;
345 * Specialized search location for animation files. They can be found in the
346 * "animations" subdirectory of any of the searched directories.
349 std::string
Animation::getPath(const std::string
& name
)
351 return Mf::Resource::getPath("animations/" + name
+ ".lua");
355 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.051443 seconds and 3 git commands to generate.