/*
 * Demo of the gltt graphics library
 * Copyright (C) 1998 Stephane Rehel
 *
 * January 1998
 */

#ifdef __linux__
#define HAVE_USLEEP
#endif

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#if defined(__AIX__) || defined(___AIX) || defined(_AIX) || \
    defined(__QNX__) || defined(___AIXV3) || defined(AIXV3) || \
    defined(_SEQUENT_)
// for select() in delay()
#include <sys/select.h>
#endif

#ifdef __CYGWIN32__
// for select()
#include <sys/socket.h>
#include <windows.h>
#endif

/////////////////////////////////////////////////////////////////////////////

void dsleep( double seconds )
{
  if( seconds <= 0. )
    return;

#ifdef __WINDOWS__
  Sleep( int(seconds * 1000. + .5) );
#else
  #ifdef HAVE_USLEEP
    usleep( int(seconds * 1e6) );
  #else
    struct timeval timer;
    timer.tv_sec= long(seconds);
    timer.tv_usec= long( (seconds-double(timer.tv_sec)) * 1e6 );
    select(0,0,0,0,&timer);
  #endif
#endif
}

/////////////////////////////////////////////////////////////////////////////

static double get_clock()
{
#ifdef __WINDOWS__
  // gettimeofday seems not to be implemented in cygwin32 (cf. _syslist.h)
  // Use: GetTickCount() = number of milliseconds since windows was started
  return double(GetTickCount()) * double(1e-3);
#else
  static struct timeval tv;
  static struct timezone tz;
  ::gettimeofday( &tv, &tz );
  return double(tv.tv_sec) + double(tv.tv_usec)/1e6;
#endif
}

/////////////////////////////////////////////////////////////////////////////

static double time_stamp= 0.;

void delay_begin()
{
  time_stamp= get_clock();
}

/////////////////////////////////////////////////////////////////////////////

void delay_end( double seconds )
{
  double time_stamp2= get_clock();

  double delta= time_stamp2 - time_stamp;

  dsleep( seconds - delta );
}

/////////////////////////////////////////////////////////////////////////////

static double chrono_stamp= 0.;

void chrono_start()
{
  chrono_stamp= get_clock();
}

/////////////////////////////////////////////////////////////////////////////

double chrono_end()
{
  return get_clock() - chrono_stamp;
}

/////////////////////////////////////////////////////////////////////////////
