dxx-rebirth/common/arch/sdl/timer.cpp

74 lines
1.8 KiB
C++
Raw Normal View History

2014-06-01 17:55:23 +00:00
/*
* This file is part of the DXX-Rebirth project <http://www.dxx-rebirth.com/>.
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
2006-03-20 17:12:09 +00:00
/*
*
* SDL library timer functions
*
*/
2013-06-30 02:22:56 +00:00
#include <SDL.h>
2006-03-20 17:12:09 +00:00
#include "maths.h"
#include "timer.h"
#include "config.h"
#include "multi.h"
2006-03-20 17:12:09 +00:00
static fix64 F64_RunTime = 0;
fix64 timer_update()
{
static bool already_initialized;
static fix64 last_tv;
const fix64 cur_tv = static_cast<fix64>(SDL_GetTicks()) * F1_0 / 1000;
const fix64 prev_tv = last_tv;
fix64 runtime = F64_RunTime;
last_tv = cur_tv;
if (unlikely(!already_initialized))
{
already_initialized = true;
}
else if (likely(prev_tv < cur_tv)) // in case SDL_GetTicks wraps, don't update and have a little hickup
F64_RunTime = (runtime += (cur_tv - prev_tv)); // increment! this value will overflow long after we are all dead... so why bother checking?
return runtime;
}
fix64 timer_query(void)
2006-03-20 17:12:09 +00:00
{
return (F64_RunTime);
2006-03-20 17:12:09 +00:00
}
2015-05-09 17:39:01 +00:00
void timer_delay_ms(unsigned milliseconds)
2006-03-20 17:12:09 +00:00
{
2015-05-09 17:39:01 +00:00
SDL_Delay(milliseconds);
2006-03-20 17:12:09 +00:00
}
// Replacement for timer_delay which considers calc time the program needs between frames (not reentrant)
2015-05-09 17:39:01 +00:00
void timer_delay_bound(const unsigned caller_bound)
{
2015-05-09 17:39:01 +00:00
static uint32_t FrameStart;
2015-05-09 17:39:01 +00:00
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 (;;)
{
2015-05-09 17:39:01 +00:00
const uint32_t tv_now = SDL_GetTicks();
if (multiplayer)
multi_do_frame(); // during long wait, keep packets flowing
2015-05-09 17:39:01 +00:00
if (!vsync)
SDL_Delay(1);
2015-05-09 17:39:01 +00:00
if (unlikely(start > tv_now))
start = tv_now;
if (unlikely(tv_now - start >= bound))
{
FrameStart = tv_now;
break;
}
}
}