labyrinth
annotate src/timer.c @ 7:b557812c45db
generating the android (ant) project stuff from the makefile
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 May 2015 02:46:35 +0300 |
parents | |
children |
rev | line source |
---|---|
nuclear@3 | 1 #include "timer.h" |
nuclear@3 | 2 |
nuclear@3 | 3 #if defined(__APPLE__) && !defined(__unix__) |
nuclear@3 | 4 #define __unix__ |
nuclear@3 | 5 #endif |
nuclear@3 | 6 |
nuclear@3 | 7 #ifdef __unix__ |
nuclear@3 | 8 #include <time.h> |
nuclear@3 | 9 #include <unistd.h> |
nuclear@3 | 10 #include <sys/time.h> |
nuclear@3 | 11 |
nuclear@3 | 12 #ifdef CLOCK_MONOTONIC |
nuclear@3 | 13 unsigned long get_time_msec(void) |
nuclear@3 | 14 { |
nuclear@3 | 15 struct timespec ts; |
nuclear@3 | 16 static struct timespec ts0; |
nuclear@3 | 17 |
nuclear@3 | 18 clock_gettime(CLOCK_MONOTONIC, &ts); |
nuclear@3 | 19 if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) { |
nuclear@3 | 20 ts0 = ts; |
nuclear@3 | 21 return 0; |
nuclear@3 | 22 } |
nuclear@3 | 23 return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000; |
nuclear@3 | 24 } |
nuclear@3 | 25 #else /* no fancy POSIX clocks, fallback to good'ol gettimeofday */ |
nuclear@3 | 26 unsigned long get_time_msec(void) |
nuclear@3 | 27 { |
nuclear@3 | 28 struct timeval tv; |
nuclear@3 | 29 static struct timeval tv0; |
nuclear@3 | 30 |
nuclear@3 | 31 gettimeofday(&tv, 0); |
nuclear@3 | 32 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { |
nuclear@3 | 33 tv0 = tv; |
nuclear@3 | 34 return 0; |
nuclear@3 | 35 } |
nuclear@3 | 36 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; |
nuclear@3 | 37 } |
nuclear@3 | 38 #endif /* !posix clock */ |
nuclear@3 | 39 |
nuclear@3 | 40 void sleep_msec(unsigned long msec) |
nuclear@3 | 41 { |
nuclear@3 | 42 usleep(msec * 1000); |
nuclear@3 | 43 } |
nuclear@3 | 44 #endif |
nuclear@3 | 45 |
nuclear@3 | 46 #ifdef WIN32 |
nuclear@3 | 47 #include <windows.h> |
nuclear@3 | 48 #pragma comment(lib, "winmm.lib") |
nuclear@3 | 49 |
nuclear@3 | 50 unsigned long get_time_msec(void) |
nuclear@3 | 51 { |
nuclear@3 | 52 return timeGetTime(); |
nuclear@3 | 53 } |
nuclear@3 | 54 |
nuclear@3 | 55 void sleep_msec(unsigned long msec) |
nuclear@3 | 56 { |
nuclear@3 | 57 Sleep(msec); |
nuclear@3 | 58 } |
nuclear@3 | 59 #endif |
nuclear@3 | 60 |
nuclear@3 | 61 double get_time_sec(void) |
nuclear@3 | 62 { |
nuclear@3 | 63 return get_time_msec() / 1000.0f; |
nuclear@3 | 64 } |
nuclear@3 | 65 |
nuclear@3 | 66 void sleep_sec(double sec) |
nuclear@3 | 67 { |
nuclear@3 | 68 if(sec > 0.0f) { |
nuclear@3 | 69 sleep_msec(sec * 1000.0f); |
nuclear@3 | 70 } |
nuclear@3 | 71 } |