]> Dogcows Code - chaz/yoink/blob - src/Moof/Engine.cc
settings subsystem now using lua
[chaz/yoink] / src / Moof / Engine.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <cstdlib> // exit
30 #include <string>
31
32 #include <SDL/SDL.h>
33 #include "fastevents.h"
34 #include <AL/alut.h>
35
36 #include "Dispatcher.hh"
37 #include "Engine.hh"
38 #include "Log.hh"
39 #include "Random.hh"
40 #include "Settings.hh"
41 #include "Timer.hh"
42 #include "Video.hh"
43
44
45 namespace Mf {
46
47
48 class Engine::Impl
49 {
50 public:
51 Impl(int argc, char* argv[], const std::string& configFile,
52 const std::string& name, const std::string& iconFile,
53 Engine* outer) :
54 interface(outer),
55 timestep(0.01),
56 printFps(false)
57 {
58 #if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
59 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
60 #else
61 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) != 0)
62 #endif
63 {
64 logError("sdl is complaining: %s", SDL_GetError());
65 throw Exception(Exception::SDL_ERROR);
66 }
67 if (FE_Init() != 0)
68 {
69 logError("fast events error: %s", FE_GetError());
70 throw Exception(Exception::SDL_ERROR);
71 }
72 alutInit(&argc, argv);
73
74 Settings& settings = Settings::getInstance();
75 settings.loadFromFile(configFile);
76 settings.parseArgs(argc, argv);
77
78 long randomSeed;
79 if (settings.get("rngseed", randomSeed)) setSeed(randomSeed);
80 else setSeed();
81
82 settings.get("timestep", timestep);
83
84 long maxFps = 40;
85 settings.get("maxfps", maxFps);
86 drawRate = 1.0 / Scalar(maxFps);
87
88 settings.get("printfps", printFps);
89
90 video = Video::alloc(name, iconFile);
91 video->makeActive();
92 }
93
94 ~Impl()
95 {
96 // the video object must be destroyed before we can shutdown SDL
97 video.reset();
98
99 alutExit();
100 FE_Quit();
101 SDL_Quit();
102 }
103
104
105 /**
106 * The main loop. This just calls dispatchEvents(), update(), and draw()
107 * over and over again. The timing of the update and draw are decoupled.
108 * The actual frame rate is also calculated here. This function will return
109 * the exit code used to stop the loop.
110 */
111
112 int run()
113 {
114 Scalar ticksNow = Timer::getTicks();
115
116 Scalar nextStep = ticksNow;
117 Scalar nextDraw = ticksNow;
118 Scalar nextFpsUpdate = ticksNow + 1.0;
119
120 Scalar totalTime = 0.0;
121 Scalar deltaTime = 0.0;
122 Scalar accumulator = timestep;
123
124 fps = 0;
125 int frameAccum = 0;
126
127 running = true;
128 do
129 {
130 Scalar newTicks = Timer::getTicks();
131 deltaTime = newTicks - ticksNow;
132 ticksNow = newTicks;
133
134 if (deltaTime >= 0.25) deltaTime = 0.25;
135 accumulator += deltaTime;
136
137 Timer::fireIfExpired(ticksNow);
138
139 while (accumulator >= timestep)
140 {
141 dispatchEvents();
142 interface->update(totalTime, timestep);
143
144 totalTime += timestep;
145 accumulator -= timestep;
146
147 nextStep += timestep;
148 }
149 if (ticksNow >= nextStep)
150 {
151 nextStep = ticksNow + timestep;
152 }
153
154 if (ticksNow >= nextDraw)
155 {
156 frameAccum++;
157
158 if (ticksNow >= nextFpsUpdate) // determine the actual fps
159 {
160 fps = frameAccum;
161 frameAccum = 0;
162
163 nextFpsUpdate += 1.0;
164 if (ticksNow >= nextFpsUpdate)
165 {
166 nextFpsUpdate = ticksNow + 1.0;
167 }
168
169 if (printFps)
170 {
171 logInfo("%d fps", fps);
172 }
173 }
174
175 interface->draw(accumulator / timestep);
176 video->swap();
177
178 nextDraw += drawRate;
179 if (ticksNow >= nextDraw)
180 {
181 // we missed some scheduled draws, so reset the schedule
182 nextDraw = ticksNow + drawRate;
183 }
184 }
185
186 // be a good citizen and give back what you don't need
187 Timer::sleep(std::min(std::min(nextStep, nextDraw),
188 Timer::getNextFire()), true);
189 }
190 while (running);
191
192 return exitCode;
193 }
194
195
196 void dispatchEvents()
197 {
198 SDL_Event event;
199
200 while (FE_PollEvent(&event) == 1)
201 {
202 switch (event.type)
203 {
204 case SDL_KEYDOWN:
205 if (event.key.keysym.sym == SDLK_ESCAPE &&
206 (SDL_GetModState() & KMOD_CTRL) )
207 {
208 exit(0);
209 }
210 break;
211
212 case SDL_VIDEORESIZE:
213 video->resize(event.resize.w, event.resize.h);
214 break;
215 }
216
217 interface->handleEvent(event);
218 }
219 }
220
221
222 Engine* interface;
223
224 VideoP video;
225
226 bool running;
227 int exitCode;
228
229 Scalar timestep;
230 Scalar drawRate;
231
232 long fps;
233 bool printFps;
234 };
235
236
237 Engine::Engine(int argc, char* argv[], const std::string& configFile,
238 const std::string& name, const std::string& iconFile) :
239 impl_(new Engine::Impl(argc, argv, configFile, name, iconFile, this)) {}
240
241 Engine::~Engine() {}
242
243
244 int Engine::run()
245 {
246 return impl_->run();
247 }
248
249 void Engine::stop(int exitCode)
250 {
251 impl_->running = false;
252 impl_->exitCode = exitCode;
253 }
254
255
256 void Engine::setTimestep(Scalar ts)
257 {
258 impl_->timestep = ts;
259 }
260
261 Scalar Engine::getTimestep()
262 {
263 return impl_->timestep;
264 }
265
266 void Engine::setMaxFrameRate(long maxFps)
267 {
268 impl_->drawRate = 1.0 / Scalar(maxFps);
269 }
270
271 long Engine::getMaxFrameRate()
272 {
273 return long(1.0 / impl_->drawRate);
274 }
275
276
277 Video& Engine::getVideo()
278 {
279 return *impl_->video;
280 }
281
282 long Engine::getFrameRate()
283 {
284 return impl_->fps;
285 }
286
287
288 void Engine::update(Scalar t, Scalar dt) {}
289 void Engine::draw(Scalar alpha) {}
290 void Engine::handleEvent(const Event& event) {}
291
292
293 } // namespace Mf
294
295 /** vim: set ts=4 sw=4 tw=80: *************************************************/
296
This page took 0.045994 seconds and 5 git commands to generate.