/*
 *  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;

  if (argc < 1)
    rb_raise(rb_eArgError,"wrong number of arguments (%d for 1)", argc);
  goal = NUM2INT(argv[0]);
  if(goal < 0)
    goal = 0;

  if(argc > 1 && argv[1] != Qnil)
    accuracy = NUM2INT(argv[1]);
  else
    accuracy = WORST_CLOCK_ACCURACY;

  ticks = accurate_delay(goal,accuracy);

  return INT2NUM(ticks);
}