Files
SRB2Kart-Saturn/src/i_time.c
T
Alug b83cceaa68 use floats for calculating ticratescaled
this does not need to be double lel
2025-10-02 16:20:59 +02:00

113 lines
2.9 KiB
C

// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 1998-2000 by DooM Legacy Team.
// Copyright (C) 1999-2022 by Sonic Team Junior.
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file i_time.c
/// \brief Timing for the system layer.
#include "i_time.h"
#include <math.h>
#include "command.h"
#include "doomtype.h"
#include "d_netcmd.h"
#include "m_fixed.h"
#include "i_system.h"
timestate_t g_time;
static CV_PossibleValue_t timescale_cons_t[] = {{FRACUNIT/20, "MIN"}, {20*FRACUNIT, "MAX"}, {0, NULL}};
consvar_t cv_timescale = {"timescale", "1.0", CV_NETVAR|CV_CHEAT|CV_FLOAT, timescale_cons_t, NULL, FRACUNIT, NULL, NULL, 0, 0, NULL};
static precise_t enterprecise, oldenterprecise;
static fixed_t entertic, oldentertics;
static double tictimer;
static double ticratescaled;
// experiment to prevent timing issues
// this returns the global time state accounted with how much time has passed
// since it was last updated
static void I_GetTimeAndFrac(tic_t *outtics, fixed_t *outfrac)
{
double elapsedseconds;
elapsedseconds = (double)(I_GetPreciseTime() - oldenterprecise) / I_GetPrecisePrecision();
double fractional, integral;
fractional = modf((tictimer + elapsedseconds) * ticratescaled, &integral);
if (outtics)
*outtics = g_time.time + (tic_t)integral;
if (outfrac)
*outfrac = DoubleToFixed(fractional);
}
tic_t I_GetTime(void)
{
tic_t tic;
I_GetTimeAndFrac(&tic, NULL);
return tic;
}
fixed_t I_GetTimeFrac(void)
{
fixed_t frac;
I_GetTimeAndFrac(NULL, &frac);
return frac;
}
void I_InitializeTime(void)
{
CV_RegisterVar(&cv_timescale);
// I_StartupTimer is preserved for potential subsystems that need to setup
// timing information for I_GetPreciseTime and sleeping
I_StartupTimer();
g_time.time = 0;
g_time.timefrac = 0;
enterprecise = I_GetPreciseTime();
oldenterprecise = enterprecise;
entertic = 0;
oldentertics = 0;
tictimer = 0.0;
ticratescaled = 1.0;
}
void I_UpdateTime(fixed_t timescale)
{
double elapsedseconds;
tic_t realtics;
// get real tics
ticratescaled = (double)((float)TICRATE * FixedToFloat(timescale));
enterprecise = I_GetPreciseTime();
elapsedseconds = (double)(enterprecise - oldenterprecise) / I_GetPrecisePrecision();
tictimer += elapsedseconds;
while (tictimer > 1.0/ticratescaled)
{
entertic += 1;
tictimer -= 1.0/ticratescaled;
}
realtics = entertic - oldentertics;
oldentertics = entertic;
oldenterprecise = enterprecise;
// Update global time state
g_time.time += realtics;
{
double fractional, integral;
fractional = modf(tictimer * ticratescaled, &integral);
g_time.timefrac = DoubleToFixed(fractional);
}
}