X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=5d703da15d745110a4caa63d938c6fc7890bcde3;hp=f4bc712b27887d0b5397c579fc861d196ffc3bc6;hb=4f62ce947db282f0bbf4d49b3aafb83d7cf51adc;hpb=e495074443d9fd7bc16137084cf9de3d031b75c4 diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index f4bc712..5d703da 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,20 +27,20 @@ *******************************************************************************/ #include -#include // exit +#include // exit, srand +#include // time #include #include +#include #include #include "fastevents.h" -#include + #include "Engine.hh" #include "Event.hh" -#include "Exception.hh" #include "Log.hh" #include "Math.hh" -#include "Random.hh" #include "Settings.hh" #include "Timer.hh" @@ -52,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 @@ -66,35 +65,59 @@ 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 } - int argc = 1; - char name[] = "hello"; - alutInit(&argc, (char**)&name); - - // now load the settings the engine needs - Settings& settings = Settings::getInstance(); + 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; + } + } + 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() @@ -102,7 +125,10 @@ public: // the video object must be destroyed before we can shutdown SDL mVideo.reset(); - alutExit(); + alcMakeContextCurrent(0); + alcDestroyContext(mAlContext); + alcCloseDevice(mAlDevice); + FE_Quit(); SDL_Quit(); } @@ -117,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++; + ++frames; + draw((ticks + mTimestep - nextUpdate) * inverseTimestep); + mVideo->swap(); - if (ticksNow >= nextFpsUpdate) // determine the actual fps - { - mFps = frameAccum; - frameAccum = 0; + nextDraw += mFramerate; - nextFpsUpdate += 1.0; - if (ticksNow >= nextFpsUpdate) - { - nextFpsUpdate = ticksNow + 1.0; - } - - 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() @@ -228,7 +230,7 @@ public: { for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - (*mStackIt)->update(mInterface, t, dt); + (*mStackIt)->update(t, dt); } } @@ -238,7 +240,7 @@ public: std::list::reverse_iterator it; for (it = mStack.rbegin(); it != mStack.rend(); ++it) { - (*it)->draw(mInterface, alpha); + (*it)->draw(alpha); } } @@ -246,7 +248,7 @@ public: { for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - if ((*mStackIt)->handleEvent(mInterface, event)) break; + if ((*mStackIt)->handleEvent(event)) break; } } @@ -255,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() @@ -266,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(); @@ -294,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(); @@ -311,79 +316,73 @@ 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; + ALCdevice* mAlDevice; + ALCcontext* mAlContext; + std::list mStack; 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 -{ - return mImpl->mVideo; -} - - -void Engine::setTimestep(int ts) +const Error& Engine::getError() const { - 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; } @@ -449,6 +448,9 @@ void Engine::dispatch(const std::string& event, } +Engine engine; + + } // namespace Mf /** vim: set ts=4 sw=4 tw=80: *************************************************/