3 Replies - 1229 Views - Last Post: 26 August 2020 - 01:48 PM Rate Topic: -----

#1 Derek911   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-August 20

Time calculation in the main game loop.

Posted 26 August 2020 - 02:00 AM

There is the code in the Quake 2 main game loop implementation:

    if (!initialized)
    {	// let base retain 16 bits of effectively random data
    	base = timeGetTime() & 0xffff0000;
    	initialized = true;
    }
    curtime = timeGetTime() - base;


I'm wondering about the line:

Quote

base = timeGetTime() & 0xffff0000
Why are they applying the

Quote

0xffff0000
mask on the retrieved time? Why not to use just:

    if (!initialized)
    {	// let base retain 16 bits of effectively random data
    	initialTime = timeGetTime();
    	initialized = true;
    }
    curtime = timeGetTime() - initialTime;


???
What is the role of that mask?

Is This A Good Question/Topic? 0
  • +

Replies To: Time calculation in the main game loop.

#2 jimblumberg   User is online

  • member icon

Reputation: 5880
  • View blog
  • Posts: 17,867
  • Joined: 25-December 09

Re: Time calculation in the main game loop.

Posted 26 August 2020 - 08:15 AM

Also posted here.
Was This Post Helpful? 2
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15894
  • View blog
  • Posts: 63,609
  • Joined: 12-June 08

Re: Time calculation in the main game loop.

Posted 26 August 2020 - 08:18 AM

Binary 'and' is used with a bitmask. My guess is to normalize what ever time value is popped out with a known bitmask. Given what ever the time is, apply the mask, and have consistently accurate time calculations going forward.

Bit mask:
https://www.learncpp...-and-bit-masks/

Misc reading:
https://stackoverflo...f-ts-0xffff0000
https://www.quora.co...expression-mean


Also seen:
https://stackoverflo...-main-game-loop
https://sourceforge....ead/4212230bf5/
Was This Post Helpful? 2
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: Time calculation in the main game loop.

Posted 26 August 2020 - 01:48 PM

I think more context is needed. That code comes from this:
/*
================
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;
}



The intent of Sys_Milliseconds() is to return the number of milliseconds since Quake has started.

As also noted in one of the responses in the SO cross-post, timeGetTime() returns the number of milliseconds since Windows has started.

In game programming, having to deal with the corner cases tends to make code more complicated, as well as slows down the code since there are more branches in the code. In this case, it looks like the Id developers were trying to get rid of the corner cases when the current time is zero, as well as the case of forcing rendering the first frame to be displayed. The code above forces a non-zero time (unless you hit that one in 2^32 case when the first call to timeGetTime() & 0x0000ffff == 0 and the next call is less than the clock resolution).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1