]>
Dogcows Code - chaz/yoink/blob - src/Animation.cc
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
14 #include <moof/manager.hh>
15 #include <moof/log.hh>
16 #include <moof/resource.hh>
17 #include <moof/script.hh>
19 #include "Animation.hh"
23 * The collection of nested animation classes. The animation implementation
24 * consists of an impl class which is allocated and initialized with the
25 * interface object. This class contains the specific fields which are
26 * required to run a single instance of an animation. The sequence data is
27 * loaded in a different class which can be shared amongst multiple animation
28 * implementation instances.
35 * Contains "global" animation data for the various animations which
36 * get loaded. This is a manager, so it will be shared amongst any
37 * animation which wants to use these loaded sequences.
39 class Data
: public moof::manager
<Data
>
44 * A frame of an animation sequence. A frame is merely an
45 * index which presumably represents a "slide" or tile which
46 * should be displayed, and the duration that is how long the
47 * slide will be shown.
53 unsigned mIndex
; ///< Frame index.
54 moof::scalar mDuration
; ///< Frame duration.
57 * Construction is initialization. The frame data is
58 * loaded from a frame map which is probably loaded
59 * within an animation file.
61 Frame(const moof::script::slot
& table
) :
65 table
.get(mIndex
, "index");
66 table
.get(mDuration
, "duration");
71 * A sequence is just a few attributes and a list of frames in
72 * the order that they should be played.
78 std::vector
<Frame
> mFrames
; ///< List of frames.
79 moof::scalar mDelay
; ///< Scale frame durations.
80 bool mLoop
; ///< Does the sequence repeat?
81 std::string mNext
; ///< Next sequence name.
84 * Construction is initialization. The constructor
85 * loads sequence data from the sequence map,
86 * presumably loaded from an animation file. The rest
87 * of the loading takes place in the frame's
88 * constructor which loads each individual frame.
90 Sequence(const moof::script::slot
& table
) :
94 table
.get(mDelay
, "delay");
95 table
.get(mLoop
, "loop");
96 table
.get(mNext
, "next");
98 // TODO - sequence class/type not yet implemented
100 moof::script::slot frameTable
= table
.push_field("frames");
101 if (frameTable
.is_table()) {
102 int max
= frameTable
.length();
103 for (int index
= 1; index
<= max
; ++index
) {
104 moof::script::slot top
= frameTable
.push_field(index
);
107 mFrames
.push_back(Frame(top
));
109 moof::log_warning
<< "invalid frame at index "
110 << index
<< std::endl
;
118 * Starts loading a file with animation data. Such a file is
119 * formatted as a map of named sequences. The sequence
120 * constructor loads each individual sequence.
122 void init(const std::string
& name
)
125 std::string path
= moof::resource::find_file("animations/"+name
, "lua");
127 script
.import_base_library();
128 moof::log::import(script
);
129 importAnimationBindings(script
);
131 if (script
.do_file(path
) != moof::script::success
) {
134 moof::log_warning(str
);
138 int defineSequence(moof::script
& script
)
140 moof::script::slot name
= script
[1].require_string();
141 moof::script::slot table
= script
[2].require_table();
146 mSequences
.insert(std::make_pair(nameStr
, Sequence(table
)));
151 void importAnimationBindings(moof::script
& script
)
153 script
.import_function("DefineSequence",
154 boost::bind(&Data::defineSequence
, this, _1
));
156 script
.globals().set_field("ATTACK", 1);
157 script
.globals().set_field("CHARGE", 2);
158 script
.globals().set_field("FLY", 3);
159 script
.globals().set_field("HIT", 4);
160 script
.globals().set_field("JUMP", 5);
161 script
.globals().set_field("RUN", 6);
162 script
.globals().set_field("STAND", 7);
165 std::map
<std::string
,Sequence
> mSequences
; ///< List of sequences.
169 * Construction is intialization.
171 impl(const std::string
& name
) :
172 mData(Data::instance(name
)),
180 * Sets up the animation classes to "play" a named sequence. If
181 * another sequence was active, it will be replaced. Future updates
182 * will progress the new sequence.
184 void startSequence(const std::string
& name
)
186 std::map
<std::string
,Data::Sequence
>::iterator it
;
188 it
= mData
->mSequences
.find(name
);
189 if (it
!= mData
->mSequences
.end()) {
190 mCurrentSequence
= &(*it
).second
;
192 mFrameIndex
= mCurrentSequence
->mFrames
[0].mIndex
;
194 mFrameDuration
= mCurrentSequence
->mDelay
*
195 mCurrentSequence
->mFrames
[0].mDuration
;
200 * Updates or progresses the animation sequence. If the time interval
201 * surpasses the duration of the current frame, a new frame becomes
202 * the current frame. If the last frame of a sequence expires, the
203 * active sequence will switch automatically to the designated "next"
204 * sequence, or if none is specified but the sequence is set to loop,
205 * the first frame of the sequence will become the current frame, and
206 * the animation essentially starts over again.
208 void update(moof::scalar t
, moof::scalar dt
)
210 if (!mCurrentSequence
) return;
214 if (mTimeAccum
>= mFrameDuration
) {
215 if (++mFrameCounter
>= mCurrentSequence
->mFrames
.size()) {
216 if (!mCurrentSequence
->mNext
.empty()) {
217 startSequence(mCurrentSequence
->mNext
);
219 else if (mCurrentSequence
->mLoop
) {
224 mCurrentSequence
= 0;
228 mFrameIndex
= mCurrentSequence
->mFrames
[mFrameCounter
].mIndex
;
229 mTimeAccum
= mFrameDuration
- mTimeAccum
;
230 mFrameDuration
= mCurrentSequence
->mDelay
*
231 mCurrentSequence
->mFrames
[mFrameCounter
].mDuration
;
235 boost::shared_ptr
<Data
> mData
; ///< Internal data.
237 Data::Sequence
* mCurrentSequence
; ///< Active sequence.
238 unsigned mFrameCounter
; ///< Current frame.
239 unsigned mFrameIndex
; ///< Index of current frame.
240 moof::scalar mTimeAccum
; ///< Time accumulation.
241 moof::scalar mFrameDuration
; ///< Scaled frame duration.
244 Animation::Animation(const std::string
& name
) :
246 impl_(new Animation::impl(name
)) {}
248 void Animation::startSequence(const std::string
& name
)
251 impl_
->startSequence(name
);
254 void Animation::update(moof::scalar t
, moof::scalar dt
)
257 impl_
->update(t
, dt
);
261 * Gets the index for the current frame. This is presumably called by some
262 * drawing code which will draw the correct current frame.
264 unsigned Animation::getFrame() const
266 return impl_
->mFrameIndex
;
This page took 0.043786 seconds and 4 git commands to generate.