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