mirror of
https://github.com/yquake2/yquake2remaster.git
synced 2024-11-10 07:12:07 +00:00
Change Windows mainloop to microseconds.
While at it have another look at it's Sys_*seconds() implementations. Also add a Sys_Nanosleep() and use it to throttle the game a litte bit.
This commit is contained in:
parent
0c3c2976cf
commit
d0cb89ff52
3 changed files with 39 additions and 30 deletions
|
@ -41,6 +41,7 @@ main(int argc, char **argv)
|
|||
int verLen, i;
|
||||
long long oldtime, newtime;
|
||||
const char* versionString;
|
||||
struct timespec t = {0, 5000};
|
||||
|
||||
/* register signal handler */
|
||||
registerHandler();
|
||||
|
@ -135,7 +136,7 @@ main(int argc, char **argv)
|
|||
/* The mainloop. The legend. */
|
||||
while (1)
|
||||
{
|
||||
struct timespec t = {0, 5000};
|
||||
// Throttle the game a little bit.
|
||||
nanosleep(&t, NULL);
|
||||
|
||||
newtime = Sys_Microseconds();
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
#include <errno.h>
|
||||
#include <dlfcn.h>
|
||||
#include <dirent.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../../common/header/common.h"
|
||||
#include "../../common/header/glob.h"
|
||||
|
@ -85,21 +86,24 @@ Sys_Microseconds(void)
|
|||
static struct timespec last;
|
||||
struct timespec now;
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
|
||||
if(last.tv_sec == 0)
|
||||
{
|
||||
clock_gettime(CLOCK_REALTIME, &last);
|
||||
clock_gettime(CLOCK_MONOTONIC, &last);
|
||||
return last.tv_nsec / 1000ll;
|
||||
}
|
||||
|
||||
long long sec = now.tv_sec - last.tv_sec;
|
||||
long long nsec = now.tv_nsec - last.tv_nsec;
|
||||
|
||||
if(nsec < 0)
|
||||
{
|
||||
nsec += 1000000000ll; // 1s in ns
|
||||
--sec;
|
||||
}
|
||||
|
||||
curtime = (int)((sec*1000000ll + nsec/1000ll) / 1000ll);
|
||||
return sec*1000000ll + nsec/1000ll;
|
||||
}
|
||||
|
||||
|
|
|
@ -414,28 +414,10 @@ ParseCommandLine(LPSTR lpCmdLine)
|
|||
|
||||
/* ======================================================================= */
|
||||
|
||||
int
|
||||
Sys_Milliseconds(void)
|
||||
{
|
||||
static int base;
|
||||
static qboolean initialized = false;
|
||||
|
||||
if (!initialized)
|
||||
{ /* let base retain 16 bits of effectively random data */
|
||||
base = timeGetTime() & 0xffff0000;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
curtime = timeGetTime() - base;
|
||||
|
||||
return curtime;
|
||||
}
|
||||
|
||||
long long
|
||||
Sys_Microseconds(void)
|
||||
{
|
||||
long long microseconds;
|
||||
long long seconds;
|
||||
static long long uSecbase;
|
||||
|
||||
FILETIME ft;
|
||||
|
@ -457,15 +439,40 @@ Sys_Microseconds(void)
|
|||
uSecbase = microseconds - 1001;
|
||||
}
|
||||
|
||||
curtime = (int)((microseconds - uSecbase) / 1000ll);
|
||||
return microseconds - uSecbase;
|
||||
}
|
||||
|
||||
int
|
||||
Sys_Milliseconds(void)
|
||||
{
|
||||
curtime = (int)(Sys_Microseconds()/1000ll);
|
||||
|
||||
return curtime;
|
||||
}
|
||||
|
||||
void
|
||||
Sys_Sleep(int msec)
|
||||
{
|
||||
Sleep(msec);
|
||||
}
|
||||
|
||||
void Sys_Nanosleep(int nanosec)
|
||||
{
|
||||
HANDLE timer;
|
||||
LARGE_INTEGER li;
|
||||
|
||||
timer = CreateWaitableTimer(NULL, TRUE, NULL);
|
||||
|
||||
// Windows has a max. resolution of 100ns.
|
||||
li.QuadPart = -nanosec / 100;
|
||||
|
||||
SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE);
|
||||
WaitForSingleObject(timer, INFINITE);
|
||||
|
||||
CloseHandle(timer);
|
||||
}
|
||||
|
||||
/* ======================================================================= */
|
||||
|
||||
static qboolean
|
||||
|
@ -774,7 +781,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|||
LPSTR lpCmdLine, int nCmdShow)
|
||||
{
|
||||
MSG msg;
|
||||
int time, oldtime, newtime;
|
||||
long long oldtime, newtime;
|
||||
|
||||
/* Previous instances do not exist in Win32 */
|
||||
if (hPrevInstance)
|
||||
|
@ -846,7 +853,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|||
Qcommon_Init(argc, argv);
|
||||
|
||||
/* Save our time */
|
||||
oldtime = Sys_Milliseconds();
|
||||
oldtime = Sys_Microseconds();
|
||||
|
||||
/* The legendary main loop */
|
||||
while (1)
|
||||
|
@ -869,14 +876,11 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
newtime = Sys_Milliseconds();
|
||||
time = newtime - oldtime;
|
||||
}
|
||||
while (time < 1);
|
||||
// Throttle the game a little bit
|
||||
Sys_Nanosleep(5000);
|
||||
|
||||
Qcommon_Frame(time);
|
||||
newtime = Sys_Microseconds();
|
||||
Qcommon_Frame(newtime - oldtime);
|
||||
oldtime = newtime;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue