Moof/Serializer.hh \
Moof/Settings.cc \
Moof/Settings.hh \
- Moof/Singleton.hh \
Moof/Sound.cc \
Moof/Sound.hh \
Moof/Sphere.cc \
#include "Dispatcher.hh"
+#include <iostream>
+
namespace Mf {
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)
#include <string>
-#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
-
-#include <Moof/Singleton.hh>
+#include <boost/shared_ptr.hpp>
namespace Mf {
* 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();
+ ~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,
#include <SDL/SDL_sound.h>
#include <AL/alut.h>
-#include "Dispatcher.hh"
#include "Engine.hh"
#include "Random.hh"
#include "Settings.hh"
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)
{
}
alutInit(&argc, argv);
+ Settings& settings = Settings::getInstance();
+ settings.parseArgs(argc, argv);
settings.loadFromFile(configFile);
long randomSeed;
Engine* interface;
- Settings settings;
- Dispatcher dispatcher;
VideoPtr video;
bool running;
#include <Moof/Dispatcher.hh>
#include <Moof/Event.hh>
#include <Moof/Math.hh>
-#include <Moof/Singleton.hh>
namespace Mf {
// 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);
+
+public:
+
virtual ~Engine();
int run();
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);
};
private:
+ Engine() {} // this class must be subclassed to be useful
+
class Impl;
boost::shared_ptr<Impl> impl_;
};
}
+Settings& Settings::getInstance()
+{
+ static Settings settings;
+ return settings;
+}
+
+
void Settings::parseArgs(int argc, char* argv[])
{
for (int i = 1; i < argc; ++i)
#include <map>
#include <string>
-#include <Moof/Singleton.hh>
#include <Moof/Serializable.hh>
namespace Mf {
-class Settings : public Singleton<Settings>
+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);
+++ /dev/null
-
-/*******************************************************************************
-
- 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: *************************************************/
-
#include "OpenGL.hh"
#include "Texture.hh"
+#include <iostream>
namespace Mf {
void contextRecreated(const Notification* note)
{
+ std::cout << "context recrated!" << std::endl;
object_ = globalObject_ = 0;
uploadToGL();
}
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);
}
unloadFromGL();
- Dispatcher::instance().removeHandler(this);
+ Dispatcher::getInstance().removeHandler(this);
}
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()
cursorVisible = true;
cursorGrab = false;
+ Settings& settings = Settings::getInstance();
+
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]);
- 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;
- 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<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]);
- 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);
}
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();
YoinkApp::~YoinkApp()
{
+ std::cerr << "yoinkapp destructor" << std::endl;
//delete heroine;
delete font;
delete testScene;
- Mf::Dispatcher::instance().removeHandler(this);
+ Mf::Dispatcher::getInstance().removeHandler(this);
}
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:
try
{
- YoinkApp app(argc, argv);
- status = app.run();
+ YoinkApp* app = new YoinkApp(argc, argv);
+ status = app->run();
+ delete app;
}
catch (Mf::Engine::Exception e)
{