]> Dogcows Code - chaz/yoink/commitdiff
script API improvements
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 14 Mar 2010 18:31:35 +0000 (12:31 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 14 Mar 2010 18:31:35 +0000 (12:31 -0600)
16 files changed:
cscope.make
data/scenes/Classic.lua
src/Animation.cc
src/GameLayer.cc
src/Makefile.am
src/Moof/Aabb.cc [new file with mode: 0644]
src/Moof/Aabb.hh
src/Moof/Interpolator.hh
src/Moof/Log.hh
src/Moof/Math.hh
src/Moof/Script.hh
src/Moof/Settings.cc
src/Moof/Settings.hh
src/Moof/Texture.cc
src/Moof/Transition.hh
src/Scene.cc

index 47c2811fd9857a8ad0c5eba3d4537d97d300e2e3..3349d6d8a71c4f16e47e78d27707820dd0b5e292 100755 (executable)
@@ -1,12 +1,12 @@
 #!/bin/sh
 
-current=`pwd`
+current="$PWD"
 cd /
 find "$current" \
 -iname "*.c"   -o \
 -iname "*.h"   -o \
 -iname "*.cc"  -o \
--iname "*.hh"  |sed '/ / s/^\(.*\)/"\1"/' >"$current/cscope.files"
+-iname "*.hh"  | sed '/ / s/^\(.*\)/"\1"/' >"$current/cscope.files"
 
 cd "$current"
 
index 75ea02a54c99777b02e3e60585403f30ca4da6d4..2d00a67755fd2fd41d7ae2d57beba36e9a3465ac 100644 (file)
@@ -5,11 +5,6 @@ LogInfo("-----",
                "Converted to Lua by Charles McGarvey",
                "-----")
 
---for key,value in pairs(_G) do print(key, value) end
-meh = Blah:new()
-meh:sayHello()
-print(meh:myStr(3.1415))
-
 -- Scene API:
 --
 -- Functions:
@@ -26,8 +21,7 @@ print(meh:myStr(3.1415))
 -- detail - level of detail of the scene (HIGH, MEDIUM, or LOW)
 
 
-SetBounds({-5, 0, -6}, {45, 15, 7})
-
+SetBounds({-5, 0, -6}, {45, 15, 4})
 
 -- Left end tower block
 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index 6df85562b7252505229c22838ae9beda7fbdbbda..bd31f9b5dc26a8248952d8d7b5d5028bc6e45a63 100644 (file)
@@ -63,17 +63,12 @@ public:
                         * animation file.
                         */
                
-                       Frame(Mf::Script& script, Mf::Script::Slot table) :
+                       Frame(const Mf::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");
                        }
                };
 
@@ -99,46 +94,36 @@ public:
                         * the frame's constructor which loads each individual frame.
                         */
 
-                       Sequence(Mf::Script& script, Mf::Script::Slot table) :
+                       Sequence(const Mf::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();
+                               Mf::Script::Slot frameTable = table.pushField("frames");
                                if (frameTable.isTable())
                                {
-                                       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();
+                                               Mf::Script::Slot top = frameTable.pushField(index);
 
                                                if (top.isTable())
                                                {
-                                                       mFrames.push_back(Frame(script, top));
+                                                       mFrames.push_back(Frame(top));
+                                               }
+                                               else
+                                               {
+                                                       Mf::logWarning << "invalid frame at index "
+                                                                                  << index << std::endl;
                                                }
-                                               else break;
-
-                                               ++index;
                                        }
                                }
-                               script.pop();
+                               frameTable.remove();
                        }
                };
 
@@ -179,8 +164,7 @@ public:
                        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;
                }
@@ -192,13 +176,13 @@ public:
                                                                  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().setField("ATTACK",     1);
+                       script.globals().setField("CHARGE",     2);
+                       script.globals().setField("FLY",        3);
+                       script.globals().setField("HIT",        4);
+                       script.globals().setField("JUMP",       5);
+                       script.globals().setField("RUN",        6);
+                       script.globals().setField("STAND",      7);
                }
 
 
index 8f2f4ec006d953ae70bfe5083b38385d0eb4abc5..d68522ee1a8ef826c3730927342a19597135d536 100644 (file)
@@ -43,8 +43,8 @@ void GameLayer::loadSceneLoader()
                throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
        }
 
-       mState.script.getGlobalTable().pushField("scenes");
-       mState.script.getTop().get(mState.sceneList);
+       mState.script.globals().pushField("scenes");
+       mState.script.top().get(mState.sceneList);
        if (mState.sceneList.size() == 0)
        {
                throw Mf::Error(Mf::Error::SCRIPT_ERROR,
@@ -69,17 +69,14 @@ void GameLayer::advanceScene(Mf::Settings& settings)
                        throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
                }
 
-               mState.script.getGlobalTable().pushField("Event");
-               if (mState.script[-1].isTable())
+               Mf::Script::Slot table = mState.script.globals().pushField("Event");
+               if (table.isTable())
                {
-                       mState.script[-1].pushField("Think");
-                       mState.script.set("Think", Mf::Script::REGISTRY);
-                       mState.script.pop(2);
-               }
-               else
-               {
-                       mState.script.pop();
+                       mState.script.push("Think");
+                       table.pushField("Think");
+                       mState.script.registry().setField();
                }
+               mState.script.pop();
        }
 }
 
@@ -149,7 +146,7 @@ void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
 
 void GameLayer::thinkTimer()
 {
-       mState.script.getRegistryTable().pushField("Think");
+       mState.script.registry().pushField("Think");
        if (mState.script[-1].isFunction()) mState.script.call();
        else                                mState.script.pop();
 }
index 9a7f5146e454789933ca73128d720488f1e5429d..b63b7d2aa431f1cbe14240c157439a2910b32b59 100644 (file)
@@ -12,6 +12,7 @@
 noinst_LIBRARIES = libmoof.a
 
 libmoof_a_SOURCES = \
+                                       Moof/Aabb.cc \
                                        Moof/Aabb.hh \
                                        Moof/Backend.cc \
                                        Moof/Backend.hh \
diff --git a/src/Moof/Aabb.cc b/src/Moof/Aabb.cc
new file mode 100644 (file)
index 0000000..125e9d1
--- /dev/null
@@ -0,0 +1,25 @@
+
+/*]  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 "Aabb.hh"
+#include "Script.hh"
+
+
+namespace Mf {
+
+
+void Aabb::importClass(Script& script)
+{
+}
+
+
+} // namespace Mf
+
index 8fcdbc3787114aca620ba3bdf832698d119f66f3..e510ce3b3b86db2191fd64200db06cb9bed3ac37 100644 (file)
@@ -26,6 +26,9 @@
 namespace Mf {
 
 
+class Script;
+
+
 /**
  * Axis-aligned Bounding Box
  */
@@ -212,6 +215,9 @@ struct Aabb : public Cullable, public Drawable, public Shape<D>
        {
                return true;
        }
+
+
+       static void importClass(Script& script);
 };
 
 
index d918a8df1bea77a7c3c79d81a901901446157b45..db91e104253dfea7516a6c8201c5bb79ba14a6c5 100644 (file)
@@ -117,7 +117,7 @@ private:
 };
 
 
-template <typename T = Scalar>
+template <class T = Scalar>
 class Linear
 {
 public:
index c7736301fc679f6ede496933d0eccbc866d530e1..324fdeed8555b0c747c1fd8eecacae903496cac7 100644 (file)
@@ -64,7 +64,7 @@ public:
                mType(type) /* only pass literal strings */ {}
 
 
-       template <typename T>
+       template <class T>
        void operator () (const T& item)
        {
                *this << item << std::endl;
@@ -72,7 +72,7 @@ public:
 
 private:
 
-       template <typename T> friend std::ostream& operator << (Log&, const T&);
+       template <class T> friend std::ostream& operator << (Log&, const T&);
 
        static Level    gLevel;
 
@@ -89,7 +89,7 @@ extern Log logWarning;
 extern Log logInfo;
 
 
-template <typename T>
+template <class T>
 inline std::ostream& operator << (Log& logObj, const T& item)
 {
        if (Log::gLevel < logObj.mLevel) return nullLog;
index 429320768c98aae2b1adf4a7643cf9095acb2f50..d557c7f15114d7a07e15d393cbd66728bfba1f76 100644 (file)
@@ -85,7 +85,7 @@ inline Vector3 promote(const Vector2& vec, Scalar extra = 0.0)
 }
 
 
-template <typename R, typename P>
+template <class R, class P>
 inline R convert(const P& p)
 {
        return R(p);
@@ -115,11 +115,11 @@ inline Vector3 convert<Vector3,Vector2>(const Vector2& vec)
        return Vector3(vec[0], vec[1], SCALAR(0.0));
 }
 
-template <typename P>
+template <class P>
 struct cast
 {
        cast(const P& p) : param(p) {}
-       template <typename R>
+       template <class R>
        operator R() { return convert<R,P>(param); }
 private:
        const P& param;
@@ -153,7 +153,7 @@ inline bool isEqual(Scalar a, Scalar b, Scalar epsilon = EPSILON)
 // Derivative_Type operator+(const Derivative_Type& other) const
 // Derivative_Type operator*(const Derivative_Type& other) const
 
-template<typename S, typename D>
+template <class S, class D>
 inline D evaluate(const S& state, Scalar t)
 {
        D derivative;
@@ -161,7 +161,7 @@ inline D evaluate(const S& state, Scalar t)
        return derivative;
 }
 
-template<typename S, typename D>
+template <class S, class D>
 inline D evaluate(S state,  Scalar t, Scalar dt, const D& derivative)
 {
        state.step(derivative, dt);
@@ -169,7 +169,7 @@ inline D evaluate(S state,  Scalar t, Scalar dt, const D& derivative)
 }
 
 
-template<typename S, typename D>
+template <class S, class D>
 inline void euler(S& state, Scalar t, Scalar dt)
 {
        D a = evaluate<S,D>(state, t);
@@ -177,7 +177,7 @@ inline void euler(S& state, Scalar t, Scalar dt)
        state.step(a, dt);
 }
 
-template<typename S, typename D>
+template <class S, class D>
 inline void rk2(S& state, Scalar t, Scalar dt)
 {
        D a = evaluate<S,D>(state, t);
@@ -186,7 +186,7 @@ inline void rk2(S& state, Scalar t, Scalar dt)
        state.step(b, dt);
 }
 
-template<typename S, typename D>
+template <class S, class D>
 inline void rk4(S& state, Scalar t, Scalar dt)
 {
        D a = evaluate<S,D>(state, t);
index 620db5bc6906c82bd58446be23a2075367c20208..08284117359418234f4df58b2d84b04bbd34ca7d 100644 (file)
@@ -99,17 +99,9 @@ public:
                int index;
 
 
-               /**
-                * A default-constructed Slot is invalid until a valid Slot is
-                * assigned to it.  The only method that should be called on such a
-                * Slot is isValid(), otherwise chaos may ensue.  In this case, the
-                * Slot will be invalid even if index is manually changed to a
-                * valid index.  You have to index the script itself to get a valid
-                * Slot.
-                */
-               Slot(lua_State* s = 0, int i = 0) :
+               Slot(const Script& s, int i = 0) :
                        index(i),
-                       mState(s) {}
+                       mScript(const_cast<Script&>(s)) {}
 
                /**
                 * A copied value presently points to the same value, except the
@@ -118,36 +110,36 @@ public:
                 * normalized index copied into the new value object.
                 */
 
-               Slot(const Slot& copy) :
-                       index(copy.getRealIndex()),
-                       mState(copy.mState) {}
+               //Slot(const Slot& copy) :
+                       //index(copy.positiveIndex()),
+                       //mScript(copy.mScript) {}
 
 
                // check the type of the value
                bool isBoolean() const
-               { return (bool)lua_isboolean(mState, index); }
+               { return (bool)lua_isboolean(mScript.mState, index); }
+               bool isImportedFunction() const
+               { return (bool)lua_iscfunction(mScript.mState, index); }
                bool isFunction() const
-               { return (bool)lua_isfunction(mState, index); }
+               { return (bool)lua_isfunction(mScript.mState, index); }
                bool isNil() const
-               { return (bool)lua_isnil(mState, index); }
+               { return (bool)lua_isnil(mScript.mState, index); }
                bool isNone() const
-               { return (bool)lua_isnone(mState, index); }
-               bool isValid() const
-               { return mState != 0 && !isNone(); }
+               { return (bool)lua_isnone(mScript.mState, index); }
                bool isNoneOrNil() const
-               { return (bool)lua_isnoneornil(mState, index); }
+               { return (bool)lua_isnoneornil(mScript.mState, index); }
                bool isNumber() const
-               { return (bool)lua_isnumber(mState, index); }
+               { return (bool)lua_isnumber(mScript.mState, index); }
                bool isString() const
-               { return (bool)lua_isstring(mState, index); }
+               { return (bool)lua_isstring(mScript.mState, index); }
                bool isTable() const
-               { return (bool)lua_istable(mState, index); }
+               { return (bool)lua_istable(mScript.mState, index); }
                bool isThread() const
-               { return (bool)lua_isthread(mState, index); }
+               { return (bool)lua_isthread(mScript.mState, index); }
                bool isData() const
-               { return (bool)lua_isuserdata(mState, index); }
+               { return (bool)lua_isuserdata(mScript.mState, index); }
                bool isLightData() const
-               { return (bool)lua_islightuserdata(mState, index); }
+               { return (bool)lua_islightuserdata(mScript.mState, index); }
 
                /**
                 * Check the value and throw an error if its the wrong type.
@@ -163,58 +155,83 @@ public:
                 * before any C++ objects are even constructed.
                 */
 
-               void requireType(Type type) const
+               void requireType(Type t) const
                {
-                       if (type != getType())
+                       if (t != type())
                        {
-                               luaL_typerror(mState, index, lua_typename(mState, type));
+                               luaL_typerror(mScript.mState, index,
+                                                         lua_typename(mScript.mState, t));
                        }
                }
 
-               void throwError(const char* error)
+               void raise(const char* error)
                {
-                       luaL_argerror(mState, index, error);
+                       luaL_argerror(mScript.mState, index, error);
                }
 
 
                Slot& requireBoolean()
                {
-                       if (!isBoolean()) luaL_typerror(mState, index, "boolean");
+                       if (!isBoolean())
+                       {
+                               luaL_typerror(mScript.mState, index, "boolean");
+                       }
                        return *this;
                }
                Slot& requireNumber()
                {
-                       if (!isNumber()) luaL_typerror(mState, index, "number");
+                       if (!isNumber())
+                       {
+                               luaL_typerror(mScript.mState, index, "number");
+                       }
                        return *this;
                }
                Slot& requireString()
                {
-                       if (!isString()) luaL_typerror(mState, index, "string");
+                       if (!isString())
+                       {
+                               luaL_typerror(mScript.mState, index, "string");
+                       }
                        return *this;
                }
                Slot& requireTable()
                {
-                       if (!isTable()) luaL_typerror(mState, index, "table");
+                       if (!isTable())
+                       {
+                               luaL_typerror(mScript.mState, index, "table");
+                       }
                        return *this;
                }
                Slot& requireFunction()
                {
-                       if (!isFunction()) luaL_typerror(mState, index, "function");
+                       if (!isFunction())
+                       {
+                               luaL_typerror(mScript.mState, index, "function");
+                       }
                        return *this;
                }
                Slot& requireData()
                {
-                       if (!isData()) luaL_typerror(mState, index, "data");
+                       if (!isData())
+                       {
+                               luaL_typerror(mScript.mState, index, "data");
+                       }
                        return *this;
                }
                Slot& requireNil()
                {
-                       if (!isNil()) luaL_typerror(mState, index, "nil");
+                       if (!isNil())
+                       {
+                               luaL_typerror(mScript.mState, index, "nil");
+                       }
                        return *this;
                }
                Slot& requireThread()
                {
-                       if (!isThread()) luaL_typerror(mState, index, "thread");
+                       if (!isThread())
+                       {
+                               luaL_typerror(mScript.mState, index, "thread");
+                       }
                        return *this;
                }
 
@@ -223,18 +240,18 @@ public:
                 * Get the type of the value.
                 */
 
-               Type getType() const
+               Type type() const
                {
-                       return (Type)lua_type(mState, index);
+                       return (Type)lua_type(mScript.mState, index);
                }
 
                /**
                 * Get the name of the type of the value as a string.
                 */
 
-               std::string getTypeName() const
+               std::string typeName() const
                {
-                       return std::string(luaL_typename(mState, index));
+                       return std::string(luaL_typename(mScript.mState, index));
                }
 
 
@@ -243,14 +260,14 @@ public:
                 * Lua.
                 */
 
-               size_t getLength() const
+               size_t length() const
                {
-                       return lua_objlen(mState, index);
+                       return lua_objlen(mScript.mState, index);
                }
 
-               int getRealIndex() const
+               int positiveIndex() const
                {
-                       if (index < 0) return lua_gettop(mState) + 1 + index;
+                       if (index < 0) return index + lua_gettop(mScript.mState) + 1;
                        else           return index;
                }
 
@@ -260,23 +277,34 @@ public:
                 * functions).
                 */
 
-               const void* getIdentifier() const
+               const void* id() const
+               {
+                       return lua_topointer(mScript.mState, index);
+               }
+
+               bool isIdentical(const Slot& rhs) const
+               {
+                       return &mScript == &(rhs.mScript) && index == rhs.index;
+               }
+
+               operator bool () const
                {
-                       return lua_topointer(mState, index);
+                       return !isNone();
                }
 
 
                bool operator == (const Slot& rhs) const
                {
-                       return (bool)lua_equal(mState, index, rhs.index);
+                       return (bool)lua_equal(mScript.mState, index, rhs.index);
                }
                bool operator != (const Slot& rhs) const
                {
                        return !(*this == rhs);
                }
+
                bool operator < (const Slot& rhs) const
                {
-                       return (bool)lua_lessthan(mState, index, rhs.index);
+                       return (bool)lua_lessthan(mScript.mState, index, rhs.index);
                }
                bool operator <= (const Slot& rhs) const
                {
@@ -290,29 +318,18 @@ public:
                {
                        return !(*this < rhs);
                }
-               operator bool () const
-               {
-                       return (bool)lua_toboolean(mState, index);
-               }
-
-               Slot& operator = (const Slot& rhs)
-               {
-                       rhs.pushCopy();
-                       replaceWithTop();
-                       return *this;
-               }
 
 
                /**
                 * Convert the underlying value to a C++ type.
                 */
 
-               template <typename T>
+               template <class T>
                bool get(T& value) const
                {
                        if (isNumber())
                        {
-                               value = (T)lua_tointeger(mState, index);
+                               value = (T)lua_tointeger(mScript.mState, index);
                                return true;
                        }
                        return false;
@@ -322,7 +339,7 @@ public:
                {
                        if (isNumber())
                        {
-                               value = (float)lua_tonumber(mState, index);
+                               value = (float)lua_tonumber(mScript.mState, index);
                                return true;
                        }
                        return false;
@@ -331,7 +348,7 @@ public:
                {
                        if (isNumber())
                        {
-                               value = (double)lua_tonumber(mState, index);
+                               value = (double)lua_tonumber(mScript.mState, index);
                                return true;
                        }
                        return false;
@@ -341,62 +358,82 @@ public:
                {
                        if (isBoolean())
                        {
-                               value = (bool)lua_toboolean(mState, index);
+                               value = (bool)lua_toboolean(mScript.mState, index);
                                return true;
                        }
                        return false;
                }
 
-               bool get(std::string& value) const
+               bool get(const char*& value, size_t& size) const
                {
                        if (isString())
                        {
-                               size_t size;
-                               const char* str = lua_tolstring(mState, index, &size);
+                               value = lua_tolstring(mScript.mState, index, &size);
+                               return true;
+                       }
+                       return false;
+               }
+
+               bool get(std::string& value) const
+               {
+                       const char*     str;
+                       size_t          size;
+                       if (get(str, size))
+                       {
                                value.assign(str, size);
                                return true;
                        }
                        return false;
                }
 
-               template <typename T>
+               bool get(void*& value) const
+               {
+                       if (isData())
+                       {
+                               value = lua_touserdata(mScript.mState, index);
+                               return true;
+                       }
+                       return false;
+               }
+
+               template <class T>
                bool get(std::vector<T>& array) const
                {
                        if (!isTable()) return false;
 
                        array.clear();
 
-                       Slot    value(mState, -1);
-                       int             realIndex = getRealIndex();
+                       Slot    value = mScript[-1];
+                       int             realIndex = positiveIndex();
 
                        bool done = false;
                        for (int i = 1; !done; ++i)
                        {
-                               lua_rawgeti(mState, realIndex, i);
+                               lua_rawgeti(mScript.mState, realIndex, i);
 
                                T v;
                                if (value.get(v)) array.push_back(v);
                                else              done = true;
 
-                               lua_pop(mState, 1);
+                               mScript.pop();
                        }
 
                        return true;
                }
 
-               template <typename T>
+               template <class T>
                bool get(std::map<std::string,T>& dictionary) const
                {
                        if (!isTable()) return false;
 
                        dictionary.clear();
 
-                       Slot    key(mState, -2);
-                       Slot    value(mState, -1);
-                       int             realIndex = getRealIndex();
+                       Slot    key = mScript[-2];
+                       Slot    value = mScript[-1];
+                       int             realIndex = positiveIndex();
 
-                       lua_pushnil(mState);
-                       while (lua_next(mState, realIndex) != 0)
+                       mScript.pushNil();
+                       while (lua_next(mScript.mState, realIndex) != 0)
                        {
                                std::string k;
                                if (!key.isNumber() && key.get(k))
@@ -404,9 +441,9 @@ public:
                                        T v;
                                        if (value.get(v)) dictionary[k] = v;
                                }
-                               lua_pop(mState, 1);
+                               mScript.pop();
                        }
-                       lua_pop(mState, 1);
+                       mScript.pop();
 
                        return true;
                }
@@ -415,37 +452,79 @@ public:
                 * Get the value of a field from the table.
                 */
 
-               template <typename T, typename V>
+               template <class T, class V>
                bool get(T& value, V field) const
                {
-                       pushField(field);
-                       bool ret = Slot(mState, -1).get(value);
-                       lua_pop(mState, 1);
+                       bool ret = pushField(field).get(value);
+                       mScript.pop();
                        return ret;
                }
 
 
+               template <class T, class V>
+               void setField(T field, V value)
+               {
+                       logWarning << "setting " << field << ", " << value << std::endl;
+                       mScript.push(field);
+                       mScript.push(value);
+                       setField();
+               }
+
+               void setField()
+               {
+                       lua_settable(mScript.mState, index);
+               }
+
+
+               template <class T>
+               void setField(const std::string& field, T value)
+               {
+                       setField(field.c_str(), value);
+               }
+               template <class T>
+               void setField(const char* field, T value)
+               {
+                       logWarning << "setfield " << field << ", " << value << std::endl;
+                       mScript.push(value);
+                       lua_setfield(mScript.mState, index, field);
+               }
+
+
                /**
-                * Copy the value and push the copy to the stack.
+                * This set method, as opposed to the others, sets the value of the
+                * actual slot.  The others set table values.
                 */
+               template <class T>
+               void set(T value)
+               {
+                       mScript.push(value);
+                       replace();
+               }
 
-               void pushCopy() const
+               void set()
                {
-                       lua_pushvalue(mState, index);
+                       replace();
                }
 
+
                /**
                 * Replace this value with the value at the top of the stack.
                 */
 
-               void replaceWithTop()
+               void replace()
                {
-                       lua_replace(mState, index);
+                       lua_replace(mScript.mState, index);
                }
 
                void remove()
                {
-                       lua_remove(mState, index);
+                       lua_remove(mScript.mState, index);
+               }
+
+               void pop()
+               {
+                       // removes this slot, taking with it everything above it
+                       mScript.pop(mScript.stackSize() - index + 1);
                }
 
                /**
@@ -455,35 +534,70 @@ public:
 
                void insertTopHere()
                {
-                       lua_insert(mState, index);
+                       lua_insert(mScript.mState, index);
                }
 
+
+               /**
+                * Copy the value and push the copy to the stack.
+                */
+
+               Slot pushCopy() const
+               {
+                       lua_pushvalue(mScript.mState, index);
+                       return mScript.top();
+               }
                
-               void pushMetaTable() const
+               Slot pushMetaTable() const
+               {
+                       lua_getmetatable(mScript.mState, index);
+                       return mScript.top();
+               }
+
+               Slot pushEnvironment() const
+               {
+                       lua_getfenv(mScript.mState, index);
+                       return mScript.top();
+               }
+
+
+               Slot pushField() const
                {
-                       lua_getmetatable(mState, index);
+                       lua_gettable(mScript.mState, index);
+                       return mScript.top();
                }
 
-               void pushField() const
+               template <class T>
+               Slot pushField(T index) const
                {
-                       lua_gettable(mState, index);
+                       mScript.push(index);
+                       return pushField();
                }
 
-               void pushField(const std::string& name) const
+               Slot pushField(const std::string& name) const
+               {
+                       return pushField(name.c_str());
+               }
+               Slot pushField(const char* name) const
                {
-                       lua_getfield(mState, index, name.c_str());
+                       lua_getfield(mScript.mState, index, name);
+                       return mScript.top();
                }
 
-               void pushField(size_t index) const
+
+               Script& script()
                {
-                       lua_pushinteger(mState, lua_Integer(index));
-                       pushField();
+                       return mScript;
                }
 
+               const Script& script() const
+               {
+                       return mScript;
+               }
 
        private:
 
-               lua_State* mState;
+               Script& mScript;
        };
 
 
@@ -508,8 +622,7 @@ public:
        {
                if (mState) destroy();
                mState = luaL_newstate();
-               lua_pushlightuserdata(mState, this);
-               lua_setfield(mState, LUA_REGISTRYINDEX, "Script_hh_Object");
+               registry().setField("Script_hh_Object", (void*)this);
        }
 
 
@@ -638,7 +751,7 @@ public:
         * after cleaning up such objects.
         */
 
-       void throwError()
+       void raise()
        {
                lua_error(mState);
        }
@@ -648,24 +761,24 @@ public:
         * Get significant values.
         */
 
-       Slot getGlobalTable() const
+       Slot globals() const
        {
-               return Slot(mState, GLOBALS);
+               return Slot(*this, GLOBALS);
        }
 
-       Slot getRegistryTable() const
+       Slot registry() const
        {
-               return Slot(mState, REGISTRY);
+               return Slot(*this, REGISTRY);
        }
 
-       Slot getEnvironmentTable() const
+       Slot environment() const
        {
-               return Slot(mState, ENVIRONMENT);
+               return Slot(*this, ENVIRONMENT);
        }
 
-       Slot getTop() const
+       Slot top() const
        {
-               return Slot(mState, lua_gettop(mState));
+               return Slot(*this, stackSize());
        }
 
        /**
@@ -673,19 +786,19 @@ public:
         * value.
         */
 
-       int getSize() const
+       int stackSize() const
        {
                return lua_gettop(mState);
        }
 
-       void setSize(int size)
+       void setStackSize(int size)
        {
                lua_settop(mState, size);
        }
 
-       void clear()
+       void clearStack()
        {
-               setSize(0);
+               setStackSize(0);
        }
 
 
@@ -707,7 +820,7 @@ public:
         * Concatenates the top-most n slots on the stack.
         */
 
-       void concat(int n = 2)
+       void concatenate(int n = 2)
        {
                lua_concat(mState, n);
        }
@@ -717,81 +830,96 @@ public:
         * Push some values onto the stack.
         */
 
-       template <typename T>
-       void push(T value)
+       template <class T>
+       Slot push(T value)
        {
                lua_pushinteger(mState, lua_Integer(value));
+               return top();
        }
 
-       void push(bool value)
+       Slot push(bool value)
        {
                lua_pushboolean(mState, int(value));
+               return top();
        }
 
-       void push(float value)
+       Slot push(float value)
        {
                lua_pushnumber(mState, (lua_Number)value);
+               return top();
        }
-       void push(double value)
+       Slot push(double value)
        {
                lua_pushnumber(mState, (lua_Number)value);
+               return top();
        }
 
-       void push(const std::string& value)
+       Slot push(const std::string& value)
        {
                lua_pushlstring(mState, value.c_str(), value.length());
+               return top();
        }
-       void push(const char* value)
+       Slot push(const char* value)
        {
                lua_pushstring(mState, value);
+               return top();
        }
-       void push(const char* value, size_t length)
+       Slot push(const char* value, size_t length)
        {
                lua_pushlstring(mState, value, length);
+               return top();
        }
 
-       void push(const Function& function)
+       Slot push(const Function& function)
        {
                mFunctions.push_back(function);
-
                lua_pushlightuserdata(mState, (void*)&mFunctions.back());
                lua_pushcclosure(mState, dispatchCall, 1);
+               return top();
        }
 
-       void push(void* data)
+       Slot push(void* data)
        {
                lua_pushlightuserdata(mState, data);
+               return top();
        }
 
-       void pushNil()
+       Slot pushNil()
        {
                lua_pushnil(mState);
+               return top();
        }
 
-       void pushFromThread(Script& thread, int n)
+       Slot pushFromThread(Script& thread, int n)
        {
                lua_xmove(thread.mState, mState, n);
+               return top();
        }
 
-       Result pushCode(const std::string& filename)
+       Slot pushCode(const std::string& file, Result& result)
        {
-               return (Result)luaL_loadfile(mState, filename.c_str());
+               result = (Result)luaL_loadfile(mState, file.c_str());
+               return top();
        }
 
-       Result pushCode(const std::string& name, const char* buffer,
-                                       size_t size)
+       Slot pushCode(const std::string& name, const char* buffer,
+                                 size_t size, Result& result)
        {
-               return (Result)luaL_loadbuffer(mState, buffer, size, name.c_str());
+               result = (Result)luaL_loadbuffer(mState,
+                                                                                buffer, size, name.c_str());
+               return top();
        }
 
-       void* pushNewData(size_t size)
+       Slot pushNewData(void*& data, size_t size)
        {
-               return lua_newuserdata(mState, size);
+               data = lua_newuserdata(mState, size);
+               return top();
        }
 
-       void pushNewTable()
+       Slot pushNewTable(int narr = 0, int nrec = 0)
        {
-               lua_newtable(mState);
+               lua_createtable(mState, narr, nrec);
+               return top();
        }
 
 
@@ -824,22 +952,7 @@ public:
 
        Slot operator [] (int index) const
        {
-               return Slot(mState, index);
-       }
-
-
-       /**
-        * Getting and setting fields of a table.
-        */
-
-       void pushField(const std::string& field, int index = GLOBALS) const
-       {
-               lua_getfield(mState, index, field.c_str());
-       }
-
-       void set(const std::string& field, int index = GLOBALS)
-       {
-               lua_setfield(mState, index, field.c_str());
+               return Slot(*this, index);
        }
 
 
@@ -920,8 +1033,10 @@ inline std::ostream& operator << (std::ostream& stream,
        }
        else if (slot.isBoolean())
        {
-               if (slot) stream << "true";
-               else      stream << "false";
+               bool value;
+               slot.get(value);
+               if (value) stream << "true";
+               else       stream << "false";
        }
        else if (slot.isNil())
        {
@@ -929,7 +1044,7 @@ inline std::ostream& operator << (std::ostream& stream,
        }
        else
        {
-               stream << slot.getTypeName() << " (" << slot.getIdentifier() << ")";
+               stream << slot.typeName() << " (" << slot.id() << ")";
        }
 
        return stream;
index bc503c52a26f8064b86fe7d4529987b29b2ebe6c..584643023892b89c2dec9bf373dcb160f9b3b2a1 100644 (file)
@@ -27,7 +27,14 @@ void Settings::parseArgs(int argc, char* argv[])
 {
        for (int i = 1; i < argc; ++i)
        {
-               mScript.doString(argv[i]);
+               if (mScript.doString(argv[i]) != Script::SUCCESS)
+               {
+                       std::string str;
+                       mScript[-1].get(str);
+                       logWarning << "invalid option: " << argv[i]
+                                          << ": " << str << std::endl;
+                       mScript.clearStack();
+               }
        }
 }
 
@@ -72,7 +79,7 @@ void Settings::loadFromFiles(const std::vector<std::string>& path)
                        std::string str;
                        mScript[-1].get(str);
                        logWarning(str);
-                       mScript.clear();
+                       mScript.clearStack();
                }
        }
 }
index 42ef70ce54719d2431e0a9260899d63ecc658209..192e2a764254ba61a4b476a23c51c989422c1db8 100644 (file)
@@ -54,7 +54,7 @@ public:
        void saveAs(const std::string& path);
        void save() const;
 
-       template <typename T>
+       template <class T>
        bool get(const std::string& key, T& value) const;
 
 private:
@@ -65,11 +65,11 @@ private:
 };
 
 
-template <typename T>
+template <class T>
 bool Settings::get(const std::string& key, T& value) const
 {
        Script::Slot top = mScript[-1];
-       Script::Slot globals = mScript.getGlobalTable();
+       Script::Slot globals = mScript.globals();
 
        std::vector<std::string> fields;
        boost::split(fields, key, boost::is_any_of("."));
@@ -85,13 +85,13 @@ bool Settings::get(const std::string& key, T& value) const
                }
                else
                {
-                       mScript.clear();
+                       mScript.clearStack();
                        return false;
                }
        }
 
        bool got = top.get(value);
-       mScript.clear();
+       mScript.clearStack();
        return got;
 }
 
index 3ca067af4ebe72dcc6552b92b3aa2c1aab09f07a..7f9f83c335b98eb7f8a39d4e02023ef0b6f68708 100644 (file)
@@ -89,20 +89,18 @@ class Texture::Impl : public Manager<Impl>
        }
 
 
-       static void bindScriptConstants(Mf::Script& script)
+       static void bindScriptConstants(Script& script)
        {
-               script.push(GL_CLAMP); script.set("CLAMP");
-               script.push(GL_REPEAT); script.set("REPEAT");
-               script.push(GL_LINEAR); script.set("LINEAR");
-               script.push(GL_NEAREST); script.set("NEAREST");
-               script.push(GL_LINEAR_MIPMAP_LINEAR);
-               script.set("LINEAR_MIPMAP_LINEAR");
-               script.push(GL_LINEAR_MIPMAP_NEAREST);
-               script.set("LINEAR_MIPMAP_NEAREST");
-               script.push(GL_NEAREST_MIPMAP_LINEAR);
-               script.set("NEAREST_MIPMAP_LINEAR");
-               script.push(GL_NEAREST_MIPMAP_NEAREST);
-               script.set("NEAREST_MIPMAP_NEAREST");
+               Script::Slot g = script.globals();
+
+               g.setField("CLAMP",   GL_CLAMP);
+               g.setField("REPEAT",  GL_REPEAT);
+               g.setField("LINEAR",  GL_LINEAR);
+               g.setField("NEAREST", GL_NEAREST);
+               g.setField("LINEAR_MIPMAP_LINEAR",   GL_LINEAR_MIPMAP_LINEAR);
+               g.setField("LINEAR_MIPMAP_NEAREST",  GL_LINEAR_MIPMAP_NEAREST);
+               g.setField("NEAREST_MIPMAP_LINEAR",  GL_NEAREST_MIPMAP_LINEAR);
+               g.setField("NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST);
        }
 
 public:
@@ -228,42 +226,29 @@ public:
 
                mImage->flip();
 
-               Mf::Script script;
+               Script script;
 
                importLogFunctions(script);
                bindScriptConstants(script);
 
-               if (script.doString(mImage->getComment()) != Mf::Script::SUCCESS)
+               if (script.doString(mImage->getComment()) != Script::SUCCESS)
                {
                        std::string str;
                        script[-1].get(str);
-                       Mf::logWarning(str);
+                       logWarning(str);
                }
                else
                {
-                       Mf::logInfo << "loading tiles from texture " << path
-                                               << std::endl;
-
-                       Mf::Script::Slot globals = script.getGlobalTable();
-                       Mf::Script::Slot top = script[-1];
-
-                       globals.pushField("tiles_s");
-                       top.get(mTilesS);
-
-                       globals.pushField("tiles_t");
-                       top.get(mTilesT);
-
-                       globals.pushField("min_filter");
-                       top.get(mMinFilter);
-
-                       globals.pushField("mag_filter");
-                       top.get(mMagFilter);
-
-                       globals.pushField("wrap_s");
-                       top.get(mWrapS);
-
-                       globals.pushField("wrap_t");
-                       top.get(mWrapT);
+                       logInfo << "loading tiles from texture " << path
+                                       << std::endl;
+
+                       Script::Slot globals = script.globals();
+                       globals.get(mTilesS, "tiles_s");
+                       globals.get(mTilesT, "tiles_t");
+                       globals.get(mMinFilter, "min_filter");
+                       globals.get(mMagFilter, "mag_filter");
+                       globals.get(mWrapS, "wrap_s");
+                       globals.get(mWrapT, "wrap_t");
                }
        }
 
index 194caf53f09c2a137c8e2504b3d9756abba7e031..2f468fc4227d8a154f94700f7ce7a7d8c3d5d335 100644 (file)
@@ -27,7 +27,7 @@
 namespace Mf {
 
 
-template <typename T>
+template <class T>
 class Transition : public Layer
 {
        LayerP  mTo;
index 918ff5250b843c8c8b0020b3935ab848e07cf4ae..16e1aef55b210903fff7caeb2e65bb4fb344df55 100644 (file)
@@ -177,19 +177,19 @@ struct Scene::Impl : public Mf::Manager<Impl>
 
                int detail = 3;
                settings.get("detail", detail);
-               script.push(detail); script.set("detail");
+               script.globals().setField("detail", detail);
 
-               script.push(1); script.set("LOW");
-               script.push(2); script.set("MEDIUM");
-               script.push(3); script.set("HIGH");
+               script.globals().setField("LOW",    1);
+               script.globals().setField("MEDIUM", 2);
+               script.globals().setField("HIGH",   3);
 
-               script.push(X); script.set("X");
-               script.push(Y); script.set("Y");
-               script.push(Z); script.set("Z");
+               script.globals().setField("X", X);
+               script.globals().setField("Y", Y);
+               script.globals().setField("Z", Z);
 
-               script.push(Quad::LEFT);  script.set("LEFT");
-               script.push(Quad::RIGHT); script.set("RIGHT");
-               script.push(Quad::TOP);   script.set("TOP");
+               script.globals().setField("LEFT",  Quad::LEFT);
+               script.globals().setField("RIGHT", Quad::RIGHT);
+               script.globals().setField("TOP",   Quad::TOP);
        }
 
 
@@ -207,26 +207,17 @@ struct Scene::Impl : public Mf::Manager<Impl>
        }
 
 
-       static int loadBox(Mf::Script& script, Mf::Aabb<3>& aabb)
+       static int loadBox(Mf::Script& script, Mf::Aabb3& aabb)
        {
                script[1].requireTable();
                script[2].requireTable();
-               script.setSize(2);
 
-               for (int i = 1; i <= 2; ++i)
-               {
-                       for (int j = 1; j <= 3; ++j)
-                       {
-                               script[i].pushField(j);
-                       }
-               }
-
-               script[3].get(aabb.min[0]);
-               script[4].get(aabb.min[1]);
-               script[5].get(aabb.min[2]);
-               script[6].get(aabb.max[0]);
-               script[7].get(aabb.max[1]);
-               script[8].get(aabb.max[2]);
+               script[1].pushField(1).get(aabb.min[0]);
+               script[1].pushField(2).get(aabb.min[1]);
+               script[1].pushField(3).get(aabb.min[2]);
+               script[2].pushField(1).get(aabb.max[0]);
+               script[2].pushField(2).get(aabb.max[1]);
+               script[2].pushField(3).get(aabb.max[2]);
 
                return 0;
        }
@@ -247,7 +238,6 @@ struct Scene::Impl : public Mf::Manager<Impl>
        int translate(Mf::Script& script)
        {
                Mf::Vector3 vec;
-
                script[1].requireNumber().get(vec[0]);
                script[2].requireNumber().get(vec[1]);
                script[3].requireNumber().get(vec[2]);
@@ -261,7 +251,7 @@ struct Scene::Impl : public Mf::Manager<Impl>
 
        int scale(Mf::Script& script)
        {
-               int size = script.getSize();
+               int size = script.stackSize();
 
                if (size == 1)
                {
@@ -285,7 +275,7 @@ struct Scene::Impl : public Mf::Manager<Impl>
                }
                else
                {
-                       script.getTop().throwError("wrong number of arguments");
+                       script.top().raise("wrong number of arguments");
                }
 
                return 0;
@@ -316,19 +306,16 @@ struct Scene::Impl : public Mf::Manager<Impl>
                Mf::Script::Slot        table = script[1].requireTable();
 
                int width  = 1;
-               int height = 1;
-               int nTiles = 0;
-
                table.get(width, "width");
 
-               nTiles = table.getLength();
+               int nTiles = table.length();
                if (nTiles % width != 0)
                {
-                       table.throwError("invalid number of tiles");
+                       table.raise("invalid number of tiles");
                }
 
-               if (width == 0) table.throwError("width field must not be zero");
-               height = nTiles / width;
+               if (width == 0) table.raise("width field must not be zero");
+               int height = nTiles / width;
 
                Mf::Vector3             vertices[height+1][width+1];
 
This page took 0.080404 seconds and 4 git commands to generate.