]>
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"
52 engine_impl(const std::string
& name
, int argc
, char* argv
[],
53 const std::string
& configFile
, engine
* outer
) : config(argc
, argv
),
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 config
.loadFromFile(configFile
);
69 screen
= video_ptr(new video(name
));
73 config
.get("engine.timestep", timestep
);
76 config
.get("engine.maxfps", maxfps
);
77 drawrate
= 1.0 / scalar(maxfps
);
80 config
.get("engine.printfps", printfps
);
85 // The video object must be destroyed before we can shutdown SDL.
95 scalar ticksNow
= ticks();
97 scalar nextStep
= ticksNow
;
98 scalar nextDraw
= ticksNow
;
99 scalar nextFPSUpdate
= ticksNow
+ 1.0;
101 scalar totalTime
= 0.0;
102 scalar accumulator
= 0.0;
105 int frameAccum
= 0.0;
112 scalar newTicks
= ticks();
113 accumulator
+= newTicks
- ticksNow
;
116 if (ticksNow
>= nextStep
)
118 interface
->update(totalTime
, timestep
);
120 totalTime
+= timestep
;
121 accumulator
-= timestep
;
123 nextStep
+= timestep
;
124 if (ticksNow
>= nextStep
) nextStep
= ticksNow
+ timestep
;
127 if (ticksNow
>= nextDraw
)
131 if (ticksNow
>= nextFPSUpdate
)
133 fps
= frameAccum
;// + (ticksNow - nextFPSUpdate) / 1.0;
136 nextFPSUpdate
+= 1.0;
137 if (ticksNow
>= nextFPSUpdate
) nextFPSUpdate
= ticksNow
+ 1.0;
141 std::cout
<< "FPS: " << fps
<< std::endl
;
145 interface
->draw(accumulator
/ timestep
);
148 nextDraw
+= drawrate
;
149 if (ticksNow
>= nextDraw
) nextDraw
= ticksNow
+ drawrate
;
152 sleep(std::min(nextStep
, nextDraw
), true);
158 void dispatchEvents()
162 while (FE_PollEvent(&event
) == 1)
167 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
&&
168 (SDL_GetModState() & KMOD_CTRL
) )
174 case SDL_VIDEORESIZE
:
175 screen
->resize(event
.resize
.w
, event
.resize
.h
);
179 interface
->dispatchEvent(event
);
199 engine::engine(const std::string
& name
, int argc
, char* argv
[],
200 const std::string
& configFile
)
201 : impl(new engine_impl(name
, argc
, argv
, configFile
, this)) {}
213 impl
->running
= false;
217 void engine::setTimestep(scalar ts
)
222 scalar
engine::getTimestep()
224 return impl
->timestep
;
227 void engine::setMaxFPS(long maxfps
)
229 impl
->drawrate
= 1.0 / scalar(maxfps
);
232 long engine::getMaxFPS()
234 return long(1.0 / impl
->drawrate
);
238 video
& engine::getVideo()
240 return *impl
->screen
;
243 long engine::getFPS()
249 void engine::update(scalar t
, scalar dt
) {}
250 void engine::draw(scalar alpha
) {}
251 void engine::dispatchEvent(const SDL_Event
& event
) {}
This page took 0.049923 seconds and 4 git commands to generate.