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