]>
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 **************************************************************************/
16 #include <moof/manager.hh>
17 #include <moof/log.hh>
18 #include <moof/resource.hh>
19 #include <moof/script.hh>
21 #include "Animation.hh"
25 * The collection of nested animation classes. The animation
26 * implementation consists of an impl class which is allocated and
27 * initialized with the interface object. This class contains the specific
28 * fields which are required to run a single instance of an animation. The
29 * sequence data is loaded in a different class which can be shared amongst
30 * multiple animation implementation instances.
38 * Contains "global" animation data for the various animations which
39 * get loaded. This is a mippleton, so it will be shared amongst any
40 * animation which wants to use these loaded sequences.
43 class Data
: public moof::manager
<Data
>
48 * A frame of an animation sequence. A frame is merely an index
49 * which presumably represents a "slide" or tile which should be
50 * displayed, and the duration that is how long the slide will be
58 unsigned mIndex
; ///< Frame index.
59 moof::scalar mDuration
; ///< Frame duration.
62 * Construction is initialization. The frame data is loaded
63 * from a frame map which is probably loaded within an
67 Frame(const moof::script::slot
& table
) :
71 table
.get(mIndex
, "index");
72 table
.get(mDuration
, "duration");
78 * A sequence is just a few attributes and a list of frames in the
79 * order that they should be played.
86 std::vector
<Frame
> mFrames
; ///< List of frames.
87 moof::scalar mDelay
; ///< Scale frame durations.
88 bool mLoop
; ///< Does the sequence repeat?
89 std::string mNext
; ///< Next sequence name.
92 * Construction is initialization. The constructor loads
93 * sequence data from the sequence map, presumably loaded from
94 * an animation file. The rest of the loading takes place in
95 * the frame's constructor which loads each individual frame.
98 Sequence(const moof::script::slot
& table
) :
102 table
.get(mDelay
, "delay");
103 table
.get(mLoop
, "loop");
104 table
.get(mNext
, "next");
106 // TODO - sequence class/type not yet implemented
108 moof::script::slot frameTable
= table
.push_field("frames");
109 if (frameTable
.is_table())
111 int max
= frameTable
.length();
112 for (int index
= 1; index
<= max
; ++index
)
114 moof::script::slot top
= frameTable
.push_field(index
);
118 mFrames
.push_back(Frame(top
));
122 moof::log_warning
<< "invalid frame at index "
123 << index
<< std::endl
;
133 * Starts loading a file with animation data. Such a file is
134 * formatted as a map of named sequences. The sequence
135 * constructor loads each individual sequence.
138 void init(const std::string
& name
)
141 std::string path
= moof::resource::find_file("animations/"+name
, "lua");
143 script
.import_base_library();
144 moof::log::import(script
);
145 importAnimationBindings(script
);
147 if (script
.do_file(path
) != moof::script::success
)
151 moof::log_warning(str
);
155 int defineSequence(moof::script
& script
)
157 moof::script::slot name
= script
[1].require_string();
158 moof::script::slot table
= script
[2].require_table();
163 mSequences
.insert(std::make_pair(nameStr
, Sequence(table
)));
169 void importAnimationBindings(moof::script
& script
)
171 script
.import_function("DefineSequence",
172 boost::bind(&Data::defineSequence
,
175 script
.globals().set_field("ATTACK", 1);
176 script
.globals().set_field("CHARGE", 2);
177 script
.globals().set_field("FLY", 3);
178 script
.globals().set_field("HIT", 4);
179 script
.globals().set_field("JUMP", 5);
180 script
.globals().set_field("RUN", 6);
181 script
.globals().set_field("STAND", 7);
185 std::map
<std::string
,Sequence
> mSequences
; ///< List of sequences.
190 * Construction is intialization.
193 impl(const std::string
& name
) :
194 mData(Data::instance(name
)),
203 * Sets up the animation classes to "play" a named sequence. If
204 * another sequence was active, it will be replaced. Future updates
205 * will progress the new sequence.
208 void startSequence(const std::string
& name
)
210 std::map
<std::string
,Data::Sequence
>::iterator it
;
212 it
= mData
->mSequences
.find(name
);
213 if (it
!= mData
->mSequences
.end())
215 mCurrentSequence
= &(*it
).second
;
217 mFrameIndex
= mCurrentSequence
->mFrames
[0].mIndex
;
219 mFrameDuration
= mCurrentSequence
->mDelay
*
220 mCurrentSequence
->mFrames
[0].mDuration
;
225 * Updates or progresses the animation sequence. If the time interval
226 * surpasses the duration of the current frame, a new frame becomes the
227 * current frame. If the last frame of a sequence expires, the active
228 * sequence will switch automatically to the designated "next"
229 * sequence, or if none is specified but the sequence is set to loop,
230 * the first frame of the sequence will become the current frame, and
231 * the animation essentially starts over again.
234 void update(moof::scalar t
, moof::scalar dt
)
236 if (!mCurrentSequence
) return;
240 if (mTimeAccum
>= mFrameDuration
)
242 if (++mFrameCounter
>= mCurrentSequence
->mFrames
.size())
244 if (!mCurrentSequence
->mNext
.empty())
246 startSequence(mCurrentSequence
->mNext
);
248 else if (mCurrentSequence
->mLoop
)
255 mCurrentSequence
= 0;
259 mFrameIndex
= mCurrentSequence
->mFrames
[mFrameCounter
].mIndex
;
260 mTimeAccum
= mFrameDuration
- mTimeAccum
;
261 mFrameDuration
= mCurrentSequence
->mDelay
*
262 mCurrentSequence
->mFrames
[mFrameCounter
].mDuration
;
266 boost::shared_ptr
<Data
> mData
; ///< Internal data.
268 Data::Sequence
* mCurrentSequence
; ///< Active sequence.
269 unsigned mFrameCounter
; ///< Current frame.
270 unsigned mFrameIndex
; ///< Index of current frame.
271 moof::scalar mTimeAccum
; ///< Time accumulation.
272 moof::scalar mFrameDuration
; ///< Scaled frame duration.
276 Animation::Animation(const std::string
& name
) :
278 impl_(new Animation::impl(name
)) {}
281 void Animation::startSequence(const std::string
& name
)
284 impl_
->startSequence(name
);
287 void Animation::update(moof::scalar t
, moof::scalar dt
)
290 impl_
->update(t
, dt
);
295 * Gets the index for the current frame. This is presumably called by some
296 * drawing code which will draw the correct current frame.
299 unsigned Animation::getFrame() const
301 return impl_
->mFrameIndex
;
This page took 0.046102 seconds and 4 git commands to generate.