X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=fca5e15423ee1c9b639412deea3f1e380002e6e6;hp=5bfaaf3cd8d715d0927c9a284a5199a6686e98a6;hb=71bd9dbaf1c1e3c55a9f63392a73865d8aeee7d4;hpb=892da43bf5796e7c5f593a6d0f53bd797a36bd3e diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 5bfaaf3..fca5e15 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,22 +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 { @@ -51,55 +52,79 @@ 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(Engine& engine) : + mInterface(engine), + 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(); + 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) { - logError("fast events error: %s", FE_GetError()); - throw Exception(Exception::SDL_ERROR); + const char* error = FE_GetError(); + throw Exception(ErrorCode::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: %s", error); + } + else + { + alcMakeContextCurrent(mAlContext); + logDebug("opened sound device `%s'", + alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER)); } - 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); + mTimestep = 1.0 / timestep; - long maxFps = 40; + Scalar maxFps = 40.0; settings.get("maxfps", maxFps); - drawRate = 1.0 / Scalar(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(); } @@ -122,9 +147,9 @@ public: Scalar totalTime = 0.0; Scalar deltaTime = 0.0; - Scalar accumulator = timestep; + Scalar accumulator = mTimestep; - fps = 0; + mFps = 0; int frameAccum = 0; do @@ -133,24 +158,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) @@ -159,7 +185,7 @@ public: if (ticksNow >= nextFpsUpdate) // determine the actual fps { - fps = frameAccum; + mFps = frameAccum; frameAccum = 0; nextFpsUpdate += 1.0; @@ -168,28 +194,30 @@ public: nextFpsUpdate = ticksNow + 1.0; } - if (printFps) + if (mPrintFps) { - logInfo("%d fps", fps); + logInfo("%d fps", mFps); } } - 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() @@ -205,12 +233,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; } @@ -221,9 +250,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(mInterface, t, dt); } } @@ -231,164 +260,219 @@ 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); + (*it)->draw(mInterface, alpha); } } 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(mInterface, event)) break; } } - void pushLayer(LayerP layer) + void push(LayerP layer) { ASSERT(layer && "cannot push null layer"); - stack.push_front(layer); - layer->pushed(interface); + mStack.push_front(layer); + logDebug("stack: %d [pushed %X]", mStack.size(), layer.get()); + layer->pushed(mInterface); } - void popLayer() + LayerP pop() { bool fixIt = false; - if (stack.begin() == stackIt) fixIt = true; + if (mStack.begin() == mStackIt) fixIt = true; + + LayerP layer = mStack.front(); + mStack.pop_front(); + logDebug("stack: %d [popped %X]", mStack.size(), layer.get()); + layer->popped(mInterface); - LayerP popped = stack.front(); - stack.pop_front(); - popped->popped(interface); + if (fixIt) mStackIt = --mStack.begin(); - if (fixIt) stackIt = --stack.begin(); + return layer; } - void popLayer(Layer* layer) + LayerP pop(Layer* layer) { bool fixIt = false; + std::list layers; + std::list::iterator it; - for (it = stack.begin(); it != stack.end(); ++it) + for (it = mStack.begin(); it != mStack.end(); ++it) { - if (it == stackIt) fixIt = true; + layers.push_back(*it); + + if (it == mStackIt) fixIt = true; if ((*it).get() == layer) { ++it; - do + mStack.erase(mStack.begin(), it); + + for (it = layers.begin(); it != layers.end(); ++it) { - LayerP popped = stack.front(); - stack.pop_front(); - popped->popped(interface); + (*it)->popped(mInterface); + logDebug("stack: %d [popped %X]", mStack.size(), (*it).get()); } - while (stack.begin() != it); - if (fixIt) stackIt = --stack.begin(); - return; + if (fixIt) mStackIt = --mStack.begin(); + + return layers.back(); } } + + return LayerP(); } - void clearLayers() + void clear() { - stack.clear(); - stackIt = stack.begin(); + mStack.clear(); + mStackIt = mStack.begin(); + logDebug("stack: 0 [cleared]"); + } + + + void capFps() + { + if (mMaxFps < mTimestep) + { + logWarning("capping maximum fps to timestep (%f)", mTimestep); + mMaxFps = mTimestep; + } } - Engine& interface; + Engine& mInterface; + VideoP mVideo; + Dispatch mDispatch; - VideoP video; + ALCdevice* mAlDevice; + ALCcontext* mAlContext; - std::list stack; - std::list::iterator stackIt; + std::list mStack; + std::list::iterator mStackIt; - Scalar timestep; - Scalar drawRate; + Scalar mTimestep; + Scalar mMaxFps; - long fps; - bool printFps; + int mFps; + bool mPrintFps; }; -static Engine* instance = 0; +Engine::Engine() : + // pass through + mImpl(new Engine::Impl(*this)) {} -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)) +Engine& Engine::getInstance() { - instance = this; + static Engine engine; + return engine; } -Engine& Engine::getInstance() +void Engine::setVideo(VideoP video) { - ASSERT(instance && "dereferencing null pointer"); - return *instance; + // pass through + mImpl->mVideo = video; } - -void Engine::run() +VideoP Engine::getVideo() const { - return impl_->run(); + return mImpl->mVideo; } -void Engine::setTimestep(Scalar ts) + +void Engine::setTimestep(int ts) { - impl_->timestep = ts; + mImpl->mTimestep = 1.0 / Scalar(ts); + mImpl->capFps(); } -Scalar Engine::getTimestep() const +int Engine::getTimestep() const { - return impl_->timestep; + return int(1.0 / mImpl->mTimestep); } -void Engine::setMaxFrameRate(long maxFps) + +void Engine::setMaxFps(int maxFps) { - impl_->drawRate = 1.0 / Scalar(maxFps); + mImpl->mMaxFps = 1.0 / Scalar(maxFps); + mImpl->capFps(); } -long Engine::getMaxFrameRate() const +int Engine::getMaxFps() const { - return long(1.0 / impl_->drawRate); + return int(1.0 / mImpl->mMaxFps); } -Video& Engine::getVideo() const +int Engine::getFps() const { - return *impl_->video; + return mImpl->mFps; } -long Engine::getFrameRate() const + +void Engine::push(LayerP layer) { - return impl_->fps; + // pass through + mImpl->push(layer); } - -void Engine::pushLayer(LayerP layer) +LayerP Engine::pop() { // pass through - impl_->pushLayer(layer); + return mImpl->pop(); } -void Engine::popLayer() +LayerP Engine::pop(Layer* layer) { // pass through - impl_->popLayer(); + return mImpl->pop(layer); } -void Engine::popLayer(Layer* layer) +void Engine::clear() { // pass through - impl_->popLayer(layer); + mImpl->clear(); +} + +int Engine::getSize() const +{ + return mImpl->mStack.size(); } -void Engine::clearLayers() + +void Engine::run() { // pass through - impl_->clearLayers(); + 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); }