*******************************************************************************/
#include <algorithm>
-#include <cstdlib> // exit
+#include <cstdlib> // exit, srand
+#include <ctime> // time
#include <list>
#include <string>
+#include <AL/alc.h>
#include <SDL/SDL.h>
#include "fastevents.h"
-#include <AL/alut.h>
-#include "Dispatcher.hh"
+
#include "Engine.hh"
#include "Event.hh"
#include "Log.hh"
-#include "Random.hh"
+#include "Math.hh"
#include "Settings.hh"
#include "Timer.hh"
-#include "Video.hh"
namespace Mf {
{
public:
- Impl(int argc, char* argv[], const std::string& name,
- const std::string& iconFile, const std::string& configFile,
- Engine& engine) :
- mInterface(engine),
+ Impl() :
+ mError(Error::NONE),
mTimestep(0.01),
mPrintFps(false)
{
-#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
+ // first, initialize the libraries
+
+#if defined(_WIN32) || defined(__WIN32__)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
#else
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) != 0)
#endif
{
- logError("sdl is complaining: %s", SDL_GetError());
- throw Exception(Exception::SDL_ERROR);
+ const char* error = SDL_GetError();
+ mError.init(Error::SDL_INIT, error);
+ //throw Exception(Error::SDL_INIT, error);
+ }
+ else
+ {
+ char vdName[128];
+ SDL_VideoDriverName(vdName, sizeof(vdName));
+ logInfo << "initialized SDL; using video driver `"
+ << vdName << "'" << std::endl;
}
+
if (FE_Init() != 0)
{
- logError("fast events error: %s", FE_GetError());
- throw Exception(Exception::SDL_ERROR);
+ const char* error = FE_GetError();
+ mError.init(Error::FASTEVENTS_INIT, error);
+ //throw Exception(Error::FASTEVENTS_INIT, error);
+ }
+
+ mAlDevice = alcOpenDevice(0);
+ mAlContext = alcCreateContext(mAlDevice, 0);
+ if (!mAlDevice || !mAlContext)
+ {
+ const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice));
+ logError << "error while creating audio context: "
+ << error << std::endl;
+ }
+ else
+ {
+ alcMakeContextCurrent(mAlContext);
+ logInfo << "opened sound device `"
+ << alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)
+ << "'" << std::endl;
}
- alutInit(&argc, argv);
+
+ // now load the settings the engine needs
Settings& settings = Settings::getInstance();
- settings.loadFromFile(configFile);
- settings.parseArgs(argc, argv);
- long randomSeed;
- if (settings.get("rngseed", randomSeed)) setSeed(randomSeed);
- else setSeed();
+ unsigned randomSeed;
+ if (settings.get("rngseed", randomSeed)) srand(randomSeed);
+ else srand(time(0));
Scalar timestep = 80.0;
settings.get("timestep", timestep);
Scalar maxFps = 40.0;
settings.get("maxfps", maxFps);
- mDrawRate = 1.0 / maxFps;
+ mMaxFps = 1.0 / maxFps;
+ capFps();
settings.get("printfps", mPrintFps);
-
- mVideo = Video::alloc(name, iconFile);
- mVideo->makeActive();
}
~Impl()
// the video object must be destroyed before we can shutdown SDL
mVideo.reset();
- alutExit();
+ alcMakeContextCurrent(0);
+ alcDestroyContext(mAlContext);
+ alcCloseDevice(mAlDevice);
+
FE_Quit();
SDL_Quit();
}
deltaTime = newTicks - ticksNow;
ticksNow = newTicks;
+ // don't slow the animation until 4Hz, which is unplayable anyway
if (deltaTime >= 0.25) deltaTime = 0.25;
accumulator += deltaTime;
Timer::fireIfExpired(ticksNow);
+ dispatchEvents();
while (accumulator >= mTimestep)
{
- dispatchEvents();
update(totalTime, mTimestep);
totalTime += mTimestep;
if (mPrintFps)
{
- logInfo("%d fps", mFps);
+ logInfo << mFps << " fps" << std::endl;
}
}
draw(accumulator / mTimestep);
mVideo->swap();
- nextDraw += mDrawRate;
+ nextDraw += mMaxFps;
if (ticksNow >= nextDraw)
{
// we missed some scheduled draws, so reset the schedule
- nextDraw = ticksNow + mDrawRate;
+ nextDraw = ticksNow + mMaxFps;
}
}
// be a good citizen and give back what you don't need
Timer::sleep(std::min(std::min(nextStep, nextDraw),
- Timer::getNextFire()), true);
+ Timer::getNextFire()), Timer::ACTUAL);
}
while (!mStack.empty());
+
+ mDispatch.dispatch("engine.stopping");
}
void dispatchEvents()
(SDL_GetModState() & KMOD_CTRL) )
{
// emergency escape
- exit(0);
+ logWarning("escape forced");
+ exit(1);
}
break;
{
ASSERT(layer && "cannot push null layer");
mStack.push_front(layer);
- logInfo(" push: %d", mStack.size());
- layer->pushed(mInterface);
+ logInfo << "stack: " << mStack.size()
+ << " [pushed " << layer.get() << "]" << std::endl;
+ layer->pushedOntoEngine();
}
LayerP pop()
bool fixIt = false;
if (mStack.begin() == mStackIt) fixIt = true;
- LayerP popped = mStack.front();
+ LayerP layer = mStack.front();
mStack.pop_front();
- logInfo(" pop: %d", mStack.size());
- popped->popped(mInterface);
+ logInfo << "stack: " << mStack.size()
+ << " [popped " << layer.get() << "]" << std::endl;
+ layer->poppedFromEngine();
if (fixIt) mStackIt = --mStack.begin();
- return popped;
+ return layer;
}
LayerP pop(Layer* layer)
{
bool fixIt = false;
- std::list<LayerP> popped;
+ std::list<LayerP> layers;
std::list<LayerP>::iterator it;
for (it = mStack.begin(); it != mStack.end(); ++it)
{
- popped.push_back(*it);
+ layers.push_back(*it);
if (it == mStackIt) fixIt = true;
++it;
mStack.erase(mStack.begin(), it);
- for (it = popped.begin(); it != popped.end(); ++it)
+ for (it = layers.begin(); it != layers.end(); ++it)
{
- (*it)->popped(mInterface);
+ (*it)->poppedFromEngine();
+ logInfo << "stack: " << mStack.size()
+ << " [popped " << (*it).get() << "]" << std::endl;
}
if (fixIt) mStackIt = --mStack.begin();
- return popped.back();
+ return layers.back();
}
}
{
mStack.clear();
mStackIt = mStack.begin();
- logInfo("clear: %d", mStack.size());
+ logInfo("stack: 0 [cleared]");
}
- Engine& mInterface;
+ void capFps()
+ {
+ if (mMaxFps < mTimestep)
+ {
+ logWarning << "capping maximum fps to timestep ("
+ << mTimestep << ")" << std::endl;
+ mMaxFps = mTimestep;
+ }
+ }
+
+
+ Error mError;
+
+ VideoP mVideo;
+ Dispatch mDispatch;
- VideoP mVideo;
+ ALCdevice* mAlDevice;
+ ALCcontext* mAlContext;
std::list<LayerP> mStack;
std::list<LayerP>::iterator mStackIt;
- Scalar mTimestep;
- Scalar mDrawRate;
+ Scalar mTimestep;
+ Scalar mMaxFps;
- long mFps;
- bool mPrintFps;
+ int mFps;
+ bool mPrintFps;
};
-static Engine* instance = 0;
-
-Engine::Engine(int argc, char* argv[], const std::string& name,
- const std::string& iconFile, const std::string& configFile) :
- mImpl(new Engine::Impl(argc, argv, name, iconFile, configFile, *this))
-{
- instance = this;
-}
+Engine::Engine() :
+ // pass through
+ mImpl(new Engine::Impl) {}
-Engine& Engine::getInstance()
+const Error& Engine::getError() const
{
- ASSERT(instance && "dereferencing null pointer");
- return *instance;
- // TODO this has not been completely thought out
- //static Engine engine;
- //return engine;
+ // pass through
+ return mImpl->mError;
}
-void Engine::run()
+void Engine::setVideo(VideoP video)
{
- return mImpl->run();
+ // pass through
+ mImpl->mVideo = video;
}
-void Engine::setTimestep(Scalar ts)
+VideoP Engine::getVideo() const
{
- mImpl->mTimestep = ts;
+ return mImpl->mVideo;
}
-Scalar Engine::getTimestep() const
+
+void Engine::setTimestep(int ts)
{
- return mImpl->mTimestep;
+ mImpl->mTimestep = 1.0 / Scalar(ts);
+ mImpl->capFps();
}
-void Engine::setMaxFrameRate(long maxFps)
+int Engine::getTimestep() const
{
- mImpl->mDrawRate = 1.0 / Scalar(maxFps);
+ return int(1.0 / mImpl->mTimestep);
}
-long Engine::getMaxFrameRate() const
+
+void Engine::setMaxFps(int maxFps)
{
- return long(1.0 / mImpl->mDrawRate);
+ mImpl->mMaxFps = 1.0 / Scalar(maxFps);
+ mImpl->capFps();
}
-
-Video& Engine::getVideo() const
+int Engine::getMaxFps() const
{
- return *mImpl->mVideo;
+ return int(1.0 / mImpl->mMaxFps);
}
-long Engine::getFrameRate() const
+
+int Engine::getFps() const
{
return mImpl->mFps;
}
mImpl->clear();
}
+int Engine::getSize() const
+{
+ return mImpl->mStack.size();
+}
+
+
+void Engine::run()
+{
+ // pass through
+ return mImpl->run();
+}
+
+
+Dispatch::Handler Engine::addHandler(const std::string& event,
+ const Dispatch::Function& callback)
+{
+ return mImpl->mDispatch.addHandler(event, callback);
+}
+
+Dispatch::Handler Engine::addHandler(const std::string& event,
+ const Dispatch::Function& callback, Dispatch::Handler handler)
+{
+ return mImpl->mDispatch.addHandler(event, callback, handler);
+}
+
+void Engine::dispatch(const std::string& event,
+ const Dispatch::Message* message)
+{
+ mImpl->mDispatch.dispatch(event, message);
+}
+
+
+Engine engine;
+
} // namespace Mf