]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Engine.cc
new timer class
[chaz/yoink] / src / Moof / Engine.cc
index d7d04a846af6bc54753551d1135d582b481fab06..e97c8b5d1b7edbae52a5b48ebc1072574e697ebe 100644 (file)
 *******************************************************************************/
 
 #include <cstdlib>                     // exit
-#include <iostream>
 #include <string>
 
 #include <SDL/SDL.h>
 #include "fastevents.h"
-#include <SDL/SDL_sound.h>
 #include <AL/alut.h>
 
+#include "Dispatcher.hh"
 #include "Engine.hh"
+#include "Log.hh"
 #include "Random.hh"
 #include "Settings.hh"
 #include "Timer.hh"
@@ -53,18 +53,19 @@ public:
                        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 Exception(SDL_GetError());
+                       logError("sdl is complaining: %s", SDL_GetError());
+                       throw Exception(Exception::SDL_ERROR);
                }
                if (FE_Init() != 0)
                {
-                       throw Exception(FE_GetError());
-               }
-               if (Sound_Init() != 0)
-               {
-                       //throw Exception(Sound_GetError());
-                       std::cerr << Sound_GetError() << std::endl;
+                       logError("fast events error: %s", FE_GetError());
+                       throw Exception(Exception::SDL_ERROR);
                }
                alutInit(&argc, argv);
 
@@ -87,7 +88,7 @@ public:
                printFps = false;
                settings.get("video.printfps", printFps);
 
-               video = VideoPtr(new Video(name, iconFile));
+               video = Video::alloc(name, iconFile);
                video->makeActive();
        }
 
@@ -97,7 +98,6 @@ public:
                video.reset();
 
                alutExit();
-               Sound_Quit();
                FE_Quit();
                SDL_Quit();
        }
@@ -112,7 +112,7 @@ public:
 
        int run()
        {
-               Scalar ticksNow = getTicks();
+               Scalar ticksNow = Timer::getTicks();
 
                Scalar nextStep = ticksNow;
                Scalar nextDraw = ticksNow;
@@ -128,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();
@@ -167,7 +169,7 @@ public:
 
                                        if (printFps)
                                        {
-                                               std::cout << "FPS: " << fps << std::endl;
+                                               logInfo("framerate: %d fps", fps);
                                        }
                                }
 
@@ -183,7 +185,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);
 
@@ -219,7 +222,7 @@ public:
 
        Engine*         interface;
 
-       VideoPtr        video;
+       VideoP          video;
 
        bool            running;
        int                     exitCode;
This page took 0.021363 seconds and 4 git commands to generate.