labyrinth
diff src/timer.c @ 3:45b91185b298
android port
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 01 May 2015 04:36:50 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/timer.c Fri May 01 04:36:50 2015 +0300 1.3 @@ -0,0 +1,71 @@ 1.4 +#include "timer.h" 1.5 + 1.6 +#if defined(__APPLE__) && !defined(__unix__) 1.7 +#define __unix__ 1.8 +#endif 1.9 + 1.10 +#ifdef __unix__ 1.11 +#include <time.h> 1.12 +#include <unistd.h> 1.13 +#include <sys/time.h> 1.14 + 1.15 +#ifdef CLOCK_MONOTONIC 1.16 +unsigned long get_time_msec(void) 1.17 +{ 1.18 + struct timespec ts; 1.19 + static struct timespec ts0; 1.20 + 1.21 + clock_gettime(CLOCK_MONOTONIC, &ts); 1.22 + if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) { 1.23 + ts0 = ts; 1.24 + return 0; 1.25 + } 1.26 + return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000; 1.27 +} 1.28 +#else /* no fancy POSIX clocks, fallback to good'ol gettimeofday */ 1.29 +unsigned long get_time_msec(void) 1.30 +{ 1.31 + struct timeval tv; 1.32 + static struct timeval tv0; 1.33 + 1.34 + gettimeofday(&tv, 0); 1.35 + if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { 1.36 + tv0 = tv; 1.37 + return 0; 1.38 + } 1.39 + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; 1.40 +} 1.41 +#endif /* !posix clock */ 1.42 + 1.43 +void sleep_msec(unsigned long msec) 1.44 +{ 1.45 + usleep(msec * 1000); 1.46 +} 1.47 +#endif 1.48 + 1.49 +#ifdef WIN32 1.50 +#include <windows.h> 1.51 +#pragma comment(lib, "winmm.lib") 1.52 + 1.53 +unsigned long get_time_msec(void) 1.54 +{ 1.55 + return timeGetTime(); 1.56 +} 1.57 + 1.58 +void sleep_msec(unsigned long msec) 1.59 +{ 1.60 + Sleep(msec); 1.61 +} 1.62 +#endif 1.63 + 1.64 +double get_time_sec(void) 1.65 +{ 1.66 + return get_time_msec() / 1000.0f; 1.67 +} 1.68 + 1.69 +void sleep_sec(double sec) 1.70 +{ 1.71 + if(sec > 0.0f) { 1.72 + sleep_msec(sec * 1000.0f); 1.73 + } 1.74 +}