Reputation: 11
I am building a physics engine in C. How to compute time difference with high precision between frames (deltatime) in C ?(I am not using any graphics api)
Upvotes: 1
Views: 214
Reputation: 51
Something like this.
#include <stdio.h>
#include <time.h>
int main()
{
struct timespec t1, t2;
long delta_t = 0;
while(1) {
printf("delta_t = %d nanoseconds\n", delta_t);
clock_gettime(CLOCK_MONOTONIC, &t1);
// do something
clock_gettime(CLOCK_MONOTONIC, &t2);
delta_t = (t2.tv_nsec - t1.tv_nsec);
}
return 0;
}
The example tested with GCC 8.1.0.
Upvotes: 1