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