From a295f8def17036c8071b56e181364f99a377cae7 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Mon, 11 Jan 2010 23:52:21 -0700 Subject: [PATCH] dispatch class not a singleton, engine is static --- src/ErrorHandler.cc | 38 ++++++++--------- src/ErrorHandler.hh | 4 +- src/GameLayer.cc | 28 ++++++------- src/GameLayer.hh | 33 ++++----------- src/GameState.hh | 65 +++++++++++++++++++++++++++++ src/Hud.cc | 4 +- src/Hud.hh | 8 ++-- src/MainLayer.cc | 51 +++++++++++++--------- src/MainLayer.hh | 8 ++-- src/Moof/Dispatch.cc | 7 ---- src/Moof/Dispatch.hh | 3 -- src/Moof/Engine.cc | 38 ++++++++++------- src/Moof/Engine.hh | 12 +++--- src/Moof/{Exception.hh => Error.hh} | 56 ++++++++++++++----------- src/Moof/Layer.hh | 14 +++---- src/Moof/Line.hh | 1 + src/Moof/Sound.cc | 6 +-- src/Moof/Sound.hh | 1 - src/Moof/Texture.cc | 7 ++-- src/Moof/Transition.hh | 20 ++++----- src/Moof/Video.cc | 7 +--- src/Scene.cc | 1 - src/TitleLayer.cc | 12 +++--- src/TitleLayer.hh | 8 ++-- 24 files changed, 243 insertions(+), 189 deletions(-) create mode 100644 src/GameState.hh rename src/Moof/{Exception.hh => Error.hh} (73%) diff --git a/src/ErrorHandler.cc b/src/ErrorHandler.cc index 9c4452c..07bd6d7 100644 --- a/src/ErrorHandler.cc +++ b/src/ErrorHandler.cc @@ -31,59 +31,59 @@ #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(); } diff --git a/src/ErrorHandler.hh b/src/ErrorHandler.hh index 2b13b1d..8132ac4 100644 --- a/src/ErrorHandler.hh +++ b/src/ErrorHandler.hh @@ -29,10 +29,10 @@ #ifndef _ERRORHANDLER_HH_ #define _ERRORHANDLER_HH_ -#include +#include -std::string getErrorString(const Mf::Exception& e); +std::string getErrorString(const Mf::Error& error); #endif // _ERRORHANDLER_HH_ diff --git a/src/GameLayer.cc b/src/GameLayer.cc index 0ed249c..7a8e98c 100644 --- a/src/GameLayer.cc +++ b/src/GameLayer.cc @@ -27,7 +27,7 @@ *******************************************************************************/ #include -#include +#include #include #include #include @@ -35,7 +35,6 @@ #include #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()); } diff --git a/src/GameLayer.hh b/src/GameLayer.hh index 73b6a23..5361361 100644 --- a/src/GameLayer.hh +++ b/src/GameLayer.hh @@ -40,11 +40,8 @@ #include -#include -#include #include #include -#include #include #include @@ -53,9 +50,8 @@ #include #include -#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 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 index 0000000..f69a6b0 --- /dev/null +++ b/src/GameState.hh @@ -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 + +#include +#include +#include + +#include "Character.hh" +#include "Heroine.hh" +#include "Scene.hh" + + +struct GameState +{ + Mf::Script script; + std::vector sceneList; + + HeroineP heroine; + SceneP scene; + + Mf::PolynomialInterpolator<5> interp; + + Mf::Camera camera; +}; + + +#endif // _GAMESTATE_HH_ + +/** vim: set ts=4 sw=4 tw=80: *************************************************/ + diff --git a/src/Hud.cc b/src/Hud.cc index 3a178f6..d77bf57 100644 --- a/src/Hud.cc +++ b/src/Hud.cc @@ -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()); } diff --git a/src/Hud.hh b/src/Hud.hh index d3675a4..016767a 100644 --- a/src/Hud.hh +++ b/src/Hud.hh @@ -39,7 +39,7 @@ #include #include -#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; diff --git a/src/MainLayer.cc b/src/MainLayer.cc index 4037736..568f0cd 100644 --- a/src/MainLayer.cc +++ b/src/MainLayer.cc @@ -52,12 +52,12 @@ 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::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(); diff --git a/src/MainLayer.hh b/src/MainLayer.hh index 731adef..4f0fc77 100644 --- a/src/MainLayer.hh +++ b/src/MainLayer.hh @@ -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: diff --git a/src/Moof/Dispatch.cc b/src/Moof/Dispatch.cc index 8ad29fd..b05ff9c 100644 --- a/src/Moof/Dispatch.cc +++ b/src/Moof/Dispatch.cc @@ -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) { diff --git a/src/Moof/Dispatch.hh b/src/Moof/Dispatch.hh index 4483f99..031f77d 100644 --- a/src/Moof/Dispatch.hh +++ b/src/Moof/Dispatch.hh @@ -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); diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 56e98b9..50711c7 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -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::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: *************************************************/ diff --git a/src/Moof/Engine.hh b/src/Moof/Engine.hh index c5e9986..07e982a 100644 --- a/src/Moof/Engine.hh +++ b/src/Moof/Engine.hh @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -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 mImpl; }; +extern Engine engine; + + } // namespace Mf #endif // _MOOF_ENGINE_HH_ diff --git a/src/Moof/Exception.hh b/src/Moof/Error.hh similarity index 73% rename from src/Moof/Exception.hh rename to src/Moof/Error.hh index 8300746..6dab32f 100644 --- a/src/Moof/Exception.hh +++ b/src/Moof/Error.hh @@ -26,30 +26,47 @@ *******************************************************************************/ -#ifndef _MOOF_EXCEPTION_HH_ -#define _MOOF_EXCEPTION_HH_ +#ifndef _MOOF_ERROR_HH_ +#define _MOOF_ERROR_HH_ -#include +#include // strncpy #include #include -#include - 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: *************************************************/ diff --git a/src/Moof/Layer.hh b/src/Moof/Layer.hh index 9ba4288..01a4145 100644 --- a/src/Moof/Layer.hh +++ b/src/Moof/Layer.hh @@ -38,22 +38,18 @@ 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; } diff --git a/src/Moof/Line.hh b/src/Moof/Line.hh index a88405e..0747e28 100644 --- a/src/Moof/Line.hh +++ b/src/Moof/Line.hh @@ -33,6 +33,7 @@ #include #include #include +#include #include #include diff --git a/src/Moof/Sound.cc b/src/Moof/Sound.cc index b91158a..f9bdfd7 100644 --- a/src/Moof/Sound.cc +++ b/src/Moof/Sound.cc @@ -36,7 +36,6 @@ #include #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; diff --git a/src/Moof/Sound.hh b/src/Moof/Sound.hh index 6e5f78a..14cd790 100644 --- a/src/Moof/Sound.hh +++ b/src/Moof/Sound.hh @@ -36,7 +36,6 @@ #include -#include #include #include diff --git a/src/Moof/Texture.cc b/src/Moof/Texture.cc index 22d3f15..6769d88 100644 --- a/src/Moof/Texture.cc +++ b/src/Moof/Texture.cc @@ -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(); diff --git a/src/Moof/Transition.hh b/src/Moof/Transition.hh index 5f8a248..f200729 100644 --- a/src/Moof/Transition.hh +++ b/src/Moof/Transition.hh @@ -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; } diff --git a/src/Moof/Video.cc b/src/Moof/Video.cc index e4579fb..8dab056 100644 --- a/src/Moof/Video.cc +++ b/src/Moof/Video.cc @@ -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); } } diff --git a/src/Scene.cc b/src/Scene.cc index 43bdf02..175f216 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include diff --git a/src/TitleLayer.cc b/src/TitleLayer.cc index f5e4ef0..ed8f4c1 100644 --- a/src/TitleLayer.cc +++ b/src/TitleLayer.cc @@ -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 >::Ptr transition = Mf::Transition >::alloc(mGameLayer, titleLayer, interp); - engine.push(transition); + Mf::engine.push(transition); return true; } diff --git a/src/TitleLayer.hh b/src/TitleLayer.hh index a781c93..20f42a7 100644 --- a/src/TitleLayer.hh +++ b/src/TitleLayer.hh @@ -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: -- 2.45.2