]>
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 *******************************************************************************/
29 #include <cstdlib> // exit
35 #include "fastevents.h"
37 #include "Dispatcher.hh"
40 #include "Settings.hh"
48 class Engine::EngineImpl
51 EngineImpl(const std::string
& name
, int argc
, char* argv
[],
52 const std::string
& configFile
, Engine
* outer
) :
56 if (SDL_Init(SDL_INIT_EVERYTHING
| SDL_INIT_EVENTTHREAD
) != 0)
58 throw std::runtime_error(SDL_GetError());
62 throw std::runtime_error(FE_GetError());
67 settings
.loadFromFile(configFile
);
69 video
= VideoPtr(new Video(name
));
73 settings
.get("engine.timestep", ts
);
74 timestep
= Scalar(ts
);
77 settings
.getNumber("video.maxfps", maxFps
);
78 drawRate
= 1.0 / Scalar(maxFps
);
81 settings
.get("video.printfps", printFps
);
86 // The video object must be destroyed before we can shutdown SDL.
95 * The main loop. This just calls dispatchEvents(), update(), and draw()
96 * over and over again. The timing of the update and draw are decoupled.
97 * The actual frame rate is also calculated here. This function will return
98 * with a value of 0 if the member variable running becomes true.
103 Scalar ticksNow
= getTicks();
105 Scalar nextStep
= ticksNow
;
106 Scalar nextDraw
= ticksNow
;
107 Scalar nextFpsUpdate
= ticksNow
+ 1.0;
109 Scalar totalTime
= 0.0;
110 Scalar deltaTime
= 0.0;
111 Scalar accumulator
= timestep
;
119 Scalar newTicks
= getTicks();
120 deltaTime
= newTicks
- ticksNow
;
123 if (deltaTime
>= 0.25) deltaTime
= 0.25;
124 accumulator
+= deltaTime
;
126 while (accumulator
>= timestep
)
129 interface
->update(totalTime
, timestep
);
131 totalTime
+= timestep
;
132 accumulator
-= timestep
;
134 nextStep
+= timestep
;
137 if (ticksNow
>= nextDraw
)
141 if (ticksNow
>= nextFpsUpdate
) // determine the actual fps
146 nextFpsUpdate
+= 1.0;
147 if (ticksNow
>= nextFpsUpdate
)
149 nextFpsUpdate
= ticksNow
+ 1.0;
154 std::cout
<< "FPS: " << fps
<< std::endl
;
158 interface
->draw(accumulator
/ timestep
);
161 nextDraw
+= drawRate
;
162 if (ticksNow
>= nextDraw
)
164 // we missed some scheduled draws, so reset the schedule
165 nextDraw
= ticksNow
+ drawRate
;
169 // be a good citizen and give back what you don't need
170 sleep(std::min(nextStep
, nextDraw
), true);
178 void dispatchEvents()
182 while (FE_PollEvent(&event
) == 1)
187 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
&&
188 (SDL_GetModState() & KMOD_CTRL
) )
194 case SDL_VIDEORESIZE
:
195 video
->resize(event
.resize
.w
, event
.resize
.h
);
199 interface
->handleEvent(event
);
205 Dispatcher dispatcher
;
220 Engine::Engine(const std::string
& name
, int argc
, char* argv
[],
221 const std::string
& configFile
) :
222 impl_(new Engine::EngineImpl(name
, argc
, argv
, configFile
, this)) {}
234 impl_
->running
= false;
238 void Engine::setTimestep(Scalar ts
)
240 impl_
->timestep
= ts
;
243 Scalar
Engine::getTimestep()
245 return impl_
->timestep
;
248 void Engine::setMaxFrameRate(long maxFps
)
250 impl_
->drawRate
= 1.0 / Scalar(maxFps
);
253 long Engine::getMaxFrameRate()
255 return long(1.0 / impl_
->drawRate
);
259 Video
& Engine::getVideo()
261 return *impl_
->video
;
264 long Engine::getFrameRate()
270 void Engine::update(Scalar t
, Scalar dt
) {}
271 void Engine::draw(Scalar alpha
) {}
272 void Engine::handleEvent(const Event
& event
) {}
277 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.043566 seconds and 4 git commands to generate.