goat3dgfx
annotate src/timer.cc @ 31:3ba80928b530
fixed the build
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Mar 2014 04:20:00 +0200 |
parents | 1873dfd13f2d |
children |
rev | line source |
---|---|
nuclear@0 | 1 #include "timer.h" |
nuclear@0 | 2 |
nuclear@15 | 3 using namespace goatgfx; |
nuclear@15 | 4 |
nuclear@0 | 5 #if defined(__APPLE__) && !defined(__unix__) |
nuclear@0 | 6 #define __unix__ |
nuclear@0 | 7 #endif |
nuclear@0 | 8 |
nuclear@15 | 9 namespace goatgfx { |
nuclear@15 | 10 |
nuclear@0 | 11 #ifdef __unix__ |
nuclear@0 | 12 #include <time.h> |
nuclear@0 | 13 #include <unistd.h> |
nuclear@0 | 14 #include <sys/time.h> |
nuclear@0 | 15 |
nuclear@0 | 16 #ifdef CLOCK_MONOTONIC |
nuclear@0 | 17 unsigned long get_time_msec(void) |
nuclear@0 | 18 { |
nuclear@0 | 19 struct timespec ts; |
nuclear@0 | 20 static struct timespec ts0; |
nuclear@0 | 21 |
nuclear@0 | 22 clock_gettime(CLOCK_MONOTONIC, &ts); |
nuclear@0 | 23 if(ts0.tv_sec == 0 && ts0.tv_nsec == 0) { |
nuclear@0 | 24 ts0 = ts; |
nuclear@0 | 25 return 0; |
nuclear@0 | 26 } |
nuclear@0 | 27 return (ts.tv_sec - ts0.tv_sec) * 1000 + (ts.tv_nsec - ts0.tv_nsec) / 1000000; |
nuclear@0 | 28 } |
nuclear@0 | 29 #else /* no fancy POSIX clocks, fallback to good'ol gettimeofday */ |
nuclear@0 | 30 unsigned long get_time_msec(void) |
nuclear@0 | 31 { |
nuclear@0 | 32 struct timeval tv; |
nuclear@0 | 33 static struct timeval tv0; |
nuclear@0 | 34 |
nuclear@0 | 35 gettimeofday(&tv, 0); |
nuclear@0 | 36 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { |
nuclear@0 | 37 tv0 = tv; |
nuclear@0 | 38 return 0; |
nuclear@0 | 39 } |
nuclear@0 | 40 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; |
nuclear@0 | 41 } |
nuclear@0 | 42 #endif /* !posix clock */ |
nuclear@0 | 43 |
nuclear@0 | 44 void sleep_msec(unsigned long msec) |
nuclear@0 | 45 { |
nuclear@0 | 46 usleep(msec * 1000); |
nuclear@0 | 47 } |
nuclear@0 | 48 #endif |
nuclear@0 | 49 |
nuclear@0 | 50 #ifdef WIN32 |
nuclear@0 | 51 #include <windows.h> |
nuclear@0 | 52 #pragma comment(lib, "winmm.lib") |
nuclear@0 | 53 |
nuclear@0 | 54 unsigned long get_time_msec(void) |
nuclear@0 | 55 { |
nuclear@0 | 56 return timeGetTime(); |
nuclear@0 | 57 } |
nuclear@0 | 58 |
nuclear@0 | 59 void sleep_msec(unsigned long msec) |
nuclear@0 | 60 { |
nuclear@0 | 61 Sleep(msec); |
nuclear@0 | 62 } |
nuclear@0 | 63 #endif |
nuclear@0 | 64 |
nuclear@0 | 65 double get_time_sec(void) |
nuclear@0 | 66 { |
nuclear@0 | 67 return get_time_msec() / 1000.0f; |
nuclear@0 | 68 } |
nuclear@0 | 69 |
nuclear@0 | 70 void sleep_sec(double sec) |
nuclear@0 | 71 { |
nuclear@0 | 72 if(sec > 0.0f) { |
nuclear@0 | 73 sleep_msec(sec * 1000.0f); |
nuclear@0 | 74 } |
nuclear@0 | 75 } |
nuclear@0 | 76 |
nuclear@15 | 77 } // namespace goatgfx |
nuclear@15 | 78 |
nuclear@0 | 79 |
nuclear@0 | 80 Timer::Timer() |
nuclear@0 | 81 { |
nuclear@0 | 82 reset(); |
nuclear@0 | 83 } |
nuclear@0 | 84 |
nuclear@0 | 85 void Timer::reset() |
nuclear@0 | 86 { |
nuclear@0 | 87 pause_time = 0; |
nuclear@0 | 88 start_time = get_time_msec(); |
nuclear@0 | 89 } |
nuclear@0 | 90 |
nuclear@0 | 91 void Timer::start() |
nuclear@0 | 92 { |
nuclear@0 | 93 if(!is_running()) { |
nuclear@0 | 94 // resuming |
nuclear@0 | 95 start_time += get_time_msec() - pause_time; |
nuclear@0 | 96 pause_time = 0; |
nuclear@0 | 97 } |
nuclear@0 | 98 } |
nuclear@0 | 99 |
nuclear@0 | 100 void Timer::stop() |
nuclear@0 | 101 { |
nuclear@0 | 102 if(is_running()) { |
nuclear@0 | 103 pause_time = get_time_msec(); |
nuclear@0 | 104 } |
nuclear@0 | 105 } |
nuclear@0 | 106 |
nuclear@0 | 107 bool Timer::is_running() const |
nuclear@0 | 108 { |
nuclear@0 | 109 return pause_time == 0; |
nuclear@0 | 110 } |
nuclear@0 | 111 |
nuclear@0 | 112 unsigned long Timer::get_msec() const |
nuclear@0 | 113 { |
nuclear@0 | 114 if(!is_running()) { |
nuclear@0 | 115 // in paused state... |
nuclear@0 | 116 return pause_time - start_time; |
nuclear@0 | 117 } |
nuclear@0 | 118 return get_time_msec() - start_time; |
nuclear@0 | 119 } |
nuclear@0 | 120 |
nuclear@0 | 121 double Timer::get_sec() const |
nuclear@0 | 122 { |
nuclear@0 | 123 return (double)get_msec() / 1000.0; |
nuclear@0 | 124 } |