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