]> Dogcows Code - chaz/yoink/blob - src/moof/application.cc
further implementing runloop support
[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 last_update_(timer::ticks()),
30 total_time_(SCALAR(0.0)),
31 accum_(SCALAR(0.0))
32 {
33 unsigned random_seed;
34 if (settings.get("rngseed", random_seed)) srand(random_seed);
35 else srand(time(0));
36
37 scalar timestep = SCALAR(80.0);
38 settings.get("timestep", timestep);
39 timestep_ = SCALAR(1.0) / timestep;
40 inverse_timestep_ = timestep;
41
42 scalar framerate = SCALAR(40.0);
43 settings.get("framerate", framerate);
44 framerate = SCALAR(1.0) / framerate;
45
46 //timer::default_source().scale(SCALAR(1.2));
47
48 //update_timer_.init(boost::bind(&application::dispatch_update, this, _1, _2),
49 //timestep_, timer::repeat, this);
50 next_update_ = update_timer_.expiration();
51
52 draw_timer_.init(boost::bind(&application::dispatch_draw, this, _1, _2),
53 framerate, timer::repeat);
54 add_timer(draw_timer_);
55 }
56
57
58 void application::dispatch_update(timer& timer, scalar t)
59 {
60 event event;
61
62 while (FE_PollEvent(&event) == 1)
63 {
64 switch (event.type)
65 {
66 case SDL_KEYDOWN:
67
68 if (event.key.keysym.sym == SDLK_ESCAPE &&
69 (SDL_GetModState() & KMOD_CTRL) )
70 {
71 // emergency escape
72 log_warning("escape forced");
73 exit(1);
74 }
75 break;
76
77 case SDL_VIDEORESIZE:
78
79 video::current()->resize(event.resize.w, event.resize.h);
80 break;
81 }
82
83 handle_event(event);
84 }
85
86
87 //const int MAX_FRAMESKIP = 15;
88
89 log_debug("updating", timer.expiration(), "/", t);
90
91 //int i = 0;
92 //while (next_update_ < t && ++i < MAX_FRAMESKIP)
93 //{
94 //total_time_ += timestep_;
95 //update(total_time_, timestep_);
96
97 //next_update_ += timestep_;
98 //log_debug("updated", next_update_, "time:", timer::ticks());
99 //}
100
101 scalar deltaTime = t - last_update_;
102 accum_ += deltaTime;
103
104 while (timestep_ <= accum_)
105 {
106 log_debug("UPDATING");
107 update(total_time_, timestep_);
108 total_time_ += timestep_;
109 accum_ -= timestep_;
110 }
111
112 last_update_ = t;
113 }
114
115 void application::dispatch_draw(timer& timer, scalar t)
116 {
117 log_debug("next update", update_timer_.expiration());
118 log_debug("draw", timer.expiration(), "/", t);
119
120 dispatch_update(timer, t);
121
122 //if (t < next_update_ - timestep_) return;
123 //scalar alpha = (t + timestep_ - next_update_) * inverse_timestep_;
124 //scalar alpha = (t + timestep_ - update_timer_.expiration()) * inverse_timestep_;
125 //scalar alpha = (next_update_ - t) * inverse_timestep_;
126 scalar alpha = accum_ * inverse_timestep_;
127 //if (alpha < SCALAR(0.0) || SCALAR(1.0) < alpha) return;
128
129 alpha = cml::clamp(alpha, SCALAR(-1.0), SCALAR(2.0));
130 if (alpha < SCALAR(0.0)) log_warning("alpha:", alpha);
131 else if (alpha > SCALAR(1.0)) log_warning("alpha:", alpha);
132 else log_debug("alpha:", alpha);
133
134 draw(alpha);
135
136 scalar begin = timer::ticks();
137 video::current()->swap(t);
138
139 scalar duration = timer::ticks() - begin;
140 log_debug("flip duration:", duration);
141 }
142
143
144 } // namespace moof
145
This page took 0.036739 seconds and 4 git commands to generate.