From a5f0d391406a68275b41448fc3f49e8d8396c497 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Mon, 31 Aug 2009 16:47:18 -0600 Subject: [PATCH] no more useless singleton class --- src/Makefile.am | 1 - src/Moof/Dispatcher.cc | 11 +++++ src/Moof/Dispatcher.hh | 10 +++-- src/Moof/Engine.cc | 8 ++-- src/Moof/Engine.hh | 13 ++++-- src/Moof/Settings.cc | 7 ++++ src/Moof/Settings.hh | 6 ++- src/Moof/Singleton.hh | 91 ------------------------------------------ src/Moof/Texture.cc | 6 ++- src/Moof/Video.cc | 40 ++++++++++--------- src/YoinkApp.cc | 21 ++++++++-- 11 files changed, 82 insertions(+), 132 deletions(-) delete mode 100644 src/Moof/Singleton.hh diff --git a/src/Makefile.am b/src/Makefile.am index 3273bea..3e3bd99 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,7 +48,6 @@ libmoof_la_SOURCES = \ Moof/Serializer.hh \ Moof/Settings.cc \ Moof/Settings.hh \ - Moof/Singleton.hh \ Moof/Sound.cc \ Moof/Sound.hh \ Moof/Sphere.cc \ diff --git a/src/Moof/Dispatcher.cc b/src/Moof/Dispatcher.cc index 9e652a3..7e33c27 100644 --- a/src/Moof/Dispatcher.cc +++ b/src/Moof/Dispatcher.cc @@ -30,6 +30,8 @@ #include "Dispatcher.hh" +#include + namespace Mf { @@ -97,6 +99,15 @@ struct Dispatcher::Impl Dispatcher::Dispatcher() : impl_(new Dispatcher::Impl) {} +Dispatcher::~Dispatcher() {} + + +Dispatcher& Dispatcher::getInstance() +{ + static Dispatcher dispatcher; + return dispatcher; +} + Dispatcher::Handler Dispatcher::addHandler(const std::string& message, const Function& callback) diff --git a/src/Moof/Dispatcher.hh b/src/Moof/Dispatcher.hh index 5747f85..a152691 100644 --- a/src/Moof/Dispatcher.hh +++ b/src/Moof/Dispatcher.hh @@ -31,10 +31,8 @@ #include -#include #include - -#include +#include namespace Mf { @@ -55,13 +53,17 @@ public: * Dispatcher of notifications to interested parties. */ -class Dispatcher : public Singleton +class Dispatcher { public: typedef void* Handler; typedef boost::function Function; Dispatcher(); + ~Dispatcher(); + + // get global instance + static Dispatcher& getInstance(); Handler addHandler(const std::string& message, const Function& callback); Handler addHandler(const std::string& message, const Function& callback, diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 7823368..d7d04a8 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -35,7 +35,6 @@ #include #include -#include "Dispatcher.hh" #include "Engine.hh" #include "Random.hh" #include "Settings.hh" @@ -52,8 +51,7 @@ public: Impl(int argc, char* argv[], const std::string& configFile, const std::string& name, const std::string& iconFile, Engine* outer) : - interface(outer), - settings(argc, argv) + interface(outer) { if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0) { @@ -70,6 +68,8 @@ public: } alutInit(&argc, argv); + Settings& settings = Settings::getInstance(); + settings.parseArgs(argc, argv); settings.loadFromFile(configFile); long randomSeed; @@ -219,8 +219,6 @@ public: Engine* interface; - Settings settings; - Dispatcher dispatcher; VideoPtr video; bool running; diff --git a/src/Moof/Engine.hh b/src/Moof/Engine.hh index 971d68f..aa75135 100644 --- a/src/Moof/Engine.hh +++ b/src/Moof/Engine.hh @@ -36,7 +36,6 @@ #include #include #include -#include namespace Mf { @@ -45,11 +44,15 @@ namespace Mf { // forward declaration class Video; -class Engine : public Singleton +class Engine { -public: +protected: + Engine(int argc, char* argv[], const std::string& configFile, const std::string& name, const std::string& iconFile); + +public: + virtual ~Engine(); int run(); @@ -63,7 +66,7 @@ public: Video& getVideo(); long getFrameRate(); - // Override these if you want. + // override these if you want virtual void update(Scalar t, Scalar dt); virtual void draw(Scalar alpha); virtual void handleEvent(const Event& event); @@ -75,6 +78,8 @@ public: }; private: + Engine() {} // this class must be subclassed to be useful + class Impl; boost::shared_ptr impl_; }; diff --git a/src/Moof/Settings.cc b/src/Moof/Settings.cc index fe98f96..ad9c3b0 100644 --- a/src/Moof/Settings.cc +++ b/src/Moof/Settings.cc @@ -44,6 +44,13 @@ Settings::Settings(int argc, char* argv[]) } +Settings& Settings::getInstance() +{ + static Settings settings; + return settings; +} + + void Settings::parseArgs(int argc, char* argv[]) { for (int i = 1; i < argc; ++i) diff --git a/src/Moof/Settings.hh b/src/Moof/Settings.hh index 9c29007..faad547 100644 --- a/src/Moof/Settings.hh +++ b/src/Moof/Settings.hh @@ -37,19 +37,21 @@ #include #include -#include #include namespace Mf { -class Settings : public Singleton +class Settings { public: Settings() {} Settings(int argc, char* argv[]); + // get global instance + static Settings& getInstance(); + void parseArgs(int argc, char* argv[]); void loadFromFile(const std::string& filePath, bool precedence = false); diff --git a/src/Moof/Singleton.hh b/src/Moof/Singleton.hh deleted file mode 100644 index 3d60ff2..0000000 --- a/src/Moof/Singleton.hh +++ /dev/null @@ -1,91 +0,0 @@ - -/******************************************************************************* - - 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 _MOOF_SINGLETON_HH_ -#define _MOOF_SINGLETON_HH_ - -#include - - -namespace Mf { - - -template -class Singleton -{ - static T* ptr_; - -public: - struct Exception : public std::runtime_error - { - explicit Exception(const std::string& what_arg) : - std::runtime_error(what_arg) {} - }; - - Singleton() - { - if (!ptr_) - { - // This hack is from Game Programming Gems. - long long offset = (long long)(T*)1 - (long long)(Singleton*)(T*)1; - ptr_ = (T*)((long long)this + offset); - } - } - ~Singleton() - { - long long offset = (long long)(T*)1 - (long long)(Singleton*)(T*)1; - if (ptr_ == (T*)((long long)this + offset)) - { - ptr_ = 0; - } - } - - static T& instance() - { - if (!ptr_) - { - throw Exception("accessing uninstantiated singleton"); - } - return *ptr_; - } - - static T* instance_ptr() - { - return ptr_; - } -}; - -template T* Singleton::ptr_ = 0; - - -} // namespace Mf - -#endif // _MOOF_SINGLETON_HH_ - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ - diff --git a/src/Moof/Texture.cc b/src/Moof/Texture.cc index 8aa66c4..46dbb48 100644 --- a/src/Moof/Texture.cc +++ b/src/Moof/Texture.cc @@ -38,6 +38,7 @@ #include "OpenGL.hh" #include "Texture.hh" +#include namespace Mf { @@ -80,6 +81,7 @@ class Texture::Impl : public Mippleton void contextRecreated(const Notification* note) { + std::cout << "context recrated!" << std::endl; object_ = globalObject_ = 0; uploadToGL(); } @@ -144,7 +146,7 @@ public: loadFromFile(); // we want to know when the GL context is recreated - Dispatcher::instance().addHandler("video.context_recreated", + Dispatcher::getInstance().addHandler("video.context_recreated", boost::bind(&Impl::contextRecreated, this, _1), this); } @@ -157,7 +159,7 @@ public: unloadFromGL(); - Dispatcher::instance().removeHandler(this); + Dispatcher::getInstance().removeHandler(this); } diff --git a/src/Moof/Video.cc b/src/Moof/Video.cc index 0130d69..0452c97 100644 --- a/src/Moof/Video.cc +++ b/src/Moof/Video.cc @@ -84,7 +84,7 @@ void Video::recreateContext() SDL_FreeSurface(context_); context_ = 0; setVideoMode(attribs_.mode); - Mf::Dispatcher::instance().dispatch("video.context_recreated"); + Mf::Dispatcher::getInstance().dispatch("video.context_recreated"); } void Video::setOpenGLAttributes() @@ -309,47 +309,49 @@ Video::Attributes::Attributes() cursorVisible = true; cursorGrab = false; + Settings& settings = Settings::getInstance(); + std::vector colors; - Settings::instance().get("video.colorbuffers", colors); + settings.get("video.colorbuffers", colors); if (colors.size() > 0) colors[0]->get(colorBuffer[0]); if (colors.size() > 1) colors[1]->get(colorBuffer[1]); if (colors.size() > 2) colors[2]->get(colorBuffer[2]); if (colors.size() > 3) colors[3]->get(colorBuffer[3]); - Settings::instance().get("video.framebuffer", frameBuffer); - Settings::instance().get("video.doublebuffer", doubleBuffer); - Settings::instance().get("video.depthbuffer", depthBuffer); - Settings::instance().get("video.stencilbuffer", stencilBuffer); + settings.get("video.framebuffer", frameBuffer); + settings.get("video.doublebuffer", doubleBuffer); + settings.get("video.depthbuffer", depthBuffer); + settings.get("video.stencilbuffer", stencilBuffer); std::vector accum; - Settings::instance().get("video.accumbuffers", accum); + settings.get("video.accumbuffers", accum); if (accum.size() > 0) accum[0]->get(accumBuffer[0]); if (accum.size() > 1) accum[1]->get(accumBuffer[1]); if (accum.size() > 2) accum[2]->get(accumBuffer[2]); if (accum.size() > 3) accum[3]->get(accumBuffer[3]); - Settings::instance().get("video.stereo", stereo); - Settings::instance().get("video.multiesamplebuffers", multisampleBuffers); - Settings::instance().get("video.multiesamplesamples", multisampleSamples); - Settings::instance().get("video.swapcontrol", swapControl); - Settings::instance().get("video.hardwareonly", hardwareonly); + settings.get("video.stereo", stereo); + settings.get("video.multiesamplebuffers", multisampleBuffers); + settings.get("video.multiesamplesamples", multisampleSamples); + settings.get("video.swapcontrol", swapControl); + settings.get("video.hardwareonly", hardwareonly); - if (!Settings::instance().get("video.caption", caption)) + if (!settings.get("video.caption", caption)) { caption = "Untitled"; } - Settings::instance().get("video.icon", icon); + settings.get("video.icon", icon); std::vector dimensions; - Settings::instance().get("video.mode", dimensions); + settings.get("video.mode", dimensions); if (dimensions.size() > 0) dimensions[0]->get(mode[0]); if (dimensions.size() > 1) dimensions[1]->get(mode[1]); if (dimensions.size() > 2) dimensions[2]->get(mode[2]); - Settings::instance().get("video.fullscreen", fullscreen); - Settings::instance().get("video.resizable", resizable); - Settings::instance().get("video.showcursor", cursorVisible); - Settings::instance().get("input.grab", cursorGrab); + settings.get("video.fullscreen", fullscreen); + settings.get("video.resizable", resizable); + settings.get("video.showcursor", cursorVisible); + settings.get("input.grab", cursorGrab); } diff --git a/src/YoinkApp.cc b/src/YoinkApp.cc index bf6d71a..626fe1a 100644 --- a/src/YoinkApp.cc +++ b/src/YoinkApp.cc @@ -107,7 +107,7 @@ YoinkApp::YoinkApp(int argc, char* argv[]) : music("NightFusion"), punchSound("RobotPunch") { - Mf::Dispatcher::instance().addHandler("video.context_recreated", + Mf::Dispatcher::getInstance().addHandler("video.context_recreated", boost::bind(&YoinkApp::contextRecreated, this, _1), this); setupGL(); @@ -138,11 +138,12 @@ YoinkApp::YoinkApp(int argc, char* argv[]) : YoinkApp::~YoinkApp() { + std::cerr << "yoinkapp destructor" << std::endl; //delete heroine; delete font; delete testScene; - Mf::Dispatcher::instance().removeHandler(this); + Mf::Dispatcher::getInstance().removeHandler(this); } @@ -396,28 +397,39 @@ void YoinkApp::handleEvent(const Mf::Event& event) if (event.key.keysym.sym == SDLK_ESCAPE) { stop(); + break; } else if (event.key.keysym.sym == SDLK_f) { getVideo().toggleFull(); + break; } else if (event.key.keysym.sym == SDLK_SPACE) { heroine->getAnimation().startSequence("Punch"); punchSound.play(); + break; } else if (event.key.keysym.sym == SDLK_r) { testScene->refresh(); + break; + } + else if (event.key.keysym.sym == SDLK_t) + { + Mf::Dispatcher::getInstance().dispatch("video.context_recreated"); + break; } else if (event.key.keysym.sym == SDLK_p) { music.togglePlayPause(); + break; } else if (event.key.keysym.sym == SDLK_l) { getVideo().toggleCursorGrab(); getVideo().toggleCursorVisible(); + break; } case SDL_KEYUP: @@ -454,8 +466,9 @@ int main(int argc, char* argv[]) try { - YoinkApp app(argc, argv); - status = app.run(); + YoinkApp* app = new YoinkApp(argc, argv); + status = app->run(); + delete app; } catch (Mf::Engine::Exception e) { -- 2.43.0