Perfectly Accurate Game Timing

These days, I designed a timing system for my game. Doesn’t really sound impressive, eh?

The problem with accurate timing, apart from hardware faults making timers change speed or jump back, is to resample a high-frequency clock running at 3+ MHz to the update rate your game is running with.

The naive approach would be to just cast the clock to a double and divide it by one 60th (if you want 60 Hz updates) of the clock’s frequency. But that’s not really such a good idea. Let me illustrate:

int main() {
  float a = 20000000;
  ++a;
  // a is still 20,000,000. The accuracy of the float has diminished so
  // much that you not only have zero decimal places, it cannot even assume
  // the value 20,000,001 -- only 20,000,002!
  
  double b = 10000000000000000;
  ++b;
  // b is still 10,000,000,000,000,000. Same reason.
}
Read More