#include "Engine.hh"
#include "Event.hh"
-#include "Exception.hh"
#include "Log.hh"
#include "Math.hh"
#include "Settings.hh"
{
public:
- Impl(Engine& engine) :
- mInterface(engine),
+ Impl() :
+ mError(Error::NONE),
mTimestep(0.01),
- mPrintFps(false)
+ mFramerate(0.02),
+ mShowFps(false)
{
- // first, initialize the libraries
-
#if defined(_WIN32) || defined(__WIN32__)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
#else
#endif
{
const char* error = SDL_GetError();
- throw Exception(ErrorCode::SDL_INIT, error);
+ mError.init(Error::SDL_INIT, error);
+ return; // fatal
}
else
{
- char vdName[128];
- SDL_VideoDriverName(vdName, sizeof(vdName));
- logDebug << "initialized SDL; using video driver `"
- << vdName << "'" << std::endl;
+ char name[128];
+ SDL_VideoDriverName(name, sizeof(name));
+ logInfo << "initialized SDL; using video driver `"
+ << name << "'" << std::endl;
}
if (FE_Init() != 0)
{
const char* error = FE_GetError();
- throw Exception(ErrorCode::FASTEVENTS_INIT, error);
+ mError.init(Error::FASTEVENTS_INIT, error);
+ return; // fatal
}
mAlDevice = alcOpenDevice(0);
if (!mAlDevice || !mAlContext)
{
const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice));
- logError << "error while creating audio context: "
- << error << std::endl;
+ mError.init(Error::OPENAL_INIT, error);
}
else
{
alcMakeContextCurrent(mAlContext);
- logDebug << "opened sound device `"
+ logInfo << "opened sound device `"
<< alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)
<< "'" << std::endl;
}
+ }
- // now load the settings the engine needs
-
- Settings& settings = Settings::getInstance();
-
+ bool initWithSettings(const Settings& settings)
+ {
unsigned randomSeed;
if (settings.get("rngseed", randomSeed)) srand(randomSeed);
else srand(time(0));
settings.get("timestep", timestep);
mTimestep = 1.0 / timestep;
- Scalar maxFps = 40.0;
- settings.get("maxfps", maxFps);
- mMaxFps = 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);
+
+ return true;
}
~Impl()
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;
-
- // don't slow the animation until 4Hz, which is unplayable anyway
- if (deltaTime >= 0.25) deltaTime = 0.25;
- accumulator += deltaTime;
-
- Timer::fireIfExpired(ticksNow);
+ Timer::fireIfExpired();
dispatchEvents();
- while (accumulator >= mTimestep)
+ int i = 0;
+ while (nextUpdate < Timer::getTicks() && i < MAX_FRAMESKIP)
{
- 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++;
+ ++frames;
+ draw((ticks + mTimestep - nextUpdate) * inverseTimestep);
+ mVideo->swap();
+
+ nextDraw += mFramerate;
- if (ticksNow >= nextFpsUpdate) // determine the actual fps
+ if (mShowFps && nextSecond < ticks)
{
- mFps = frameAccum;
- frameAccum = 0;
+ mFps = frames;
+ frames = 0;
- nextFpsUpdate += 1.0;
- if (ticksNow >= nextFpsUpdate)
- {
- nextFpsUpdate = ticksNow + 1.0;
- }
+ logInfo << mFps << " fps" << std::endl;
- if (mPrintFps)
- {
- logInfo << mFps << " fps" << std::endl;
- }
- }
-
- draw(accumulator / mTimestep);
- mVideo->swap();
-
- nextDraw += mMaxFps;
- if (ticksNow >= nextDraw)
- {
- // we missed some scheduled draws, so reset the schedule
- nextDraw = ticksNow + mMaxFps;
+ 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()), Timer::ACTUAL);
+ Timer::sleep(0.0);
}
while (!mStack.empty());
{
for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
{
- (*mStackIt)->update(mInterface, t, dt);
+ (*mStackIt)->update(t, dt);
}
}
std::list<LayerP>::reverse_iterator it;
for (it = mStack.rbegin(); it != mStack.rend(); ++it)
{
- (*it)->draw(mInterface, alpha);
+ (*it)->draw(alpha);
}
}
{
for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt)
{
- if ((*mStackIt)->handleEvent(mInterface, event)) break;
+ if ((*mStackIt)->handleEvent(event)) break;
}
}
{
ASSERT(layer && "cannot push null layer");
mStack.push_front(layer);
- logDebug << "stack: " << mStack.size()
+ logInfo << "stack: " << mStack.size()
<< " [pushed " << layer.get() << "]" << std::endl;
- layer->pushed(mInterface);
+ layer->pushedOntoEngine();
}
LayerP pop()
LayerP layer = mStack.front();
mStack.pop_front();
- logDebug << "stack: " << mStack.size()
+ logInfo << "stack: " << mStack.size()
<< " [popped " << layer.get() << "]" << std::endl;
- layer->popped(mInterface);
+ layer->poppedFromEngine();
if (fixIt) mStackIt = --mStack.begin();
for (it = layers.begin(); it != layers.end(); ++it)
{
- (*it)->popped(mInterface);
- logDebug << "stack: " << mStack.size()
+ (*it)->poppedFromEngine();
+ logInfo << "stack: " << mStack.size()
<< " [popped " << (*it).get() << "]" << std::endl;
}
{
mStack.clear();
mStackIt = mStack.begin();
- logDebug("stack: 0 [cleared]");
+ logInfo("stack: 0 [cleared]");
}
void capFps()
{
- if (mMaxFps < mTimestep)
- {
- logWarning << "capping maximum fps to timestep ("
- << mTimestep << ")" << std::endl;
- mMaxFps = mTimestep;
- }
+ //if (mFramerate < mTimestep)
+ //{
+ //logWarning << "capping maximum fps to timestep ("
+ //<< mTimestep << ")" << std::endl;
+ //mFramerate = mTimestep;
+ //}
}
- Engine& mInterface;
+ Error mError;
+
VideoP mVideo;
Dispatch mDispatch;
std::list<LayerP>::iterator mStackIt;
Scalar mTimestep;
- Scalar mMaxFps;
+ Scalar mFramerate;
int mFps;
- bool mPrintFps;
+ bool mShowFps;
};
Engine::Engine() :
// pass through
- mImpl(new Engine::Impl(*this)) {}
+ mImpl(new Engine::Impl) {}
-Engine& Engine::getInstance()
-{
- static Engine engine;
- return engine;
-}
-
-void Engine::setVideo(VideoP video)
+bool Engine::initWithSettings(const Settings& settings)
{
// pass through
- mImpl->mVideo = video;
+ return mImpl->initWithSettings(settings);
}
-VideoP Engine::getVideo() const
+const Error& Engine::getError() const
{
- return mImpl->mVideo;
-}
-
-
-void Engine::setTimestep(int ts)
-{
- mImpl->mTimestep = 1.0 / Scalar(ts);
- mImpl->capFps();
+ // pass through
+ return mImpl->mError;
}
-int Engine::getTimestep() const
+void Engine::clearError()
{
- return int(1.0 / mImpl->mTimestep);
+ // pass through
+ mImpl->mError.init(Error::NONE);
}
-void Engine::setMaxFps(int maxFps)
+void Engine::setVideo(VideoP video)
{
- mImpl->mMaxFps = 1.0 / Scalar(maxFps);
- mImpl->capFps();
+ // pass through
+ mImpl->mVideo = video;
}
-int Engine::getMaxFps() const
+VideoP Engine::getVideo() const
{
- return int(1.0 / mImpl->mMaxFps);
+ return mImpl->mVideo;
}
}
+Engine engine;
+
+
} // namespace Mf
/** vim: set ts=4 sw=4 tw=80: *************************************************/