Improve timer logic

Accurate interval to duration conversion.
This commit is contained in:
Kawe Mazidjatari 2022-08-31 02:15:54 +02:00
parent 45c147087a
commit e1cf700355
3 changed files with 16 additions and 4 deletions

View File

@ -6,6 +6,7 @@
#include "core/stdafx.h" #include "core/stdafx.h"
#include "tier1/cvar.h" #include "tier1/cvar.h"
#include "public/utility/utility.h"
#include "engine/sdk_dll.h" #include "engine/sdk_dll.h"
#ifndef DEDICATED #ifndef DEDICATED
#include "gameui/IBrowser.h" #include "gameui/IBrowser.h"
@ -23,9 +24,7 @@ void CEngineSDK::FixedFrame()
g_pBrowser->Think(); g_pBrowser->Think();
g_pConsole->Think(); g_pConsole->Think();
#endif // !DEDICATED #endif // !DEDICATED
std::this_thread::sleep_for(IntervalToDuration(sdk_fixedframe_tickinterval->GetFloat()));
std::chrono::duration<float> interval{ sdk_fixedframe_tickinterval->GetFloat()};
std::this_thread::sleep_for(interval);
} }
} }

View File

@ -835,4 +835,13 @@ string PrintPercentageEscape(const string& svInput)
} }
} }
return result; return result;
} }
///////////////////////////////////////////////////////////////////////////////
// For obtaining a duration from a certain interval.
std::chrono::nanoseconds IntervalToDuration(const float flInterval)
{
using namespace std::chrono;
using fsec = duration<float>;
return round<nanoseconds>(fsec{ flInterval });
}

View File

@ -68,3 +68,7 @@ void AppendPrintf(char* pBuffer, size_t nBufSize, char const* pFormat, ...);
string PrintPercentageEscape(const string& svInput); string PrintPercentageEscape(const string& svInput);
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Time
std::chrono::nanoseconds IntervalToDuration(const float flInterval);
/////////////////////////////////////////////////////////////////////////////