-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathybot_draw.xtm
380 lines (314 loc) · 13.7 KB
/
ybot_draw.xtm
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
;; lib-loading config
(if *impc:compiler:with-cache* (sys:load "libs/aot-cache/ybot_draw.xtm" 'quiet))
(sys:load-preload-check 'ybot_draw)
(define *xtmlib-ybot_draw-loaded* #t)
(impc:aot:suppress-aot-do
(sys:load "libs/external/glfw3.xtm")
(sys:load "libs/core/math.xtm"))
(impc:aot:insert-forms
(sys:load "libs/external/glfw3.xtm" 'quiet)
(sys:load "libs/core/math.xtm" 'quiet))
(impc:aot:insert-header "xtmybot_draw")
(impc:aot:import-ll "xtmybot_draw")
(bind-val width float 800.)
(bind-val height float 600.)
(bind-val window GLFWwindow*
(begin
(glfwSetErrorCallback (convert (get_native_fptr glfw_error_callback)))
(glfw_init_and_create_interaction_window (convert width) (convert height))))
;; (bind-val window GLFWwindow*
;; (begin
;; (glfwSetErrorCallback (convert (get_native_fptr glfw_error_callback)))
;; (glfw_init_and_create_fullscreen_window 0)))
;; Note that we must only load and intitialise nanovg after creating a window
(sys:load "libs/external/nanovg.xtm")
(nvg_init) ;; init nanovg
(bind-val vg NVGcontext* (nvg_create_context))
(bind-val pixel_ratio float (glfw_get_pixel_ratio window))
;;(bind-val width float (glfw_get_window_width window))
;;(bind-val height float (glfw_get_window_height window))
(bind-val cx float (/ width 2.0:float))
(bind-val cy float (/ height 2.0:float))
(bind-val FRAMERATE float 25.)
(bind-val FRAMEDURATION float (/ 1. FRAMERATE))
(bind-val FRAMEDELTA float (* SAMPLERATE (/ 1. FRAMERATE)))
(bind-func draw_meter
(lambda (i:i64 n:i64 v:float mn:float mx:float fill:NVGcolor* stroke:NVGcolor*)
(let* ((h:float (/ height (i64tof n)))
(d:float (if (> h 40.) 20. (/ h 2.)))
(r:float (/ d 2.))
(x:float (* width 0.1))
(w:float (* width 0.8))
(t:float (+ x (* w (/ (- v mn) (- mx mn)))))
(y:float (+ (* height 0.1) (* (* height 0.8) (/ (i64tof i) (i64tof n))))))
(nvgBeginPath vg)
(tfill! fill 1.0 1.0 0.0 1.0)
(nvgRoundedRect vg x y w d r)
(nvgFillColor vg fill)
(nvgFill vg)
(tfill! stroke 0.0 0.0 1.0 1.0)
(nvgStrokeColor vg stroke)
(nvgStroke vg)
(nvgBeginPath vg)
(tfill! fill .0 0.0 1.0 1.0)
;;(nvgCircle vg (+ t r) (+ y r) r)
(nvgRoundedRect vg t y d d r)
(nvgFillColor vg fill)
(nvgFill vg))))
;; (bind-func draw_buffer
;; (lambda (buffer:SAMPLE* frames:i64 channel:i64 fill:NVGcolor* stroke:NVGcolor*)
;; (let* ((mid_x:float (/ width (convert 2.)))
;; (mid_y:float (/ height (convert 2.)))
;; (delta_x:float (/ width (i64tof oscilloscope_buffer_size)))
;; (half_delta_x:float (/ delta_x (convert 2.)))
;; (xn:[SAMPLE,i64]*
;; (lambda (step)
;; (+ half_delta_x (* delta_x (i64tof step)))))
;; (i:i64 1))
;; ;;(nvgRoundedRect vg 0. 0. 100. 100. 5.)
;; (nvgFillColor vg fill)
;; (nvgFill vg)
;; (tfill! stroke 1.0 1.0 1.0 1.0)
;; (nvgBeginPath vg)
;; (nvgMoveTo vg (xn cc) (+ mid_y (* mid_y (pref buffer 0))))
;; (dotimes (i oscilloscope_buffer_size)
;; (nvgLineTo vg (xn (+ i cc)) (+ mid_y (* mid_y (pref buffer (+ i cc))))))
;; (nvgStrokeColor vg stroke)
;; (nvgStroke vg))))
(bind-func _clear_background
(lambda (vg color)
(nvgFillColor vg color)
(nvgBeginPath vg)
(nvgRect vg 0. 0. width height)
(nvgFill vg)
void))
(bind-func clear_background
(lambda (vg color)
(nvgBeginFrame vg (convert width) (convert height) pixel_ratio)
(_clear_background vg color)
(nvgEndFrame vg)
(glfwSwapBuffers window)
(nvgBeginFrame vg (convert width) (convert height) pixel_ratio)
(_clear_background vg color)
(nvgEndFrame vg)
(glfwSwapBuffers window)
void))
(bind-alias curve_t [float,float]*)
(bind-func straight_line:curve_t (lambda (x) x))
(bind-func sine_curve:curve_t (lambda (x) (+ 0.5 (* 0.5 (sin (* PIf (- x 0.5)))))))
(bind-alias aniparam_t [float,i64]*)
(bind-func animate_parameter:[aniparam_t,i64,i64,float,float,curve_t,i64]*
(lambda (start_time duration start_value end_value curve loop_style)
(let ((end_time:i64 (+ start_time duration))
(triggered_next:bool #f))
(cond
((= 0 duration) ;; instant move to end_value
(lambda (time:i64) end_value))
(else
(cond
((= loop_style 1) ;; plain looping (phasor style)
(lambda (time)
(cond
((<= time start_time) start_value)
((>= time end_time)
(set! start_time (+ start_time duration))
(set! end_time (+ start_time duration))
end_value)
(else
(let* ((t:float (/ (i64tof (- time start_time)) (i64tof duration)))
(s (curve t)))
(+ (* t end_value) (* (- 1.0:float t) start_value)))))))
((= loop_style 2) ;; forward-backward looping
(lambda (time)
(cond
((<= time start_time) start_value)
((>= time end_time)
(cond
((< time (+ start_time (* 2 duration)))
(let* ((t:float (/ (i64tof (- time (+ start_time duration))) (i64tof duration)))
(s (curve t)))
(+ (* t end_value) (* (- 1.0:float t) start_value))))
(else
(set! start_time (+ start_time (* 2 duration)))
(set! end_time (+ start_time duration))
end_value)))
(else
(let* ((t:float (/ (i64tof (- time start_time)) (i64tof duration)))
(s (curve t)))
(+ (* t start_value) (* (- 1.0:float t) end_value)))))))
(else ;; Don't loop
(lambda (time)
(cond
((<= time start_time) start_value)
((>= time end_time) end_value)
(else
(let* ((t:float (/ (i64tof (- time start_time)) (i64tof duration)))
(s (curve t)))
(+ (* t start_value) (* (- 1.0:float t) end_value)))))))))))))
;; (bind-val g1 float*)
;; ($ (printf "%2.2f\n" g1))
;; (bind-func draw_frame
;; (let ((bgc (NVGcolor 0.0 0.0 0.0 0.1)))
;; (lambda ()
;; (nvgBeginFrame vg (convert width) (convert height) pixel_ratio)
;; (nvgResetTransform vg)
;; ( _clear_background vg bgc) ;; Fade to black background
;; (nvgBeginPath vg)
;; (draw_meter 1 1 (pref g1 0) 0.0 1024.0 (NVGcolor_hex #xFFFFFF) (NVGcolor_hex #xFF00FF))
;; ;;(nvgCircle vg (x (now)) (y (now)) (radius (now)))
;; ;;(nvgStrokeColor vg (NVGcolor 1.0 0.0 0.0 0.5))
;; ;;(nvgStrokeWidth vg 1.0)
;; ;(nvgStroke vg)
;; (nvgEndFrame vg))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;; Crazy image generators #1 ;;;;;;;;;;
;; animate_parameter arguments
;; start_time duration start_value end_value curve loop_style)
(bind-func draw_frame:[void]*
(let ((bgc (NVGcolor 1.0 1.0 1.0 0.01))
(rd:aniparam_t (animate_parameter (now) 12345 0.0:float 0.5:float sine_curve 2))
(gr:aniparam_t (animate_parameter (now) 23456 0.0:float 0.5:float sine_curve 2))
(bl:aniparam_t (animate_parameter (now) 34567 0.0:float 0.5:float sine_curve 2))
(toggle:bool #f))
(lambda ()
(nvgBeginFrame vg (convert width) (convert height) pixel_ratio)
;;(nvgResetTransform vg)
;;( _clear_background vg bgc)
(nvgBeginPath vg)
(let ((fgc (NVGcolor (rd (now)) (gr (now)) (bl (now)) 0.5))
(x:float (* width (dtof (random))))
(y:float (* height (dtof (random))))
(d:float (* height 0.15:float (dtof (random)))))
(cond
(toggle
(nvgRect vg x 0.0:float d height)
(nvgCircle vg x y d))
(else
(nvgRect vg 0.0:float y width d)
(nvgCircle vg x y d)))
(set! toggle (not toggle))
(nvgFillColor vg fgc)
(nvgFill vg))
(nvgEndFrame vg))))
;; Ripple model
(bind-val one_sec i64 44100)
(bind-val one_secf float 44100.0:float)
(bind-val sos float 34000.0)
(bind-val slow_down i64 1000)
(bind-val ww float (/ height 16.0))
(bind-func time2samples:[i64,float]*
(lambda (t)
(ftoi64 (* t 44100.0))))
(bind-func samples2time:[float,i64]*
(lambda (n)
(/ (i64tof n) 44100.0:float)))
(bind-func time2cm:[float,float]*
(lambda (t:float)
(* t sos)))
(bind-func cm2time:[float,float]*
(lambda (d:float)
(/ d sos)))
(bind-func cm2samples:[i64,float]*
(lambda (d:float)
(ftoi64 (* one_secf (cm2time d)))))
(bind-func samples2cm:[float,i64]*
(lambda (n:i64)
(time2cm (samples2time n))))
(bind-func cm2pixels:[float,float]*
(lambda (d:float)
(* (/ d 4.71) ww)))
(bind-func pixels2cm:[float,float]*
(lambda (p:float)
(* (/ p ww) 4.71)))
(bind-func pixels2samples:[i64,float]*
(lambda (p)
(cm2samples (pixels2cm p))))
(bind-func samples2pixels:[float,i64]*
(lambda (n:i64)
(cm2pixels (samples2cm n))))
(bind-func draw_frame:[void]*
(let* ((bgc (NVGcolor 0.0 0.0 0.0 0.1)) (slow_down:i64 1000)
(ww:float (/ height 16.0))
(d1:float 0.0) (d8:float 0.0)
(d2:float (* ww (/ 2.46 4.71))) (d7:float (* ww (/ 2.46 4.71)))
(d3:float (* ww (/ 9.83 4.71))) (d6:float (* ww (/ 9.83 4.71)))
(d4:float (* ww (/ 4.91 4.71))) (d5:float (* ww (/ 4.91 4.71)))
(x1:float 0.0) (y1:float (* ww 7.0)) (delay1:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d1)))))
(x2:float 0.0) (y2:float (* ww 7.5)) (delay2:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d2)))))
(x3:float 0.0) (y3:float (* ww 8.0)) (delay3:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d3)))))
(x4:float 0.0) (y4:float (* ww 8.5)) (delay4:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d4)))))
(x5:float 0.0) (y5:float (* ww 9.0)) (delay5:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d5)))))
(x6:float 0.0) (y6:float (* ww 9.5)) (delay6:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d6)))))
(x7:float 0.0) (y7:float (* ww 10.0)) (delay7:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d7)))))
(x8:float 0.0) (y8:float (* ww 10.5)) (delay8:i64 (ftoi64 (* one_secf (distance2time (* 2.0:float d8)))))
(r1:aniparam_t (animate_parameter (+ (now) (* slow_down delay1)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r2:aniparam_t (animate_parameter (+ (now) (* slow_down delay2)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r3:aniparam_t (animate_parameter (+ (now) (* slow_down delay3)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r4:aniparam_t (animate_parameter (+ (now) (* slow_down delay4)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r5:aniparam_t (animate_parameter (+ (now) (* slow_down delay5)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r6:aniparam_t (animate_parameter (+ (now) (* slow_down delay6)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r7:aniparam_t (animate_parameter (+ (now) (* slow_down delay7)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1))
(r8:aniparam_t (animate_parameter (+ (now) (* slow_down delay8)) (* slow_down (distance2samples width)) 0.0:float width straight_line 1)))
(lambda ()
(nvgBeginFrame vg (convert width) (convert height) pixel_ratio)
;;(nvgResetTransform vg)
( _clear_background vg bgc)
(nvgBeginPath vg)
(let ((fgc (NVGcolor 1.0 1.0 1.0 0.5)))
(nvgCircle vg x1 y1 (r1 (now)))
(nvgCircle vg x2 y2 (r2 (now)))
(nvgCircle vg x3 y3 (r3 (now)))
(nvgCircle vg x4 y4 (r4 (now)))
(nvgCircle vg x5 y5 (r5 (now)))
(nvgCircle vg x6 y6 (r6 (now)))
(nvgCircle vg x7 y7 (r7 (now)))
(nvgCircle vg x8 y8 (r8 (now)))
(nvgStrokeColor vg fgc)
(nvgStroke vg))
(nvgEndFrame vg))))
(draw_frame.delay3)
;;;;;;;;;;;; Crazy image generators #1 ;;;;;;;;;;
;; animate_parameter arguments
;; start_time duration start_value end_value curve loop_style)
(bind-func draw_frame:[void]*
(let ((bgc (NVGcolor 1.0 1.0 1.0 0.01))
(rd:aniparam_t (animate_parameter (now) 12345 0.0:float 0.5:float sine_curve 2))
(gr:aniparam_t (animate_parameter (now) 23456 0.0:float 0.5:float sine_curve 2))
(bl:aniparam_t (animate_parameter (now) 34567 0.0:float 0.5:float sine_curve 2))
(toggle:bool #f))
(lambda ()
(nvgBeginFrame vg (convert width) (convert height) pixel_ratio)
;;(nvgResetTransform vg)
;;( _clear_background vg bgc)
(nvgBeginPath vg)
(let ((fgc (NVGcolor (rd (now)) (gr (now)) (bl (now)) 0.5))
(x:float (* width (dtof (random))))
(y:float (* height (dtof (random))))
(d:float (* height 0.15:float (dtof (random)))))
(cond
(toggle
(nvgRect vg x 0.0:float d height)
(nvgCircle vg x y d))
(else
(nvgRect vg 0.0:float y width d)
(nvgCircle vg x y d)))
(set! toggle (not toggle))
(nvgFillColor vg fgc)
(nvgFill vg))
(nvgEndFrame vg))))
(bind-func nvg_draw_loop
(lambda (time:i64 delta:float)
;;(nvg_clear)
(draw_frame)
(glfwPollEvents)
(glfwSwapBuffers window)
(let ((next_time (+ time (convert delta))))
(callback next_time nvg_draw_loop next_time delta))))
($ (clear_background vg (NVGcolor 0.0 0.0 0.0 1.0)))
($ (nvg_draw_loop (now) FRAMEDELTA))
($ (clear_background vg (NVGcolor 0.0 0.0 0.0 0.0)))
;; to stop the draw loop, eval this version of nvg_draw_loop
(bind-func nvg_draw_loop
(lambda (time:i64 delta:float)
(println "nvg_draw_loop callback stopped")
void))