Monday, November 20, 2006

timegettime and unix gettimeofday

here is what i used to replace windows timegettime

from advance progamming in the unix environment page 173

the gettimeofday function provides greater resolution (up to
a microsecond) than the time function.

#include

int gettimeofday(struct timevale *restrcited tp, void *restrict tzp);

here is my one off.

Uint32 ticks;
struct timeval now;
gettimeofday(&now, NULL);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
return(ticks);

i also found some helpfull information by looking at SDL library
look at the source file src/timer/unix/SDL_systimer.c

#ifdef HAVE_CLOCK_GETTIME
static struct timespec start;
#else
static struct timeval start;
#error "i do have it"
#endif /* HAVE_CLOCK_GETTIME */


void SDL_StartTicks(void)
{
/* Set first ticks value */
#if HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_MONOTONIC,&start);
error "clock_gettime"
#else
gettimeofday(&start, NULL);
#error "gettimeofday"
#endif
}

Uint32 SDL_GetTicks (void)
{
#if HAVE_CLOCK_GETTIME
Uint32 ticks;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC,&now);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_nsec-start.tv_nsec)/1000000;
return(ticks);
#else
Uint32 ticks;
struct timeval now;
gettimeofday(&now, NULL);
ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
return(ticks);
#endif
}

No comments: