X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=50711c744c9758cd1a15e965b6166582a5ec35b9;hp=16da8d40620edd59199a02e46ef43302113f8984;hb=a295f8def17036c8071b56e181364f99a377cae7;hpb=a4debfe4a5f5d339410788971b698ba00cb7f09c diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 16da8d4..50711c7 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,22 +27,22 @@ *******************************************************************************/ #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 "Log.hh" -#include "Random.hh" +#include "Math.hh" #include "Settings.hh" #include "Timer.hh" -#include "Video.hh" namespace Mf { @@ -51,57 +51,84 @@ namespace Mf { class Engine::Impl { public: - Impl(int argc, char* argv[], const std::string& name, - const std::string& iconFile, const std::string& configFile, - Engine& engine) : - interface(engine), - timestep(0.01), - printFps(false) + + Impl() : + mError(Error::NONE), + mTimestep(0.01), + mPrintFps(false) { -#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__) + // 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 { - logError("sdl is complaining: %s", SDL_GetError()); - throw Exception(Exception::SDL_ERROR); + const char* error = SDL_GetError(); + mError.init(Error::SDL_INIT, error); + //throw Exception(Error::SDL_INIT, error); } + else + { + char vdName[128]; + SDL_VideoDriverName(vdName, sizeof(vdName)); + logInfo << "initialized SDL; using video driver `" + << vdName << "'" << 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); + //throw Exception(Error::FASTEVENTS_INIT, error); + } + + mAlDevice = alcOpenDevice(0); + mAlContext = alcCreateContext(mAlDevice, 0); + if (!mAlDevice || !mAlContext) + { + const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice)); + logError << "error while creating audio context: " + << error << std::endl; + } + else + { + alcMakeContextCurrent(mAlContext); + logInfo << "opened sound device `" + << alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER) + << "'" << std::endl; } - alutInit(&argc, argv); + + // 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); - timestep = 1.0 / timeStep; + Scalar timestep = 80.0; + settings.get("timestep", timestep); + mTimestep = 1.0 / timestep; Scalar maxFps = 40.0; settings.get("maxfps", maxFps); - drawRate = 1.0 / maxFps; - - settings.get("printfps", printFps); + mMaxFps = 1.0 / maxFps; + capFps(); - video = Video::alloc(name, iconFile); - video->makeActive(); + settings.get("printfps", mPrintFps); } ~Impl() { // the video object must be destroyed before we can shutdown SDL - video.reset(); + mVideo.reset(); + + alcMakeContextCurrent(0); + alcDestroyContext(mAlContext); + alcCloseDevice(mAlDevice); - alutExit(); FE_Quit(); SDL_Quit(); } @@ -124,9 +151,9 @@ public: Scalar totalTime = 0.0; Scalar deltaTime = 0.0; - Scalar accumulator = timestep; + Scalar accumulator = mTimestep; - fps = 0; + mFps = 0; int frameAccum = 0; do @@ -135,24 +162,25 @@ 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 >= timestep) + while (accumulator >= mTimestep) { - dispatchEvents(); - update(totalTime, timestep); + update(totalTime, mTimestep); - totalTime += timestep; - accumulator -= timestep; + totalTime += mTimestep; + accumulator -= mTimestep; - nextStep += timestep; + nextStep += mTimestep; } if (ticksNow >= nextStep) { - nextStep = ticksNow + timestep; + nextStep = ticksNow + mTimestep; } if (ticksNow >= nextDraw) @@ -161,7 +189,7 @@ public: if (ticksNow >= nextFpsUpdate) // determine the actual fps { - fps = frameAccum; + mFps = frameAccum; frameAccum = 0; nextFpsUpdate += 1.0; @@ -170,28 +198,30 @@ public: nextFpsUpdate = ticksNow + 1.0; } - if (printFps) + if (mPrintFps) { - logInfo("%d fps", fps); + logInfo << mFps << " fps" << std::endl; } } - draw(accumulator / timestep); - video->swap(); + draw(accumulator / mTimestep); + mVideo->swap(); - nextDraw += drawRate; + nextDraw += mMaxFps; if (ticksNow >= nextDraw) { // we missed some scheduled draws, so reset the schedule - nextDraw = ticksNow + drawRate; + 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 (!stack.empty()); + while (!mStack.empty()); + + mDispatch.dispatch("engine.stopping"); } void dispatchEvents() @@ -207,12 +237,13 @@ public: (SDL_GetModState() & KMOD_CTRL) ) { // emergency escape - exit(0); + logWarning("escape forced"); + exit(1); } break; case SDL_VIDEORESIZE: - video->resize(event.resize.w, event.resize.h); + mVideo->resize(event.resize.w, event.resize.h); break; } @@ -223,9 +254,9 @@ public: void update(Scalar t, Scalar dt) { - for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt) + for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - (*stackIt)->update(t, dt); + (*mStackIt)->update(t, dt); } } @@ -233,7 +264,7 @@ public: { // FIXME - this will crash if the layer being drawn pops itself std::list::reverse_iterator it; - for (it = stack.rbegin(); it != stack.rend(); ++it) + for (it = mStack.rbegin(); it != mStack.rend(); ++it) { (*it)->draw(alpha); } @@ -241,9 +272,9 @@ public: void handleEvent(const Event& event) { - for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt) + for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) { - if ((*stackIt)->handleEvent(event)) break; + if ((*mStackIt)->handleEvent(event)) break; } } @@ -251,52 +282,56 @@ public: void push(LayerP layer) { ASSERT(layer && "cannot push null layer"); - stack.push_front(layer); - logInfo(" push: %d", stack.size()); - layer->pushed(interface); + mStack.push_front(layer); + logInfo << "stack: " << mStack.size() + << " [pushed " << layer.get() << "]" << std::endl; + layer->pushedOntoEngine(); } LayerP pop() { bool fixIt = false; - if (stack.begin() == stackIt) fixIt = true; + if (mStack.begin() == mStackIt) fixIt = true; - LayerP popped = stack.front(); - stack.pop_front(); - logInfo(" pop: %d", stack.size()); - popped->popped(interface); + LayerP layer = mStack.front(); + mStack.pop_front(); + logInfo << "stack: " << mStack.size() + << " [popped " << layer.get() << "]" << std::endl; + layer->poppedFromEngine(); - if (fixIt) stackIt = --stack.begin(); + 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 = stack.begin(); it != stack.end(); ++it) + for (it = mStack.begin(); it != mStack.end(); ++it) { - popped.push_back(*it); + layers.push_back(*it); - if (it == stackIt) fixIt = true; + if (it == mStackIt) fixIt = true; if ((*it).get() == layer) { ++it; - stack.erase(stack.begin(), it); + mStack.erase(mStack.begin(), it); - for (it = popped.begin(); it != popped.end(); ++it) + for (it = layers.begin(); it != layers.end(); ++it) { - (*it)->popped(interface); + (*it)->poppedFromEngine(); + logInfo << "stack: " << mStack.size() + << " [popped " << (*it).get() << "]" << std::endl; } - if (fixIt) stackIt = --stack.begin(); + if (fixIt) mStackIt = --mStack.begin(); - return popped.back(); + return layers.back(); } } @@ -305,108 +340,155 @@ public: void clear() { - stack.clear(); - stackIt = stack.begin(); - logInfo("clear: %d", stack.size()); + mStack.clear(); + mStackIt = mStack.begin(); + logInfo("stack: 0 [cleared]"); } - Engine& interface; + void capFps() + { + if (mMaxFps < mTimestep) + { + logWarning << "capping maximum fps to timestep (" + << mTimestep << ")" << std::endl; + mMaxFps = mTimestep; + } + } - VideoP video; - std::list stack; - std::list::iterator stackIt; + Error mError; - Scalar timestep; - Scalar drawRate; + VideoP mVideo; + Dispatch mDispatch; - long fps; - bool printFps; -}; + ALCdevice* mAlDevice; + ALCcontext* mAlContext; + std::list mStack; + std::list::iterator mStackIt; -static Engine* instance = 0; + Scalar mTimestep; + Scalar mMaxFps; + + int mFps; + bool mPrintFps; +}; -Engine::Engine(int argc, char* argv[], const std::string& name, - const std::string& iconFile, const std::string& configFile) : - impl_(new Engine::Impl(argc, argv, name, iconFile, configFile, *this)) -{ - instance = this; -} + +Engine::Engine() : + // pass through + mImpl(new Engine::Impl) {} -Engine& Engine::getInstance() +const Error& Engine::getError() const { - ASSERT(instance && "dereferencing null pointer"); - return *instance; - //static Engine engine; - //return engine; + // pass through + return mImpl->mError; } -void Engine::run() +void Engine::setVideo(VideoP video) { - return impl_->run(); + // pass through + mImpl->mVideo = video; } -void Engine::setTimestep(Scalar ts) +VideoP Engine::getVideo() const { - impl_->timestep = ts; + return mImpl->mVideo; } -Scalar Engine::getTimestep() const + +void Engine::setTimestep(int ts) { - return impl_->timestep; + mImpl->mTimestep = 1.0 / Scalar(ts); + mImpl->capFps(); } -void Engine::setMaxFrameRate(long maxFps) +int Engine::getTimestep() const { - impl_->drawRate = 1.0 / Scalar(maxFps); + return int(1.0 / mImpl->mTimestep); } -long Engine::getMaxFrameRate() const + +void Engine::setMaxFps(int maxFps) { - return long(1.0 / impl_->drawRate); + mImpl->mMaxFps = 1.0 / Scalar(maxFps); + mImpl->capFps(); } - -Video& Engine::getVideo() const +int Engine::getMaxFps() const { - return *impl_->video; + return int(1.0 / mImpl->mMaxFps); } -long Engine::getFrameRate() const + +int Engine::getFps() const { - return impl_->fps; + return mImpl->mFps; } void Engine::push(LayerP layer) { // pass through - impl_->push(layer); + mImpl->push(layer); } LayerP Engine::pop() { // pass through - return impl_->pop(); + return mImpl->pop(); } LayerP Engine::pop(Layer* layer) { // pass through - return impl_->pop(layer); + return mImpl->pop(layer); } void Engine::clear() { // pass through - impl_->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); +} + + +Engine engine; + + } // namespace Mf /** vim: set ts=4 sw=4 tw=80: *************************************************/