3
\$\begingroup\$

If you check Quake 1 source code (sys_linux.c), you can see something like this:

double          time, oldtime, newtime;

double Sys_FloatTime (void)
{
  struct timeval tp;
  struct timezone tzp; 
  static int      secbase; // new epoch

  gettimeofday(&tp, &tzp);  

  if (!secbase)
  {
    secbase = tp.tv_sec;
    return tp.tv_usec/1000000.0;
  }

  return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
}

while (1)
{
  // find time spent rendering last frame
  newtime = Sys_FloatTime ();
  time = newtime - oldtime;

  if (time > sys_ticrate.value*2) // 50 ms * 2
    oldtime = newtime;
  else
    oldtime += time;

  Host_Frame (time);
}

Why does Quake do = some time and += at other times? I suppose it must be some floating point issue, but Sys_FloatTime() returns a double, which is very accurate. It is also adjusted to return seconds from the start of the program, it doesn't count from 1/1/1970. Changing the code to use assignment at all times, doesn't seem to change anything.

EDIT: If you check QW source code, you will see, that Sys_FloatTime() is renamed to Sys_DoubleTime(). But even if a float was returned, why would using += vs = make any difference? IMO, ever using a float for something like this was wrong, plain and simple.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'm not sure if there's something I'm missing, but it looks like both lines oldtime = newtime; and oldtime += time; do the same thing. I doubt this is to manipulate float calculations, as in the worst case your result might be 0.0001 milliseconds different, which makes absolutely no difference when you are calculating frames. Are newtime and oldtime defined as double as well? \$\endgroup\$ Commented Dec 5, 2019 at 10:33
  • \$\begingroup\$ It's not the same thing. newtime is the current time. Where time is the elapsed time. So for some reason when elapsed time is greater than sys_ticrate.value * 2, whatever this is (linux specific?) they're not adding elapsed time for the next iteration. Maybe it has something to do with the hardware used at that time? \$\endgroup\$ Commented Dec 5, 2019 at 10:37
  • \$\begingroup\$ @user743414 From what I understand, newtime is current time of the frame (from the start of the application). oldtime is the same, but of the previous frame. time is the difference between them. So oldtime + time == newtime no? So the statements oldtime = newtime is the same as oldtime = oldtime +time. Please correct me if I'm wrong, as I find this topic very interesting. \$\endgroup\$ Commented Dec 5, 2019 at 10:44
  • \$\begingroup\$ @TomTsagk It would be absolutely the same, were it not for floating-point arithmetics. If you want to find the code online google for Sys_FloatTime. It looks like an unsuccessful attempt to use floats to me, where they don't belong. Both are double yes, added to the code. \$\endgroup\$ Commented Dec 5, 2019 at 10:50

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.