]>
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
33 #include "fastevents.h"
36 #include "Dispatcher.hh"
40 #include "Settings.hh"
51 Impl(int argc
, char* argv
[], const std::string
& configFile
,
52 const std::string
& name
, const std::string
& iconFile
,
56 #if defined(_WIN32) || defined (_WIN64) || defined(__WIN32__)
57 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_TIMER
) != 0)
59 if (SDL_Init(SDL_INIT_VIDEO
| SDL_INIT_EVENTTHREAD
) != 0)
62 logError("sdl is complaining: %s", SDL_GetError());
63 throw Exception(Exception::SDL_ERROR
);
67 logError("fast events error: %s", FE_GetError());
68 throw Exception(Exception::SDL_ERROR
);
70 alutInit(&argc
, argv
);
72 Settings
& settings
= Settings::getInstance();
73 settings
.parseArgs(argc
, argv
);
74 settings
.loadFromFile(configFile
);
77 if (settings
.get("engine.rngseed", randomSeed
)) setSeed(randomSeed
);
81 settings
.get("engine.timestep", ts
);
82 timestep
= Scalar(ts
);
85 settings
.getNumber("video.maxfps", maxFps
);
86 drawRate
= 1.0 / Scalar(maxFps
);
89 settings
.get("video.printfps", printFps
);
91 video
= Video::alloc(name
, iconFile
);
97 // the video object must be destroyed before we can shutdown SDL
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.
115 Scalar ticksNow
= Timer::getTicks();
117 Scalar nextStep
= ticksNow
;
118 Scalar nextDraw
= ticksNow
;
119 Scalar nextFpsUpdate
= ticksNow
+ 1.0;
121 Scalar totalTime
= 0.0;
122 Scalar deltaTime
= 0.0;
123 Scalar accumulator
= timestep
;
131 Scalar newTicks
= Timer::getTicks();
132 deltaTime
= newTicks
- ticksNow
;
135 if (deltaTime
>= 0.25) deltaTime
= 0.25;
136 accumulator
+= deltaTime
;
138 Timer::fireIfExpired(ticksNow
);
140 while (accumulator
>= timestep
)
143 interface
->update(totalTime
, timestep
);
145 totalTime
+= timestep
;
146 accumulator
-= timestep
;
148 nextStep
+= timestep
;
150 if (ticksNow
>= nextStep
)
152 nextStep
= ticksNow
+ timestep
;
155 if (ticksNow
>= nextDraw
)
159 if (ticksNow
>= nextFpsUpdate
) // determine the actual fps
164 nextFpsUpdate
+= 1.0;
165 if (ticksNow
>= nextFpsUpdate
)
167 nextFpsUpdate
= ticksNow
+ 1.0;
172 logInfo("framerate: %d fps", fps
);
176 interface
->draw(accumulator
/ timestep
);
179 nextDraw
+= drawRate
;
180 if (ticksNow
>= nextDraw
)
182 // we missed some scheduled draws, so reset the schedule
183 nextDraw
= ticksNow
+ drawRate
;
187 // be a good citizen and give back what you don't need
188 Timer::sleep(std::min(std::min(nextStep
, nextDraw
),
189 Timer::getNextFire()), true);
197 void dispatchEvents()
201 while (FE_PollEvent(&event
) == 1)
206 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
&&
207 (SDL_GetModState() & KMOD_CTRL
) )
213 case SDL_VIDEORESIZE
:
214 video
->resize(event
.resize
.w
, event
.resize
.h
);
218 interface
->handleEvent(event
);
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)) {}
250 void Engine::stop(int exitCode
)
252 impl_
->running
= false;
253 impl_
->exitCode
= exitCode
;
257 void Engine::setTimestep(Scalar ts
)
259 impl_
->timestep
= ts
;
262 Scalar
Engine::getTimestep()
264 return impl_
->timestep
;
267 void Engine::setMaxFrameRate(long maxFps
)
269 impl_
->drawRate
= 1.0 / Scalar(maxFps
);
272 long Engine::getMaxFrameRate()
274 return long(1.0 / impl_
->drawRate
);
278 Video
& Engine::getVideo()
280 return *impl_
->video
;
283 long Engine::getFrameRate()
289 void Engine::update(Scalar t
, Scalar dt
) {}
290 void Engine::draw(Scalar alpha
) {}
291 void Engine::handleEvent(const Event
& event
) {}
296 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.05038 seconds and 5 git commands to generate.