From d50942708db230dc5c43b8df89ede45525e1c394 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Tue, 15 Sep 2009 19:20:04 -0600 Subject: [PATCH] minor cleanups --- README | 38 +++++++++++++++++---- src/Character.cc | 8 ++--- src/Character.hh | 25 ++++++++------ src/Makefile.am | 2 +- src/Moof/Camera.hh | 2 +- src/Moof/Dispatcher.hh | 4 +-- src/Moof/Interpolator.hh | 50 +++++++++++++++------------- src/Moof/{Physics.hh => RK4.hh} | 18 +++++++--- src/Moof/cml/mathlib/interpolation.h | 2 +- src/YoinkApp.cc | 30 ++++++----------- src/YoinkApp.hh | 10 ++---- 11 files changed, 106 insertions(+), 83 deletions(-) rename src/Moof/{Physics.hh => RK4.hh} (83%) diff --git a/README b/README index 0193c48..39092e7 100644 --- 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. diff --git a/src/Character.cc b/src/Character.cc index 1b184fa..c7de260 100644 --- a/src/Character.cc +++ b/src/Character.cc @@ -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) { diff --git a/src/Character.hh b/src/Character.hh index 5aeaeba..96ac499 100644 --- a/src/Character.hh +++ b/src/Character.hh @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include @@ -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_ diff --git a/src/Makefile.am b/src/Makefile.am index 01c521e..dfcee15 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/Moof/Camera.hh b/src/Moof/Camera.hh index fae2507..b5927f4 100644 --- a/src/Moof/Camera.hh +++ b/src/Moof/Camera.hh @@ -81,7 +81,7 @@ private: Matrix4 modelview_; Frustum frustum_; - Lerpv3 pInterp_; + Lerp3 pInterp_; }; diff --git a/src/Moof/Dispatcher.hh b/src/Moof/Dispatcher.hh index da82f38..0d75390 100644 --- a/src/Moof/Dispatcher.hh +++ b/src/Moof/Dispatcher.hh @@ -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) diff --git a/src/Moof/Interpolator.hh b/src/Moof/Interpolator.hh index 2c33844..b32482c 100644 --- a/src/Moof/Interpolator.hh +++ b/src/Moof/Interpolator.hh @@ -35,6 +35,8 @@ namespace Mf { +// TODO - cleanup these classes + class Interpolator { void clamp(Scalar& value) @@ -117,7 +119,7 @@ private: bool stopped_; }; -template +template class InterpolatorBase : public Interpolator { public: @@ -153,13 +155,13 @@ private: }; -template -class BinomialInterpolator : public InterpolatorBase +template +class PolynomialInterpolator : public InterpolatorBase { 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 BinomialInterpolator : public InterpolatorBase +class PolynomialInterpolator<1,T> : public InterpolatorBase { 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(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 Lerps; // linear -typedef BinomialInterpolator Lerpv2; -typedef BinomialInterpolator Lerpv3; -typedef BinomialInterpolator Lerpv4; +typedef PolynomialInterpolator<1> Lerp; // linear +typedef PolynomialInterpolator<1,Vector2> Lerp2; +typedef PolynomialInterpolator<1,Vector3> Lerp3; +typedef PolynomialInterpolator<1,Vector4> Lerp4; -typedef BinomialInterpolator Qerps; // quadratic -typedef BinomialInterpolator Qerpv2; -typedef BinomialInterpolator Qerpv3; -typedef BinomialInterpolator Qerpv4; +typedef PolynomialInterpolator<2> Qerp; // quadratic +typedef PolynomialInterpolator<2,Vector2> Qerp2; +typedef PolynomialInterpolator<2,Vector3> Qerp3; +typedef PolynomialInterpolator<2,Vector4> Qerp4; -typedef BinomialInterpolator Cerps; // cubic -typedef BinomialInterpolator Cerpv2; -typedef BinomialInterpolator Cerpv3; -typedef BinomialInterpolator Cerpv4; +typedef PolynomialInterpolator<3> Cerp; // cubic +typedef PolynomialInterpolator<3,Vector2> Cerp2; +typedef PolynomialInterpolator<3,Vector3> Cerp3; +typedef PolynomialInterpolator<3,Vector4> Cerp4; } // namespace Mf diff --git a/src/Moof/Physics.hh b/src/Moof/RK4.hh similarity index 83% rename from src/Moof/Physics.hh rename to src/Moof/RK4.hh index a79a5c3..a2e0f1c 100644 --- a/src/Moof/Physics.hh +++ b/src/Moof/RK4.hh @@ -26,8 +26,8 @@ *******************************************************************************/ -#ifndef _MOOF_PHYSICS_HH_ -#define _MOOF_PHYSICS_HH_ +#ifndef _MOOF_RK4_HH_ +#define _MOOF_RK4_HH_ #include @@ -35,7 +35,16 @@ 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 inline D evaluate(const S& state, Scalar t) @@ -62,14 +71,13 @@ inline void integrate(S& state, Scalar t, Scalar dt) D c = evaluate(state, t, dt * 0.5, b); D d = evaluate(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: *************************************************/ diff --git a/src/Moof/cml/mathlib/interpolation.h b/src/Moof/cml/mathlib/interpolation.h index 4a9fd54..79d57ae 100644 --- a/src/Moof/cml/mathlib/interpolation.h +++ b/src/Moof/cml/mathlib/interpolation.h @@ -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; } diff --git a/src/YoinkApp.cc b/src/YoinkApp.cc index d36f6da..2b4cb8c 100644 --- a/src/YoinkApp.cc +++ b/src/YoinkApp.cc @@ -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; diff --git a/src/YoinkApp.hh b/src/YoinkApp.hh index ad8d694..b24eff7 100644 --- a/src/YoinkApp.hh +++ b/src/YoinkApp.hh @@ -43,12 +43,9 @@ #include #include #include - -#include "Character.hh" -#include "TilemapFont.hh" - #include +#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; -- 2.43.0