]> Dogcows Code - chaz/yoink/blobdiff - src/Animation.cc
fixed some resource management bugs
[chaz/yoink] / src / Animation.cc
index 6df85562b7252505229c22838ae9beda7fbdbbda..fead5cab1697f67682d5df9546f14f7308356779 100644 (file)
 **************************************************************************/
 
 #include <map>
+#include <stdexcept>
 #include <vector>
 
-#include <Moof/Error.hh>
-#include <Moof/Manager.hh>
-#include <Moof/Log.hh>
-#include <Moof/Script.hh>
+#include <moof/manager.hh>
+#include <moof/log.hh>
+#include <moof/script.hh>
 
 #include "Animation.hh"
 
 
 /**
  * The collection of nested animation classes.  The animation
- * implementation consists of an Impl class which is allocated and
+ * 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
+class Animation::impl
 {
 public:
 
@@ -39,7 +39,7 @@ public:
         * animation which wants to use these loaded sequences.
         */
 
-       class Data : public Mf::Manager<Data>
+       class Data : public moof::manager<Data>
        {
        public:
 
@@ -55,7 +55,7 @@ public:
                public:
 
                        unsigned        mIndex;                                 ///< Frame index.
-                       Mf::Scalar      mDuration;                              ///< Frame duration.
+                       moof::scalar    mDuration;                              ///< Frame duration.
 
                        /**
                         * Construction is initialization.  The frame data is loaded
@@ -63,17 +63,12 @@ public:
                         * animation file.
                         */
                
-                       Frame(Mf::Script& script, Mf::Script::Slot table) :
+                       Frame(const moof::script::slot& table) :
                                mIndex(0),
                                mDuration(1.0)
                        {
-                               table.pushField("index");
-                               script[-1].get(mIndex);
-                               script.pop();
-
-                               table.pushField("duration");
-                               script[-1].get(mDuration);
-                               script.pop();
+                               table.get(mIndex,    "index");
+                               table.get(mDuration, "duration");
                        }
                };
 
@@ -88,7 +83,7 @@ public:
                public:
 
                        std::vector<Frame>      mFrames;        ///< List of frames.
-                       Mf::Scalar                      mDelay;         ///< Scale frame durations.
+                       moof::scalar                    mDelay;         ///< Scale frame durations.
                        bool                            mLoop;          ///< Does the sequence repeat?
                        std::string                     mNext;          ///< Next sequence name.
 
@@ -99,46 +94,36 @@ public:
                         * the frame's constructor which loads each individual frame.
                         */
 
-                       Sequence(Mf::Script& script, Mf::Script::Slot table) :
+                       Sequence(const moof::script::slot& table) :
                                mDelay(0.0),
                                mLoop(true)
                        {
-                               table.pushField("delay");
-                               script[-1].get(mDelay);
-                               script.pop();
-
-                               table.pushField("loop");
-                               script[-1].get(mLoop);
-                               script.pop();
-
-                               table.pushField("next");
-                               script[-1].get(mNext);
-                               script.pop();
+                               table.get(mDelay, "delay");
+                               table.get(mLoop,  "loop");
+                               table.get(mNext,  "next");
 
                                // TODO - sequence class/type not yet implemented
 
-                               table.pushField("frames");
-                               Mf::Script::Slot frameTable = script.getTop();
-                               if (frameTable.isTable())
+                               moof::script::slot frameTable = table.push_field("frames");
+                               if (frameTable.is_table())
                                {
-                                       Mf::Script::Slot top = script[-1];
-                                       int index = 1;
-
-                                       for (;;)
+                                       int max = frameTable.length();
+                                       for (int index = 1; index <= max; ++index)
                                        {
-                                               script.push(index);
-                                               frameTable.pushField();
+                                               moof::script::slot top = frameTable.push_field(index);
 
-                                               if (top.isTable())
+                                               if (top.is_table())
                                                {
-                                                       mFrames.push_back(Frame(script, top));
+                                                       mFrames.push_back(Frame(top));
+                                               }
+                                               else
+                                               {
+                                                       moof::log_warning << "invalid frame at index "
+                                                                                  << index << std::endl;
                                                }
-                                               else break;
-
-                                               ++index;
                                        }
                                }
-                               script.pop();
+                               frameTable.remove();
                        }
                };
 
@@ -151,54 +136,53 @@ public:
 
                void init(const std::string& name)
                {
-                       Mf::Script script;
-                       std::string path(name);
+                       moof::script script;
+                       std::string path = moof::resource::find_file("animations/"+name, "lua");
                        
-                       if (!Animation::getPath(path))
-                       {
-                               Mf::Error(Mf::Error::RESOURCE_NOT_FOUND).raise();
-                       }
+                       //if (!resource::find(path))
+                       //{
+                               //throw std::runtime_error("cannot find resource " + name);
+                       //}
 
-                       script.importBaseLibrary();
-                       importLogFunctions(script);
+                       script.import_base_library();
+                       moof::log::import(script);
                        importAnimationBindings(script);
 
-                       if (script.doFile(path) != Mf::Script::SUCCESS)
+                       if (script.do_file(path) != moof::script::success)
                        {
                                std::string str;
                                script[-1].get(str);
-                               Mf::logWarning(str);
+                               moof::log_warning(str);
                        }
                }
 
-               int defineSequence(Mf::Script& script)
+               int defineSequence(moof::script& script)
                {
-                       Mf::Script::Slot name = script[1].requireString();
-                       Mf::Script::Slot table = script[2].requireTable();
+                       moof::script::slot name = script[1].require_string();
+                       moof::script::slot table = script[2].require_table();
 
                        std::string nameStr;
                        name.get(nameStr);
 
-                       mSequences.insert(std::make_pair(nameStr,
-                                                                                        Sequence(script, table)));
+                       mSequences.insert(std::make_pair(nameStr, Sequence(table)));
 
                        return 0;
                }
 
 
-               void importAnimationBindings(Mf::Script& script)
+               void importAnimationBindings(moof::script& script)
                {
-                       script.importFunction("DefineSequence",
+                       script.import_function("DefineSequence",
                                                                  boost::bind(&Data::defineSequence,
                                                                                          this, _1));
 
-                       script.push(1); script.set("ATTACK");
-                       script.push(2); script.set("CHARGE");
-                       script.push(3); script.set("FLY");
-                       script.push(4); script.set("HIT");
-                       script.push(5); script.set("JUMP");
-                       script.push(6); script.set("RUN");
-                       script.push(7); script.set("STAND");
+                       script.globals().set_field("ATTACK",    1);
+                       script.globals().set_field("CHARGE",    2);
+                       script.globals().set_field("FLY",       3);
+                       script.globals().set_field("HIT",       4);
+                       script.globals().set_field("JUMP",      5);
+                       script.globals().set_field("RUN",       6);
+                       script.globals().set_field("STAND",     7);
                }
 
 
@@ -210,8 +194,8 @@ public:
         * Construction is intialization.
         */
 
-       Impl(const std::string& name) :
-               mData(Data::getInstance(name)),
+       impl(const std::string& name) :
+               mData(Data::instance(name)),
                mCurrentSequence(0),
                mFrameCounter(0),
                mFrameIndex(0),
@@ -230,7 +214,6 @@ public:
                std::map<std::string,Data::Sequence>::iterator it;
 
                it = mData->mSequences.find(name);
-
                if (it != mData->mSequences.end())
                {
                        mCurrentSequence = &(*it).second;
@@ -252,7 +235,7 @@ public:
         * the animation essentially starts over again.
         */
 
-       void update(Mf::Scalar t, Mf::Scalar dt)
+       void update(moof::scalar t, moof::scalar dt)
        {
                if (!mCurrentSequence) return;
 
@@ -289,26 +272,26 @@ public:
        Data::Sequence*                 mCurrentSequence;       ///< Active sequence.
        unsigned                                mFrameCounter;          ///< Current frame.
        unsigned                                mFrameIndex;            ///< Index of current frame.
-       Mf::Scalar                              mTimeAccum;                     ///< Time accumulation.
-       Mf::Scalar                              mFrameDuration;         ///< Scaled frame duration.
+       moof::scalar                            mTimeAccum;                     ///< Time accumulation.
+       moof::scalar                            mFrameDuration;         ///< Scaled frame duration.
 };
 
 
 Animation::Animation(const std::string& name) :
        // pass through
-       mImpl(new Animation::Impl(name)) {}
+       impl_(new Animation::impl(name)) {}
 
 
 void Animation::startSequence(const std::string& name)
 {
        // pass through
-       mImpl->startSequence(name);
+       impl_->startSequence(name);
 }
 
-void Animation::update(Mf::Scalar t, Mf::Scalar dt)
+void Animation::update(moof::scalar t, moof::scalar dt)
 {
        // pass through
-       mImpl->update(t, dt);
+       impl_->update(t, dt);
 }
 
 
@@ -319,17 +302,6 @@ void Animation::update(Mf::Scalar t, Mf::Scalar dt)
 
 unsigned Animation::getFrame() const
 {
-       return mImpl->mFrameIndex;
-}
-
-
-/**
- * Specialized search location for animation files.  They can be found in
- * the "animations" subdirectory of any of the search directories.
- */
-
-bool Animation::getPath(std::string& name)
-{
-       return Mf::Resource::getPath(name, "animations/", "lua");
+       return impl_->mFrameIndex;
 }
 
This page took 0.027875 seconds and 4 git commands to generate.