Time calculation in the main game loop.

Page 1 of 1

3 Replies - 1229 Views - Last Post: 26 August 2020 - 01:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=419982&amp;s=87862409af86ceee207e64def600b5ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Derek911

Reputation: 0
• 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;
```

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

Reputation: 5880
• 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.

#3 modi123_1

• Suitor #2

Reputation: 15894
• 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.

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

Also seen:
https://stackoverflo...-main-game-loop

#4 Skydiver

• Code herder

Reputation: 7530
• 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).