X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FEngine.cc;h=80a203e6989b368a86cb12c0790568fef0bc7b6c;hp=be8ca5fac61e184f0b54a0da244355e5b33e89a2;hb=5250c138b1a692e4e893a8f424d2856e519fd652;hpb=16d1a05b0777e97a45c48e2874aa4e5cc791282e diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index be8ca5f..80a203e 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,14 +27,15 @@ *******************************************************************************/ #include // exit -#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" @@ -44,35 +45,37 @@ namespace Mf { -class Engine::EngineImpl +class Engine::Impl { public: - EngineImpl(int argc, char* argv[], const std::string& configFile, + Impl(int argc, char* argv[], const std::string& configFile, const std::string& name, const std::string& iconFile, Engine* outer) : - interface(outer), - settings(argc, argv) + interface(outer) { +#if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__) + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) +#else if (SDL_Init(SDL_INIT_EVERYTHING | 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.parseArgs(argc, argv); settings.loadFromFile(configFile); long randomSeed; - if (settings.get("engine.rngseed", randomSeed)) - { - setSeed(randomSeed); - } - else - { - setSeed(); - } + if (settings.get("engine.rngseed", randomSeed)) setSeed(randomSeed); + else setSeed(); double ts = 0.01; settings.get("engine.timestep", ts); @@ -85,15 +88,16 @@ public: printFps = false; settings.get("video.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,7 +107,7 @@ 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() @@ -141,6 +145,10 @@ public: nextStep += timestep; } + if (ticksNow >= nextStep) + { + nextStep = ticksNow + timestep; + } if (ticksNow >= nextDraw) { @@ -159,7 +167,7 @@ public: if (printFps) { - std::cout << "FPS: " << fps << std::endl; + logInfo("framerate: %d fps", fps); } } @@ -179,7 +187,7 @@ public: } while (running); - return 0; + return exitCode; } @@ -211,11 +219,10 @@ public: Engine* interface; - Settings settings; - Dispatcher dispatcher; - VideoPtr video; + VideoP video; bool running; + int exitCode; Scalar timestep; Scalar drawRate; @@ -227,8 +234,7 @@ public: 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)) -{} + impl_(new Engine::Impl(argc, argv, configFile, name, iconFile, this)) {} Engine::~Engine() {} @@ -238,9 +244,10 @@ int Engine::run() return impl_->run(); } -void Engine::stop() +void Engine::stop(int exitCode) { impl_->running = false; + impl_->exitCode = exitCode; }