]> Dogcows Code - chaz/yoink/blob - src/Moof/Engine.cc
better logging (maybe) and exception handling
[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 <iostream>
31 #include <string>
32
33 #include <SDL/SDL.h>
34 #include "fastevents.h"
35 #include <SDL/SDL_sound.h>
36 #include <AL/alut.h>
37
38 #include "Engine.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 {
56 if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0)
57 {
58 std::cerr << "sdl is complaining: " << SDL_GetError() << std::endl;
59 throw Exception(Exception::SDL_ERROR);
60 }
61 if (FE_Init() != 0)
62 {
63 std::cerr << "fast events error: " << FE_GetError() << std::endl;
64 throw Exception(Exception::SDL_ERROR);
65 }
66 if (Sound_Init() == 0)
67 {
68 std::cerr << "sound initialization failed: " << Sound_GetError()
69 << std::endl;
70 throw Exception(Exception::SDL_ERROR);
71 }
72 alutInit(&argc, argv);
73
74 Settings& settings = Settings::getInstance();
75 settings.parseArgs(argc, argv);
76 settings.loadFromFile(configFile);
77
78 long randomSeed;
79 if (settings.get("engine.rngseed", randomSeed)) setSeed(randomSeed);
80 else setSeed();
81
82 double ts = 0.01;
83 settings.get("engine.timestep", ts);
84 timestep = Scalar(ts);
85
86 long maxFps = 40;
87 settings.getNumber("video.maxfps", maxFps);
88 drawRate = 1.0 / Scalar(maxFps);
89
90 printFps = false;
91 settings.get("video.printfps", printFps);
92
93 video = VideoPtr(new Video(name, iconFile));
94 video->makeActive();
95 }
96
97 ~Impl()
98 {
99 // the video object must be destroyed before we can shutdown SDL
100 video.reset();
101
102 alutExit();
103 Sound_Quit();
104 FE_Quit();
105 SDL_Quit();
106 }
107
108
109 /**
110 * The main loop. This just calls dispatchEvents(), update(), and draw()
111 * over and over again. The timing of the update and draw are decoupled.
112 * The actual frame rate is also calculated here. This function will return
113 * the exit code used to stop the loop.
114 */
115
116 int run()
117 {
118 Scalar ticksNow = getTicks();
119
120 Scalar nextStep = ticksNow;
121 Scalar nextDraw = ticksNow;
122 Scalar nextFpsUpdate = ticksNow + 1.0;
123
124 Scalar totalTime = 0.0;
125 Scalar deltaTime = 0.0;
126 Scalar accumulator = timestep;
127
128 fps = 0;
129 int frameAccum = 0;
130
131 running = true;
132 do
133 {
134 Scalar newTicks = getTicks();
135 deltaTime = newTicks - ticksNow;
136 ticksNow = newTicks;
137
138 if (deltaTime >= 0.25) deltaTime = 0.25;
139 accumulator += deltaTime;
140
141 while (accumulator >= timestep)
142 {
143 dispatchEvents();
144 interface->update(totalTime, timestep);
145
146 totalTime += timestep;
147 accumulator -= timestep;
148
149 nextStep += timestep;
150 }
151 if (ticksNow >= nextStep)
152 {
153 nextStep = ticksNow + timestep;
154 }
155
156 if (ticksNow >= nextDraw)
157 {
158 frameAccum++;
159
160 if (ticksNow >= nextFpsUpdate) // determine the actual fps
161 {
162 fps = frameAccum;
163 frameAccum = 0;
164
165 nextFpsUpdate += 1.0;
166 if (ticksNow >= nextFpsUpdate)
167 {
168 nextFpsUpdate = ticksNow + 1.0;
169 }
170
171 if (printFps)
172 {
173 std::cout << "FPS: " << fps << std::endl;
174 }
175 }
176
177 interface->draw(accumulator / timestep);
178 video->swap();
179
180 nextDraw += drawRate;
181 if (ticksNow >= nextDraw)
182 {
183 // we missed some scheduled draws, so reset the schedule
184 nextDraw = ticksNow + drawRate;
185 }
186 }
187
188 // be a good citizen and give back what you don't need
189 sleep(std::min(nextStep, nextDraw), true);
190 }
191 while (running);
192
193 return exitCode;
194 }
195
196
197 void dispatchEvents()
198 {
199 SDL_Event event;
200
201 while (FE_PollEvent(&event) == 1)
202 {
203 switch (event.type)
204 {
205 case SDL_KEYDOWN:
206 if (event.key.keysym.sym == SDLK_ESCAPE &&
207 (SDL_GetModState() & KMOD_CTRL) )
208 {
209 exit(0);
210 }
211 break;
212
213 case SDL_VIDEORESIZE:
214 video->resize(event.resize.w, event.resize.h);
215 break;
216 }
217
218 interface->handleEvent(event);
219 }
220 }
221
222
223 Engine* interface;
224
225 VideoPtr video;
226
227 bool running;
228 int exitCode;
229
230 Scalar timestep;
231 Scalar drawRate;
232
233 long fps;
234 bool printFps;
235 };
236
237
238 Engine::Engine(int argc, char* argv[], const std::string& configFile,
239 const std::string& name, const std::string& iconFile) :
240 impl_(new Engine::Impl(argc, argv, configFile, name, iconFile, this)) {}
241
242 Engine::~Engine() {}
243
244
245 int Engine::run()
246 {
247 return impl_->run();
248 }
249
250 void Engine::stop(int exitCode)
251 {
252 impl_->running = false;
253 impl_->exitCode = exitCode;
254 }
255
256
257 void Engine::setTimestep(Scalar ts)
258 {
259 impl_->timestep = ts;
260 }
261
262 Scalar Engine::getTimestep()
263 {
264 return impl_->timestep;
265 }
266
267 void Engine::setMaxFrameRate(long maxFps)
268 {
269 impl_->drawRate = 1.0 / Scalar(maxFps);
270 }
271
272 long Engine::getMaxFrameRate()
273 {
274 return long(1.0 / impl_->drawRate);
275 }
276
277
278 Video& Engine::getVideo()
279 {
280 return *impl_->video;
281 }
282
283 long Engine::getFrameRate()
284 {
285 return impl_->fps;
286 }
287
288
289 void Engine::update(Scalar t, Scalar dt) {}
290 void Engine::draw(Scalar alpha) {}
291 void Engine::handleEvent(const Event& event) {}
292
293
294 } // namespace Mf
295
296 /** vim: set ts=4 sw=4 tw=80: *************************************************/
297
This page took 0.050993 seconds and 5 git commands to generate.