]> Dogcows Code - chaz/yoink/blob - src/moof/application.cc
build system enhancements
[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 accum_(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 //timer::default_source().scale(SCALAR(0.76));
46
47 //update_timer_.init(boost::bind(&application::dispatch_update,
48 //this, _1, _2),
49 //timestep_, timer::repeat);
50 //add_timer(update_timer_);
51
52 game_time_.reset(timestep_);
53
54 draw_timer_.init(boost::bind(&application::dispatch_draw,
55 this, _1, _2), framerate, timer::repeat);
56 add_timer(draw_timer_);
57 }
58
59
60 void application::dispatch_update(timer& timer, scalar t)
61 {
62 event event;
63
64 while (FE_PollEvent(&event) == 1)
65 {
66 switch (event.type)
67 {
68 case SDL_KEYDOWN:
69
70 if (event.key.keysym.sym == SDLK_ESCAPE &&
71 (SDL_GetModState() & KMOD_CTRL) )
72 {
73 // emergency escape
74 log_warning("escape forced");
75 exit(1);
76 }
77 break;
78
79 case SDL_VIDEORESIZE:
80
81 video::current()->resize(event.resize.w, event.resize.h);
82 break;
83 }
84
85 handle_event(event);
86 }
87
88
89 const int MAX_CONSECUTIVE_UPDATES = 15;
90
91 log_debug("updating", timer.expiration(), "/", t);
92
93 scalar deltaTime = t - last_update_;
94 accum_ += deltaTime;
95
96 int i = 0;
97 while (timestep_ <= accum_ && i < MAX_CONSECUTIVE_UPDATES)
98 {
99 log_debug("UPDATING");
100 update(game_time_.ticks(), timestep_);
101 game_time_.step();
102 accum_ -= timestep_;
103 }
104
105 last_update_ = t;
106 }
107
108 void application::dispatch_draw(timer& timer, scalar t)
109 {
110 log_debug("draw", timer.expiration(), "/", t);
111
112 // XXX temporary
113 thread::main_runloop().run_once();
114
115 dispatch_update(timer, t);
116
117 scalar alpha = accum_ * inverse_timestep_;
118
119 alpha = cml::clamp(alpha, SCALAR(-1.0), SCALAR(2.0));
120 if (alpha < SCALAR(0.0)) log_warning("alpha:", alpha);
121 else if (alpha > SCALAR(1.0)) log_warning("alpha:", alpha);
122 else log_debug("alpha:", alpha);
123
124 draw(alpha);
125
126 scalar begin = timer::ticks();
127 video::current()->swap(t);
128
129 scalar duration = timer::ticks() - begin;
130 log_debug("flip duration:", duration);
131 }
132
133
134 } // namespace moof
135
This page took 0.04228 seconds and 5 git commands to generate.