X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=5d703da15d745110a4caa63d938c6fc7890bcde3;hp=de7c0cba94356afdc190110e97afcef3121cdc73;hb=4f62ce947db282f0bbf4d49b3aafb83d7cf51adc;hpb=d11d8c63ab52c7f6eca2815e47cd6401e72f2c8c diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index de7c0cb..5d703da 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,7 +27,8 @@ *******************************************************************************/ #include -#include // exit +#include // exit, srand +#include // time #include #include @@ -38,10 +39,8 @@ #include "Engine.hh" #include "Event.hh" -#include "Exception.hh" #include "Log.hh" #include "Math.hh" -#include "Random.hh" #include "Settings.hh" #include "Timer.hh" @@ -53,13 +52,12 @@ class Engine::Impl { 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 @@ -67,13 +65,22 @@ public: #endif { const char* error = SDL_GetError(); - throw Exception(ErrorCode::SDL_INIT, error); + 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) { const char* error = FE_GetError(); - throw Exception(ErrorCode::FASTEVENTS_INIT, error); + mError.init(Error::FASTEVENTS_INIT, error); + return; // fatal } mAlDevice = alcOpenDevice(0); @@ -81,33 +88,36 @@ public: if (!mAlDevice || !mAlContext) { const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice)); - logError("error while creating audio context: %s", error); + mError.init(Error::OPENAL_INIT, error); } else { alcMakeContextCurrent(mAlContext); - logDebug("opened sound device \"%s\"", - alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)); + 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)) setSeed(randomSeed); - else setSeed(); + 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); - 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() @@ -133,83 +143,59 @@ public: 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++; - - 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 += 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()); + + mDispatch.dispatch("engine.stopping"); } void dispatchEvents() @@ -244,7 +230,7 @@ public: { for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - (*mStackIt)->update(mInterface, t, dt); + (*mStackIt)->update(t, dt); } } @@ -254,7 +240,7 @@ public: std::list::reverse_iterator it; for (it = mStack.rbegin(); it != mStack.rend(); ++it) { - (*it)->draw(mInterface, alpha); + (*it)->draw(alpha); } } @@ -262,7 +248,7 @@ public: { for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - if ((*mStackIt)->handleEvent(mInterface, event)) break; + if ((*mStackIt)->handleEvent(event)) break; } } @@ -271,8 +257,9 @@ public: { ASSERT(layer && "cannot push null layer"); mStack.push_front(layer); - logDebug("stack: %d [pushed %X]", mStack.size(), layer.get()); - layer->pushed(mInterface); + logInfo << "stack: " << mStack.size() + << " [pushed " << layer.get() << "]" << std::endl; + layer->pushedOntoEngine(); } LayerP pop() @@ -282,8 +269,9 @@ public: LayerP layer = mStack.front(); mStack.pop_front(); - logDebug("stack: %d [popped %X]", mStack.size(), layer.get()); - layer->popped(mInterface); + logInfo << "stack: " << mStack.size() + << " [popped " << layer.get() << "]" << std::endl; + layer->poppedFromEngine(); if (fixIt) mStackIt = --mStack.begin(); @@ -310,8 +298,9 @@ public: for (it = layers.begin(); it != layers.end(); ++it) { - (*it)->popped(mInterface); - logDebug("stack: %d [popped %X]", mStack.size(), (*it).get()); + (*it)->poppedFromEngine(); + logInfo << "stack: " << mStack.size() + << " [popped " << (*it).get() << "]" << std::endl; } if (fixIt) mStackIt = --mStack.begin(); @@ -327,21 +316,23 @@ public: { mStack.clear(); mStackIt = mStack.begin(); - logDebug("stack: 0 [cleared]"); + logInfo("stack: 0 [cleared]"); } void capFps() { - if (mMaxFps < mTimestep) - { - logWarning("capping maximum fps to timestep (%f)", mTimestep); - mMaxFps = mTimestep; - } + //if (mFramerate < mTimestep) + //{ + //logWarning << "capping maximum fps to timestep (" + //<< mTimestep << ")" << std::endl; + //mFramerate = mTimestep; + //} } - Engine& mInterface; + Error mError; + VideoP mVideo; Dispatch mDispatch; @@ -352,57 +343,46 @@ public: std::list::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; } @@ -468,6 +448,9 @@ void Engine::dispatch(const std::string& event, } +Engine engine; + + } // namespace Mf /** vim: set ts=4 sw=4 tw=80: *************************************************/