*******************************************************************************/
#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)
+ mFramerate(0.02),
+ mShowFps(false)
{
-#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
+#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);
+ return; // fatal
+ }
+ else
+ {
+ char name[128];
+ SDL_VideoDriverName(name, sizeof(name));
+ logInfo << "initialized SDL; using video driver `"
+ << name << "'" << 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);
+ return; // fatal
}
- alutInit(&argc, argv);
- Settings& settings = Settings::getInstance();
- settings.loadFromFile(configFile);
- settings.parseArgs(argc, argv);
+ mAlDevice = alcOpenDevice(0);
+ mAlContext = alcCreateContext(mAlDevice, 0);
+ if (!mAlDevice || !mAlContext)
+ {
+ const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice));
+ mError.init(Error::OPENAL_INIT, error);
+ }
+ else
+ {
+ alcMakeContextCurrent(mAlContext);
+ logInfo << "opened sound device `"
+ << alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)
+ << "'" << std::endl;
+ }
+ }
- long randomSeed;
- if (settings.get("rngseed", randomSeed)) setSeed(randomSeed);
- else setSeed();
+ bool initWithSettings(const Settings& settings)
+ {
+ unsigned randomSeed;
+ if (settings.get("rngseed", randomSeed)) srand(randomSeed);
+ else srand(time(0));
Scalar timestep = 80.0;
settings.get("timestep", timestep);
mTimestep = 1.0 / timestep;
- Scalar maxFps = 40.0;
- settings.get("maxfps", maxFps);
- mDrawRate = 1.0 / maxFps;
+ Scalar framerate = 40.0;
+ settings.get("framerate", framerate);
+ mFramerate = 1.0 / framerate;
+ capFps();
- settings.get("printfps", mPrintFps);
+ mShowFps = false;
+ settings.get("showfps", mShowFps);
- mVideo = Video::alloc(name, iconFile);
- mVideo->makeActive();
+ return true;
}
~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();
}
void run()
{
- Scalar ticksNow = Timer::getTicks();
-
- Scalar nextStep = ticksNow;
- Scalar nextDraw = ticksNow;
- Scalar nextFpsUpdate = ticksNow + 1.0;
-
Scalar totalTime = 0.0;
- Scalar deltaTime = 0.0;
- Scalar accumulator = mTimestep;
+ Scalar ticks = Timer::getTicks();
+
+ Scalar nextUpdate = ticks;
+ Scalar nextDraw = ticks;
+ Scalar nextSecond = ticks + SCALAR(1.0);
mFps = 0;
- int frameAccum = 0;
+ int frames = 0;
+
+ const int MAX_FRAMESKIP = 15;
+ const Scalar inverseTimestep = SCALAR(1.0) / mTimestep;
do
{
- Scalar newTicks = Timer::getTicks();
- deltaTime = newTicks - ticksNow;
- ticksNow = newTicks;
+ Timer::fireIfExpired();
+ dispatchEvents();
- if (deltaTime >= 0.25) deltaTime = 0.25;
- accumulator += deltaTime;
-
- Timer::fireIfExpired(ticksNow);
-
- while (accumulator >= mTimestep)
+ int i = 0;
+ while (nextUpdate < Timer::getTicks() && i < MAX_FRAMESKIP)
{
- dispatchEvents();
- update(totalTime, mTimestep);
-
totalTime += mTimestep;
- accumulator -= mTimestep;
+ update(totalTime, mTimestep);
- nextStep += mTimestep;
- }
- if (ticksNow >= nextStep)
- {
- nextStep = ticksNow + mTimestep;
+ nextUpdate += mTimestep;
+ ++i;
}
- if (ticksNow >= nextDraw)
+ if (nextDraw < (ticks = Timer::getTicks()))
{
- frameAccum++;
-
- if (ticksNow >= nextFpsUpdate) // determine the actual fps
- {
- mFps = frameAccum;
- frameAccum = 0;
+ ++frames;
+ draw((ticks + mTimestep - nextUpdate) * inverseTimestep);
+ mVideo->swap();
- nextFpsUpdate += 1.0;
- if (ticksNow >= nextFpsUpdate)
- {
- nextFpsUpdate = ticksNow + 1.0;
- }
+ nextDraw += mFramerate;
- if (mPrintFps)
- {
- logInfo("%d fps", mFps);
- }
- }
+ if (mShowFps && nextSecond < ticks)
+ {
+ mFps = frames;
+ frames = 0;
- draw(accumulator / mTimestep);
- mVideo->swap();
+ logInfo << mFps << " fps" << std::endl;
- nextDraw += mDrawRate;
- if (ticksNow >= nextDraw)
- {
- // we missed some scheduled draws, so reset the schedule
- nextDraw = ticksNow + mDrawRate;
+ nextSecond += SCALAR(1.0);
}
}
// be a good citizen and give back what you don't need
- Timer::sleep(std::min(std::min(nextStep, nextDraw),
- Timer::getNextFire()), true);
+ Timer::sleep(0.0);
}
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 (mFramerate < mTimestep)
+ //{
+ //logWarning << "capping maximum fps to timestep ("
+ //<< mTimestep << ")" << std::endl;
+ //mFramerate = 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 mFramerate;
- long mFps;
- bool mPrintFps;
+ int mFps;
+ bool mShowFps;
};
-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()
+bool Engine::initWithSettings(const Settings& settings)
{
- ASSERT(instance && "dereferencing null pointer");
- return *instance;
- // TODO this has not been completely thought out
- //static Engine engine;
- //return engine;
+ // pass through
+ return mImpl->initWithSettings(settings);
}
-
-void Engine::run()
+const Error& Engine::getError() const
{
- return mImpl->run();
+ // pass through
+ return mImpl->mError;
}
-void Engine::setTimestep(Scalar ts)
+void Engine::clearError()
{
- mImpl->mTimestep = ts;
+ // pass through
+ mImpl->mError.init(Error::NONE);
}
-Scalar Engine::getTimestep() const
-{
- return mImpl->mTimestep;
-}
-void Engine::setMaxFrameRate(long maxFps)
+void Engine::setVideo(VideoP video)
{
- mImpl->mDrawRate = 1.0 / Scalar(maxFps);
+ // pass through
+ mImpl->mVideo = video;
}
-long Engine::getMaxFrameRate() const
+VideoP Engine::getVideo() const
{
- return long(1.0 / mImpl->mDrawRate);
+ return mImpl->mVideo;
}
-Video& Engine::getVideo() const
-{
- return *mImpl->mVideo;
-}
-
-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