]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Engine.cc
settings subsystem now using lua
[chaz/yoink] / src / Moof / Engine.cc
index 53fca73e9e883cfab1d18aced70cd125391f26d1..18ce5d7e5f8c6d0b4664da080b16ba1ad2623ace 100644 (file)
 *******************************************************************************/
 
 #include <cstdlib>                     // exit
-#include <iostream>
 #include <string>
 
 #include <SDL/SDL.h>
 #include "fastevents.h"
+#include <AL/alut.h>
 
 #include "Dispatcher.hh"
 #include "Engine.hh"
+#include "Log.hh"
 #include "Random.hh"
 #include "Settings.hh"
 #include "Timer.hh"
 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)
+               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();
        }
@@ -108,7 +111,7 @@ public:
 
        int run()
        {
-               Scalar ticksNow = getTicks();
+               Scalar ticksNow = Timer::getTicks();
 
                Scalar nextStep = ticksNow;
                Scalar nextDraw = ticksNow;
@@ -124,13 +127,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();
@@ -163,7 +168,7 @@ public:
 
                                        if (printFps)
                                        {
-                                               std::cout << "FPS: " << fps << std::endl;
+                                               logInfo("%d fps", fps);
                                        }
                                }
 
@@ -179,7 +184,8 @@ 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);
 
@@ -215,9 +221,7 @@ public:
 
        Engine*         interface;
 
-       Settings        settings;
-       Dispatcher      dispatcher;
-       VideoPtr        video;
+       VideoP          video;
 
        bool            running;
        int                     exitCode;
@@ -232,8 +236,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() {}
 
This page took 0.020767 seconds and 4 git commands to generate.