]>
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
.get("engine.maxfps", maxfps
);
79 drawrate
= 1.0 / scalar(maxfps
);
82 config
.get("engine.printfps", printfps
);
87 // The video object must be destroyed before we can shutdown SDL.
97 scalar ticksNow
= ticks();
99 scalar nextStep
= ticksNow
;
100 scalar nextDraw
= ticksNow
;
101 scalar nextFPSUpdate
= ticksNow
+ 1.0;
103 scalar totalTime
= 0.0;
104 scalar accumulator
= timestep
;
114 scalar newTicks
= ticks();
115 accumulator
+= newTicks
- ticksNow
;
118 if (accumulator
>= timestep
)
120 interface
->update(totalTime
, timestep
);
122 totalTime
+= timestep
;
123 accumulator
-= timestep
;
125 nextStep
+= timestep
;
126 if (ticksNow
>= nextStep
)
128 // we missed some scheduled steps, so reset the schedule
129 nextStep
= ticksNow
+ timestep
;
134 if (ticksNow
>= nextDraw
)
138 if (ticksNow
>= nextFPSUpdate
) // determine the actual fps
143 nextFPSUpdate
+= 1.0;
144 if (ticksNow
>= nextFPSUpdate
)
146 nextFPSUpdate
= ticksNow
+ 1.0;
151 std::cout
<< "FPS: " << fps
<< std::endl
;
155 interface
->draw(accumulator
/ timestep
);
158 nextDraw
+= drawrate
;
159 if (ticksNow
>= nextDraw
)
161 // we missed some scheduled draws, so reset the schedule
162 nextDraw
= ticksNow
+ drawrate
;
166 // be a good citizen and give back what you don't need
167 sleep(std::min(nextStep
, nextDraw
), true);
175 void dispatchEvents()
179 while (FE_PollEvent(&e
) == 1)
184 if (e
.key
.keysym
.sym
== SDLK_ESCAPE
&&
185 (SDL_GetModState() & KMOD_CTRL
) )
191 case SDL_VIDEORESIZE
:
192 screen
->resize(e
.resize
.w
, e
.resize
.h
);
196 interface
->handleEvent(e
);
217 engine::engine(const std::string
& name
, int argc
, char* argv
[],
218 const std::string
& configFile
) :
219 impl(new engine::engine_impl(name
, argc
, argv
, configFile
, this)) {}
231 impl
->running
= false;
235 void engine::setTimestep(scalar ts
)
240 scalar
engine::getTimestep()
242 return impl
->timestep
;
245 void engine::setMaxFPS(long maxfps
)
247 impl
->drawrate
= 1.0 / scalar(maxfps
);
250 long engine::getMaxFPS()
252 return long(1.0 / impl
->drawrate
);
256 video
& engine::getVideo()
258 return *impl
->screen
;
261 long engine::getFPS()
267 void engine::update(scalar t
, scalar dt
) {}
268 void engine::draw(scalar alpha
) {}
269 void engine::handleEvent(const SDL_Event
& e
) {}
274 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.044721 seconds and 4 git commands to generate.