]>
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
34 #include "fastevents.h"
35 #include <SDL/SDL_sound.h>
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_EVERYTHING
) != 0)
59 if (SDL_Init(SDL_INIT_EVERYTHING
| SDL_INIT_EVENTTHREAD
) != 0)
62 std::cerr
<< "sdl is complaining: " << SDL_GetError() << std::endl
;
63 throw Exception(Exception::SDL_ERROR
);
67 std::cerr
<< "fast events error: " << FE_GetError() << std::endl
;
68 throw Exception(Exception::SDL_ERROR
);
70 if (Sound_Init() == 0)
72 std::cerr
<< "sound initialization failed: " << Sound_GetError()
74 throw Exception(Exception::SDL_ERROR
);
76 alutInit(&argc
, argv
);
78 Settings
& settings
= Settings::getInstance();
79 settings
.parseArgs(argc
, argv
);
80 settings
.loadFromFile(configFile
);
83 if (settings
.get("engine.rngseed", randomSeed
)) setSeed(randomSeed
);
87 settings
.get("engine.timestep", ts
);
88 timestep
= Scalar(ts
);
91 settings
.getNumber("video.maxfps", maxFps
);
92 drawRate
= 1.0 / Scalar(maxFps
);
95 settings
.get("video.printfps", printFps
);
97 video
= Video::alloc(name
, iconFile
);
103 // the video object must be destroyed before we can shutdown SDL
114 * The main loop. This just calls dispatchEvents(), update(), and draw()
115 * over and over again. The timing of the update and draw are decoupled.
116 * The actual frame rate is also calculated here. This function will return
117 * the exit code used to stop the loop.
122 Scalar ticksNow
= getTicks();
124 Scalar nextStep
= ticksNow
;
125 Scalar nextDraw
= ticksNow
;
126 Scalar nextFpsUpdate
= ticksNow
+ 1.0;
128 Scalar totalTime
= 0.0;
129 Scalar deltaTime
= 0.0;
130 Scalar accumulator
= timestep
;
138 Scalar newTicks
= getTicks();
139 deltaTime
= newTicks
- ticksNow
;
142 if (deltaTime
>= 0.25) deltaTime
= 0.25;
143 accumulator
+= deltaTime
;
145 while (accumulator
>= timestep
)
148 interface
->update(totalTime
, timestep
);
150 totalTime
+= timestep
;
151 accumulator
-= timestep
;
153 nextStep
+= timestep
;
155 if (ticksNow
>= nextStep
)
157 nextStep
= ticksNow
+ timestep
;
160 if (ticksNow
>= nextDraw
)
164 if (ticksNow
>= nextFpsUpdate
) // determine the actual fps
169 nextFpsUpdate
+= 1.0;
170 if (ticksNow
>= nextFpsUpdate
)
172 nextFpsUpdate
= ticksNow
+ 1.0;
177 std::cout
<< "FPS: " << fps
<< std::endl
;
181 interface
->draw(accumulator
/ timestep
);
184 nextDraw
+= drawRate
;
185 if (ticksNow
>= nextDraw
)
187 // we missed some scheduled draws, so reset the schedule
188 nextDraw
= ticksNow
+ drawRate
;
192 // be a good citizen and give back what you don't need
193 sleep(std::min(nextStep
, nextDraw
), true);
201 void dispatchEvents()
205 while (FE_PollEvent(&event
) == 1)
210 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
&&
211 (SDL_GetModState() & KMOD_CTRL
) )
217 case SDL_VIDEORESIZE
:
218 video
->resize(event
.resize
.w
, event
.resize
.h
);
222 interface
->handleEvent(event
);
242 Engine::Engine(int argc
, char* argv
[], const std::string
& configFile
,
243 const std::string
& name
, const std::string
& iconFile
) :
244 impl_(new Engine::Impl(argc
, argv
, configFile
, name
, iconFile
, this)) {}
254 void Engine::stop(int exitCode
)
256 impl_
->running
= false;
257 impl_
->exitCode
= exitCode
;
261 void Engine::setTimestep(Scalar ts
)
263 impl_
->timestep
= ts
;
266 Scalar
Engine::getTimestep()
268 return impl_
->timestep
;
271 void Engine::setMaxFrameRate(long maxFps
)
273 impl_
->drawRate
= 1.0 / Scalar(maxFps
);
276 long Engine::getMaxFrameRate()
278 return long(1.0 / impl_
->drawRate
);
282 Video
& Engine::getVideo()
284 return *impl_
->video
;
287 long Engine::getFrameRate()
293 void Engine::update(Scalar t
, Scalar dt
) {}
294 void Engine::draw(Scalar alpha
) {}
295 void Engine::handleEvent(const Event
& event
) {}
300 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.048299 seconds and 4 git commands to generate.