mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
write code for a timing
This commit is contained in:
39
src/game/time.c
Normal file
39
src/game/time.c
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "time.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#if __has_include(<features.h>)
|
||||||
|
#include <features.h>
|
||||||
|
#endif
|
||||||
|
#if __has_include(<features.h>) && _POSIX_C_SOURCE >= 199309L
|
||||||
|
#include <bits/time.h>
|
||||||
|
static void gettime(struct timespec* ts) {
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, ts);
|
||||||
|
}
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#include <profileapi.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winnt.h>
|
||||||
|
static void gettime(struct timespec* ts) {
|
||||||
|
LARGE_INTEGER cnt, frq;
|
||||||
|
QueryPerformanceCounter(&cnt);
|
||||||
|
QueryPerformanceFrequency(&frq);
|
||||||
|
ts->tv_sec = (time_t)(cnt.QuadPart / frq.QuadPart);
|
||||||
|
ts->tv_nsec = (time_t)((cnt.QuadPart % frq.QuadPart) * 1000000000 / frq.QuadPart);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#error no implementation of a monotonic clock was available
|
||||||
|
#endif
|
||||||
|
|
||||||
|
time_t time_pull(void) {
|
||||||
|
struct timespec ts;
|
||||||
|
gettime(&ts);
|
||||||
|
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
int time_poll(time_t curr, time_t delta, time_t* restrict proj) {
|
||||||
|
bool tpass = curr >= *proj;
|
||||||
|
*proj += tpass * ((curr + delta) - *proj); // adds 0, or the difference to proj
|
||||||
|
return tpass;
|
||||||
|
}
|
||||||
12
src/game/time.h
Normal file
12
src/game/time.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/* gets the current time in milliseconds */
|
||||||
|
time_t time_pull(void);
|
||||||
|
|
||||||
|
/* Polls the time whether a given timeout has passed, comparing against `curr` as the current time.
|
||||||
|
* if `curr` ≥ `*proj`, `curr` + `delta` is written to `*proj`. `1` is returned.
|
||||||
|
* otherwise, we just return `0`. */
|
||||||
|
__nonnull((3)) int time_poll(time_t curr, time_t delta, time_t* restrict proj);
|
||||||
Reference in New Issue
Block a user