-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
word.rkt
216 lines (197 loc) · 5.96 KB
/
word.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#lang racket/base
(require racket/list
racket/match
racket/contract/base
racket/flonum
racket/math
racket/format
racket/generic
lux/chaos)
(define-generics word
(word-fps word)
(word-label word frame-time)
(word-evt word)
(word-event word evt)
(word-tick word)
(word-output word)
(word-return word)
#:fallbacks
[(define (word-fps w)
60.0)
(define (word-label w frame-time)
(lux-standard-label "Lux" frame-time))
(define (word-evt w)
never-evt)
(define (word-event w e) w)
(define (word-tick w) w)
(define (word-output w) #f)
(define (word-return w) w)])
(define (default b f d) (if b (f b) d))
(struct *word (fps label evt event tick output return)
#:methods gen:word
[(define (word-fps w) (*word-fps w))
(define (word-label w ft)
(define h (*word-label w))
(if (string? h) h (h ft)))
(define (word-evt w) (*word-evt w))
(define (word-event w e)
(define h (*word-event w))
(if h (h e) w))
(define (word-tick w)
(define h (*word-tick w))
(if h (h) w))
(define (word-output w) (*word-output w))
(define (word-return w)
(or (*word-return w) w))])
(define (word [b #f]
#:fps [fps (default b word-fps 60.0)]
#:label [label (if b
(λ (ft) (word-label b ft))
(λ (ft) (lux-standard-label "Lux" ft)))]
#:evt [evt (default b word-evt never-evt)]
#:event [event
(if b
(λ (e) (word-event b e))
#f)]
#:tick [tick
(if b
(λ () (word-tick b))
#f)]
#:output [output (default b word-output #f)]
#:return [return (default b word-return #f)])
(*word fps label evt event tick output return))
(define (lux-standard-label l frame-time)
(define fps (fl/ 1000.0 frame-time))
(~a l
": "
"Frame time: "
(~r frame-time
#:min-width 5
#:precision 1)
"ms; "
"FPS: "
(if (infinite? fps)
"inf"
(~r fps
#:min-width 10
#:precision 2))))
(define current-chaos (make-parameter #f))
(define (call-with-chaos c t)
(chaos-start! c)
(parameterize ([current-chaos c])
(dynamic-wind void t (λ () (chaos-stop! c)))))
(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 (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 (continue-or-word-return next-w old-w k)
(cond
[(not next-w)
(word-return old-w)]
[else
(k next-w)]))
(define (factum-fiat-lux c w)
(define (output&process-input&wait frame-start-time w)
(define pre-output-time (current-inexact-milliseconds))
(chaos-output! c (word-output w))
(define frame-end-time (current-inexact-milliseconds))
(define frame-time (- frame-end-time frame-start-time))
#;(printf "W: ~v\tG: ~v\tT: ~v\n"
(- pre-output-time frame-start-time)
(- frame-end-time pre-output-time)
frame-time)
(define new-label (word-label w frame-time))
(chaos-label! c new-label)
;; Ideally we could compute how much time we have available for GC
;; and just use that so we never have any pauses. That's a very
;; big wish though.
(collect-garbage 'incremental)
(define fps (word-fps w))
(define next-time (compute-next-time frame-start-time #;frame-end-time fps))
(define deadline-evt (alarm-evt next-time))
(define input-enabled? (fl= 0.0 fps))
(define w-evt (word-evt w))
(define c-evt (chaos-event c))
(define w-or-c-evt (choice-evt w-evt c-evt))
(define continue
(λ (next-w)
(output&process-input&wait frame-end-time next-w)))
(define THE-W w)
(define wait-evt
(handle-evt deadline-evt
(λ (_)
(define next-w (word-tick THE-W))
(continue-or-word-return
next-w THE-W
continue))))
(define input-continue
(λ (next-w)
(cond
[input-enabled?
(output&process-input&wait frame-end-time next-w)]
[else
(set! THE-W next-w)
(process-input&wait)])))
(define input-evt
(handle-evt w-or-c-evt
(λ (e)
(define next-w (word-event THE-W e))
(continue-or-word-return
next-w THE-W
input-continue))))
(define both-evt
(choice-evt input-evt wait-evt))
(define timeout-f
(λ () (chaos-yield c both-evt)))
(define (process-input&wait)
(sync/timeout timeout-f input-evt))
(process-input&wait))
(chaos-swap! c (λ () (output&process-input&wait (current-inexact-milliseconds) w))))
(define-syntax-rule (word/rec id . more)
(letrec ([id (word . more)]) id))
(provide
gen:word
word/rec
(contract-out
[word?
(-> any/c word?)]
[word (->* () ((or/c #f word?)
#:fps real?
#:label (or/c string? (-> real? string?))
#:evt evt?
#:event (-> any/c (or/c #f word?))
#:tick (-> (or/c #f word?))
#:output any/c
#:return any/c)
word?)]
[lux-standard-label
(-> string? flonum?
string?)]
[call-with-chaos
(-> chaos? (-> any)
any)]
[fiat-lux
(-> word?
any)]))
(module+ generics
(provide
(contract-out
[word-fps
(-> word? flonum?)]
[word-label
(-> word? flonum? string?)]
[word-evt
(-> word? evt?)]
[word-event
(-> word? any/c word?)]
[word-tick
(-> word? word?)]
[word-output
(-> word? any/c)]
[word-return
(-> word? any/c)])))