X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=e97c8b5d1b7edbae52a5b48ebc1072574e697ebe;hp=bcc336c1244f1f8b0a8ed041507c4b5f3e0f7834;hb=33842c860fe18ca8cf087905992885687434320c;hpb=c2321281bf12a7efaedde930422c7ddbc92080d4 diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index bcc336c..e97c8b5 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,15 +27,15 @@ *******************************************************************************/ #include // exit -#include -#include #include #include #include "fastevents.h" +#include #include "Dispatcher.hh" #include "Engine.hh" +#include "Log.hh" #include "Random.hh" #include "Settings.hh" #include "Timer.hh" @@ -45,29 +45,37 @@ namespace Mf { -class Engine::EngineImpl +class Engine::Impl { public: - EngineImpl(const std::string& name, int argc, char* argv[], - const std::string& configFile, Engine* outer) : - settings(argc, argv), + Impl(int argc, char* argv[], const std::string& configFile, + const std::string& name, const std::string& iconFile, + Engine* outer) : interface(outer) { - 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 std::runtime_error(SDL_GetError()); + logError("sdl is complaining: %s", SDL_GetError()); + throw Exception(Exception::SDL_ERROR); } if (FE_Init() != 0) { - throw std::runtime_error(FE_GetError()); + logError("fast events error: %s", FE_GetError()); + throw Exception(Exception::SDL_ERROR); } + alutInit(&argc, argv); - setSeed(); - + Settings& settings = Settings::getInstance(); + settings.parseArgs(argc, argv); settings.loadFromFile(configFile); - video = VideoPtr(new Video(name)); - video->makeActive(); + long randomSeed; + if (settings.get("engine.rngseed", randomSeed)) setSeed(randomSeed); + else setSeed(); double ts = 0.01; settings.get("engine.timestep", ts); @@ -79,13 +87,17 @@ public: printFps = false; settings.get("video.printfps", printFps); + + video = Video::alloc(name, iconFile); + video->makeActive(); } - ~EngineImpl() + ~Impl() { - // The video object must be destroyed before we can shutdown SDL. + // the video object must be destroyed before we can shutdown SDL video.reset(); + alutExit(); FE_Quit(); SDL_Quit(); } @@ -95,12 +107,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() { - Scalar ticksNow = getTicks(); + Scalar ticksNow = Timer::getTicks(); Scalar nextStep = ticksNow; Scalar nextDraw = ticksNow; @@ -116,13 +128,15 @@ public: 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(); @@ -133,6 +147,10 @@ public: nextStep += timestep; } + if (ticksNow >= nextStep) + { + nextStep = ticksNow + timestep; + } if (ticksNow >= nextDraw) { @@ -151,7 +169,7 @@ public: if (printFps) { - std::cout << "FPS: " << fps << std::endl; + logInfo("framerate: %d fps", fps); } } @@ -167,11 +185,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; + return exitCode; } @@ -201,25 +220,24 @@ public: } - Settings settings; - Dispatcher dispatcher; - VideoPtr video; + Engine* interface; + + VideoP video; bool running; + int exitCode; Scalar timestep; Scalar drawRate; long fps; bool printFps; - - Engine* interface; }; -Engine::Engine(const std::string& name, int argc, char* argv[], - const std::string& configFile) : - impl_(new Engine::EngineImpl(name, argc, argv, configFile, this)) {} +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() {} @@ -229,9 +247,10 @@ int Engine::run() return impl_->run(); } -void Engine::stop() +void Engine::stop(int exitCode) { impl_->running = false; + impl_->exitCode = exitCode; }