X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=f4bc712b27887d0b5397c579fc861d196ffc3bc6;hp=18ce5d7e5f8c6d0b4664da080b16ba1ad2623ace;hb=e495074443d9fd7bc16137084cf9de3d031b75c4;hpb=ca0f7bdfba63140dca0bd20586d31980f3938eb2 diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 18ce5d7..f4bc712 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -26,20 +26,23 @@ *******************************************************************************/ +#include #include // exit +#include #include #include #include "fastevents.h" #include -#include "Dispatcher.hh" #include "Engine.hh" +#include "Event.hh" +#include "Exception.hh" #include "Log.hh" +#include "Math.hh" #include "Random.hh" #include "Settings.hh" #include "Timer.hh" -#include "Video.hh" namespace Mf { @@ -48,53 +51,56 @@ namespace Mf { class Engine::Impl { public: - Impl(int argc, char* argv[], const std::string& configFile, - const std::string& name, const std::string& iconFile, - Engine* outer) : - interface(outer), - 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); } 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); } - alutInit(&argc, argv); + int argc = 1; + char name[] = "hello"; + alutInit(&argc, (char**)&name); + + // now load the settings the engine needs Settings& settings = Settings::getInstance(); - settings.loadFromFile(configFile); - settings.parseArgs(argc, argv); - long randomSeed; + unsigned randomSeed; if (settings.get("rngseed", randomSeed)) setSeed(randomSeed); else setSeed(); + 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(); alutExit(); FE_Quit(); @@ -109,7 +115,7 @@ public: * the exit code used to stop the loop. */ - int run() + void run() { Scalar ticksNow = Timer::getTicks(); @@ -119,36 +125,36 @@ public: Scalar totalTime = 0.0; Scalar deltaTime = 0.0; - Scalar accumulator = timestep; + Scalar accumulator = mTimestep; - fps = 0; + mFps = 0; int frameAccum = 0; - running = true; 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); + dispatchEvents(); - while (accumulator >= timestep) + while (accumulator >= mTimestep) { - dispatchEvents(); - interface->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) @@ -157,7 +163,7 @@ public: if (ticksNow >= nextFpsUpdate) // determine the actual fps { - fps = frameAccum; + mFps = frameAccum; frameAccum = 0; nextFpsUpdate += 1.0; @@ -166,33 +172,30 @@ public: nextFpsUpdate = ticksNow + 1.0; } - if (printFps) + if (mPrintFps) { - logInfo("%d fps", fps); + logInfo("%d fps", mFps); } } - interface->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 (running); - - return exitCode; + while (!mStack.empty()); } - void dispatchEvents() { SDL_Event event; @@ -205,89 +208,245 @@ public: if (event.key.keysym.sym == SDLK_ESCAPE && (SDL_GetModState() & KMOD_CTRL) ) { - exit(0); + // emergency escape + 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; } - interface->handleEvent(event); + handleEvent(event); + } + } + + + void update(Scalar t, Scalar dt) + { + for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) + { + (*mStackIt)->update(mInterface, t, dt); + } + } + + void draw(Scalar alpha) + { + // FIXME - this will crash if the layer being drawn pops itself + std::list::reverse_iterator it; + for (it = mStack.rbegin(); it != mStack.rend(); ++it) + { + (*it)->draw(mInterface, alpha); + } + } + + void handleEvent(const Event& event) + { + for (mStackIt = mStack.begin(); mStackIt != mStack.end(); ++mStackIt) + { + if ((*mStackIt)->handleEvent(mInterface, event)) break; + } + } + + + void push(LayerP layer) + { + ASSERT(layer && "cannot push null layer"); + mStack.push_front(layer); + logDebug("stack: %d [pushed %X]", mStack.size(), layer.get()); + layer->pushed(mInterface); + } + + LayerP pop() + { + bool fixIt = false; + 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); + + if (fixIt) mStackIt = --mStack.begin(); + + return layer; + } + + LayerP pop(Layer* layer) + { + bool fixIt = false; + + std::list layers; + + std::list::iterator it; + for (it = mStack.begin(); it != mStack.end(); ++it) + { + layers.push_back(*it); + + if (it == mStackIt) fixIt = true; + + if ((*it).get() == layer) + { + ++it; + mStack.erase(mStack.begin(), 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 layers.back(); + } } + + return LayerP(); } + void clear() + { + mStack.clear(); + mStackIt = mStack.begin(); + logDebug("stack: 0 [cleared]"); + } - Engine* interface; - VideoP video; + void capFps() + { + if (mMaxFps < mTimestep) + { + logWarning("capping maximum fps to timestep (%f)", mTimestep); + mMaxFps = mTimestep; + } + } + + + Engine& mInterface; + VideoP mVideo; + Dispatch mDispatch; - bool running; - int exitCode; + std::list mStack; + std::list::iterator mStackIt; - Scalar timestep; - Scalar drawRate; + Scalar mTimestep; + Scalar mMaxFps; - long fps; - bool printFps; + int mFps; + bool mPrintFps; }; -Engine::Engine(int argc, char* argv[], const std::string& configFile, - const std::string& name, const std::string& iconFile) : - impl_(new Engine::Impl(argc, argv, configFile, name, iconFile, this)) {} +Engine::Engine() : + // pass through + mImpl(new Engine::Impl(*this)) {} + +Engine& Engine::getInstance() +{ + static Engine engine; + return engine; +} + + +void Engine::setVideo(VideoP video) +{ + // pass through + mImpl->mVideo = video; +} + +VideoP Engine::getVideo() const +{ + return mImpl->mVideo; +} + -Engine::~Engine() {} +void Engine::setTimestep(int ts) +{ + mImpl->mTimestep = 1.0 / Scalar(ts); + mImpl->capFps(); +} + +int Engine::getTimestep() const +{ + return int(1.0 / mImpl->mTimestep); +} -int Engine::run() +void Engine::setMaxFps(int maxFps) { - return impl_->run(); + mImpl->mMaxFps = 1.0 / Scalar(maxFps); + mImpl->capFps(); } -void Engine::stop(int exitCode) +int Engine::getMaxFps() const { - impl_->running = false; - impl_->exitCode = exitCode; + return int(1.0 / mImpl->mMaxFps); } -void Engine::setTimestep(Scalar ts) +int Engine::getFps() const { - impl_->timestep = ts; + return mImpl->mFps; } -Scalar Engine::getTimestep() + +void Engine::push(LayerP layer) { - return impl_->timestep; + // pass through + mImpl->push(layer); } -void Engine::setMaxFrameRate(long maxFps) +LayerP Engine::pop() { - impl_->drawRate = 1.0 / Scalar(maxFps); + // pass through + return mImpl->pop(); } -long Engine::getMaxFrameRate() +LayerP Engine::pop(Layer* layer) { - return long(1.0 / impl_->drawRate); + // pass through + return mImpl->pop(layer); +} + +void Engine::clear() +{ + // pass through + mImpl->clear(); +} + +int Engine::getSize() const +{ + return mImpl->mStack.size(); } -Video& Engine::getVideo() +void Engine::run() { - return *impl_->video; + // pass through + return mImpl->run(); } -long Engine::getFrameRate() + +Dispatch::Handler Engine::addHandler(const std::string& event, + const Dispatch::Function& callback) { - return impl_->fps; + 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::update(Scalar t, Scalar dt) {} -void Engine::draw(Scalar alpha) {} -void Engine::handleEvent(const Event& event) {} +void Engine::dispatch(const std::string& event, + const Dispatch::Message* message) +{ + mImpl->mDispatch.dispatch(event, message); +} } // namespace Mf