dbf-halloween2015
annotate src/timer.cc @ 4:4316c0c879e9
fixed RUN script for macosx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 06:18:18 +0200 |
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 } |
nuclear@0 | 72 |
nuclear@0 | 73 |
nuclear@0 | 74 Timer::Timer() |
nuclear@0 | 75 { |
nuclear@0 | 76 reset(); |
nuclear@0 | 77 } |
nuclear@0 | 78 |
nuclear@0 | 79 void Timer::reset() |
nuclear@0 | 80 { |
nuclear@0 | 81 pause_time = 0; |
nuclear@0 | 82 start_time = get_time_msec(); |
nuclear@0 | 83 } |
nuclear@0 | 84 |
nuclear@0 | 85 void Timer::start() |
nuclear@0 | 86 { |
nuclear@0 | 87 if(!is_running()) { |
nuclear@0 | 88 // resuming |
nuclear@0 | 89 start_time += get_time_msec() - pause_time; |
nuclear@0 | 90 pause_time = 0; |
nuclear@0 | 91 } |
nuclear@0 | 92 } |
nuclear@0 | 93 |
nuclear@0 | 94 void Timer::stop() |
nuclear@0 | 95 { |
nuclear@0 | 96 if(is_running()) { |
nuclear@0 | 97 pause_time = get_time_msec(); |
nuclear@0 | 98 } |
nuclear@0 | 99 } |
nuclear@0 | 100 |
nuclear@0 | 101 bool Timer::is_running() const |
nuclear@0 | 102 { |
nuclear@0 | 103 return pause_time == 0; |
nuclear@0 | 104 } |
nuclear@0 | 105 |
nuclear@0 | 106 unsigned long Timer::get_msec() const |
nuclear@0 | 107 { |
nuclear@0 | 108 if(!is_running()) { |
nuclear@0 | 109 // in paused state... |
nuclear@0 | 110 return pause_time - start_time; |
nuclear@0 | 111 } |
nuclear@0 | 112 return get_time_msec() - start_time; |
nuclear@0 | 113 } |
nuclear@0 | 114 |
nuclear@0 | 115 double Timer::get_sec() const |
nuclear@0 | 116 { |
nuclear@0 | 117 return (double)get_msec() / 1000.0; |
nuclear@0 | 118 } |