]>
Dogcows Code - chaz/yoink/blob - src/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"
40 #include "settings.hh"
41 #include "dispatcher.hh"
49 class engine::engine_impl
52 engine_impl(const std::string
& name
, int argc
, char* argv
[],
53 const std::string
& configFile
, engine
* outer
) :
57 if (SDL_Init(SDL_INIT_EVERYTHING
| SDL_INIT_EVENTTHREAD
) != 0)
59 throw std::runtime_error(SDL_GetError());
63 throw std::runtime_error(FE_GetError());
68 config
.loadFromFile(configFile
);
70 screen
= video_ptr(new video(name
));
74 config
.get("engine.timestep", ts
);
75 timestep
= scalar(ts
);
78 config
.getNumber("video.maxfps", maxfps
);
79 drawrate
= 1.0 / scalar(maxfps
);
82 config
.get("video.printfps", printfps
);
87 // The video object must be destroyed before we can shutdown SDL.
96 * The main loop. This just calls dispatchEvents(), update(), and draw()
97 * over and over again. The timing of the update and draw are decoupled.
98 * The actual frame rate is also calculated here. This function will return
99 * with a value of 0 if the member variable running becomes true.
104 scalar ticksNow
= ticks();
106 scalar nextStep
= ticksNow
;
107 scalar nextDraw
= ticksNow
;
108 scalar nextFPSUpdate
= ticksNow
+ 1.0;
110 scalar totalTime
= 0.0;
111 scalar deltaTime
= 0.0;
112 scalar accumulator
= timestep
;
120 scalar newTicks
= ticks();
121 deltaTime
= newTicks
- ticksNow
;
124 if (deltaTime
>= 0.25) deltaTime
= 0.25;
125 accumulator
+= deltaTime
;
127 while (accumulator
>= timestep
)
130 interface
->update(totalTime
, timestep
);
132 totalTime
+= timestep
;
133 accumulator
-= timestep
;
135 nextStep
+= timestep
;
138 if (ticksNow
>= nextDraw
)
142 if (ticksNow
>= nextFPSUpdate
) // determine the actual fps
147 nextFPSUpdate
+= 1.0;
148 if (ticksNow
>= nextFPSUpdate
)
150 nextFPSUpdate
= ticksNow
+ 1.0;
155 std::cout
<< "FPS: " << fps
<< std::endl
;
159 interface
->draw(accumulator
/ timestep
);
162 nextDraw
+= drawrate
;
163 if (ticksNow
>= nextDraw
)
165 // we missed some scheduled draws, so reset the schedule
166 nextDraw
= ticksNow
+ drawrate
;
170 // be a good citizen and give back what you don't need
171 sleep(std::min(nextStep
, nextDraw
), true);
179 void dispatchEvents()
183 while (FE_PollEvent(&e
) == 1)
188 if (e
.key
.keysym
.sym
== SDLK_ESCAPE
&&
189 (SDL_GetModState() & KMOD_CTRL
) )
195 case SDL_VIDEORESIZE
:
196 screen
->resize(e
.resize
.w
, e
.resize
.h
);
200 interface
->handleEvent(e
);
221 engine::engine(const std::string
& name
, int argc
, char* argv
[],
222 const std::string
& configFile
) :
223 impl(new engine::engine_impl(name
, argc
, argv
, configFile
, this)) {}
235 impl
->running
= false;
239 void engine::setTimestep(scalar ts
)
244 scalar
engine::getTimestep()
246 return impl
->timestep
;
249 void engine::setMaxFPS(long maxfps
)
251 impl
->drawrate
= 1.0 / scalar(maxfps
);
254 long engine::getMaxFPS()
256 return long(1.0 / impl
->drawrate
);
260 video
& engine::getVideo()
262 return *impl
->screen
;
265 long engine::getFPS()
271 void engine::update(scalar t
, scalar dt
) {}
272 void engine::draw(scalar alpha
) {}
273 void engine::handleEvent(const SDL_Event
& e
) {}
278 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.048372 seconds and 4 git commands to generate.