X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=5bfaaf3cd8d715d0927c9a284a5199a6686e98a6;hp=be8ca5fac61e184f0b54a0da244355e5b33e89a2;hb=892da43bf5796e7c5f593a6d0f53bd797a36bd3e;hpb=16d1a05b0777e97a45c48e2874aa4e5cc791282e diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index be8ca5f..5bfaaf3 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -26,15 +26,19 @@ *******************************************************************************/ +#include #include // exit -#include +#include #include #include #include "fastevents.h" +#include #include "Dispatcher.hh" #include "Engine.hh" +#include "Event.hh" +#include "Log.hh" #include "Random.hh" #include "Settings.hh" #include "Timer.hh" @@ -44,56 +48,58 @@ namespace Mf { -class Engine::EngineImpl +class Engine::Impl { public: - EngineImpl(int argc, char* argv[], const std::string& configFile, - const std::string& name, const std::string& iconFile, - Engine* outer) : - interface(outer), - settings(argc, argv) + 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) { - if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0) +#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) +#else + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) != 0) +#endif { - throw Exception(SDL_GetError()); + logError("sdl is complaining: %s", SDL_GetError()); + throw Exception(Exception::SDL_ERROR); } if (FE_Init() != 0) { - throw Exception(FE_GetError()); + logError("fast events error: %s", FE_GetError()); + throw Exception(Exception::SDL_ERROR); } + alutInit(&argc, argv); + Settings& settings = Settings::getInstance(); settings.loadFromFile(configFile); + settings.parseArgs(argc, argv); long randomSeed; - if (settings.get("engine.rngseed", randomSeed)) - { - setSeed(randomSeed); - } - else - { - setSeed(); - } + if (settings.get("rngseed", randomSeed)) setSeed(randomSeed); + else setSeed(); - double ts = 0.01; - settings.get("engine.timestep", ts); - timestep = Scalar(ts); + settings.get("timestep", timestep); long maxFps = 40; - settings.getNumber("video.maxfps", maxFps); + settings.get("maxfps", maxFps); drawRate = 1.0 / Scalar(maxFps); - printFps = false; - settings.get("video.printfps", printFps); + settings.get("printfps", printFps); - video = VideoPtr(new Video(name, iconFile)); + video = Video::alloc(name, iconFile); video->makeActive(); } - ~EngineImpl() + ~Impl() { // the video object must be destroyed before we can shutdown SDL video.reset(); + alutExit(); FE_Quit(); SDL_Quit(); } @@ -103,12 +109,12 @@ public: * The main loop. This just calls dispatchEvents(), update(), and draw() * over and over again. The timing of the update and draw are decoupled. * The actual frame rate is also calculated here. This function will return - * with a value of 0 if the member variable running becomes true. + * the exit code used to stop the loop. */ - int run() + void run() { - Scalar ticksNow = getTicks(); + Scalar ticksNow = Timer::getTicks(); Scalar nextStep = ticksNow; Scalar nextDraw = ticksNow; @@ -121,26 +127,31 @@ public: fps = 0; int frameAccum = 0; - running = true; do { - Scalar newTicks = getTicks(); + Scalar newTicks = Timer::getTicks(); deltaTime = newTicks - ticksNow; ticksNow = newTicks; if (deltaTime >= 0.25) deltaTime = 0.25; accumulator += deltaTime; + Timer::fireIfExpired(ticksNow); + while (accumulator >= timestep) { dispatchEvents(); - interface->update(totalTime, timestep); + update(totalTime, timestep); totalTime += timestep; accumulator -= timestep; nextStep += timestep; } + if (ticksNow >= nextStep) + { + nextStep = ticksNow + timestep; + } if (ticksNow >= nextDraw) { @@ -159,11 +170,11 @@ public: if (printFps) { - std::cout << "FPS: " << fps << std::endl; + logInfo("%d fps", fps); } } - interface->draw(accumulator / timestep); + draw(accumulator / timestep); video->swap(); nextDraw += drawRate; @@ -175,14 +186,12 @@ public: } // be a good citizen and give back what you don't need - sleep(std::min(nextStep, nextDraw), true); + Timer::sleep(std::min(std::min(nextStep, nextDraw), + Timer::getNextFire()), true); } - while (running); - - return 0; + while (!stack.empty()); } - void dispatchEvents() { SDL_Event event; @@ -195,7 +204,8 @@ public: if (event.key.keysym.sym == SDLK_ESCAPE && (SDL_GetModState() & KMOD_CTRL) ) { - exit(0); + // emergency escape + exit(0); } break; @@ -204,52 +214,133 @@ public: break; } - interface->handleEvent(event); + handleEvent(event); } } - Engine* interface; + void update(Scalar t, Scalar dt) + { + for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt) + { + (*stackIt)->update(t, dt); + } + } - Settings settings; - Dispatcher dispatcher; - VideoPtr video; + void draw(Scalar alpha) + { + // FIXME - this will crash if the layer being drawn pops itself + std::list::reverse_iterator it; + for (it = stack.rbegin(); it != stack.rend(); ++it) + { + (*it)->draw(alpha); + } + } - bool running; + void handleEvent(const Event& event) + { + for (stackIt = stack.begin(); stackIt != stack.end(); ++stackIt) + { + if ((*stackIt)->handleEvent(event)) break; + } + } - Scalar timestep; - Scalar drawRate; - long fps; - bool printFps; -}; + void pushLayer(LayerP layer) + { + ASSERT(layer && "cannot push null layer"); + stack.push_front(layer); + layer->pushed(interface); + } + + void popLayer() + { + bool fixIt = false; + if (stack.begin() == stackIt) fixIt = true; + + LayerP popped = stack.front(); + stack.pop_front(); + popped->popped(interface); + + if (fixIt) stackIt = --stack.begin(); + } + + void popLayer(Layer* layer) + { + bool fixIt = false; + + std::list::iterator it; + for (it = stack.begin(); it != stack.end(); ++it) + { + if (it == stackIt) fixIt = true; + if ((*it).get() == layer) + { + ++it; + do + { + LayerP popped = stack.front(); + stack.pop_front(); + popped->popped(interface); + } + while (stack.begin() != it); -Engine::Engine(int argc, char* argv[], const std::string& configFile, - const std::string& name, const std::string& iconFile) : - impl_(new Engine::EngineImpl(argc, argv, configFile, name, iconFile, this)) -{} + if (fixIt) stackIt = --stack.begin(); + return; + } + } + } + + void clearLayers() + { + stack.clear(); + stackIt = stack.begin(); + } -Engine::~Engine() {} + Engine& interface; -int Engine::run() + VideoP video; + + std::list stack; + std::list::iterator stackIt; + + Scalar timestep; + Scalar drawRate; + + long fps; + bool printFps; +}; + + +static Engine* instance = 0; + +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)) { - return impl_->run(); + instance = this; } -void Engine::stop() + +Engine& Engine::getInstance() { - impl_->running = false; + ASSERT(instance && "dereferencing null pointer"); + return *instance; } +void Engine::run() +{ + return impl_->run(); +} + void Engine::setTimestep(Scalar ts) { impl_->timestep = ts; } -Scalar Engine::getTimestep() +Scalar Engine::getTimestep() const { return impl_->timestep; } @@ -259,26 +350,46 @@ void Engine::setMaxFrameRate(long maxFps) impl_->drawRate = 1.0 / Scalar(maxFps); } -long Engine::getMaxFrameRate() +long Engine::getMaxFrameRate() const { return long(1.0 / impl_->drawRate); } -Video& Engine::getVideo() +Video& Engine::getVideo() const { return *impl_->video; } -long Engine::getFrameRate() +long Engine::getFrameRate() const { return impl_->fps; } -void Engine::update(Scalar t, Scalar dt) {} -void Engine::draw(Scalar alpha) {} -void Engine::handleEvent(const Event& event) {} +void Engine::pushLayer(LayerP layer) +{ + // pass through + impl_->pushLayer(layer); +} + +void Engine::popLayer() +{ + // pass through + impl_->popLayer(); +} + +void Engine::popLayer(Layer* layer) +{ + // pass through + impl_->popLayer(layer); +} + +void Engine::clearLayers() +{ + // pass through + impl_->clearLayers(); +} } // namespace Mf