Game loop and time measurement.
-
There is the function `Sys_Milliseconds`:
int curtime;
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; }
Used in time calculation in the Quake 2 main game loop implementation:
//...
oldtime = Sys_Milliseconds();
//...
while (1) {
//...while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) Com\_Quit(); sys\_msg\_time = msg.time; TranslateMessage(&msg); DispatchMessage(&msg); } do { newtime = Sys\_Milliseconds(); time = newtime - oldtime; } while (time < 1); //... }
I'm wondering about the usage of the `Sys_Milliseconds` at all. What's the point in `base = timeGetTime() & 0xffff0000` line? Why are they applying the 0xffff0000 mask on the retrieved time? Why not just use the `timeGetTime` function directly:
//...
oldtime = timeGetTime();
//...
while (1) {
//...while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) Com\_Quit(); sys\_msg\_time = msg.time; TranslateMessage(&msg); DispatchMessage(&msg); } do { newtime = timeGetTime(); time = newtime - oldtime; } while (time < 1); //... }
-
There is the function `Sys_Milliseconds`:
int curtime;
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; }
Used in time calculation in the Quake 2 main game loop implementation:
//...
oldtime = Sys_Milliseconds();
//...
while (1) {
//...while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) Com\_Quit(); sys\_msg\_time = msg.time; TranslateMessage(&msg); DispatchMessage(&msg); } do { newtime = Sys\_Milliseconds(); time = newtime - oldtime; } while (time < 1); //... }
I'm wondering about the usage of the `Sys_Milliseconds` at all. What's the point in `base = timeGetTime() & 0xffff0000` line? Why are they applying the 0xffff0000 mask on the retrieved time? Why not just use the `timeGetTime` function directly:
//...
oldtime = timeGetTime();
//...
while (1) {
//...while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) Com\_Quit(); sys\_msg\_time = msg.time; TranslateMessage(&msg); DispatchMessage(&msg); } do { newtime = timeGetTime(); time = newtime - oldtime; } while (time < 1); //... }
Member 15212768 wrote:
What's the point in `base = timeGetTime() & 0xffff0000` line?
To avoid being too close to rollover point. From "timeGetTime function | Microsoft Docs"[^]:
Quote:
The return value wraps around to 0 every 2^32 milliseconds, which is about 49.71 days. This can cause problems in code that directly uses the timeGetTime return value in computations, particularly where the value is used to control code execution. You should always use the difference between two timeGetTime return values in computations.
Mircea
-
There is the function `Sys_Milliseconds`:
int curtime;
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; }
Used in time calculation in the Quake 2 main game loop implementation:
//...
oldtime = Sys_Milliseconds();
//...
while (1) {
//...while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) Com\_Quit(); sys\_msg\_time = msg.time; TranslateMessage(&msg); DispatchMessage(&msg); } do { newtime = Sys\_Milliseconds(); time = newtime - oldtime; } while (time < 1); //... }
I'm wondering about the usage of the `Sys_Milliseconds` at all. What's the point in `base = timeGetTime() & 0xffff0000` line? Why are they applying the 0xffff0000 mask on the retrieved time? Why not just use the `timeGetTime` function directly:
//...
oldtime = timeGetTime();
//...
while (1) {
//...while (PeekMessage(&msg, NULL, 0, 0, PM\_NOREMOVE)) { if (!GetMessage(&msg, NULL, 0, 0)) Com\_Quit(); sys\_msg\_time = msg.time; TranslateMessage(&msg); DispatchMessage(&msg); } do { newtime = timeGetTime(); time = newtime - oldtime; } while (time < 1); //... }
Thanks to parallel processing these days you can feed in your game loop a constant value as time variable. (there is no fluctuation between frames as far as time is concerned). You should care about Frame Rate Independent movement only if you`re developing a game meant to hit the store shelf.