]>
Dogcows Code - chaz/yoink/blob - src/Moof/Engine.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
30 #include <cstdlib> // exit
35 #include "fastevents.h"
38 #include "Dispatcher.hh"
43 #include "Settings.hh"
54 Impl(int argc
, char* argv
[], const std::string
& name
,
55 const std::string
& iconFile
, const std::string
& configFile
,
61 #if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
62 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0)
64 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_EVENTTHREAD
) != 0)
67 logError("sdl is complaining: %s", SDL_GetError());
68 throw Exception(Exception::SDL_ERROR
);
72 logError("fast events error: %s", FE_GetError());
73 throw Exception(Exception::SDL_ERROR
);
75 alutInit(&argc
, argv
);
77 Settings
& settings
= Settings::getInstance();
78 settings
.loadFromFile(configFile
);
79 settings
.parseArgs(argc
, argv
);
82 if (settings
.get("rngseed", randomSeed
)) setSeed(randomSeed
);
85 settings
.get("timestep", timestep
);
88 settings
.get("maxfps", maxFps
);
89 drawRate
= 1.0 / Scalar(maxFps
);
91 settings
.get("printfps", printFps
);
93 video
= Video::alloc(name
, iconFile
);
99 // the video object must be destroyed before we can shutdown SDL
109 * The main loop. This just calls dispatchEvents(), update(), and draw()
110 * over and over again. The timing of the update and draw are decoupled.
111 * The actual frame rate is also calculated here. This function will return
112 * the exit code used to stop the loop.
117 Scalar ticksNow
= Timer::getTicks();
119 Scalar nextStep
= ticksNow
;
120 Scalar nextDraw
= ticksNow
;
121 Scalar nextFpsUpdate
= ticksNow
+ 1.0;
123 Scalar totalTime
= 0.0;
124 Scalar deltaTime
= 0.0;
125 Scalar accumulator
= timestep
;
132 Scalar newTicks
= Timer::getTicks();
133 deltaTime
= newTicks
- ticksNow
;
136 if (deltaTime
>= 0.25) deltaTime
= 0.25;
137 accumulator
+= deltaTime
;
139 Timer::fireIfExpired(ticksNow
);
141 while (accumulator
>= timestep
)
144 update(totalTime
, timestep
);
146 totalTime
+= timestep
;
147 accumulator
-= timestep
;
149 nextStep
+= timestep
;
151 if (ticksNow
>= nextStep
)
153 nextStep
= ticksNow
+ timestep
;
156 if (ticksNow
>= nextDraw
)
160 if (ticksNow
>= nextFpsUpdate
) // determine the actual fps
165 nextFpsUpdate
+= 1.0;
166 if (ticksNow
>= nextFpsUpdate
)
168 nextFpsUpdate
= ticksNow
+ 1.0;
173 logInfo("%d fps", fps
);
177 draw(accumulator
/ timestep
);
180 nextDraw
+= drawRate
;
181 if (ticksNow
>= nextDraw
)
183 // we missed some scheduled draws, so reset the schedule
184 nextDraw
= ticksNow
+ drawRate
;
188 // be a good citizen and give back what you don't need
189 Timer::sleep(std::min(std::min(nextStep
, nextDraw
),
190 Timer::getNextFire()), true);
192 while (!stack
.empty());
195 void dispatchEvents()
199 while (FE_PollEvent(&event
) == 1)
204 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
&&
205 (SDL_GetModState() & KMOD_CTRL
) )
212 case SDL_VIDEORESIZE
:
213 video
->resize(event
.resize
.w
, event
.resize
.h
);
222 void update(Scalar t
, Scalar dt
)
224 for (stackIt
= stack
.begin(); stackIt
!= stack
.end(); ++stackIt
)
226 (*stackIt
)->update(t
, dt
);
230 void draw(Scalar alpha
)
232 // FIXME - this will crash if the layer being drawn pops itself
233 std::list
<LayerP
>::reverse_iterator it
;
234 for (it
= stack
.rbegin(); it
!= stack
.rend(); ++it
)
240 void handleEvent(const Event
& event
)
242 for (stackIt
= stack
.begin(); stackIt
!= stack
.end(); ++stackIt
)
244 if ((*stackIt
)->handleEvent(event
)) break;
249 void pushLayer(LayerP layer
)
251 ASSERT(layer
&& "cannot push null layer");
252 stack
.push_front(layer
);
253 layer
->pushed(interface
);
259 if (stack
.begin() == stackIt
) fixIt
= true;
261 LayerP popped
= stack
.front();
263 popped
->popped(interface
);
265 if (fixIt
) stackIt
= --stack
.begin();
268 void popLayer(Layer
* layer
)
272 std::list
<LayerP
>::iterator it
;
273 for (it
= stack
.begin(); it
!= stack
.end(); ++it
)
275 if (it
== stackIt
) fixIt
= true;
277 if ((*it
).get() == layer
)
282 LayerP popped
= stack
.front();
284 popped
->popped(interface
);
286 while (stack
.begin() != it
);
288 if (fixIt
) stackIt
= --stack
.begin();
297 stackIt
= stack
.begin();
305 std::list
<LayerP
> stack
;
306 std::list
<LayerP
>::iterator stackIt
;
316 static Engine
* instance
= 0;
318 Engine::Engine(int argc
, char* argv
[], const std::string
& name
,
319 const std::string
& iconFile
, const std::string
& configFile
) :
320 impl_(new Engine::Impl(argc
, argv
, name
, iconFile
, configFile
, *this))
326 Engine
& Engine::getInstance()
328 ASSERT(instance
&& "dereferencing null pointer");
338 void Engine::setTimestep(Scalar ts
)
340 impl_
->timestep
= ts
;
343 Scalar
Engine::getTimestep() const
345 return impl_
->timestep
;
348 void Engine::setMaxFrameRate(long maxFps
)
350 impl_
->drawRate
= 1.0 / Scalar(maxFps
);
353 long Engine::getMaxFrameRate() const
355 return long(1.0 / impl_
->drawRate
);
359 Video
& Engine::getVideo() const
361 return *impl_
->video
;
364 long Engine::getFrameRate() const
370 void Engine::pushLayer(LayerP layer
)
373 impl_
->pushLayer(layer
);
376 void Engine::popLayer()
382 void Engine::popLayer(Layer
* layer
)
385 impl_
->popLayer(layer
);
388 void Engine::clearLayers()
391 impl_
->clearLayers();
397 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.060845 seconds and 5 git commands to generate.