]> Dogcows Code - chaz/yoink/commitdiff
no more useless singleton class
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Mon, 31 Aug 2009 22:47:18 +0000 (16:47 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Mon, 31 Aug 2009 22:47:18 +0000 (16:47 -0600)
src/Makefile.am
src/Moof/Dispatcher.cc
src/Moof/Dispatcher.hh
src/Moof/Engine.cc
src/Moof/Engine.hh
src/Moof/Settings.cc
src/Moof/Settings.hh
src/Moof/Singleton.hh [deleted file]
src/Moof/Texture.cc
src/Moof/Video.cc
src/YoinkApp.cc

index 3273beaccf7ee7130e36436c4ace9bfad77b8871..3e3bd99a2c93b9d5b0e094c2ac56bc5434a163b9 100644 (file)
@@ -48,7 +48,6 @@ libmoof_la_SOURCES = \
                                   Moof/Serializer.hh \
                                   Moof/Settings.cc \
                                   Moof/Settings.hh \
                                   Moof/Serializer.hh \
                                   Moof/Settings.cc \
                                   Moof/Settings.hh \
-                                  Moof/Singleton.hh \
                                   Moof/Sound.cc \
                                   Moof/Sound.hh \
                                   Moof/Sphere.cc \
                                   Moof/Sound.cc \
                                   Moof/Sound.hh \
                                   Moof/Sphere.cc \
index 9e652a37a8983614010cf974f92172c310caae88..7e33c270d848050caf7fe43c8a17633106928fb0 100644 (file)
@@ -30,6 +30,8 @@
 
 #include "Dispatcher.hh"
 
 
 #include "Dispatcher.hh"
 
+#include <iostream>
+
 
 namespace Mf {
 
 
 namespace Mf {
 
@@ -97,6 +99,15 @@ struct Dispatcher::Impl
 Dispatcher::Dispatcher() :
        impl_(new 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)
 
 Dispatcher::Handler Dispatcher::addHandler(const std::string& message,
                const Function& callback)
index 5747f8581ddbc3622eed9be8ce5c3f0eebd03293..a152691a8184d170d62890df2fb72dbc51959dc2 100644 (file)
 
 #include <string>
 
 
 #include <string>
 
-#include <boost/shared_ptr.hpp>
 #include <boost/function.hpp>
 #include <boost/function.hpp>
-
-#include <Moof/Singleton.hh>
+#include <boost/shared_ptr.hpp>
 
 
 namespace Mf {
 
 
 namespace Mf {
@@ -55,13 +53,17 @@ public:
  * Dispatcher of notifications to interested parties.
  */
 
  * Dispatcher of notifications to interested parties.
  */
 
-class Dispatcher : public Singleton<Dispatcher>
+class Dispatcher
 {
 public:
        typedef void* Handler;
        typedef boost::function<void(const Notification*)> Function;
 
        Dispatcher();
 {
 public:
        typedef void* Handler;
        typedef boost::function<void(const Notification*)> 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,
 
        Handler addHandler(const std::string& message, const Function& callback);
        Handler addHandler(const std::string& message, const Function& callback,
index 78233688d60f69dba90c880a0c9e94b9518f0814..d7d04a846af6bc54753551d1135d582b481fab06 100644 (file)
@@ -35,7 +35,6 @@
 #include <SDL/SDL_sound.h>
 #include <AL/alut.h>
 
 #include <SDL/SDL_sound.h>
 #include <AL/alut.h>
 
-#include "Dispatcher.hh"
 #include "Engine.hh"
 #include "Random.hh"
 #include "Settings.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) :
        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)
                {
        {
                if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0)
                {
@@ -70,6 +68,8 @@ public:
                }
                alutInit(&argc, argv);
 
                }
                alutInit(&argc, argv);
 
+               Settings& settings = Settings::getInstance();
+               settings.parseArgs(argc, argv);
                settings.loadFromFile(configFile);
 
                long randomSeed;
                settings.loadFromFile(configFile);
 
                long randomSeed;
@@ -219,8 +219,6 @@ public:
 
        Engine*         interface;
 
 
        Engine*         interface;
 
-       Settings        settings;
-       Dispatcher      dispatcher;
        VideoPtr        video;
 
        bool            running;
        VideoPtr        video;
 
        bool            running;
index 971d68fe1e28eb59e3536056dd19c4355ec0668c..aa751356f6a81b6ec1712cf1ee7bdf322384d749 100644 (file)
@@ -36,7 +36,6 @@
 #include <Moof/Dispatcher.hh>
 #include <Moof/Event.hh>
 #include <Moof/Math.hh>
 #include <Moof/Dispatcher.hh>
 #include <Moof/Event.hh>
 #include <Moof/Math.hh>
-#include <Moof/Singleton.hh>
 
 
 namespace Mf {
 
 
 namespace Mf {
@@ -45,11 +44,15 @@ namespace Mf {
 // forward declaration
 class Video;
 
 // forward declaration
 class Video;
 
-class Engine : public Singleton<Engine>
+class Engine
 {
 {
-public:
+protected:
+
        Engine(int argc, char* argv[], const std::string& configFile,
                        const std::string& name, const std::string& iconFile);
        Engine(int argc, char* argv[], const std::string& configFile,
                        const std::string& name, const std::string& iconFile);
+
+public:
+
        virtual ~Engine();
 
        int run();
        virtual ~Engine();
 
        int run();
@@ -63,7 +66,7 @@ public:
        Video& getVideo();
        long getFrameRate();
 
        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);
        virtual void update(Scalar t, Scalar dt);
        virtual void draw(Scalar alpha);
        virtual void handleEvent(const Event& event);
@@ -75,6 +78,8 @@ public:
        };
 
 private:
        };
 
 private:
+       Engine() {} // this class must be subclassed to be useful
+
        class Impl;
        boost::shared_ptr<Impl> impl_;
 };
        class Impl;
        boost::shared_ptr<Impl> impl_;
 };
index fe98f964d079b40e4c580141ef436fb784af0eff..ad9c3b0fdebbb9b9eaf14a4ae5d752cad1acb278 100644 (file)
@@ -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)
 void Settings::parseArgs(int argc, char* argv[])
 {
        for (int i = 1; i < argc; ++i)
index 9c29007613f90d6b35a7c8500208581dae05a5a5..faad547c86f20820e91cbe80a1b5a88b87d32d49 100644 (file)
 #include <map>
 #include <string>
 
 #include <map>
 #include <string>
 
-#include <Moof/Singleton.hh>
 #include <Moof/Serializable.hh>
 
 
 namespace Mf {
 
 
 #include <Moof/Serializable.hh>
 
 
 namespace Mf {
 
 
-class Settings : public Singleton<Settings>
+class Settings
 {
 public:
        Settings() {}
        Settings(int argc, char* argv[]);
 
 {
 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);
        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 (file)
index 3d60ff2..0000000
+++ /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 <stdexcept>
-
-
-namespace Mf {
-
-
-template <typename T>
-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>*)(T*)1;
-                       ptr_ = (T*)((long long)this + offset);
-               }
-       }
-       ~Singleton()
-       {
-               long long offset = (long long)(T*)1 - (long long)(Singleton<T>*)(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 <typename T> T* Singleton<T>::ptr_ = 0;
-
-
-} // namespace Mf
-
-#endif // _MOOF_SINGLETON_HH_
-
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-
index 8aa66c4f1ec7c64c95b3d35016f4ef1e9068593d..46dbb480afc3fc179f06b2db8192ac5b421c0735 100644 (file)
@@ -38,6 +38,7 @@
 #include "OpenGL.hh"
 #include "Texture.hh"
 
 #include "OpenGL.hh"
 #include "Texture.hh"
 
+#include <iostream>
 
 namespace Mf {
 
 
 namespace Mf {
 
@@ -80,6 +81,7 @@ class Texture::Impl : public Mippleton<Impl>
 
        void contextRecreated(const Notification* note)
        {
 
        void contextRecreated(const Notification* note)
        {
+               std::cout << "context recrated!" << std::endl;
                object_ = globalObject_ = 0;
                uploadToGL();
        }
                object_ = globalObject_ = 0;
                uploadToGL();
        }
@@ -144,7 +146,7 @@ public:
                loadFromFile();
 
                // we want to know when the GL context is recreated
                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);
        }
 
                                boost::bind(&Impl::contextRecreated, this, _1), this);
        }
 
@@ -157,7 +159,7 @@ public:
 
                unloadFromGL();
 
 
                unloadFromGL();
 
-               Dispatcher::instance().removeHandler(this);
+               Dispatcher::getInstance().removeHandler(this);
        }
 
 
        }
 
 
index 0130d695f0900ce912069c1446fb824c545f0f8e..0452c973cd91264f0a82c2120de786625c079d7b 100644 (file)
@@ -84,7 +84,7 @@ void Video::recreateContext()
        SDL_FreeSurface(context_);
        context_ = 0;
        setVideoMode(attribs_.mode);
        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()
 }
 
 void Video::setOpenGLAttributes()
@@ -309,47 +309,49 @@ Video::Attributes::Attributes()
        cursorVisible = true;
        cursorGrab = false;
 
        cursorVisible = true;
        cursorGrab = false;
 
+       Settings& settings = Settings::getInstance();
+
        std::vector<SerializablePtr> colors;
        std::vector<SerializablePtr> 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]);
 
        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<SerializablePtr> accum;
 
        std::vector<SerializablePtr> 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]);
 
        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";
        }
        {
                caption = "Untitled";
        }
-       Settings::instance().get("video.icon", icon);
+       settings.get("video.icon", icon);
 
        std::vector<SerializablePtr> dimensions;
 
        std::vector<SerializablePtr> 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]);
 
        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);
 }
 
 
 }
 
 
index bf6d71ab8d462d81507ee6525be954287111a51e..626fe1aea9ab18e823c49635573a19dad9dfac8c 100644 (file)
@@ -107,7 +107,7 @@ YoinkApp::YoinkApp(int argc, char* argv[]) :
        music("NightFusion"),
        punchSound("RobotPunch")
 {
        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();
 
                        boost::bind(&YoinkApp::contextRecreated, this, _1), this);
        setupGL();
 
@@ -138,11 +138,12 @@ YoinkApp::YoinkApp(int argc, char* argv[]) :
 
 YoinkApp::~YoinkApp()
 {
 
 YoinkApp::~YoinkApp()
 {
+       std::cerr << "yoinkapp destructor" << std::endl;
        //delete heroine;
        delete font;
        delete testScene;
 
        //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();
                        if (event.key.keysym.sym == SDLK_ESCAPE)
                        {
                                stop();
+                               break;
                        }
                        else if (event.key.keysym.sym == SDLK_f)
                        {
                                getVideo().toggleFull();
                        }
                        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();
                        }
                        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();
                        }
                        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();
                        }
                        else if (event.key.keysym.sym == SDLK_p)
                        {
                                music.togglePlayPause();
+                               break;
                        }
                        else if (event.key.keysym.sym == SDLK_l)
                        {
                                getVideo().toggleCursorGrab();
                                getVideo().toggleCursorVisible();
                        }
                        else if (event.key.keysym.sym == SDLK_l)
                        {
                                getVideo().toggleCursorGrab();
                                getVideo().toggleCursorVisible();
+                               break;
                        }
 
                case SDL_KEYUP:
                        }
 
                case SDL_KEYUP:
@@ -454,8 +466,9 @@ int main(int argc, char* argv[])
 
        try
        {
 
        try
        {
-               YoinkApp app(argc, argv);
-               status = app.run();
+               YoinkApp* app = new YoinkApp(argc, argv);
+               status = app->run();
+               delete app;
        }
        catch (Mf::Engine::Exception e)
        {
        }
        catch (Mf::Engine::Exception e)
        {
This page took 0.03734 seconds and 4 git commands to generate.