#include "Timer.h"

Timer::Timer(int f)
{

     fps = f;
     frame_len = M_SEC / fps;

}

int Timer::delay(void)
{

     delay(frame_len);

}

/* Delay for i milliseconds, returns milliseconds paused. */
int Timer::delay(unsigned long i)
{
     struct timeval timeout;
  
     if( i ) {
	  timeout = ltotv(i);
	  select(0, NULL, NULL, NULL, &timeout);
	  return i;
     }

     return 0;

}


/* Like gettimeofday, but for our integer time representation */
unsigned long Timer::gettime(void)
{
     struct timeval tv;
     
     gettimeofday( &tv, NULL );
     return tvtol(tv);

}


/* Converts a long int to a struct timeval */
struct timeval Timer::ltotv(unsigned long t)
{
    struct timeval tv;

    tv.tv_sec  =  t / M_SEC;
    tv.tv_usec =  (t % M_SEC) * 1000;

    return tv;

}

