]> Dogcows Code - chaz/yoink/blobdiff - src/engine.cc
big batch of progress
[chaz/yoink] / src / engine.cc
index 3e40361a3213f73c220d2ced348931c9dfd4e8cb..ec6faa15e9d0505c027c659aa2867ae62eb263ba 100644 (file)
 namespace dc {
 
 
-class engine_impl
+class engine::engine_impl
 {
 public:
        engine_impl(const std::string& name, int argc, char* argv[],
-                       const std::string& configFile, engine* outer) : config(argc, argv),
-                       interface(outer)
+                       const std::string& configFile, engine* outer) :
+               config(argc, argv),
+               interface(outer)
        {
                if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0)
                {
@@ -69,8 +70,9 @@ public:
                screen = video_ptr(new video(name));
                screen->makeActive();
 
-               timestep = 0.01;
-               config.get("engine.timestep", timestep);
+               double ts = 0.01;
+               config.get("engine.timestep", ts);
+               timestep = scalar(ts);
 
                long maxfps = 40;
                config.get("engine.maxfps", maxfps);
@@ -99,10 +101,10 @@ public:
                scalar nextFPSUpdate = ticksNow + 1.0;
 
                scalar totalTime = 0.0;
-               scalar accumulator = 0.0;
+               scalar accumulator = timestep;
 
-               fps = 0.0;
-               int frameAccum = 0.0;
+               fps = 0;
+               int frameAccum = 0;
 
                running = true;
                do
@@ -113,7 +115,7 @@ public:
                        accumulator += newTicks - ticksNow;
                        ticksNow = newTicks;
 
-                       if (ticksNow >= nextStep)
+                       if (accumulator >= timestep)
                        {
                                interface->update(totalTime, timestep);
 
@@ -121,20 +123,28 @@ public:
                                accumulator -= timestep;
 
                                nextStep += timestep;
-                               if (ticksNow >= nextStep) nextStep = ticksNow + timestep;
+                               if (ticksNow >= nextStep)
+                               {
+                                       // we missed some scheduled steps, so reset the schedule
+                                       nextStep = ticksNow + timestep;
+                                       accumulator = 0.0;
+                               }
                        }
 
                        if (ticksNow >= nextDraw)
                        {
                                frameAccum++;
 
-                               if (ticksNow >= nextFPSUpdate)
+                               if (ticksNow >= nextFPSUpdate) // determine the actual fps
                                {
-                                       fps = frameAccum;// + (ticksNow - nextFPSUpdate) / 1.0;
+                                       fps = frameAccum;
                                        frameAccum = 0;
 
                                        nextFPSUpdate += 1.0;
-                                       if (ticksNow >= nextFPSUpdate) nextFPSUpdate = ticksNow + 1.0;
+                                       if (ticksNow >= nextFPSUpdate)
+                                       {
+                                               nextFPSUpdate = ticksNow + 1.0;
+                                       }
 
                                        if (printfps)
                                        {
@@ -146,25 +156,32 @@ public:
                                screen->swap();
 
                                nextDraw += drawrate;
-                               if (ticksNow >= nextDraw) nextDraw = ticksNow + drawrate;
+                               if (ticksNow >= nextDraw)
+                               {
+                                       // we missed some scheduled draws, so reset the schedule
+                                       nextDraw = ticksNow + drawrate;
+                               }
                        }
 
+                       // be a good citizen and give back what you don't need
                        sleep(std::min(nextStep, nextDraw), true);
                }
                while (running);
+
+               return 0;
        }
 
 
        void dispatchEvents()
        {
-               SDL_Event event;
+               SDL_Event e;
 
-               while (FE_PollEvent(&event) == 1)
+               while (FE_PollEvent(&e) == 1)
                {
-                       switch (event.type)
+                       switch (e.type)
                        {
                                case SDL_KEYDOWN:
-                                       if (event.key.keysym.sym == SDLK_ESCAPE &&
+                                       if (e.key.keysym.sym == SDLK_ESCAPE &&
                                                        (SDL_GetModState() & KMOD_CTRL) )
                                        {
                                           exit(0);
@@ -172,11 +189,11 @@ public:
                                        break;
 
                                case SDL_VIDEORESIZE:
-                                       screen->resize(event.resize.w, event.resize.h);
+                                       screen->resize(e.resize.w, e.resize.h);
                                        break;
                        }
 
-                       interface->dispatchEvent(event);
+                       interface->handleEvent(e);
                }
        }
 
@@ -196,16 +213,17 @@ public:
        engine* interface;
 };
 
+
 engine::engine(const std::string& name, int argc, char* argv[],
-               const std::string& configFile)
-               : impl(new engine_impl(name, argc, argv, configFile, this)) {}
+               const std::string& configFile) :
+       impl(new engine::engine_impl(name, argc, argv, configFile, this)) {}
 
 engine::~engine() {}
 
 
 int engine::run()
 {
-       impl->run();
+       return impl->run();
 }
 
 void engine::stop()
@@ -248,8 +266,10 @@ long engine::getFPS()
 
 void engine::update(scalar t, scalar dt) {}
 void engine::draw(scalar alpha) {}
-void engine::dispatchEvent(const SDL_Event& event) {}
+void engine::handleEvent(const SDL_Event& e) {}
 
 
 } // namespace dc
 
+/** vim: set ts=4 sw=4 tw=80: *************************************************/
+
This page took 0.021719 seconds and 4 git commands to generate.