]> Dogcows Code - chaz/yoink/blobdiff - src/moof/application.cc
initial runloop implementation
[chaz/yoink] / src / moof / application.cc
diff --git a/src/moof/application.cc b/src/moof/application.cc
new file mode 100644 (file)
index 0000000..fd9ea45
--- /dev/null
@@ -0,0 +1,105 @@
+
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#include <cstdlib>                                     // exit, srand
+#include <boost/noncopyable.hpp>
+
+#include <SDL/SDL.h>
+#include "fastevents.h"
+
+#include "application.hh"
+#include "log.hh"
+#include "settings.hh"
+#include "timer.hh"
+#include "video.hh"
+
+
+namespace moof {
+
+
+application::application(settings& settings) :
+       next_update_(timer::ticks()),
+       total_time_(SCALAR(0.0))
+{
+       unsigned random_seed;
+       if (settings.get("rngseed", random_seed)) srand(random_seed);
+       else srand(time(0));
+
+       scalar timestep = SCALAR(80.0);
+       settings.get("timestep", timestep);
+       timestep_ = SCALAR(1.0) / timestep;
+       inverse_timestep_ = timestep;
+
+       scalar framerate = SCALAR(40.0);
+       settings.get("framerate", framerate);
+       framerate = SCALAR(1.0) / framerate;
+
+       update_timer_.init(boost::bind(&application::dispatch_update, this, _1, _2),
+                                          timestep_, timer::repeat, this);
+       draw_timer_.init(boost::bind(&application::dispatch_draw, this, _1, _2),
+                                        framerate, timer::repeat, this);
+}
+
+
+void application::dispatch_update(timer& timer, scalar t)
+{
+       event event;
+
+       while (FE_PollEvent(&event) == 1)
+       {
+               switch (event.type)
+               {
+                       case SDL_KEYDOWN:
+
+                               if (event.key.keysym.sym == SDLK_ESCAPE &&
+                                               (SDL_GetModState() & KMOD_CTRL) )
+                               {
+                                       // emergency escape
+                                       log_warning("escape forced");
+                                       exit(1);
+                               }
+                               break;
+
+                       case SDL_VIDEORESIZE:
+
+                               video::current()->resize(event.resize.w, event.resize.h);
+                               break;
+               }
+
+               handle_event(event);
+       }
+
+
+       const int MAX_FRAMESKIP = 15;
+
+       int i = 0;
+       while (next_update_ < t && ++i < MAX_FRAMESKIP)
+       {
+               total_time_ += timestep_;
+               update(total_time_, timestep_);
+
+               next_update_ += timestep_;
+       }
+}
+
+void application::dispatch_draw(timer& timer, scalar t)
+{
+       scalar alpha = (t + timestep_ - next_update_) * inverse_timestep_;
+       if (alpha < SCALAR(0.0)) log_error("UH OH!!!!!  It's NEGATIVE", alpha);
+       if (alpha > SCALAR(1.0)) log_error("UH OH!!!!!  It's POSITIVE", alpha);
+
+       draw(alpha);
+       video::current()->swap();
+}
+
+
+} // namespace moof
+
This page took 0.021038 seconds and 4 git commands to generate.