44 lines
1.1 KiB
C
44 lines
1.1 KiB
C
#include "timer.h"
|
|
|
|
int getTime(struct PTime *time) {
|
|
#if _WIN32 | _WIN64
|
|
int ticks = SDL_GetTicks();
|
|
time->s = ticks/1000;
|
|
time->m = ticks;
|
|
time->n = ticks*1000000;
|
|
#elif __MACH__
|
|
clock_serv_t cclock;
|
|
mach_timespec_t mts;
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
clock_get_time(cclock, &mts);
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
time->s = mts.tv_sec;
|
|
time->m = mts.tv_sec*1000;
|
|
time->n = mts.tv_nsec;
|
|
printf("LOL\n");
|
|
#else
|
|
struct timespec ts;
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
time->s = ts.tv_sec;
|
|
time->m = ts.tv_sec*1000;
|
|
time->n = ts.tv_nsec;
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
void doSleep(long seconds, long milliseconds, long nanoseconds) {
|
|
#if _WIN32 | _WIN64
|
|
SDL_Delay(1000*seconds + milliseconds + (nanoseconds/1000/1000));
|
|
#else
|
|
struct timespec ts;
|
|
ts.tv_sec = seconds;
|
|
ts.tv_nsec = (milliseconds*1000000) + nanoseconds;
|
|
if (ts.tv_nsec < 0) ts.tv_nsec = 0;
|
|
printf("snoozing for %ld\n", ts.tv_nsec);
|
|
/*if (nanosleep(&ts, NULL) < 0)
|
|
printf("ERR: had trouble sleeping\n"); */ // FIXME: THIS ERRORS ALOT
|
|
nanosleep(&ts, NULL);
|
|
#endif
|
|
}
|