phantasia

Phantasia - 2D SDL3 RPG prototype.
git clone git://git.beep.wimdupont.com/phantasia.git
Log | Files | Refs | README | LICENSE

clock.c (531B)


      1 #include "engine/clock.h"
      2 
      3 int
      4 ph_clock_steps(PhClock *clock, uint64_t elapsed_ns, int hz, int max_steps)
      5 {
      6 	const uint64_t second = 1000000000;
      7 	uint64_t budget;
      8 	int steps;
      9 
     10 	if (hz <= 0 || max_steps <= 0) {
     11 		clock->credit = 0;
     12 		return 0;
     13 	}
     14 	budget = (uint64_t)max_steps * second;
     15 	if (clock->credit >= budget || elapsed_ns > (budget - clock->credit) / (unsigned)hz)
     16 		clock->credit = budget;
     17 	else clock->credit += elapsed_ns * (unsigned)hz;
     18 	steps = (int)(clock->credit / second);
     19 	clock->credit %= second;
     20 	return steps;
     21 }