]> Dogcows Code - chaz/yoink/blob - src/moof/application.cc
initial runloop implementation
[chaz/yoink] / src / moof / application.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <cstdlib> // exit, srand
13 #include <boost/noncopyable.hpp>
14
15 #include <SDL/SDL.h>
16 #include "fastevents.h"
17
18 #include "application.hh"
19 #include "log.hh"
20 #include "settings.hh"
21 #include "timer.hh"
22 #include "video.hh"
23
24
25 namespace moof {
26
27
28 application::application(settings& settings) :
29 next_update_(timer::ticks()),
30 total_time_(SCALAR(0.0))
31 {
32 unsigned random_seed;
33 if (settings.get("rngseed", random_seed)) srand(random_seed);
34 else srand(time(0));
35
36 scalar timestep = SCALAR(80.0);
37 settings.get("timestep", timestep);
38 timestep_ = SCALAR(1.0) / timestep;
39 inverse_timestep_ = timestep;
40
41 scalar framerate = SCALAR(40.0);
42 settings.get("framerate", framerate);
43 framerate = SCALAR(1.0) / framerate;
44
45 update_timer_.init(boost::bind(&application::dispatch_update, this, _1, _2),
46 timestep_, timer::repeat, this);
47 draw_timer_.init(boost::bind(&application::dispatch_draw, this, _1, _2),
48 framerate, timer::repeat, this);
49 }
50
51
52 void application::dispatch_update(timer& timer, scalar t)
53 {
54 event event;
55
56 while (FE_PollEvent(&event) == 1)
57 {
58 switch (event.type)
59 {
60 case SDL_KEYDOWN:
61
62 if (event.key.keysym.sym == SDLK_ESCAPE &&
63 (SDL_GetModState() & KMOD_CTRL) )
64 {
65 // emergency escape
66 log_warning("escape forced");
67 exit(1);
68 }
69 break;
70
71 case SDL_VIDEORESIZE:
72
73 video::current()->resize(event.resize.w, event.resize.h);
74 break;
75 }
76
77 handle_event(event);
78 }
79
80
81 const int MAX_FRAMESKIP = 15;
82
83 int i = 0;
84 while (next_update_ < t && ++i < MAX_FRAMESKIP)
85 {
86 total_time_ += timestep_;
87 update(total_time_, timestep_);
88
89 next_update_ += timestep_;
90 }
91 }
92
93 void application::dispatch_draw(timer& timer, scalar t)
94 {
95 scalar alpha = (t + timestep_ - next_update_) * inverse_timestep_;
96 if (alpha < SCALAR(0.0)) log_error("UH OH!!!!! It's NEGATIVE", alpha);
97 if (alpha > SCALAR(1.0)) log_error("UH OH!!!!! It's POSITIVE", alpha);
98
99 draw(alpha);
100 video::current()->swap();
101 }
102
103
104 } // namespace moof
105
This page took 0.037124 seconds and 4 git commands to generate.