dispatch class not a singleton, engine is static
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 12 Jan 2010 06:52:21 +0000 (23:52 -0700)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Tue, 12 Jan 2010 06:52:21 +0000 (23:52 -0700)
24 files changed:
src/ErrorHandler.cc
src/ErrorHandler.hh
src/GameLayer.cc
src/GameLayer.hh
src/GameState.hh [new file with mode: 0644]
src/Hud.cc
src/Hud.hh
src/MainLayer.cc
src/MainLayer.hh
src/Moof/Dispatch.cc
src/Moof/Dispatch.hh
src/Moof/Engine.cc
src/Moof/Engine.hh
src/Moof/Error.hh [moved from src/Moof/Exception.hh with 73% similarity]
src/Moof/Layer.hh
src/Moof/Line.hh
src/Moof/Sound.cc
src/Moof/Sound.hh
src/Moof/Texture.cc
src/Moof/Transition.hh
src/Moof/Video.cc
src/Scene.cc
src/TitleLayer.cc
src/TitleLayer.hh

index 9c4452cd8c33ba4f2704ae23fd55740af4b6923f..07bd6d7d6c1d34330eef9e070ac289cf7e5af0dc 100644 (file)
 #include "ErrorHandler.hh"
 
 
-std::string getErrorString(const Mf::Exception& e)
+std::string getErrorString(const Mf::Error& error)
 {
        std::string str;
 
-       switch(e.code())
+       switch(error.code())
        {
-               case Mf::ErrorCode::ALC_INIT:
+               case Mf::Error::ALC_INIT:
                        str += "An error occurred during OpenAL initialization: ";
-                       str += e.what();
+                       str += error.what();
                        return str;
 
-               case Mf::ErrorCode::FASTEVENTS_INIT:
-               case Mf::ErrorCode::SDL_INIT:
+               case Mf::Error::FASTEVENTS_INIT:
+               case Mf::Error::SDL_INIT:
                        str += "An error occurred during SDL initialization: ";
-                       str += e.what();
+                       str += error.what();
                        return str;
 
-               case Mf::ErrorCode::FILE_NOT_FOUND:
+               case Mf::Error::FILE_NOT_FOUND:
                        str += "A required file (";
-                       str += e.what();
+                       str += error.what();
                        str += ") could not be found.";
                        return str;
 
-               case Mf::ErrorCode::RESOURCE_NOT_FOUND:
+               case Mf::Error::RESOURCE_NOT_FOUND:
                        str += "A required resource (";
-                       str += e.what();
+                       str += error.what();
                        str += ") could not be found.";
                        return str;
 
-               case Mf::ErrorCode::SCRIPT_ERROR:
+               case Mf::Error::SCRIPT_ERROR:
                        str += "An error occurred in a script: ";
-                       str == e.what();
+                       str == error.what();
                        return str;
 
-               case Mf::ErrorCode::SDL_VIDEOMODE:
+               case Mf::Error::SDL_VIDEOMODE:
                        str += "An error occurred while trying to set up the video mode.";
                        return str;
 
-               case Mf::ErrorCode::UNKNOWN_AUDIO_FORMAT:
+               case Mf::Error::UNKNOWN_AUDIO_FORMAT:
                        str += "An error occurred while trying to load an audio file, ";
-                       str += e.what();
+                       str += error.what();
                        str += ".";
                        return str;
 
-               case Mf::ErrorCode::UNKNOWN_IMAGE_FORMAT:
+               case Mf::Error::UNKNOWN_IMAGE_FORMAT:
                        str += "An error occurred while trying to load an image file, ";
-                       str += e.what();
+                       str += error.what();
                        str += ".";
                        return str;
        }
 
        std::ostringstream stream;
-       stream << "An unknown error (code " << e.code() << ") occurred.";
+       stream << "An unknown error (code " << error.code() << ") occurred.";
        return stream.str();
 }
 
index 2b13b1d44043b705625b09bf8110be8b8182863d..8132ac4738aa2d8fbbc3b28ed9f9e11ddb89c53f 100644 (file)
 #ifndef _ERRORHANDLER_HH_
 #define _ERRORHANDLER_HH_
 
-#include <Moof/Exception.hh>
+#include <Moof/Error.hh>
 
 
-std::string getErrorString(const Mf::Exception& e);
+std::string getErrorString(const Mf::Error& error);
 
 
 #endif // _ERRORHANDLER_HH_
index 0ed249c8af964ffffd1bd009c21592350459280e..7a8e98cc32471455749b47cf933df38e816d541c 100644 (file)
@@ -27,7 +27,7 @@
 *******************************************************************************/
 
 #include <Moof/Engine.hh>
-#include <Moof/Exception.hh>
+#include <Moof/Error.hh>
 #include <Moof/Log.hh>
 #include <Moof/Math.hh>
 #include <Moof/OpenGL.hh>
@@ -35,7 +35,6 @@
 #include <Moof/Video.hh>
 
 #include "GameLayer.hh"
-#include "Hud.hh"
 
 #if HAVE_CONFIG_H
 #include "config.h"
@@ -50,7 +49,7 @@ void GameLayer::loadSceneLoader()
        std::string loaderPath = Scene::getPath("loader");
        if (loaderPath == "")
        {
-               throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
+               throw Mf::Error(Mf::Error::RESOURCE_NOT_FOUND, "loader");
        }
 
        Mf::Script::Result status = mState.script.doFile(loaderPath);
@@ -59,14 +58,14 @@ void GameLayer::loadSceneLoader()
                std::string str;
                mState.script[-1].get(str);
 
-               throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
+               throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
        }
 
        mState.script.getGlobalTable().pushField("scenes");
        mState.script.getTop().get(mState.sceneList);
        if (mState.sceneList.size() == 0)
        {
-               throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR,
+               throw Mf::Error(Mf::Error::SCRIPT_ERROR,
                                "no variable `scenes' within loader");
        }
 }
@@ -84,7 +83,7 @@ void GameLayer::advanceScene()
                        std::string str;
                        mState.script[-1].get(str);
 
-                       throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str);
+                       throw Mf::Error(Mf::Error::SCRIPT_ERROR, str);
                }
 
                mState.script.getGlobalTable().pushField("Event");
@@ -103,6 +102,7 @@ void GameLayer::advanceScene()
 
 
 GameLayer::GameLayer() :
+       mHud(Hud::alloc(mState)),
        mMusic("NightFusionIntro"),
        mPunchSound("Thump")
 {
@@ -131,9 +131,9 @@ GameLayer::GameLayer() :
 }
 
 
-void GameLayer::pushed(Mf::Engine& engine)
+void GameLayer::pushedOntoEngine()
 {
-       engine.push(Hud::alloc(mState));
+       Mf::engine.push(mHud);
 
        mRay.direction.set(1.0, 0.0);
 
@@ -147,7 +147,7 @@ void GameLayer::pushed(Mf::Engine& engine)
 }
 
 
-void GameLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
+void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
 {
        mState.camera.update(t, dt);
        mState.heroine->update(t, dt);
@@ -200,7 +200,7 @@ void GameLayer::rayTimer()
 }
 
 
-void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
+void GameLayer::draw(Mf::Scalar alpha) const
 {
        mState.camera.uploadToGL(alpha);
 
@@ -219,7 +219,7 @@ void GameLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
        mSphere.draw();
 }
 
-bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
+bool GameLayer::handleEvent(const Mf::Event& event)
 {
        switch (event.type)
        {
@@ -259,12 +259,12 @@ bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
                case SDL_KEYUP:
                        if (event.key.keysym.sym == SDLK_ESCAPE)
                        {
-                               engine.pop(this);
+                               Mf::engine.pop(this);
                                return true;
                        }
                        else if (event.key.keysym.sym == SDLK_h)
                        {
-                               engine.push(Hud::alloc(mState));
+                               Mf::engine.push(mHud);
                                return true;
                        }
                        return mState.heroine->handleEvent(event);
@@ -285,7 +285,7 @@ bool GameLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
 
 void GameLayer::setProjection()
 {
-       Mf::VideoP video = Mf::Engine::getInstance().getVideo();
+       Mf::VideoP video = Mf::engine.getVideo();
        setProjection(video->getWidth(), video->getHeight());
 }
 
index 73b6a23e6a141ec2315cb9f5f2a5f34e91e90dc8..53613617b513aa7e95ef993fe376b21ec1bf335a 100644 (file)
 
 #include <boost/shared_ptr.hpp>
 
-#include <Moof/Camera.hh>
-#include <Moof/Interpolator.hh>
 #include <Moof/Layer.hh>
 #include <Moof/Math.hh>
-#include <Moof/Script.hh>
 #include <Moof/Sound.hh>
 
 #include <Moof/Line.hh>
@@ -53,9 +50,8 @@
 #include <Moof/Sphere.hh>
 #include <Moof/Timer.hh>
 
-#include "Character.hh"
-#include "Heroine.hh"
-#include "Scene.hh"
+#include "Hud.hh"
+#include "GameState.hh"
 
 
 class GameLayer;
@@ -72,24 +68,11 @@ public:
                return GameLayerP(new GameLayer);
        }
 
-       void pushed(Mf::Engine& engine);
+       void pushedOntoEngine();
 
-       void update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt);
-       void draw(Mf::Engine& engine, Mf::Scalar alpha) const;
-       bool handleEvent(Mf::Engine& engine, const Mf::Event& event);
-
-       struct State
-       {
-               Mf::Script                                      script;
-               std::vector<std::string>        sceneList;
-
-               HeroineP                heroine;
-               SceneP                  scene;
-
-               Mf::PolynomialInterpolator<5> interp;
-
-               Mf::Camera              camera;
-       };
+       void update(Mf::Scalar t, Mf::Scalar dt);
+       void draw(Mf::Scalar alpha) const;
+       bool handleEvent(const Mf::Event& event);
 
 private:
 
@@ -101,9 +84,11 @@ private:
        void setProjection();
        void setProjection(Mf::Scalar width, Mf::Scalar height);
 
-       State                   mState;
+       GameState               mState;
        Mf::Timer               mThinkTimer;
 
+       HudP                    mHud;
+
        Mf::SoundStream mMusic;
        Mf::Sound               mPunchSound;
 
diff --git a/src/GameState.hh b/src/GameState.hh
new file mode 100644 (file)
index 0000000..f69a6b0
--- /dev/null
@@ -0,0 +1,65 @@
+
+/*******************************************************************************
+
+ Copyright (c) 2009, Charles McGarvey
+ All rights reserved.
+ Redistribution   and   use  in  source  and  binary  forms,  with  or  without
+ modification, are permitted provided that the following conditions are met:
+   * Redistributions  of  source  code  must retain the above copyright notice,
+     this list of conditions and the following disclaimer.
+   * Redistributions  in binary form must reproduce the above copyright notice,
+     this  list of conditions and the following disclaimer in the documentation
+     and/or other materials provided with the distribution.
+ THIS  SOFTWARE  IS  PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND  ANY  EXPRESS  OR  IMPLIED  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED.  IN  NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES  (INCLUDING,  BUT  NOT  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES;  LOSS  OF  USE,  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+*******************************************************************************/
+
+#ifndef _GAMESTATE_HH_
+#define _GAMESTATE_HH_
+
+/**
+ * @file GameState.hh
+ * The data.
+ */
+
+#include <boost/shared_ptr.hpp>
+
+#include <Moof/Camera.hh>
+#include <Moof/Interpolator.hh>
+#include <Moof/Script.hh>
+
+#include "Character.hh"
+#include "Heroine.hh"
+#include "Scene.hh"
+
+
+struct GameState
+{
+       Mf::Script                                      script;
+       std::vector<std::string>        sceneList;
+
+       HeroineP                heroine;
+       SceneP                  scene;
+
+       Mf::PolynomialInterpolator<5> interp;
+
+       Mf::Camera              camera;
+};
+
+
+#endif // _GAMESTATE_HH_
+
+/** vim: set ts=4 sw=4 tw=80: *************************************************/
+
index 3a178f627491699ec3b5d921035e8a817b964a69..d77bf57bd88146de508d155fa9a2c23dbc1f89d4 100644 (file)
@@ -118,13 +118,13 @@ void ProgressBar::draw(Mf::Scalar alpha) const
 }
 
 
-Hud::Hud(GameLayer::State& state) :
+Hud::Hud(GameState& state) :
        mState(state),
        mBar1(Tilemap("StatusBars"), 0),
        mBar2(Tilemap("StatusBars"), 2),
        mFont("Font")
 {
-       Mf::VideoP video = Mf::Engine::getInstance().getVideo();
+       Mf::VideoP video = Mf::engine.getVideo();
        resize(video->getWidth(), video->getHeight());
 }
 
index d3675a476c85b62dcb5782256dd2856bfce9f506..016767a5a8ba48ee97bff20743c16645c3eab692 100644 (file)
@@ -39,7 +39,7 @@
 #include <Moof/Math.hh>
 #include <Moof/Rectangle.hh>
 
-#include "GameLayer.hh"
+#include "GameState.hh"
 #include "Tilemap.hh"
 
 
@@ -77,9 +77,9 @@ class Hud : public Mf::Layer
 {
 public:
 
-       Hud(GameLayer::State& state);
+       Hud(GameState& state);
 
-       static HudP alloc(GameLayer::State& state)
+       static HudP alloc(GameState& state)
        {
                return HudP(new Hud(state));
        }
@@ -106,7 +106,7 @@ public:
 
 private:
 
-       GameLayer::State&               mState;
+       GameState&      mState;
 
        ProgressBar     mBar1;
        ProgressBar     mBar2;
index 40377365d18149cf02c7ec8fec0ae4cbd1e4e899..568f0cd0ebe84b2185945895692d5045c89c7454 100644 (file)
 
 MainLayer::MainLayer()
 {
-       mDispatchHandler = Mf::Engine::getInstance().addHandler("video.newcontext",
+       mDispatchHandler = Mf::engine.addHandler("video.newcontext",
                        boost::bind(&MainLayer::contextRecreated, this));
        setupGL();
 }
 
-void MainLayer::pushed(Mf::Engine& engine)
+void MainLayer::pushedOntoEngine()
 {
        //Mf::Scalar coeff[] = {0.0, 1.0};
        //Mf::Lerp interp(coeff, 0.25);
@@ -67,21 +67,21 @@ void MainLayer::pushed(Mf::Engine& engine)
                //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
        //engine->push(transition);
        //engine->push(GameLayer::alloc());
-       engine.push(TitleLayer::alloc());
+       Mf::engine.push(TitleLayer::alloc());
 }
 
 
-void MainLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
+void MainLayer::update(Mf::Scalar t, Mf::Scalar dt)
 {
-       if (engine.getSize() == 1)
+       if (Mf::engine.getSize() == 1)
        {
                // this is the only layer left on the stack
-               //engine.push(TitleLayer::alloc());
-               engine.clear();
+               //Mf::engine.push(TitleLayer::alloc());
+               Mf::engine.clear();
        }
 }
 
-void MainLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
+void MainLayer::draw(Mf::Scalar alpha) const
 {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -92,22 +92,22 @@ void MainLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
        glLoadIdentity();
 }
 
-bool MainLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
+bool MainLayer::handleEvent(const Mf::Event& event)
 {
        switch (event.type)
        {
                case SDL_KEYUP:
                        if (event.key.keysym.sym == SDLK_ESCAPE)
                        {
-                               engine.clear();
+                               Mf::engine.clear();
                        }
                        else if (event.key.keysym.sym == SDLK_f)
                        {
-                               engine.getVideo()->toggleFull();
+                               Mf::engine.getVideo()->toggleFull();
                        }
                        else if (event.key.keysym.sym == SDLK_l)
                        {
-                               Mf::VideoP video = engine.getVideo();
+                               Mf::VideoP video = Mf::engine.getVideo();
                                video->toggleCursorGrab();
                                video->toggleCursorVisible();
                        }
@@ -118,7 +118,7 @@ bool MainLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
                        break;
 
                case SDL_QUIT:
-                       engine.clear();
+                       Mf::engine.clear();
                        break;
        }
 
@@ -271,6 +271,20 @@ int main(int argc, char* argv[])
        atexit(goodbye);
 
 
+       // make sure the engine started up okay
+       if (Mf::engine.getError().isError())
+       {
+               Mf::ModalDialog dialog;
+               dialog.title = PACKAGE_STRING;
+               dialog.text1 = "Fatal Error";
+               dialog.text2 = getErrorString(Mf::engine.getError());
+               dialog.type = Mf::ModalDialog::CRITICAL;
+               dialog.run();
+
+               return 1;
+       }
+
+
 #if        YOINK_LOGLEVEL >= 3
        Mf::Log::setLevel(Mf::Log::INFO);
 #elif  YOINK_LOGLEVEL >= 2
@@ -325,18 +339,17 @@ int main(int argc, char* argv[])
 
        try
        {
-               Mf::Engine& app = Mf::Engine::getInstance();
-               app.setVideo(Mf::Video::alloc(PACKAGE_STRING, iconFile));
-               app.push(MainLayer::alloc());
+               Mf::engine.setVideo(Mf::Video::alloc(PACKAGE_STRING, iconFile));
+               Mf::engine.push(MainLayer::alloc());
 
-               app.run();
+               Mf::engine.run();
        }
-       catch (const Mf::Exception& e)
+       catch (const Mf::Error& error)
        {
                Mf::ModalDialog dialog;
                dialog.title = PACKAGE_STRING;
                dialog.text1 = "Unhandled Exception";
-               dialog.text2 = getErrorString(e);
+               dialog.text2 = getErrorString(error);
                dialog.type = Mf::ModalDialog::CRITICAL;
                dialog.run();
 
index 731adef4e2239018d91708afa184f334aed47831..4f0fc77d3b2d254905b301734fc7f22895d85d66 100644 (file)
@@ -59,11 +59,11 @@ public:
                return MainLayerP(new MainLayer);
        }
 
-       void pushed(Mf::Engine& engine);
+       void pushedOntoEngine();
 
-       void update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt);
-       void draw(Mf::Engine& engine, Mf::Scalar alpha) const;
-       bool handleEvent(Mf::Engine& engine, const Mf::Event& event);
+       void update(Mf::Scalar t, Mf::Scalar dt);
+       void draw(Mf::Scalar alpha) const;
+       bool handleEvent(const Mf::Event& event);
 
 private:
 
index 8ad29fd7b14ebfb53cdfacb659526d11b4801091..b05ff9c9a74355529828094e4ad276eba16cc036 100644 (file)
@@ -122,13 +122,6 @@ Dispatch::Dispatch() :
        mImpl(new Dispatch::Impl) {}
 
 
-Dispatch& Dispatch::getInstance()
-{
-       static Dispatch dispatch;
-       return dispatch;
-}
-
-
 Dispatch::Handler Dispatch::addHandler(const std::string& event,
                const Function& callback)
 {
index 4483f99cca0177815d783705872162a00fc468a0..031f77d0bc65507aee31bfdff50afd4d0f057050 100644 (file)
@@ -109,9 +109,6 @@ public:
 
        Dispatch();
 
-       // create and/or get a global instance
-       static Dispatch& getInstance();
-
        Handler addHandler(const std::string& event, const Function& callback);
        Handler addHandler(const std::string& event, const Function& callback,
                        Handler handler);
index 56e98b926bf6fed2f6fe7edadfd8c742b4339659..50711c744c9758cd1a15e965b6166582a5ec35b9 100644 (file)
@@ -39,7 +39,6 @@
 
 #include "Engine.hh"
 #include "Event.hh"
-#include "Exception.hh"
 #include "Log.hh"
 #include "Math.hh"
 #include "Settings.hh"
@@ -53,8 +52,8 @@ class Engine::Impl
 {
 public:
 
-       Impl(Engine& engine) :
-               mInterface(engine),
+       Impl() :
+               mError(Error::NONE),
                mTimestep(0.01),
                mPrintFps(false)
        {
@@ -67,7 +66,8 @@ public:
 #endif
                {
                        const char* error = SDL_GetError();
-                       throw Exception(ErrorCode::SDL_INIT, error);
+                       mError.init(Error::SDL_INIT, error);
+                       //throw Exception(Error::SDL_INIT, error);
                }
                else
                {
@@ -80,7 +80,8 @@ public:
                if (FE_Init() != 0)
                {
                        const char* error = FE_GetError();
-                       throw Exception(ErrorCode::FASTEVENTS_INIT, error);
+                       mError.init(Error::FASTEVENTS_INIT, error);
+                       //throw Exception(Error::FASTEVENTS_INIT, error);
                }
 
                mAlDevice = alcOpenDevice(0);
@@ -255,7 +256,7 @@ public:
        {
                for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
                {
-                       (*mStackIt)->update(mInterface, t, dt);
+                       (*mStackIt)->update(t, dt);
                }
        }
 
@@ -265,7 +266,7 @@ public:
                std::list<LayerP>::reverse_iterator it;
                for (it = mStack.rbegin(); it != mStack.rend(); ++it)
                {
-                       (*it)->draw(mInterface, alpha);
+                       (*it)->draw(alpha);
                }
        }
 
@@ -273,7 +274,7 @@ public:
        {
                for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
                {
-                       if ((*mStackIt)->handleEvent(mInterface, event)) break;
+                       if ((*mStackIt)->handleEvent(event)) break;
                }
        }
 
@@ -284,7 +285,7 @@ public:
                mStack.push_front(layer);
                logInfo << "stack: " << mStack.size()
                                 << " [pushed " << layer.get() << "]" << std::endl;
-               layer->pushed(mInterface);
+               layer->pushedOntoEngine();
        }
 
        LayerP pop()
@@ -296,7 +297,7 @@ public:
                mStack.pop_front();
                logInfo << "stack: " << mStack.size()
                                 << " [popped " << layer.get() << "]" << std::endl;
-               layer->popped(mInterface);
+               layer->poppedFromEngine();
 
                if (fixIt) mStackIt = --mStack.begin();
 
@@ -323,7 +324,7 @@ public:
 
                                for (it = layers.begin(); it != layers.end(); ++it)
                                {
-                                       (*it)->popped(mInterface);
+                                       (*it)->poppedFromEngine();
                                        logInfo << "stack: " << mStack.size()
                                                         << " [popped " << (*it).get() << "]" << std::endl;
                                }
@@ -356,7 +357,8 @@ public:
        }
 
 
-       Engine&                                         mInterface;
+       Error                                           mError;
+
        VideoP                                          mVideo;
        Dispatch                                        mDispatch;
 
@@ -376,12 +378,13 @@ public:
 
 Engine::Engine() :
        // pass through
-       mImpl(new Engine::Impl(*this)) {}
+       mImpl(new Engine::Impl) {}
+
 
-Engine& Engine::getInstance()
+const Error& Engine::getError() const
 {
-       static Engine engine;
-       return engine;
+       // pass through
+       return mImpl->mError;
 }
 
 
@@ -483,6 +486,9 @@ void Engine::dispatch(const std::string& event,
 }
 
 
+Engine engine;
+
+
 } // namespace Mf
 
 /** vim: set ts=4 sw=4 tw=80: *************************************************/
index c5e99864f103e49dedffee2721609f48e136fe84..07e982abd9ee4207478d077b8365cedc01897643 100644 (file)
@@ -32,6 +32,7 @@
 #include <boost/shared_ptr.hpp>
 
 #include <Moof/Dispatch.hh>
+#include <Moof/Error.hh>
 #include <Moof/Video.hh>
 #include <Moof/Layer.hh>
 
@@ -42,7 +43,7 @@ namespace Mf {
 /**
  * The engine is essentially a stack of layers.  While running, it updates each
  * layer from the bottom up every timestep.  It also draws each layer from the
- * bottom up, adhering to the maximum frame-rate.  Events are send to each layer
+ * bottom up, adhering to the maximum frame-rate.  Events are sent to each layer
  * from the top down until a layer signals the event was handled.  The engine is
  * also responsible for firing timers on time.  The engine will continue running
  * as long as there are layers on the stack.
@@ -52,10 +53,10 @@ class Engine
 {
 public:
 
+       Engine();
        ~Engine() {}
 
-       // get global instance
-       static Engine& getInstance();
+       const Error& getError() const;
 
        // setting the video is required before you can run the engine and should
        // probably be done before adding any layers
@@ -90,13 +91,14 @@ public:
 
 private:
 
-       Engine();               // use getInstance() to access the engine
-
        class Impl;
        boost::shared_ptr<Impl> mImpl;
 };
 
 
+extern Engine engine;
+
+
 } // namespace Mf
 
 #endif // _MOOF_ENGINE_HH_
similarity index 73%
rename from src/Moof/Exception.hh
rename to src/Moof/Error.hh
index 8300746fc57c284411487e7548fbb100fadf73a8..6dab32ffd07e14e93d8cbad3a6bc0620be5e4f7e 100644 (file)
 
 *******************************************************************************/
 
-#ifndef _MOOF_EXCEPTION_HH_
-#define _MOOF_EXCEPTION_HH_
+#ifndef _MOOF_ERROR_HH_
+#define _MOOF_ERROR_HH_
 
-#include <cstring>
+#include <cstring>                     // strncpy
 #include <exception>
 #include <string>
 
-#include <Moof/Math.hh>
-
 
 namespace Mf {
 
 
-class Exception : public std::exception
+class Error : public std::exception
 {
 public:
 
-       explicit Exception(unsigned code, const std::string& what = "")
+       enum Code
+       {
+               NONE = 0,
+               ALC_INIT,                                       // description
+               FASTEVENTS_INIT,                        // description
+               FILE_NOT_FOUND,                         // path of missing file
+               RESOURCE_NOT_FOUND,                     // name of missing resource
+               SCRIPT_ERROR,                           // description
+               SDL_INIT,                                       // description
+               SDL_VIDEOMODE,                          // -
+               UNKNOWN_AUDIO_FORMAT,           // name of resource
+               UNKNOWN_IMAGE_FORMAT,           // name of resource
+       };
+
+       explicit Error(unsigned code, const std::string& what = "")
+       {
+               init(code, what);
+       }
+       virtual ~Error() throw() {}
+
+       void init(unsigned code, const std::string& what = "")
        {
                mWhat[sizeof(mWhat)-1] = '\0';
                strncpy(mWhat, what.c_str(), sizeof(mWhat)-1);
                mCode = code;
        }
-       virtual ~Exception() throw() {}
 
        virtual void raise()
        {
@@ -66,32 +83,21 @@ public:
                return mWhat;
        }
 
+       bool isError() const throw()
+       {
+               return mCode != NONE;
+       }
+
 private:
 
        unsigned        mCode;
        char            mWhat[1024];
 };
 
-namespace ErrorCode {
-enum Code
-{
-       NONE = 0,
-       ALC_INIT,                                       // description
-       FASTEVENTS_INIT,                        // description
-       FILE_NOT_FOUND,                         // path of missing file
-       RESOURCE_NOT_FOUND,                     // name of missing resource
-       SCRIPT_ERROR,                           // description
-       SDL_INIT,                                       // description
-       SDL_VIDEOMODE,                          // -
-       UNKNOWN_AUDIO_FORMAT,           // name of resource
-       UNKNOWN_IMAGE_FORMAT,           // name of resource
-};
-} // namespace ErrorCode
-
 
 } // namespace Mf
 
-#endif // _MOOF_EXCEPTION_HH_
+#endif // _MOOF_ERROR_HH_
 
 /** vim: set ts=4 sw=4 tw=80: *************************************************/
 
index 9ba428899c6461060dacfbd97c0b0b2806c1ca30..01a4145ee168f69d97e772ceedc62773fe463ce0 100644 (file)
 namespace Mf {
 
 
-// forward declaration
-class Engine;
-
-
 class Layer
 {
 public:
 
        virtual ~Layer() {}
 
-       virtual void pushed(Engine& engine) {}
-       virtual void popped(Engine& engine) {}
+       virtual void pushedOntoEngine() {}
+       virtual void poppedFromEngine() {}
 
-       virtual void update(Engine& engine, Scalar t, Scalar dt) {}
-       virtual void draw(Engine& engine, Scalar alpha) const {}
-       virtual bool handleEvent(Engine& engine, const Event& event)
+       virtual void update(Scalar t, Scalar dt) {}
+       virtual void draw(Scalar alpha) const {}
+       virtual bool handleEvent(const Event& event)
        {
                return false;
        }
index a88405effe417bf8d3329dc362e3ae64038f6826..0747e280fffe42e6449569cc6fe90566fb16db1f 100644 (file)
@@ -33,6 +33,7 @@
 #include <Moof/Math.hh>
 #include <Moof/OpenGL.hh>
 #include <Moof/Ray.hh>
+#include <Moof/Shape.hh>
 #include <Moof/Texture.hh>
 
 #include <Moof/Log.hh>
index b91158a591e318fae4dba1e0ac03aa8ca5871e72..f9bdfd7a99622c22bb231405a768e2343c1ab690 100644 (file)
@@ -36,7 +36,6 @@
 #include <vorbis/vorbisfile.h>
 
 #include "Engine.hh"
-#include "Exception.hh"
 #include "Library.hh"
 #include "Log.hh"
 #include "Sound.hh"
@@ -99,7 +98,7 @@ public:
                        {
                                logWarning << "error while loading sound "
                                                   << getName() << std::endl;
-                               throw Exception(ErrorCode::UNKNOWN_AUDIO_FORMAT, getName());
+                               throw Error(Error::UNKNOWN_AUDIO_FORMAT, getName());
                        }
 
                        vorbis_info* vorbisInfo = ov_info(&mOggStream, -1);
@@ -207,9 +206,6 @@ public:
 
        void init()
        {
-               // make sure the engine is initialized
-               Engine::getInstance();
-
                mIsLoaded = false;
                mIsPlaying = false;
                mIsLooping = false;
index 6e5f78a23fc4311e736000aecd922f534a003a3a..14cd79041ac10cbdc2e0b53d4f295b5090bc26b6 100644 (file)
@@ -36,7 +36,6 @@
 
 #include <boost/shared_ptr.hpp>
 
-#include <Moof/Exception.hh>
 #include <Moof/Math.hh>
 #include <Moof/Resource.hh>
 
index 22d3f15e92cab597259673d86c08cd2f4692d292..6769d8887644a1924d2fdc67974b1afa4df4083d 100644 (file)
@@ -33,7 +33,7 @@
 
 #include "Dispatch.hh"
 #include "Engine.hh"
-#include "Exception.hh"
+#include "Error.hh"
 #include "Image.hh"
 #include "Library.hh"
 #include "Log.hh"
@@ -121,8 +121,7 @@ public:
                mWrapT(GL_CLAMP),
                mObject(0)
        {
-               // make sure the engine is initialized
-               Engine& engine = Engine::getInstance();
+               // make sure we have a video
                VideoP video = engine.getVideo();
                ASSERT(video && "cannot load textures without a current video context");
 
@@ -220,7 +219,7 @@ public:
                if (!mImage.isValid())
                {
                        logWarning << "texture not found: " << getName() << std::endl;
-                       throw Exception(ErrorCode::RESOURCE_NOT_FOUND, getName());
+                       throw Error(Error::RESOURCE_NOT_FOUND, getName());
                }
 
                mImage.flip();
index 5f8a248f91a9cefb7818fd7347bc438eedba241d..f200729ec6bba01bde3ab802f7313fcaf16eefc6 100644 (file)
@@ -66,17 +66,17 @@ public:
        }
 
 
-       void popped(Engine& engine)
+       void poppedFromEngine()
        {
                if (mTo) engine.push(mTo);
        }
 
-       void update(Engine& engine, Scalar t, Scalar dt)
+       void update(Scalar t, Scalar dt)
        {
                mInterp.update(t, dt);
 
-               if (mFrom) mFrom->update(engine, t, dt);
-               if (mTo) mTo->update(engine, t, dt);
+               if (mFrom) mFrom->update(t, dt);
+               if (mTo) mTo->update(t, dt);
 
                if (mInterp.isDone())
                {
@@ -118,7 +118,7 @@ public:
                glPopMatrix();
        }
 
-       void draw(Engine& engine, Scalar alpha) const
+       void draw(Scalar alpha) const
        {
                Scalar a = mInterp.getState(alpha);
                logInfo << "transition state: " << a << std::endl;
@@ -130,7 +130,7 @@ public:
                        glPushMatrix();
                        glLoadIdentity();
                        glRotate(180.0 * a, 0.0, 1.0, 0.0);
-                       mFrom->draw(engine, alpha);
+                       mFrom->draw(alpha);
                        glPopMatrix();
                }
                        //drawFade(a);
@@ -140,21 +140,21 @@ public:
                        glPushMatrix();
                        glLoadIdentity();
                        glRotate(180.0 * (1.0 - a), 0.0, 1.0, 0.0);
-                       mTo->draw(engine, alpha);
+                       mTo->draw(alpha);
                        glPopMatrix();
                }
                        //drawFade(1.0 - a);
        }
 
-       bool handleEvent(Engine& engine, const Event& event)
+       bool handleEvent(const Event& event)
        {
                if (mTo)
                {
-                       return mTo->handleEvent(engine, event);
+                       return mTo->handleEvent(event);
                }
                else if (mFrom)
                {
-                       return mFrom->handleEvent(engine, event);
+                       return mFrom->handleEvent(event);
                }
                return false;
        }
index e4579fbc700951215a32b0669a2602c5b7f62fbf..8dab056c645843d391e78868ce9b72a8e1c9b6dc 100644 (file)
@@ -28,7 +28,7 @@
 
 #include "Dispatch.hh"
 #include "Engine.hh"
-#include "Exception.hh"
+#include "Error.hh"
 #include "Image.hh"
 #include "Log.hh"
 #include "Settings.hh"
@@ -64,9 +64,6 @@ Video::Video(const std::string& caption, const std::string& icon)
 
 void Video::init(const Attributes& attribs)
 {
-       // make sure the engine is initialized before setting up the video
-       Engine::getInstance();
-
        mContext = 0;
        mFlags = 0;
        mAttribs = attribs;
@@ -140,7 +137,7 @@ void Video::setVideoMode(const long mode[3])
                        logInfo("video context recreated");
 #endif
                }
-               else throw Exception(ErrorCode::SDL_VIDEOMODE);
+               else throw Error(Error::SDL_VIDEOMODE);
        }
 }
 
index 43bdf02e3d46393aed9b66a39f1dff5ed5f5fa35..175f216c951693dba11a4c4015893fa769f0c2e0 100644 (file)
@@ -31,7 +31,6 @@
 #include <Moof/Aabb.hh>
 #include <Moof/Camera.hh>
 #include <Moof/Entity.hh>
-#include <Moof/Exception.hh>
 #include <Moof/Library.hh>
 #include <Moof/Line.hh>
 #include <Moof/Log.hh>
index f5e4ef02a423300c928e5cc77c52dd153dfc96af..ed8f4c1acd340d2ca952c32b90275f704d696f2a 100644 (file)
@@ -34,7 +34,7 @@
 #include "TitleLayer.hh"
 
 
-void TitleLayer::pushed(Mf::Engine& engine)
+void TitleLayer::pushedOntoEngine()
 {
        Mf::Scalar coeff[] = {0.0, 1.0};
        mFadeIn.init(coeff, 0.1);
@@ -42,18 +42,18 @@ void TitleLayer::pushed(Mf::Engine& engine)
        mGameLayer = GameLayer::alloc();
 }
 
-void TitleLayer::update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt)
+void TitleLayer::update(Mf::Scalar t, Mf::Scalar dt)
 {
        if (!mFadeIn.isDone()) mFadeIn.update(t, dt);
 }
 
-void TitleLayer::draw(Mf::Engine& engine, Mf::Scalar alpha) const
+void TitleLayer::draw(Mf::Scalar alpha) const
 {
        glClearColor(0.0, 0.0, mFadeIn.getState(alpha), 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
 }
 
-bool TitleLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
+bool TitleLayer::handleEvent(const Mf::Event& event)
 {
        switch (event.type)
        {
@@ -63,7 +63,7 @@ bool TitleLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
                                //break;
                        //}
 
-                       Mf::LayerP titleLayer = engine.pop(this);
+                       Mf::LayerP titleLayer = Mf::engine.pop(this);
                        //engine.pushLayer(GameLayer::alloc());
 
                        Mf::Scalar coeff[] = {0.0, 0.75, 0.99, 1.0};
@@ -72,7 +72,7 @@ bool TitleLayer::handleEvent(Mf::Engine& engine, const Mf::Event& event)
                        //Mf::LayerP mGameLayer = GameLayer::alloc();
                        Mf::Transition<Mf::PolynomialInterpolator<3> >::Ptr transition =
                                Mf::Transition<Mf::PolynomialInterpolator<3> >::alloc(mGameLayer, titleLayer, interp);
-                       engine.push(transition);
+                       Mf::engine.push(transition);
                        return true;
        }
 
index a781c936508959e881013a24b8dfad4cf2e9261a..20f42a7b3db9f28a9a8aa0da1b5304e131cc44db 100644 (file)
@@ -48,11 +48,11 @@ public:
                return TitleLayerP(new TitleLayer);
        }
 
-       void pushed(Mf::Engine& engine);
+       void pushedOntoEngine();
 
-       void update(Mf::Engine& engine, Mf::Scalar t, Mf::Scalar dt);
-       void draw(Mf::Engine& engine, Mf::Scalar alpha) const;
-       bool handleEvent(Mf::Engine& engine, const Mf::Event& event);
+       void update(Mf::Scalar t, Mf::Scalar dt);
+       void draw(Mf::Scalar alpha) const;
+       bool handleEvent(const Mf::Event& event);
 
 private:
 
This page took 0.067095 seconds and 4 git commands to generate.