Files
forge-steel/src/timer.c
T
klemek 54b166d33f
Clang Build CI / build-release (push) Has been cancelled
Clang Build CI / run-no-video (push) Has been cancelled
Clang Build CI / run-video (push) Has been cancelled
Clang Lint CI / lint-no-video (push) Successful in 1m14s
Clang Lint CI / lint-video (push) Successful in 1m16s
fix: clock_gettime instead of clock
2026-05-17 00:57:59 +02:00

38 lines
784 B
C

#include <bits/time.h>
#include <limits.h>
#include <time.h>
#include "types.h"
#include "timer.h"
void timer_init(Timer *timer, const unsigned int target) {
timer->counter = 0;
timer->target = target;
clock_gettime(CLOCK_REALTIME, &timer->start);
}
bool timer_inc(Timer *timer) {
timer->counter += 1;
return timer->counter == UINT_MAX || timer->counter >= timer->target;
}
double timer_reset(Timer *timer) {
struct timespec stop;
double secs;
double per_secs;
if (clock_gettime(CLOCK_REALTIME, &stop) != 0) {
return 0.0;
}
secs = (double)(stop.tv_sec - timer->start.tv_sec) +
(double)(stop.tv_nsec - timer->start.tv_nsec) / 1e9;
per_secs = (double)timer->counter / secs;
timer->start = stop;
timer->counter = 0;
return per_secs;
}