]>
Dogcows Code - chaz/yoink/blob - src/Animation.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
15 #include <Moof/Error.hh>
16 #include <Moof/Manager.hh>
17 #include <Moof/Log.hh>
18 #include <Moof/Script.hh>
20 #include "Animation.hh"
24 * The collection of nested animation classes. The animation
25 * implementation consists of an Impl class which is allocated and
26 * initialized with the interface object. This class contains the specific
27 * fields which are required to run a single instance of an animation. The
28 * sequence data is loaded in a different class which can be shared amongst
29 * multiple animation implementation instances.
37 * Contains "global" animation data for the various animations which
38 * get loaded. This is a mippleton, so it will be shared amongst any
39 * animation which wants to use these loaded sequences.
42 class Data
: public Mf::Manager
<Data
>
47 * A frame of an animation sequence. A frame is merely an index
48 * which presumably represents a "slide" or tile which should be
49 * displayed, and the duration that is how long the slide will be
57 unsigned mIndex
; ///< Frame index.
58 Mf::Scalar mDuration
; ///< Frame duration.
61 * Construction is initialization. The frame data is loaded
62 * from a frame map which is probably loaded within an
66 Frame(Mf::Script
& script
, Mf::Script::Slot table
) :
70 table
.pushField("index");
71 script
[-1].get(mIndex
);
74 table
.pushField("duration");
75 script
[-1].get(mDuration
);
82 * A sequence is just a few attributes and a list of frames in the
83 * order that they should be played.
90 std::vector
<Frame
> mFrames
; ///< List of frames.
91 Mf::Scalar mDelay
; ///< Scale frame durations.
92 bool mLoop
; ///< Does the sequence repeat?
93 std::string mNext
; ///< Next sequence name.
96 * Construction is initialization. The constructor loads
97 * sequence data from the sequence map, presumably loaded from
98 * an animation file. The rest of the loading takes place in
99 * the frame's constructor which loads each individual frame.
102 Sequence(Mf::Script
& script
, Mf::Script::Slot table
) :
106 table
.pushField("delay");
107 script
[-1].get(mDelay
);
110 table
.pushField("loop");
111 script
[-1].get(mLoop
);
114 table
.pushField("next");
115 script
[-1].get(mNext
);
118 // TODO - sequence class/type not yet implemented
120 table
.pushField("frames");
121 Mf::Script::Slot frameTable
= script
.getTop();
122 if (frameTable
.isTable())
124 Mf::Script::Slot top
= script
[-1];
130 frameTable
.pushField();
134 mFrames
.push_back(Frame(script
, top
));
147 * Starts loading a file with animation data. Such a file is
148 * formatted as a map of named sequences. The sequence
149 * constructor loads each individual sequence.
152 void init(const std::string
& name
)
155 std::string
path(name
);
157 if (!Animation::getPath(path
))
159 Mf::Error(Mf::Error::RESOURCE_NOT_FOUND
).raise();
162 script
.importBaseLibrary();
163 importLogFunctions(script
);
164 importAnimationBindings(script
);
166 if (script
.doFile(path
) != Mf::Script::SUCCESS
)
174 int defineSequence(Mf::Script
& script
)
176 Mf::Script::Slot name
= script
[1].requireString();
177 Mf::Script::Slot table
= script
[2].requireTable();
182 mSequences
.insert(std::make_pair(nameStr
,
183 Sequence(script
, table
)));
189 void importAnimationBindings(Mf::Script
& script
)
191 script
.importFunction("DefineSequence",
192 boost::bind(&Data::defineSequence
,
195 script
.push(1); script
.set("ATTACK");
196 script
.push(2); script
.set("CHARGE");
197 script
.push(3); script
.set("FLY");
198 script
.push(4); script
.set("HIT");
199 script
.push(5); script
.set("JUMP");
200 script
.push(6); script
.set("RUN");
201 script
.push(7); script
.set("STAND");
205 std::map
<std::string
,Sequence
> mSequences
; ///< List of sequences.
210 * Construction is intialization.
213 Impl(const std::string
& name
) :
214 mData(Data::getInstance(name
)),
223 * Sets up the animation classes to "play" a named sequence. If
224 * another sequence was active, it will be replaced. Future updates
225 * will progress the new sequence.
228 void startSequence(const std::string
& name
)
230 std::map
<std::string
,Data::Sequence
>::iterator it
;
232 it
= mData
->mSequences
.find(name
);
234 if (it
!= mData
->mSequences
.end())
236 mCurrentSequence
= &(*it
).second
;
238 mFrameIndex
= mCurrentSequence
->mFrames
[0].mIndex
;
240 mFrameDuration
= mCurrentSequence
->mDelay
*
241 mCurrentSequence
->mFrames
[0].mDuration
;
246 * Updates or progresses the animation sequence. If the time interval
247 * surpasses the duration of the current frame, a new frame becomes the
248 * current frame. If the last frame of a sequence expires, the active
249 * sequence will switch automatically to the designated "next"
250 * sequence, or if none is specified but the sequence is set to loop,
251 * the first frame of the sequence will become the current frame, and
252 * the animation essentially starts over again.
255 void update(Mf::Scalar t
, Mf::Scalar dt
)
257 if (!mCurrentSequence
) return;
261 if (mTimeAccum
>= mFrameDuration
)
263 if (++mFrameCounter
>= mCurrentSequence
->mFrames
.size())
265 if (!mCurrentSequence
->mNext
.empty())
267 startSequence(mCurrentSequence
->mNext
);
269 else if (mCurrentSequence
->mLoop
)
276 mCurrentSequence
= 0;
280 mFrameIndex
= mCurrentSequence
->mFrames
[mFrameCounter
].mIndex
;
281 mTimeAccum
= mFrameDuration
- mTimeAccum
;
282 mFrameDuration
= mCurrentSequence
->mDelay
*
283 mCurrentSequence
->mFrames
[mFrameCounter
].mDuration
;
287 boost::shared_ptr
<Data
> mData
; ///< Internal data.
289 Data::Sequence
* mCurrentSequence
; ///< Active sequence.
290 unsigned mFrameCounter
; ///< Current frame.
291 unsigned mFrameIndex
; ///< Index of current frame.
292 Mf::Scalar mTimeAccum
; ///< Time accumulation.
293 Mf::Scalar mFrameDuration
; ///< Scaled frame duration.
297 Animation::Animation(const std::string
& name
) :
299 mImpl(new Animation::Impl(name
)) {}
302 void Animation::startSequence(const std::string
& name
)
305 mImpl
->startSequence(name
);
308 void Animation::update(Mf::Scalar t
, Mf::Scalar dt
)
311 mImpl
->update(t
, dt
);
316 * Gets the index for the current frame. This is presumably called by some
317 * drawing code which will draw the correct current frame.
320 unsigned Animation::getFrame() const
322 return mImpl
->mFrameIndex
;
327 * Specialized search location for animation files. They can be found in
328 * the "animations" subdirectory of any of the search directories.
331 bool Animation::getPath(std::string
& name
)
333 return Mf::Resource::getPath(name
, "animations/", "lua");
This page took 0.050573 seconds and 4 git commands to generate.