Shorten timer_delay2

This commit is contained in:
Kp 2015-05-09 17:39:01 +00:00
parent 9312b0b00f
commit b1992b5e59
2 changed files with 22 additions and 13 deletions

View file

@ -21,6 +21,10 @@ fix64 timer_update();
__attribute_warn_unused_result
fix64 timer_query();
void timer_delay(fix seconds);
void timer_delay2(int fps);
void timer_delay_bound(unsigned bound);
static inline void timer_delay2(int fps)
{
timer_delay_bound(1000u / fps);
}
#endif

View file

@ -47,22 +47,27 @@ void timer_delay(fix seconds)
}
// Replacement for timer_delay which considers calc time the program needs between frames (not reentrant)
void timer_delay2(int fps)
void timer_delay_bound(const unsigned caller_bound)
{
static u_int32_t FrameStart=0;
u_int32_t FrameLoop=0;
static uint32_t FrameStart;
while (FrameLoop < 1000u/(GameCfg.VSync?MAXIMUM_FPS:fps))
uint32_t start = FrameStart;
const auto multiplayer = Game_mode & GM_MULTI;
const auto vsync = GameCfg.VSync;
const auto bound = vsync ? 1000u / MAXIMUM_FPS : caller_bound;
for (;;)
{
u_int32_t tv_now = SDL_GetTicks();
if (Game_mode & GM_MULTI)
const uint32_t tv_now = SDL_GetTicks();
if (multiplayer)
multi_do_frame(); // during long wait, keep packets flowing
if (FrameStart > tv_now)
FrameStart = tv_now;
if (!GameCfg.VSync)
if (!vsync)
SDL_Delay(1);
FrameLoop=tv_now-FrameStart;
if (unlikely(start > tv_now))
start = tv_now;
if (unlikely(tv_now - start >= bound))
{
FrameStart = tv_now;
break;
}
}
FrameStart=SDL_GetTicks();
}