lux/word.rkt

114 lines
2.9 KiB
Racket
Raw Normal View History

2014-11-19 22:48:05 +00:00
#lang racket/base
(require racket/list
racket/match
racket/contract/base
racket/flonum
racket/format
racket/generic
lux/chaos)
(define-generics word
2014-11-21 18:26:01 +00:00
(word-fps word)
2014-11-19 22:48:05 +00:00
(word-label word frame-time)
2014-11-20 15:53:15 +00:00
(word-event word evt)
(word-tick word)
(word-output word)
(word-return word)
2014-11-19 22:48:05 +00:00
#:fallbacks
2014-11-21 18:26:01 +00:00
[(define (word-fps w)
60.0)
(define (word-label w frame-time)
2014-11-19 22:48:05 +00:00
(lux-standard-label "Lux" frame-time))
2014-11-20 15:53:15 +00:00
(define (word-event w e) w)
(define (word-tick w) w)
2014-11-22 18:54:08 +00:00
(define (word-output w) #f)
2014-11-20 15:53:15 +00:00
(define (word-return w) w)])
2014-11-19 22:48:05 +00:00
(define (lux-standard-label l frame-time)
(~a l
": "
"Frame time: "
(~r frame-time
#:min-width 5
#:precision 1)
"ms; "
"FPS: "
(~r (fl/ 1000.0 frame-time)
#:min-width 10
#:precision 2)))
(define current-chaos (make-parameter #f))
(define (call-with-chaos c t)
2014-11-26 22:21:55 +00:00
(chaos-start! c)
2014-11-19 22:48:05 +00:00
(parameterize ([current-chaos c])
2014-11-26 22:21:55 +00:00
(begin0 (t)
(chaos-stop! c))))
2014-11-19 22:48:05 +00:00
(define (fiat-lux w)
(define c (current-chaos))
(unless c
(error 'fiat-lux "Not called within call-with-chaos"))
(factum-fiat-lux c w))
(define (factum-fiat-lux c w)
2014-11-21 20:46:17 +00:00
(define (update-word w f make-next-time)
2014-11-20 21:31:02 +00:00
(define start-time (current-inexact-milliseconds))
(define new-w (f w))
(match new-w
[#f
(word-return w)]
[_
2014-11-21 18:26:01 +00:00
(chaos-output! c (word-output new-w))
2014-11-20 21:31:02 +00:00
(define end-time (current-inexact-milliseconds))
(define frame-time (fl- end-time start-time))
(define new-label
(word-label new-w frame-time))
(chaos-label! c new-label)
2014-11-21 20:46:17 +00:00
(define next-time (make-next-time new-w start-time))
(body next-time new-w)]))
(define (compute-next-time start-time fps)
(define time-incr (fl* (fl/ 1.0 fps) 1000.0))
(define next-time (fl+ start-time time-incr))
next-time)
(define (body next-time w)
2014-11-19 22:48:05 +00:00
(chaos-yield
c
2014-11-20 15:53:15 +00:00
(choice-evt
(handle-evt
(chaos-event c)
(λ (e)
2014-11-20 21:31:02 +00:00
(update-word w
(λ (w)
2014-11-21 20:46:17 +00:00
(word-event w e))
(λ (new-w start-time)
(define old-fps (word-fps w))
(define fps (word-fps new-w))
(if (= old-fps fps)
next-time
(compute-next-time start-time fps))))))
2014-11-20 15:53:15 +00:00
(handle-evt
2014-11-21 20:46:17 +00:00
(alarm-evt next-time)
2014-11-20 15:53:15 +00:00
(λ (_)
2014-11-20 21:31:02 +00:00
(update-word w
2014-11-21 20:46:17 +00:00
word-tick
(λ (new-w start-time)
(define fps (word-fps new-w))
(compute-next-time start-time fps))))))))
(chaos-swap! c (λ () (body 0 w))))
2014-11-19 22:48:05 +00:00
(provide
gen:word
(contract-out
2014-11-22 18:54:08 +00:00
[word?
(-> any/c word?)]
2014-11-19 22:48:05 +00:00
[lux-standard-label
(-> string? flonum?
string?)]
[call-with-chaos
(-> chaos? (-> any)
any)]
[fiat-lux
(-> word?
any)]))