X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=785cb4b41ed67ee342cf8df535f9e6726e5d3a35;hp=9ff5e1a8f1158188a4e58832a8a36a02c78ebcfb;hb=f0aed8dbdbdd61ac9d0728058ba5eb9693b4b94c;hpb=8a1acac01b444dccf8b57cedf08392ada2e473c1 diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 9ff5e1a..785cb4b 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,23 +27,23 @@ *******************************************************************************/ #include -#include // exit +#include // exit, srand +#include // time #include #include +#include #include #include "fastevents.h" -#include -#include "Dispatcher.hh" + #include "Engine.hh" #include "Event.hh" #include "Exception.hh" #include "Log.hh" -#include "Random.hh" +#include "Math.hh" #include "Settings.hh" #include "Timer.hh" -#include "Video.hh" namespace Mf { @@ -53,38 +53,56 @@ class Engine::Impl { public: - Impl(int argc, char* argv[], const std::string& name, - const std::string& iconFile, const std::string& configFile, - Engine& engine) : + Impl(Engine& engine) : mInterface(engine), mTimestep(0.01), mPrintFps(false) { -#if defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__) + // 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 { const char* error = SDL_GetError(); - logError("sdl is complaining: %s", error); throw Exception(ErrorCode::SDL_INIT, error); } + else + { + char vdName[128]; + SDL_VideoDriverName(vdName, sizeof(vdName)); + logDebug("initialized SDL; using video driver `%s'", vdName); + } + if (FE_Init() != 0) { const char* error = FE_GetError(); - logError("fast events error: %s", error); throw Exception(ErrorCode::FASTEVENTS_INIT, error); } - alutInit(&argc, argv); + + mAlDevice = alcOpenDevice(0); + mAlContext = alcCreateContext(mAlDevice, 0); + if (!mAlDevice || !mAlContext) + { + const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice)); + logError("error while creating audio context: %s", error); + } + else + { + alcMakeContextCurrent(mAlContext); + logDebug("opened sound device `%s'", + alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)); + } + + // 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); @@ -92,12 +110,10 @@ public: 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() @@ -105,7 +121,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(); } @@ -139,14 +158,15 @@ public: 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; @@ -183,17 +203,17 @@ public: 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()); } @@ -211,7 +231,8 @@ public: (SDL_GetModState() & KMOD_CTRL) ) { // emergency escape - exit(0); + logWarning("escape forced"); + exit(1); } break; @@ -229,7 +250,7 @@ public: { for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - (*mStackIt)->update(t, dt); + (*mStackIt)->update(mInterface, t, dt); } } @@ -239,7 +260,7 @@ public: std::list::reverse_iterator it; for (it = mStack.rbegin(); it != mStack.rend(); ++it) { - (*it)->draw(alpha); + (*it)->draw(mInterface, alpha); } } @@ -247,7 +268,7 @@ public: { for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - if ((*mStackIt)->handleEvent(event)) break; + if ((*mStackIt)->handleEvent(mInterface, event)) break; } } @@ -256,7 +277,7 @@ public: { ASSERT(layer && "cannot push null layer"); mStack.push_front(layer); - logInfo(" push: %d", mStack.size()); + logDebug("stack: %d [pushed %X]", mStack.size(), layer.get()); layer->pushed(mInterface); } @@ -265,26 +286,26 @@ public: 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); + logDebug("stack: %d [popped %X]", mStack.size(), layer.get()); + layer->popped(mInterface); if (fixIt) mStackIt = --mStack.begin(); - return popped; + return layer; } LayerP pop(Layer* layer) { bool fixIt = false; - std::list popped; + std::list layers; std::list::iterator it; for (it = mStack.begin(); it != mStack.end(); ++it) { - popped.push_back(*it); + layers.push_back(*it); if (it == mStackIt) fixIt = true; @@ -293,14 +314,15 @@ public: ++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); + logDebug("stack: %d [popped %X]", mStack.size(), (*it).get()); } if (fixIt) mStackIt = --mStack.begin(); - return popped.back(); + return layers.back(); } } @@ -311,77 +333,86 @@ public: { mStack.clear(); mStackIt = mStack.begin(); - logInfo("clear: %d", mStack.size()); + logDebug("stack: 0 [cleared]"); } - Engine& mInterface; + void capFps() + { + if (mMaxFps < mTimestep) + { + logWarning("capping maximum fps to timestep (%f)", mTimestep); + mMaxFps = mTimestep; + } + } - VideoP mVideo; + + Engine& mInterface; + VideoP mVideo; + Dispatch mDispatch; + + ALCdevice* mAlDevice; + ALCcontext* mAlContext; std::list mStack; std::list::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(*this)) {} Engine& Engine::getInstance() { - ASSERT(instance && "dereferencing null pointer"); - return *instance; - // TODO this has not been completely thought out - //static Engine engine; - //return engine; + static Engine engine; + return engine; } -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; } @@ -411,6 +442,37 @@ void Engine::clear() 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); +} + } // namespace Mf