]> Dogcows Code - chaz/yoink/blob - src/moof/application.cc
e0b73b78d9ac053e7a5f8805a2431acc4c730235
[chaz/yoink] / src / moof / application.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #include <cstdlib> // exit, srand
11 #include <boost/noncopyable.hpp>
12
13 #include <SDL/SDL.h>
14 #include "fastevents.h"
15
16 #include "application.hh"
17 #include "log.hh"
18 #include "settings.hh"
19 #include "timer.hh"
20 #include "video.hh"
21
22
23 namespace moof {
24
25
26 application::application(settings& settings) :
27 last_update_(timer::ticks()),
28 accum_(SCALAR(0.0))
29 {
30 unsigned random_seed;
31 if (settings.get("rngseed", random_seed)) srand(random_seed);
32 else srand(time(0));
33
34 scalar timestep = SCALAR(80.0);
35 settings.get("timestep", timestep);
36 timestep_ = SCALAR(1.0) / timestep;
37 inverse_timestep_ = timestep;
38
39 scalar framerate = SCALAR(40.0);
40 settings.get("framerate", framerate);
41 framerate = SCALAR(1.0) / framerate;
42
43 //update_timer_.init(boost::bind(&application::dispatch_update,
44 //this, _1, _2), timestep_, timer::repeat);
45 //add_timer(update_timer_);
46
47 game_time_.reset(timestep_);
48 //game_time_.scale(SCALAR(0.5));
49
50 draw_timer_.init(boost::bind(&application::dispatch_draw,
51 this, _1, _2), framerate, timer::repeat);
52 add_timer(draw_timer_);
53
54 //timer::default_source().scale(SCALAR(0.2));
55 }
56
57 void application::dispatch_update(timer& timer, scalar t)
58 {
59 event event;
60
61 while (FE_PollEvent(&event) == 1)
62 {
63 switch (event.type)
64 {
65 case SDL_KEYDOWN:
66 if (event.key.keysym.sym == SDLK_ESCAPE &&
67 (SDL_GetModState() & KMOD_CTRL))
68 {
69 log_warning("escape forced");
70 exit(1);
71 }
72 break;
73
74 case SDL_VIDEORESIZE:
75 video::current()->resize(event.resize.w,
76 event.resize.h);
77 break;
78 }
79 handle_event(event);
80 }
81
82 const int MAX_CONSECUTIVE_UPDATES = 15;
83
84 log_debug("updating", timer.expiration(), "/", t);
85
86 scalar deltaTime = t - last_update_;
87 accum_ += deltaTime;
88
89 int i = 0;
90 while (timestep_ <= accum_ && i < MAX_CONSECUTIVE_UPDATES)
91 {
92 scalar dt = game_time_.step();
93 update(game_time_.ticks(), dt);
94 accum_ -= timestep_;
95 }
96
97 last_update_ = t;
98 }
99
100 void application::dispatch_draw(timer& timer, scalar t)
101 {
102 log_debug("draw", timer.expiration(), "/", t);
103
104 // XXX temporary
105 thread::main_runloop().run_once();
106
107 dispatch_update(timer, t);
108
109 scalar alpha = accum_ * inverse_timestep_;
110
111 //alpha = cml::clamp(alpha, SCALAR(-1.0), SCALAR(2.0));
112 if (alpha < SCALAR(0.0)) log_warning("alpha:", alpha);
113 else if (alpha > SCALAR(1.0)) log_warning("alpha:", alpha);
114 else log_debug("alpha:", alpha);
115
116 draw(alpha);
117
118 scalar begin = timer::ticks();
119 video::current()->swap(t);
120 scalar duration = timer::ticks() - begin;
121 log_debug("flip duration:", duration, begin, timer::ticks());
122
123 log_info("draw difference:", t - last_draw_);
124 last_draw_ = t;
125 }
126
127
128 } // namespace moof
129
This page took 0.038195 seconds and 3 git commands to generate.