Hello,

When using GLUT, if you want your display function to be called as
fast as possible by the GLUT main loop, you do this:

    (glutIdleFunc glutPostRedisplay)

But what if you want to call the display-function 'n' times per
second?

I currently have a procedure 'frames-per-second' in this library:

http://github.com/dharmatech/agave/blob/master/glamour/frames-per-second.sls

You use it like so:

    (glutIdleFunc (frames-per-second 30))

In that case, 'glutPostRedisplay' will be called at most 30 times per
second.

The thing is, looking at the 'frames-per-second' library, there surely
is an abstraction there.

One approach to the factor is as follows:

----------------------------------------------------------------------
(import (srfi :19 time))

(define (current-time-in-nanoseconds)
  (let ((val (current-time)))
    (+ (* (time-second val) 1000000000)
       (time-nanosecond val))))

(define-syntax limit-call-rate
  (syntax-rules ()
    ((limit-call-rate calls-per-second (proc param ...))
     (let ((last-call-time 0)
           (nanoseconds-per-call (/ 1e9 calls-per-second)))
       (define (nanoseconds-since-last-call)
         (- (current-time-in-nanoseconds)
            last-call-time))
       (lambda (param ...)
         (if (> (nanoseconds-since-last-call) nanoseconds-per-call)
             (begin
               (set! last-call-time (current-time-in-nanoseconds))
               (proc param ...))))))))
----------------------------------------------------------------------

Let's define a procedure that displays it's three arguments:

(define (display-three a b c)
    (display a)
    (display b)
    (display c)
    (newline))

Make a version of it which will run at most once per second:

(define display-three/limited
  (limit-call-rate 1 (display-three a b c)))

Test it out:

(let loop ()
  (display-three/limited (random 10) (random 10) (random 10))
  (loop))

...

Another example: have GLUT call the display function at most 30 times
per second:

    (glutIdleFunc (limit-call-rate 30 glutPostRedisplay))

Anywho, just wanted to check to see if anyone has something like this
already or suggestions of other approaches.

Ed

PS: I've been wondering if we need an 'r6rs-cafe' list for the general
R6RS community? I'd post design questions such as these there for
example. The r6rs-cafe would have a much narrower scope than
comp.lang.scheme; c.l.s also suffers from cross posting and spam. The
existing r6rs-discuss seems to be for flames. :-)

Reply via email to