(Feat): Initial Commit.
This commit is contained in:
43
src/timing.c
Normal file
43
src/timing.c
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @file timing.c
|
||||
* @brief High-precision timing utilities implementation
|
||||
* @author ASCII3D Project
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include "timing.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
double timing_get_seconds(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec / 1.0e9;
|
||||
}
|
||||
|
||||
void timing_sleep_us(unsigned int microseconds)
|
||||
{
|
||||
struct timespec req;
|
||||
req.tv_sec = microseconds / 1000000;
|
||||
req.tv_nsec = (microseconds % 1000000) * 1000L;
|
||||
nanosleep(&req, NULL);
|
||||
}
|
||||
|
||||
void timing_limit_fps(double frame_start_time, int target_fps)
|
||||
{
|
||||
if (target_fps <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double target_frame_time = 1.0 / (double)target_fps;
|
||||
double elapsed = timing_get_seconds() - frame_start_time;
|
||||
double sleep_time = target_frame_time - elapsed;
|
||||
|
||||
if (sleep_time > 0.0) {
|
||||
unsigned int sleep_us = (unsigned int)(sleep_time * 1.0e6);
|
||||
timing_sleep_us(sleep_us);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user