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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
;;; Chickadee Game Toolkit
;;; Copyright © 2016 David Thompson <davet@gnu.org>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
(define-module (chickadee render sprite)
#:use-module (rnrs bytevectors)
#:use-module (srfi srfi-4)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-11)
#:use-module (chickadee math matrix)
#:use-module (chickadee math rect)
#:use-module (chickadee math vector)
#:use-module (chickadee render)
#:use-module (chickadee render shader)
#:use-module (chickadee render texture)
#:use-module (chickadee render buffer)
#:export (draw-sprite
with-batched-sprites
draw-nine-patch))
(define default-shader
(delay
(strings->shader
"
#version 330
in vec2 position;
in vec2 tex;
out vec2 frag_tex;
uniform mat4 mvp;
void main(void) {
frag_tex = tex;
gl_Position = mvp * vec4(position.xy, 0.0, 1.0);
}
"
"
#version 330
in vec2 frag_tex;
uniform sampler2D color_texture;
void main (void) {
gl_FragColor = texture2D(color_texture, frag_tex);
}
")))
(define draw-sprite-unbatched
(let* ((position-buffer
(delay
(make-streaming-typed-buffer 'vec2 'float 4
#:name "unbatched-sprite-vertices")))
(texcoord-buffer
(delay
(make-streaming-typed-buffer 'vec2 'float 4
#:name "unbatched-sprite-texcoords")))
(index-buffer
(delay
(make-typed-buffer #:name "unbatched-sprite-indices"
#:type 'scalar
#:component-type 'unsigned-int
#:buffer (make-buffer (u32vector 0 3 2 0 2 1)
#:target 'index))))
(vertex-array
(delay
(make-vertex-array #:indices (force index-buffer)
#:attributes
`((0 . ,(force position-buffer))
(1 . ,(force texcoord-buffer))))))
(tmp-matrix (make-null-matrix4))
(mvp (make-null-matrix4))
(position (vec2 0 0)))
(lambda (texture region scale rotation blend-mode shader texture-region)
(with-mapped-typed-buffer (force position-buffer)
(let* ((x1 0)
(y1 0)
(x2 (rect-width region))
(y2 (rect-height region))
(bv (typed-buffer-data (force position-buffer))))
(f32vector-set! bv 0 x1)
(f32vector-set! bv 1 y1)
(f32vector-set! bv 2 x2)
(f32vector-set! bv 3 y1)
(f32vector-set! bv 4 x2)
(f32vector-set! bv 5 y2)
(f32vector-set! bv 6 x1)
(f32vector-set! bv 7 y2)))
(with-mapped-typed-buffer (force texcoord-buffer)
(let ((s1 (rect-left texture-region))
(t1 (rect-bottom texture-region))
(s2 (rect-right texture-region))
(t2 (rect-top texture-region))
(bv (typed-buffer-data (force texcoord-buffer))))
(f32vector-set! bv 0 s1)
(f32vector-set! bv 1 t1)
(f32vector-set! bv 2 s2)
(f32vector-set! bv 3 t1)
(f32vector-set! bv 4 s2)
(f32vector-set! bv 5 t2)
(f32vector-set! bv 6 s1)
(f32vector-set! bv 7 t2)))
(matrix4-identity! mvp)
(when rotation
(matrix4-rotate-z! tmp-matrix rotation)
(matrix4-mult! mvp mvp tmp-matrix))
(when scale
(matrix4-scale! tmp-matrix scale)
(matrix4-mult! mvp mvp tmp-matrix))
(set-vec2-x! position (rect-x region))
(set-vec2-y! position (rect-y region))
(matrix4-translate! tmp-matrix position)
(matrix4-mult! mvp mvp tmp-matrix)
(matrix4-mult! mvp mvp (current-projection))
(with-blend-mode blend-mode
(with-texture 0 texture
(gpu-apply shader (force vertex-array) #:mvp mvp))))))
;;;
;;; Sprite Batch
;;;
(define-record-type <sprite-batch>
(%make-sprite-batch texture blend-mode shader size capacity index-buffer
position-buffer texture-buffer vertex-array)
sprite-batch?
(texture sprite-batch-texture set-sprite-batch-texture!)
(blend-mode sprite-batch-blend-mode set-sprite-batch-blend-mode!)
(shader sprite-batch-shader set-sprite-batch-shader!)
(size sprite-batch-size set-sprite-batch-size!)
(capacity sprite-batch-capacity set-sprite-batch-capacity!)
(index-buffer sprite-batch-index-buffer set-sprite-batch-index-buffer!)
(position-buffer sprite-batch-position-buffer set-sprite-batch-position-buffer!)
(texture-buffer sprite-batch-texture-buffer set-sprite-batch-texture-buffer!)
(vertex-array sprite-batch-vertex-array set-sprite-batch-vertex-array!))
(define (init-sprite-batch batch capacity)
(let* ((index (make-streaming-typed-buffer 'scalar
'unsigned-int
(* capacity 6)
#:target 'index))
(pos (make-streaming-typed-buffer 'vec2 'float (* capacity 4)
#:name "batched-sprite-vertices"))
(tex (make-streaming-typed-buffer 'vec2 'float (* capacity 4)
#:name "batched-sprite-vertices"))
(va (make-vertex-array #:indices index
#:attributes `((0 . ,pos) (1 . ,tex)))))
(set-sprite-batch-capacity! batch capacity)
(set-sprite-batch-index-buffer! batch index)
(set-sprite-batch-position-buffer! batch pos)
(set-sprite-batch-texture-buffer! batch tex)
(set-sprite-batch-vertex-array! batch va)))
(define (make-sprite-batch capacity)
"Make a sprite batch that can hold CAPACITY sprites."
(let ((batch (%make-sprite-batch #f #f #f 0 0 #f #f #f #f)))
(init-sprite-batch batch capacity)
batch))
(define (sprite-batch-full? batch)
(= (sprite-batch-capacity batch) (sprite-batch-size batch)))
(define (double-sprite-batch-size! batch)
(let* ((old-index (sprite-batch-index-buffer batch))
(old-verts (sprite-batch-position-buffer batch))
(old-tex (sprite-batch-texture-buffer batch))
(old-index-data (typed-buffer-data old-index))
(old-verts-data (typed-buffer-data old-verts))
(old-tex-data (typed-buffer-data old-tex)))
(unmap-typed-buffer! old-index)
(unmap-typed-buffer! old-verts)
(unmap-typed-buffer! old-tex)
(init-sprite-batch batch (* (sprite-batch-capacity batch) 2))
(sprite-batch-begin! batch)
(let ((new-index (sprite-batch-index-buffer batch))
(new-verts (sprite-batch-position-buffer batch))
(new-tex (sprite-batch-texture-buffer batch)))
(define (copy from to)
(bytevector-copy! from 0
(typed-buffer-data to) 0
(bytevector-length from)))
(copy old-index-data new-index)
(copy old-verts-data new-verts)
(copy old-tex-data new-tex))))
(define (sprite-batch-reset! batch)
"Reset BATCH to size 0."
(set-sprite-batch-texture! batch #f)
(set-sprite-batch-blend-mode! batch #f)
(set-sprite-batch-shader! batch #f)
(set-sprite-batch-size! batch 0))
(define (sprite-batch-begin! batch)
(map-typed-buffer! (sprite-batch-index-buffer batch))
(map-typed-buffer! (sprite-batch-position-buffer batch))
(map-typed-buffer! (sprite-batch-texture-buffer batch)))
(define (sprite-batch-flush! batch)
"Render the contents of BATCH and clear the cache."
(unless (zero? (sprite-batch-size batch))
(with-blend-mode (sprite-batch-blend-mode batch)
(with-texture 0 (sprite-batch-texture batch)
(unmap-typed-buffer! (sprite-batch-index-buffer batch))
(unmap-typed-buffer! (sprite-batch-position-buffer batch))
(unmap-typed-buffer! (sprite-batch-texture-buffer batch))
(gpu-apply* (sprite-batch-shader batch)
(sprite-batch-vertex-array batch)
(* (sprite-batch-size batch) 6)
#:mvp (current-projection))
(sprite-batch-reset! batch)))))
(define sprite-batch-add!
(let ((tmp-matrix (make-null-matrix4))
(matrix (make-null-matrix4))
(position (vec2 0.0 0.0))
(world1 (vec2 0.0 0.0))
(world2 (vec2 0.0 0.0))
(offset-bv (make-u32vector 1)))
(define (set-offset offset)
(u32vector-set! offset-bv 0 offset))
(define (offset)
(u32vector-ref offset-bv 0))
(lambda (batch texture region scale rotation blend-mode
shader texture-region)
;; Expand the buffers when necessary.
(when (sprite-batch-full? batch)
(double-sprite-batch-size! batch))
;; Flush the batch if any GL state needs changing.
(unless (and (eq? (sprite-batch-texture batch) texture)
(eq? (sprite-batch-blend-mode batch) blend-mode)
(eq? (sprite-batch-shader batch) shader))
(sprite-batch-flush! batch)
(sprite-batch-begin! batch)
(set-sprite-batch-texture! batch texture)
(set-sprite-batch-blend-mode! batch blend-mode)
(set-sprite-batch-shader! batch shader))
(let ((size (sprite-batch-size batch)))
(let* ((indices (typed-buffer-data (sprite-batch-index-buffer batch)))
(vertices (typed-buffer-data (sprite-batch-position-buffer batch)))
(texcoords (typed-buffer-data (sprite-batch-texture-buffer batch)))
(rx (rect-x region))
(ry (rect-y region))
(local-x1 0.0)
(local-y1 0.0)
(local-x2 (rect-width region))
(local-y2 (rect-height region))
(s1 (rect-left texture-region))
(t1 (rect-bottom texture-region))
(s2 (rect-right texture-region))
(t2 (rect-top texture-region)))
(if (or rotation scale)
(begin
(matrix4-identity! matrix)
(when rotation
(matrix4-rotate-z! tmp-matrix rotation)
(matrix4-mult! matrix matrix tmp-matrix))
(when scale
(matrix4-scale! tmp-matrix scale)
(matrix4-mult! matrix matrix tmp-matrix))
(set-vec2-x! position rx)
(set-vec2-y! position ry)
(matrix4-translate! tmp-matrix position)
(matrix4-mult! matrix matrix tmp-matrix)
(set-vec2-x! world1 local-x1)
(set-vec2-y! world1 local-y1)
(set-vec2-x! world2 local-x2)
(set-vec2-y! world2 local-y2)
(transform! matrix world1)
(transform! matrix world2))
(begin
(set-vec2-x! world1 (+ local-x1 rx))
(set-vec2-y! world1 (+ local-y1 ry))
(set-vec2-x! world2 (+ local-x2 rx))
(set-vec2-y! world2 (+ local-y2 ry))))
(let ((world-x1 (vec2-x world1))
(world-y1 (vec2-y world1))
(world-x2 (vec2-x world2))
(world-y2 (vec2-y world2)))
;; Add indices.
(set-offset (* size 4))
(let ((index-vertex-offset (offset)))
(set-offset (* size 6))
(u32vector-set! indices (offset) index-vertex-offset)
(u32vector-set! indices (+ (offset) 1) (+ index-vertex-offset 3))
(u32vector-set! indices (+ (offset) 2) (+ index-vertex-offset 2))
(u32vector-set! indices (+ (offset) 3) index-vertex-offset)
(u32vector-set! indices (+ (offset) 4) (+ index-vertex-offset 2))
(u32vector-set! indices (+ (offset) 5) (+ index-vertex-offset 1)))
;; Add vertices.
(set-offset (* size 8)) ;; 4 vertices, 2 floats per vertex
;; Bottom-left
(f32vector-set! vertices (offset) world-x1)
(f32vector-set! vertices (+ (offset) 1) world-y1)
;; Bottom-right
(f32vector-set! vertices (+ (offset) 2) world-x2)
(f32vector-set! vertices (+ (offset) 3) world-y1)
;; Top-right
(f32vector-set! vertices (+ (offset) 4) world-x2)
(f32vector-set! vertices (+ (offset) 5) world-y2)
;; Top-left
(f32vector-set! vertices (+ (offset) 6) world-x1)
(f32vector-set! vertices (+ (offset) 7) world-y2)
;; Add texture coordinates.
(set-offset (* size 8))
;; Bottom-left
(f32vector-set! texcoords (offset) s1)
(f32vector-set! texcoords (+ (offset) 1) t1)
;; Bottom-right
(f32vector-set! texcoords (+ (offset) 2) s2)
(f32vector-set! texcoords (+ (offset) 3) t1)
;; Top-right
(f32vector-set! texcoords (+ (offset) 4) s2)
(f32vector-set! texcoords (+ (offset) 5) t2)
;; Top-left
(f32vector-set! texcoords (+ (offset) 6) s1)
(f32vector-set! texcoords (+ (offset) 7) t2)
(set-sprite-batch-size! batch (1+ size))))))))
(define *batch?* #f)
(define %batch (delay (make-sprite-batch 256)))
(define (draw-sprite-batched texture region
scale rotation blend-mode shader
texture-region)
(sprite-batch-add! (force %batch) texture region
scale rotation blend-mode shader
texture-region))
(define-syntax-rule (with-batched-sprites body ...)
"Use batched rendering for all draw-sprite calls within BODY."
(if *batch?*
(begin body ...)
(dynamic-wind
(lambda ()
(set! *batch?* #t))
(lambda ()
(sprite-batch-reset! (force %batch))
body ...
(sprite-batch-flush! (force %batch)))
(lambda ()
(set! *batch?* #f)))))
(define texture-gl-size
(@@ (chickadee render texture) texture-gl-size))
(define texture-region-gl-size
(@@ (chickadee render texture) texture-region-gl-size))
(define texture-region-gl-rect
(@@ (chickadee render texture) texture-region-gl-rect))
(define %default-texcoords (make-rect 0.0 0.0 1.0 1.0))
(define draw-sprite
(let ((rect (make-rect 0.0 0.0 0.0 0.0)))
(lambda* (texture region #:key
scale rotation (blend-mode 'alpha)
(texcoords
(if (texture-region? texture)
(texture-region-gl-rect texture)
%default-texcoords))
(shader (force default-shader)))
"Draw TEXTURE over the area defined by the rect REGION. Instead
of a rect, REGION may be a vec2 representing the position of the
sprite, in which case the width and height of the sprite corresponds
to the size of the texture. TEXTURE may be a texture or a texture
region. ROTATION specifies by how many radians the sprite will be
rotated. SCALE specifies the scaling factor. By default, alpha
blending is used but can be changed by setting BLEND-MODE. Finally,
advanced users may pass SHADER to change the way the sprite is
rendered entirely."
(let* ((size (if (texture-region? texture)
(texture-region-gl-size texture)
(texture-gl-size texture)))
(texture (if (texture-region? texture)
(texture-region-texture texture)
texture))
(region (if (rect? region)
region
(begin
(rect-move-vec2! rect region)
(set-rect-width! rect (f32vector-ref size 0))
(set-rect-height! rect (f32vector-ref size 1))
rect))))
(if *batch?*
(draw-sprite-batched texture region
scale rotation blend-mode shader
texcoords)
(draw-sprite-unbatched texture region
scale rotation blend-mode shader
texcoords))))))
;;;
;;; Nine Patches
;;;
(define draw-nine-patch
(let ((rect (make-rect 0.0 0.0 0.0 0.0))
(trect (make-rect 0.0 0.0 0.0 0.0)))
(lambda* (texture region #:key (margin 0)
(top-margin margin) (bottom-margin margin)
(left-margin margin) (right-margin margin)
scale rotation (blend-mode 'alpha)
(shader (force default-shader)))
"Draw a \"nine patch\" sprite. A nine patch sprite renders
TEXTURE as a WIDTH x HEIGHT rectangle whose stretchable areas are
defined by the given margin measurements. The corners are never
stretched, the left and right edges may be stretched vertically, the
top and bottom edges may be stretched horizontally, and the center may
be stretched in both directions. This rendering technique is
particularly well suited for resizable windows and buttons in
graphical user interfaces.
MARGIN specifies the margin size for all sides of the nine patch. To
make margins of differing sizes, the TOP-MARGIN, BOTTOM-MARGIN,
LEFT-MARGIN, and RIGHT-MARGIN arguments may be used."
(let* ((texcoords (if (texture-region? texture)
(texture-region-gl-rect texture)
%default-texcoords))
(texsize (if (texture-region? texture)
(texture-region-gl-size texture)
(texture-gl-size texture)))
(w (rect-width region))
(h (rect-height region))
(border-x1 (rect-left region))
(border-y1 (rect-bottom region))
(border-x2 (rect-right region))
(border-y2 (rect-top region))
(fill-x1 (+ border-x1 left-margin))
(fill-y1 (+ border-y1 bottom-margin))
(fill-x2 (- border-x2 right-margin))
(fill-y2 (- border-y2 top-margin))
(tw (f32vector-ref texsize 0))
(th (f32vector-ref texsize 1))
(border-s1 (rect-left texcoords))
(border-t1 (rect-bottom texcoords))
(border-s2 (rect-right texcoords))
(border-t2 (rect-top texcoords))
(fill-s1 (+ border-s1 (/ left-margin tw)))
(fill-t1 (+ border-t1 (/ bottom-margin th)))
(fill-s2 (- border-s2 (/ right-margin tw)))
(fill-t2 (- border-t2 (/ top-margin th))))
(define (draw-piece x1 y1 x2 y2 s1 t1 s2 t2)
(set-rect-x! rect x1)
(set-rect-y! rect y1)
(set-rect-width! rect (- x2 x1))
(set-rect-height! rect (- y2 y1))
(set-rect-x! trect s1)
(set-rect-y! trect t1)
(set-rect-width! trect (- s2 s1))
(set-rect-height! trect (- t2 t1))
(draw-sprite texture rect
#:texcoords trect
#:scale scale
#:rotation rotation
#:blend-mode blend-mode
#:shader shader))
(with-batched-sprites
;; bottom-left
(draw-piece border-x1 border-y1 fill-x1 fill-y1
border-s1 border-t1 fill-s1 fill-t1)
;; bottom-center
(draw-piece fill-x1 border-y1 fill-x2 fill-y1
fill-s1 border-t1 fill-s2 fill-t1)
;; dbottom-right
(draw-piece fill-x2 border-y1 border-x2 fill-y1
fill-s2 border-t1 border-s2 fill-t1)
;; center-left
(draw-piece border-x1 fill-y1 fill-x1 fill-y2
border-s1 fill-t1 fill-s1 fill-t2)
;; center
(draw-piece fill-x1 fill-y1 fill-x2 fill-y2
fill-s1 fill-t1 fill-s2 fill-t2)
;; center-right
(draw-piece fill-x2 fill-y1 border-x2 fill-y2
fill-s2 fill-t1 border-s2 fill-t2)
;; top-left
(draw-piece border-x1 fill-y2 fill-x1 border-y2
border-s1 fill-t2 fill-s1 border-t2)
;; top-center
(draw-piece fill-x1 fill-y2 fill-x2 border-y2
fill-s1 fill-t2 fill-s2 border-t2)
;; top-right
(draw-piece fill-x2 fill-y2 border-x2 border-y2
fill-s2 fill-t2 border-s2 border-t2))))))
|