mirror of
https://github.com/KartKrewDev/RingRacers.git
synced 2026-04-27 04:21:47 +00:00
Timestamp function for Lua
# Conflicts: # src/lua_baselib.c
This commit is contained in:
parent
d5d8124ab8
commit
9577dc26b9
3 changed files with 16 additions and 4 deletions
|
|
@ -54,7 +54,7 @@ float I_GetTimeFrac(void);
|
||||||
*/
|
*/
|
||||||
precise_t I_GetPreciseTime(void);
|
precise_t I_GetPreciseTime(void);
|
||||||
|
|
||||||
/** \brief Returns the difference between precise times as microseconds.
|
/** \brief Converts a precise_t to microseconds and casts it to a 32 bit integer.
|
||||||
*/
|
*/
|
||||||
int I_PreciseToMicros(precise_t d);
|
int I_PreciseToMicros(precise_t d);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,9 +35,8 @@
|
||||||
#include "d_netcmd.h" // IsPlayerAdmin
|
#include "d_netcmd.h" // IsPlayerAdmin
|
||||||
#include "m_menu.h" // Player Setup menu color stuff
|
#include "m_menu.h" // Player Setup menu color stuff
|
||||||
#include "m_misc.h" // M_MapNumber
|
#include "m_misc.h" // M_MapNumber
|
||||||
|
|
||||||
// SRB2Kart
|
|
||||||
#include "p_spec.h" // P_StartQuake
|
#include "p_spec.h" // P_StartQuake
|
||||||
|
#include "i_system.h" // I_GetPreciseTime, I_PreciseToMicros
|
||||||
|
|
||||||
#include "lua_script.h"
|
#include "lua_script.h"
|
||||||
#include "lua_libs.h"
|
#include "lua_libs.h"
|
||||||
|
|
@ -3843,6 +3842,12 @@ static int lib_kDeclareWeakspot(lua_State *L)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int lib_iGetTimestamp(lua_State *L)
|
||||||
|
{
|
||||||
|
lua_pushinteger(L, I_PreciseToMicros(I_GetPreciseTime()));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static luaL_Reg lib[] = {
|
static luaL_Reg lib[] = {
|
||||||
{"print", lib_print},
|
{"print", lib_print},
|
||||||
{"chatprint", lib_chatprint},
|
{"chatprint", lib_chatprint},
|
||||||
|
|
@ -4076,6 +4081,7 @@ static luaL_Reg lib[] = {
|
||||||
{"G_TicsToSeconds",lib_gTicsToSeconds},
|
{"G_TicsToSeconds",lib_gTicsToSeconds},
|
||||||
{"G_TicsToCentiseconds",lib_gTicsToCentiseconds},
|
{"G_TicsToCentiseconds",lib_gTicsToCentiseconds},
|
||||||
{"G_TicsToMilliseconds",lib_gTicsToMilliseconds},
|
{"G_TicsToMilliseconds",lib_gTicsToMilliseconds},
|
||||||
|
{"I_GetTimestamp",lib_iGetTimestamp},
|
||||||
|
|
||||||
// k_kart
|
// k_kart
|
||||||
{"K_PlayAttackTaunt", lib_kAttackSound},
|
{"K_PlayAttackTaunt", lib_kAttackSound},
|
||||||
|
|
|
||||||
|
|
@ -1671,7 +1671,13 @@ precise_t I_GetPreciseTime(void)
|
||||||
|
|
||||||
int I_PreciseToMicros(precise_t d)
|
int I_PreciseToMicros(precise_t d)
|
||||||
{
|
{
|
||||||
return (int)(d / (timer_frequency / 1000000.0));
|
// d is going to be converted into a double. So remove the highest bits
|
||||||
|
// to avoid loss of precision in the lower bits, for the (probably rare) case
|
||||||
|
// that the higher bits are actually used.
|
||||||
|
d &= ((precise_t)1 << 53) - 1; // The mantissa of a double can handle 53 bits at most.
|
||||||
|
// The resulting double from the calculation is converted first to UINT64 to avoid overflow,
|
||||||
|
// which is undefined behaviour when converting floating point values to integers.
|
||||||
|
return (int)(UINT64)(d / (timer_frequency / 1000000.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue