mirror of
https://github.com/Harshit-Dhanwalkar/AsciiCam.git
synced 2026-06-24 11:08:06 +02:00
Organise project
This commit is contained in:
parent
c766d23028
commit
02926a2623
13 changed files with 398 additions and 288 deletions
22
src/timing.c
Normal file
22
src/timing.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "timing.h"
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
static long frame_duration_ns = 0; // nanoseconds per frame
|
||||
|
||||
void timing_init(int fps) {
|
||||
frame_duration_ns = 1000000000L / fps;
|
||||
}
|
||||
|
||||
void timing_sleep(struct timespec *start_time) {
|
||||
struct timespec end_time;
|
||||
clock_gettime(CLOCK_MONOTONIC, &end_time);
|
||||
|
||||
long elapsed_ns = (end_time.tv_sec - start_time->tv_sec) * 1000000000L +
|
||||
(end_time.tv_nsec - start_time->tv_nsec);
|
||||
long sleep_ns = frame_duration_ns - elapsed_ns;
|
||||
if (sleep_ns > 0) {
|
||||
struct timespec ts = { sleep_ns / 1000000000L, sleep_ns % 1000000000L };
|
||||
while (nanosleep(&ts, &ts) == -1 && errno == EINTR);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue