X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FAnimation.cc;h=2a3d03a5922e62a6ef68e4cf106dd3a0e8c1c99b;hp=593f44e1aff3df4a492ed2c277bfa91cd46704ff;hb=ed5fcf5f1357fc42749408f705e9ec55531ff006;hpb=7f3984f3f9524f5b6813e01ceb2fe576dadff94e diff --git a/src/Animation.cc b/src/Animation.cc index 593f44e..2a3d03a 100644 --- a/src/Animation.cc +++ b/src/Animation.cc @@ -1,35 +1,18 @@ -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ #include #include -#include +#include #include #include @@ -37,45 +20,46 @@ /** - * The collection of nested animation classes. The animation implementation - * consists of an Impl class which is allocated and initialized with the - * interface object. This class contains the specific fields which are required - * to run a single instance of an animation. The sequence data is loaded in a - * different class which can be shared amongst multiple animation implementation - * instances. + * The collection of nested animation classes. The animation + * implementation consists of an Impl class which is allocated and + * initialized with the interface object. This class contains the specific + * fields which are required to run a single instance of an animation. The + * sequence data is loaded in a different class which can be shared amongst + * multiple animation implementation instances. */ class Animation::Impl { - friend class Animation; +public: /** - * Contains "global" animation data for the various animations which get - * loaded. This is a mippleton, so it will be shared amongst any animation - * which wants to use these loaded sequences. + * Contains "global" animation data for the various animations which + * get loaded. This is a mippleton, so it will be shared amongst any + * animation which wants to use these loaded sequences. */ - class Data : public Mf::Library + class Data : public Mf::Manager { - friend class Impl; - friend class Mf::Library; + public: /** - * A frame of an animation sequence. A frame is merely an index which - * presumably represents a "slide" or tile which should be displayed, - * and the duration that is how long the slide will be shown. + * A frame of an animation sequence. A frame is merely an index + * which presumably represents a "slide" or tile which should be + * displayed, and the duration that is how long the slide will be + * shown. */ class Frame { - friend class Impl; + public: unsigned mIndex; ///< Frame index. Mf::Scalar mDuration; ///< Frame duration. /** - * Construction is initialization. The frame data is loaded from a - * frame map which is probably loaded within an animation file. + * Construction is initialization. The frame data is loaded + * from a frame map which is probably loaded within an + * animation file. */ Frame(Mf::Script& script, Mf::Script::Slot table) : @@ -94,24 +78,24 @@ class Animation::Impl /** - * A sequence is just a few attributes and a list of frames in the order - * that they should be played. + * A sequence is just a few attributes and a list of frames in the + * order that they should be played. */ - struct Sequence + class Sequence { - friend class Impl; + public: - std::vector mFrames; ///< List of frames. - Mf::Scalar mDelay; ///< Scale frame durations. - bool mLoop; ///< Does the sequence repeat? - std::string mNext; ///< Next sequence name. + std::vector mFrames; ///< List of frames. + Mf::Scalar mDelay; ///< Scale frame durations. + bool mLoop; ///< Does the sequence repeat? + std::string mNext; ///< Next sequence name. /** - * Construction is initialization. The constructor loads sequence - * data from the sequence map, presumably loaded from an animation - * file. The rest of the loading takes place in the frame's - * constructor which loads each individual frame. + * Construction is initialization. The constructor loads + * sequence data from the sequence map, presumably loaded from + * an animation file. The rest of the loading takes place in + * the frame's constructor which loads each individual frame. */ Sequence(Mf::Script& script, Mf::Script::Slot table) : @@ -144,8 +128,11 @@ class Animation::Impl script.push(index); frameTable.pushField(); - if (top.isTable()) mFrames.push_back(Frame(script, top)); - else break; + if (top.isTable()) + { + mFrames.push_back(Frame(script, top)); + } + else break; ++index; } @@ -156,18 +143,18 @@ class Animation::Impl /** - * Starts loading a file with animation data. Such a file is formatted - * as a map of named sequences. The sequence constructor loads each - * individual sequence. + * Starts loading a file with animation data. Such a file is + * formatted as a map of named sequences. The sequence + * constructor loads each individual sequence. */ - void loadFromFile() + void init(const std::string& name) { Mf::Script script; - std::string filePath = Animation::getPath(getName()); + std::string filePath = Animation::getPath(name); script.importBaseLibrary(); - importLogPrintFunction(script); + importLogFunctions(script); importAnimationBindings(script); if (script.doFile(filePath) != Mf::Script::SUCCESS) @@ -186,8 +173,8 @@ class Animation::Impl std::string nameStr; name.get(nameStr); - sequences.insert(std::pair(nameStr, - Sequence(script, table))); + mSequences.insert(std::make_pair(nameStr, + Sequence(script, table))); return 0; } @@ -196,7 +183,8 @@ class Animation::Impl void importAnimationBindings(Mf::Script& script) { script.importFunction("DefineSequence", - boost::bind(&Data::defineSequence, this, _1)); + boost::bind(&Data::defineSequence, + this, _1)); script.push(1); script.set("ATTACK"); script.push(2); script.set("CHARGE"); @@ -208,18 +196,7 @@ class Animation::Impl } - /** - * Construction is initialization. The animation class data container - * registers itself as a mippleton and then loads the animation data. - */ - - explicit Data(const std::string& name) : - Mf::Library(name) - { - loadFromFile(); - } - - std::map sequences; ///< List of sequences. + std::map mSequences; ///< List of sequences. }; @@ -237,25 +214,25 @@ class Animation::Impl /** - * Sets up the animation classes to "play" a named sequence. If another - * sequence was active, it will be replaced. Future updates will progress - * the new sequence. + * Sets up the animation classes to "play" a named sequence. If + * another sequence was active, it will be replaced. Future updates + * will progress the new sequence. */ void startSequence(const std::string& name) { std::map::iterator it; - it = mData->sequences.find(name); + it = mData->mSequences.find(name); - if (it != mData->sequences.end()) + if (it != mData->mSequences.end()) { mCurrentSequence = &(*it).second; mFrameCounter = 0; mFrameIndex = mCurrentSequence->mFrames[0].mIndex; mTimeAccum = 0.0; mFrameDuration = mCurrentSequence->mDelay * - mCurrentSequence->mFrames[0].mDuration; + mCurrentSequence->mFrames[0].mDuration; } } @@ -263,42 +240,41 @@ class Animation::Impl * Updates or progresses the animation sequence. If the time interval * surpasses the duration of the current frame, a new frame becomes the * current frame. If the last frame of a sequence expires, the active - * sequence will switch automatically to the designated "next" sequence, or - * if none is specified but the sequence is set to loop, the first frame of - * the sequence will become the current frame, and the animation essentially - * starts over again. + * sequence will switch automatically to the designated "next" + * sequence, or if none is specified but the sequence is set to loop, + * the first frame of the sequence will become the current frame, and + * the animation essentially starts over again. */ void update(Mf::Scalar t, Mf::Scalar dt) { - if (mCurrentSequence) - { - mTimeAccum += dt; + if (!mCurrentSequence) return; + + mTimeAccum += dt; - if (mTimeAccum >= mFrameDuration) + if (mTimeAccum >= mFrameDuration) + { + if (++mFrameCounter >= mCurrentSequence->mFrames.size()) { - if (++mFrameCounter >= mCurrentSequence->mFrames.size()) + if (!mCurrentSequence->mNext.empty()) { - if (!mCurrentSequence->mNext.empty()) - { - startSequence(mCurrentSequence->mNext); - } - else if (mCurrentSequence->mLoop) - { - mFrameCounter = 0; - } - else - { - mFrameCounter--; - mCurrentSequence = 0; - } + startSequence(mCurrentSequence->mNext); + } + else if (mCurrentSequence->mLoop) + { + mFrameCounter = 0; + } + else + { + mFrameCounter--; + mCurrentSequence = 0; } - - mFrameIndex = mCurrentSequence->mFrames[mFrameCounter].mIndex; - mTimeAccum = mFrameDuration - mTimeAccum; - mFrameDuration = mCurrentSequence->mDelay * - mCurrentSequence->mFrames[mFrameCounter].mDuration; } + + mFrameIndex = mCurrentSequence->mFrames[mFrameCounter].mIndex; + mTimeAccum = mFrameDuration - mTimeAccum; + mFrameDuration = mCurrentSequence->mDelay * + mCurrentSequence->mFrames[mFrameCounter].mDuration; } } @@ -342,8 +318,8 @@ unsigned Animation::getFrame() const /** - * Specialized search location for animation files. They can be found in the - * "animations" subdirectory of any of the searched directories. + * Specialized search location for animation files. They can be found in + * the "animations" subdirectory of any of the search directories. */ std::string Animation::getPath(const std::string& name) @@ -351,6 +327,3 @@ std::string Animation::getPath(const std::string& name) return Mf::Resource::getPath("animations/" + name + ".lua"); } - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ -