c31ffbf05b4979c6f9b9e60507c5408825bd94d6
[chickadee.git] / examples / sprite-batch.scm
1 (use-modules (chickadee)
2 (chickadee math matrix)
3 (chickadee math rect)
4 (chickadee math vector)
5 (chickadee render)
6 (chickadee render font)
7 (chickadee render sprite)
8 (chickadee render texture)
9 (chickadee scripting)
10 (ice-9 format)
11 (ice-9 match)
12 ((sdl2) #:select (sdl-ticks))
13 (srfi srfi-1)
14 (statprof))
15
16 (define texture #f)
17 (define start-time (sdl-ticks))
18 (define avg-frame-time 16)
19 (define num-sprites 5000)
20 (define sprites
21 (list-tabulate num-sprites
22 (lambda (n)
23 (list (rect (* (random:uniform) 640.0)
24 (* (random:uniform) 480.0)
25 16.0 16.0)
26 (vec2 (* (- (random:uniform) 0.5) 0.5)
27 (* (- (random:uniform) 0.5) 0.5))))))
28 (define matrix (make-identity-matrix4))
29
30 (define (stats-message)
31 (format #f "sprites: ~d fps: ~1,2f"
32 num-sprites
33 (/ 1000.0 avg-frame-time)))
34
35 (define stats-text (stats-message))
36
37 (define (load)
38 (set! *random-state* (random-state-from-platform))
39 (set! texture (load-image "images/shot.png"))
40 (script
41 (forever
42 (sleep 60)
43 (set! stats-text (stats-message)))))
44
45 (define stats-text-pos (vec2 4.0 464.0))
46 (define (draw alpha)
47 (with-batched-sprites
48 (for-each (match-lambda
49 ((r v)
50 (set-rect-x! r (+ (rect-x r) (vec2-x v)))
51 (set-rect-y! r (+ (rect-y r) (vec2-y v)))
52 (draw-sprite* texture r matrix)))
53 sprites))
54 (draw-text stats-text stats-text-pos)
55 (let ((current-time (sdl-ticks)))
56 (set! avg-frame-time
57 (+ (* (- current-time start-time) 0.1)
58 (* avg-frame-time 0.9)))
59 (set! start-time current-time)))
60
61 (define (update dt)
62 (update-agenda 1))
63
64 (gcprof
65 (lambda ()
66 (run-game #:load load #:draw draw #:update update)))