/*
 *  call-seq:
 *    delay( time, gran=12 )  ->  Integer
 *
 *  time:: how many milliseconds to delay.
 *  gran:: the granularity (in milliseconds) to assume for the system. A
 *         smaller value should use less CPU time, but if it's lower than the
 *         actual system granularity, this function might wait too long. The
 *         default, 12 ms, has a fairly low risk of over-waiting for many
 *         systems.

 *  Use the CPU to more accurately wait for the given period. Returns the
 *  actual delay time, in milliseconds. This function is more accurate than 
 *  #wait, but is also somewhat more CPU-intensive.
 *
 *  The Rubygame timer system will be initialized when you call this function,
 *  if it has not been already.
 *
 */
VALUE rbgm_time_delay(int argc, VALUE *argv, VALUE module)
{
  int ticks, goal, accuracy;
  VALUE vtime, vgran;

  rb_scan_args(argc,argv,"11", &vtime, &vgran);

  goal = NUM2INT(vtime);
  if(goal < 0)
    goal = 0;

  if( RTEST(vgran) )
    accuracy = NUM2INT(vgran);
  else
    accuracy = WORST_CLOCK_ACCURACY;

  ticks = accurate_delay(goal,accuracy);

  return INT2NUM(ticks);
}