Add macro GET_TIME_MS() (windows + linux support)

This commit is contained in:
jonny_jr9 2023-12-12 16:51:30 +01:00
parent aa242676e9
commit d8952223f4
3 changed files with 37 additions and 0 deletions

View File

@ -37,6 +37,7 @@ set(SOURCES
src/render.c
src/snake.c
src/map.c
src/common.c
)

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include "config.h"
//===========================
@ -41,3 +42,12 @@
#include <unistd.h>
#define DELAY(ms) usleep((ms) * 1000)
#endif
//===========================
//======= GET_TIME_MS =======
//===========================
// macro to get time in milliseconds
#define GET_TIME_MS() get_current_time()
// defined in common.c due to differences with windows and other systems
uint64_t get_current_time();

26
src/common.c Normal file
View File

@ -0,0 +1,26 @@
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#include "common.h"
//============================
//==== get_current_time() ====
//============================
// Function that returns current time in milliseconds (can be used on windows and other systems)
uint64_t get_current_time() {
#ifdef _WIN32
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart / 10000; // convert 100-nanoseconds to milliseconds
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; // convert microseconds to milliseconds
#endif
}