-
Notifications
You must be signed in to change notification settings - Fork 1
/
ml.rkt
165 lines (144 loc) · 4.88 KB
/
ml.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
#lang racket/base
(require
racket/gui
racket/fixnum
mode-lambda
mode-lambda/static
mode-lambda/text
mode-lambda/shot
(prefix-in gl: mode-lambda/backend/gl)
;(prefix-in soft: mode-lambda/backend/software)
"draw-utils.rkt"
)
(define gl? #t)
(define frames '()) ; list of last few frame times
(define (add-frame-time current-time)
(set! frames (cons current-time (take frames (min 10 (length frames))))))
(define (output-fps)
(define start (list-ref frames (- (length frames) 1)))
(define end (first frames))
(define span (/ (- end start) 1000))
(printf "FPS: ~a\n" (truncate (/ (- (length frames) 1) span))))
(define W 500)
(define H 500)
(define tx 0.0)
(define ty 0.0)
(define tm 1.0)
(define sd (make-sprite-db))
(let ()
(local-require pict)
(add-sprite!/file sd 'one "images/blue-frigate.png")
(add-sprite!/value sd 'white (colorize (filled-rectangle 300 100) "white"))
(add-sprite!/value sd 'red (colorize (filled-rectangle 1 1) "red"))
(add-sprite!/value sd 'blue (colorize (filled-rectangle 1 2) "blue"))
(add-sprite!/value sd 'green (colorize (filled-rectangle 2 2) "green")))
(define textfont (load-font! sd #:size 12.0
#:family 'modern
;#:face "Verdana"
#:weight 'bold
;#:smoothing 'smoothed
;#:hinting 'aligned
))
#;(define textfont (load-font! sd
#:size 14.0
#:face "Triplicate T4c"
#:smoothing 'smoothed
#:family 'modern))
;A
(define csd (compile-sprite-db sd))
(save-csd! csd "csd" #:debug? #t)
(define textr (make-text-renderer textfont csd))
(define textar (make-text-aligned-renderer textfont csd))
(define NLAYERS 20)
(define (layers)
(for/vector ((i NLAYERS))
(layer (/ W 2.0) (/ H 2.0))))
(define render (
#;(if gl? gl:stage-draw/dc soft:stage-draw/dc)
gl:stage-draw/dc csd W H NLAYERS))
(define nsprites 1)
(define scale 1.0)
(define rot 0.0)
;(gl:gl-screenshot! (screenshot-in-dir! "."))
(gl:gl-smoothing? #t)
(define (draw-screen canvas dc)
;(set! nsprites (+ 10 nsprites))
;(time
(define s
(for/list ((i nsprites))
(sprite (+ (* 1 i) 105.0 tx) (+ (* 1 i) 105.0 ty) (sprite-idx csd 'one)
#:m scale #:theta rot #:r 0 #:g 0 #:b 0
#:layer (modulo i NLAYERS)
)))
(define rs (sprite (+ 200.0 tx) (+ 200.0 ty) (sprite-idx csd 'red) #:m scale))
(define bs (sprite (+ 210.0 tx) (+ 210.0 ty) (sprite-idx csd 'blue) #:m scale))
(define gs (sprite (+ 220.0 tx) (+ 220.0 ty) (sprite-idx csd 'green) #:m scale))
(define bg (sprite 250.0 250.0 (sprite-idx csd 'white) #:m scale #:a 1.0))
(define ss (sprite 215.0 215.0 (sprite-idx csd 'one) #:layer 0))
(set! s (append (list #;ss #;bg rs bs gs #;ss) null s))
(define t (textr "IIIII"
(+ 200.0 tx) (+ 200.0 ty) #:layer 1
#:r 255 #:g 255 #:b 255
#:mx tm #:my tm))
(define t2 (textar "IIIII"
(+ 200.0 tx) (+ 210.0 ty) #:layer 1
#:r 255 #:g 255 #:b 255
#:mx tm #:my tm))
(define r (render (layers) '() (append s (list t t2))))
(r (send canvas get-width) (send canvas get-height) dc)
)
(define frame (new frame%
(label "mode-lambda test")
(width W)
(height H)
; use below instead for fullscreen
; (x (- left-inset))
; (y (- top-inset))
; (width screen-w)
; (height screen-h)
; (style '(hide-menu-bar no-caption no-resize-border))
))
(define glconfig (new gl-config%))
(send glconfig set-legacy? #f)
(define my-canvas%
(class canvas%
(super-new)
(define/override (on-char event)
(define kc (send event get-key-code))
(case kc
((escape)
(exit 0))
((#\j)
(set! scale (* 1.1 scale)))
((#\k)
(set! scale (/ scale 1.1)))
((#\r)
(set! rot (+ rot 0.1)))
((#\n)
(set! nsprites (+ nsprites 10)))
((left)
(set! tx (+ tx 0.1)))
((down)
(set! ty (+ ty 0.1)))
((up)
(set! tm (+ tm 0.1))))
(send this refresh))))
(define canvas
(new my-canvas%
(parent frame)
(min-width (inexact->exact (round (/ W 1.0))))
(min-height (inexact->exact (round (/ H 1.0))))
(paint-callback draw-screen)
(gl-config glconfig)
(style '(no-autoclear gl))))
(send frame show #t)
(define (client-loop)
(add-frame-time (current-milliseconds))
(send canvas refresh-now)
(sleep/yield 5)
(flush-output)
(collect-garbage 'incremental)
;(output-fps)
(client-loop))
(add-frame-time (current-milliseconds))
(queue-callback client-loop #f)