]> Dogcows Code - chaz/yoink/commitdiff
minor cleanups
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 16 Sep 2009 01:20:04 +0000 (19:20 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 16 Sep 2009 01:20:04 +0000 (19:20 -0600)
README
src/Character.cc
src/Character.hh
src/Makefile.am
src/Moof/Camera.hh
src/Moof/Dispatcher.hh
src/Moof/Interpolator.hh
src/Moof/RK4.hh [moved from src/Moof/Physics.hh with 83% similarity]
src/Moof/cml/mathlib/interpolation.h
src/YoinkApp.cc
src/YoinkApp.hh

diff --git a/README b/README
index 0193c48f71a21d4b25711902e9b8110b6d85d4ea..39092e738866da52b9bcdede7322b63ee0e5ad6c 100644 (file)
--- a/README
+++ b/README
@@ -10,14 +10,38 @@ The new code is released under the Simplified BSD License.  The old code and
 original resources are provided under the zlib/libpng License.  See COPYING
 for complete details.
 
-
 Dependencies:
 
-boost headers  1.35.0
-libGL
-libSDL                 1.2.13
-libSDL_image   1.2.7
+boost headers
+OpenGL (libGL, libGL or opengl32, glu32)
+libSDL
+libSDL_image (with libpng support)
+libSDL_sound (with libogg support)
+libopenal
+libalut
+
+
+Notes regarding the code:
+
+I've made some effort to put the more generic or reusable code into a separate
+library called Moof.  I've also made an effort to incorporate 3rd-party code
+that happened to fit well into what I needed.  So, generally, the source code is
+separated into these three categories:
+
+1. Yoink-specific code.
+
+This is the code directly in src/.  These classes reside in no namespace.
+
+2. Reusable code.
+
+Currently, the code is in src/Moof/, and it is compiled as a convenience
+library.  These classes and other helper functions reside in the Mf namespace.
+Since I wrote this code alongside the Yoink-specific stuff, there is somewhat of
+a blurry line between the two categories.
+
+3. 3rd-party code.
 
-Note: The version numbers are the versions I have been using for development,
-not necessarily the minimum required version numbers.
+This is made up of free code from other projects or libraries (aside from the
+explicit dependencies above), the licenses of which are also in the COPYING
+file.  This code resides in various namespaces and in various subdirectories.
 
index 1b184fa2d0160ad353ef3645823d25626397b3cf..c7de260d6cb61190e5ceaf60b2be6701b455f309 100644 (file)
@@ -39,8 +39,10 @@ Character::Character(const std::string& name) :
        current.mass = 1.0;
        current.inverseMass = 1.0 / current.mass;
 
+       // gravity
        current.force = Mf::Vector2(0.0, -120.0);
 
+       // starting position
        current.position = Mf::Vector2(64.0, 64.0);
        current.momentum = Mf::Vector2(0.0, 0.0);
        current.recalculate();
@@ -50,12 +52,6 @@ Character::Character(const std::string& name) :
        updateContainers();
 }
 
-Character::~Character()
-{
-       //delete texture;
-       //delete anim;
-}
-
 
 void Character::update(Mf::Scalar t, Mf::Scalar dt)
 {
index 5aeaeba0a7af1b8a5ea6e1c6f7f53fbee43c48e2..96ac499001110bf1882d5f93e3c2634f4aa265b2 100644 (file)
@@ -36,7 +36,7 @@
 #include <Moof/Event.hh>
 #include <Moof/Math.hh>
 #include <Moof/Octree.hh>
-#include <Moof/Physics.hh>
+#include <Moof/RK4.hh>
 #include <Moof/Tilemap.hh>
 
 
@@ -111,6 +111,9 @@ struct Character : public Mf::Entity
                        recalculate();
                }
 
+               // these two operator overloads all using the state in generic
+               // interpolator implementations
+
                State operator*(Mf::Scalar scalar) const
                {
                        State state = *this;
@@ -153,7 +156,7 @@ public:
        }
 
        Character(const std::string& name);
-       virtual ~Character();
+       inline virtual ~Character() {}
 
        void update(Mf::Scalar t, Mf::Scalar dt);
        void handleEvent(const Mf::Event& event);
@@ -164,15 +167,15 @@ public:
 };
 
 
-inline Character::State operator*(Mf::Scalar scalar,
-               const Character::State& state)
-{
-       Character::State newState = state;
-       newState.position *= scalar;
-       newState.momentum *= scalar;
-       newState.recalculate();
-       return newState;
-}
+//inline Character::State operator*(Mf::Scalar scalar,
+               //const Character::State& state)
+//{
+       //Character::State newState = state;
+       //newState.position *= scalar;
+       //newState.momentum *= scalar;
+       //newState.recalculate();
+       //return newState;
+//}
 
 
 #endif // _CHARACTER_HH_
index 01c521e9e789704856ae9fde7347316864064708..dfcee15950a51c1fef802e42bbe6c5eb895a945b 100644 (file)
@@ -33,7 +33,6 @@ libmoof_la_SOURCES = \
                                   Moof/Octree.cc \
                                   Moof/Octree.hh \
                                   Moof/OpenGL.hh \
-                                  Moof/Physics.hh \
                                   Moof/Plane.cc \
                                   Moof/Plane.hh \
                                   Moof/Random.cc \
@@ -42,6 +41,7 @@ libmoof_la_SOURCES = \
                                   Moof/Rectangle.hh \
                                   Moof/Resource.cc \
                                   Moof/Resource.hh \
+                                  Moof/RK4.hh \
                                   Moof/Scene.cc \
                                   Moof/Scene.hh \
                                   Moof/Serializable.cc \
index fae25079ec700d88d7e1a5edb1baff0e3324874e..b5927f438e0d3e27f376f8e5a559da6e5d9190f5 100644 (file)
@@ -81,7 +81,7 @@ private:
        Matrix4         modelview_;
        Frustum         frustum_;
 
-       Lerpv3          pInterp_;
+       Lerp          pInterp_;
 };
 
 
index da82f3824256b70bf50ee283f446133594a1c009..0d75390d7f7adaa4902ef62ee761564519a9f057 100644 (file)
@@ -84,13 +84,13 @@ namespace dispatcher {
 inline Dispatcher::Handler addHandler(const std::string& message,
                const Dispatcher::Function& callback)
 {
-       Dispatcher::getInstance().addHandler(message, callback);
+       return Dispatcher::getInstance().addHandler(message, callback);
 }
 
 inline Dispatcher::Handler addHandler(const std::string& message,
                const Dispatcher::Function& callback, Dispatcher::Handler id)
 {
-       Dispatcher::getInstance().addHandler(message, callback, id);
+       return Dispatcher::getInstance().addHandler(message, callback, id);
 }
 
 inline void removeHandler(Dispatcher::Handler id)
index 2c33844262db6835cc5eaf02258f09cf5d11d767..b32482c73a9e502667530293786db06f04b4d60a 100644 (file)
@@ -35,6 +35,8 @@
 namespace Mf {
 
 
+// TODO - cleanup these classes
+
 class Interpolator
 {
        void clamp(Scalar& value)
@@ -117,7 +119,7 @@ private:
        bool    stopped_;
 };
 
-template <class T>
+template <class T = Scalar>
 class InterpolatorBase : public Interpolator
 {
 public:
@@ -153,13 +155,13 @@ private:
 };
 
 
-template <class T, int D>
-class BinomialInterpolator : public InterpolatorBase<T>
+template <int D, class T = Scalar>
+class PolynomialInterpolator : public InterpolatorBase<T>
 {
 public:
-       BinomialInterpolator() {}
+       PolynomialInterpolator() {}
 
-       explicit BinomialInterpolator(const T coefficients[D+1],
+       PolynomialInterpolator(const T coefficients[D+1],
                        Scalar seconds = 1.0, Interpolator::Mode mode = Interpolator::STOP)
        {
                init(coefficients, seconds, mode);
@@ -174,13 +176,13 @@ public:
                fac[1] = 1.0;
 
                // build an array of the computed factorials we will need
-               for (int i = 2; i <= D; i++)
+               for (int i = 2; i <= D; ++i)
                {
                        fac[i] = i * fac[i - 1];
                }
 
                // combine the coefficients for fast updating
-               for (int i = 0; i <= D; i++)
+               for (int i = 0; i <= D; ++i)
                {
                        // n! / (k! * (n - k)!)
                        coefficients_[i] = coefficients[i] * fac[D] / (fac[i] * fac[D - i]);
@@ -196,7 +198,7 @@ public:
 
                value = coefficients_[0] * std::pow(beta, D);
 
-               for (int i = 1; i <= D; i++)
+               for (int i = 1; i <= D; ++i)
                {
                        value += coefficients_[i] * std::pow(beta, D - i) *
                                std::pow(alpha, i);
@@ -208,13 +210,15 @@ private:
 };
 
 
+// specialized linear interpolator
+
 template <class T>
-class BinomialInterpolator<T,1> : public InterpolatorBase<T>
+class PolynomialInterpolator<1,T> : public InterpolatorBase<T>
 {
 public:
-       BinomialInterpolator() {}
+       PolynomialInterpolator() {}
 
-       explicit BinomialInterpolator(const T coefficients[2], Scalar seconds = 1.0,
+       PolynomialInterpolator(const T coefficients[2], Scalar seconds = 1.0,
                        Interpolator::Mode mode = Interpolator::STOP)
                //InterpolatorBase<T>(seconds, mode)
        {
@@ -246,20 +250,20 @@ private:
 // interpolation functions in cml for other types of interpolation such as
 // slerp and some multi-alpha interpolators.
 
-typedef BinomialInterpolator<Scalar, 1>        Lerps;  // linear
-typedef BinomialInterpolator<Vector2,1>        Lerpv2;
-typedef BinomialInterpolator<Vector3,1>        Lerpv3;
-typedef BinomialInterpolator<Vector4,1>        Lerpv4;
+typedef PolynomialInterpolator<1>                      Lerp;   // linear
+typedef PolynomialInterpolator<1,Vector2>      Lerp2;
+typedef PolynomialInterpolator<1,Vector3>      Lerp3;
+typedef PolynomialInterpolator<1,Vector4>      Lerp4;
 
-typedef BinomialInterpolator<Scalar ,2>        Qerps;  // quadratic
-typedef BinomialInterpolator<Vector2,2>        Qerpv2;
-typedef BinomialInterpolator<Vector3,2>        Qerpv3;
-typedef BinomialInterpolator<Vector4,2>        Qerpv4;
+typedef PolynomialInterpolator<2>                      Qerp;   // quadratic
+typedef PolynomialInterpolator<2,Vector2>      Qerp2;
+typedef PolynomialInterpolator<2,Vector3>      Qerp3;
+typedef PolynomialInterpolator<2,Vector4>      Qerp4;
 
-typedef BinomialInterpolator<Scalar ,3>        Cerps;  // cubic
-typedef BinomialInterpolator<Vector2,3>        Cerpv2;
-typedef BinomialInterpolator<Vector3,3>        Cerpv3;
-typedef BinomialInterpolator<Vector4,3>        Cerpv4;
+typedef PolynomialInterpolator<3>                      Cerp;   // cubic
+typedef PolynomialInterpolator<3,Vector2>      Cerp2;
+typedef PolynomialInterpolator<3,Vector3>      Cerp3;
+typedef PolynomialInterpolator<3,Vector4>      Cerp4;
 
 
 } // namespace Mf
similarity index 83%
rename from src/Moof/Physics.hh
rename to src/Moof/RK4.hh
index a79a5c3604b8b88c66b2a63039c5bc8d8d57db9c..a2e0f1cbe713c62e0eb9b6267c2b2febaecd065a 100644 (file)
@@ -26,8 +26,8 @@
 
 *******************************************************************************/
 
-#ifndef _MOOF_PHYSICS_HH_
-#define _MOOF_PHYSICS_HH_
+#ifndef _MOOF_RK4_HH_
+#define _MOOF_RK4_HH_
 
 #include <Moof/Math.hh>
 
 namespace Mf {
 
 // Generic implementation of the RK4 integrator.  To use, you need one type
-// representing the state and another containing the derivatives of the state.
+// representing the state and another containing the derivatives of the primary
+// state variables.  The state class must implement these methods:
+//
+// void getDerivative(Derivative_Type& derivative, Scalar absoluteTime);
+// void applyDerivative(const Derivative_Type& derivative, Scalar deltaTime);
+//
+// Additionally, the derivative class must overload a few operators:
+//
+// Derivative_Type operator+(const Derivative_Type& other) const
+// Derivative_Type operator*(const Derivative_Type& other) const
 
 template<typename S, typename D>
 inline D evaluate(const S& state, Scalar t)
@@ -62,14 +71,13 @@ inline void integrate(S& state, Scalar t, Scalar dt)
        D c = evaluate<S,D>(state, t, dt * 0.5, b);
        D d = evaluate<S,D>(state, t, dt, c);
 
-       //state += (a + (b + c) * 2.0 + d) * (1.0/6.0) * dt;
        state.applyDerivative((a + (b + c) * 2.0 + d) * (1.0/6.0), dt);
 }
 
 
 } // namespace Mf
 
-#endif // _MOOF_PHYSICS_HH_
+#endif // _MOOF_RK4_HH_
 
 /** vim: set ts=4 sw=4 tw=80: *************************************************/
 
index 4a9fd54cd58a464cd24e1cf8faad53fcaac3bd69..79d57ae83f197cf655be52a60e275bf04364a9b5 100644 (file)
@@ -989,7 +989,7 @@ lerp(const T1& val0, const T2& val1, Scalar u)
     temporary_type result;
     detail::InterpResize(result, val1, size_tag());
     
-    result = (Scalar(1) - u) * val0 + u * val1;
+    result = val0 * (Scalar(1) - u) + val1 * u;
     return result;
 }
 
index d36f6da3ef8f52f9f8e61d82c85a4f5cb6f655b4..2b4cb8cfdcc77db88754f04151af4b4a2ce1fa73 100644 (file)
@@ -116,17 +116,11 @@ YoinkApp::YoinkApp(int argc, char* argv[]) :
        heroine = Character::alloc("RobotTrooper");
        heroine->getAnimation().startSequence("Run");
 
-       font = new TilemapFont;
+       Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -1.5, 1.0};
+       interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
 
-       Mf::Scalar coeffs[4];
-       coeffs[0] = 0.0;
-       coeffs[1] = 1.5;
-       coeffs[2] = -0.5;
-       coeffs[3] = 1.0;
-       interp.init(coeffs, 1.0, Mf::Interpolator::OSCILLATE);
-
-       Mf::Scalar coeff[2] = {1.0, 0.0};
-       fadeIn.init(coeff, 0.1);
+       Mf::Scalar b[2] = {1.0, 0.0};
+       fadeIn.init(b, 1.0);
 
        testScene = Mf::Scene::alloc("Test");
        heroine->treeNode = testScene->getOctree()->insert(heroine);
@@ -134,9 +128,6 @@ YoinkApp::YoinkApp(int argc, char* argv[]) :
 
 YoinkApp::~YoinkApp()
 {
-       //delete heroine;
-       delete font;
-
        Mf::dispatcher::removeHandler(this);
 }
 
@@ -175,15 +166,15 @@ void YoinkApp::setupGL()
 
 void YoinkApp::contextRecreated(const Mf::Notification* note)
 {
-       // Whenever the context and a new one created, it probably won't contain our
-       // state so we need to set that up again.
+       // Whenever the context is destroyed and a new one created, it probably
+       // won't contain our state so we need to set that up again.
        setupGL();
 }
 
 
 void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
 {
-       //dt *= 0.5;
+       //dt *= 0.1;
 
        music.update(t, dt);
        fadeIn.update(dt);
@@ -198,8 +189,8 @@ void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
        camera.setPosition(Mf::Vector3(-heroine->current.position[0], -heroine->current.position[1], -256));
 
        interp.update(dt);
-       hud.setBar1Progress(interp.getValue());
-       hud.setBar2Progress(1.0 - interp.getValue());
+       hud.setBar1Progress(interp.getState(dt));
+       hud.setBar2Progress(1.0 - interp.getState(dt));
 }
 
 
@@ -342,7 +333,8 @@ int main(int argc, char* argv[])
        {
                Mf::logError("unhandled exception: <<%s>>", e.what());
                Mf::logInfo("it's time to crash now :-(");
-               status = 1;
+               //status = 1;
+               throw e;
        }
 
        std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
index ad8d694e62619aee1c947e50f901e864310db95d..b24eff770d2ffc0c768f8509ba2216958a1c16d8 100644 (file)
 #include <Moof/Interpolator.hh>
 #include <Moof/Math.hh>
 #include <Moof/Scene.hh>
-
-#include "Character.hh"
-#include "TilemapFont.hh"
-
 #include <Moof/Sound.hh>
 
+#include "Character.hh"
 #include "Hud.hh"
 
 
@@ -73,10 +70,9 @@ private:
 
        CharacterP heroine;
        Mf::Sound punchSound;
-       TilemapFont *font;
 
-       Mf::Cerps interp;
-       Mf::Lerps fadeIn;
+       Mf::PolynomialInterpolator<5> interp;
+       Mf::Lerp fadeIn;
 
        Mf::Camera camera;
        Mf::SceneP testScene;
This page took 0.041306 seconds and 4 git commands to generate.