istereo2

annotate src/timer.c @ 22:c6971ff4795e

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