summaryrefslogtreecommitdiff
path: root/examples/sprite-batch.scm
blob: b63179f0b05ff986a5902fd892947f4ab800b3b3 (plain)
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
(use-modules (chickadee)
             (chickadee math matrix)
             (chickadee math rect)
             (chickadee math vector)
             (chickadee graphics color)
             (chickadee graphics font)
             (chickadee graphics sprite)
             (chickadee graphics texture)
             (chickadee scripting)
             (ice-9 format)
             (ice-9 match)
             (srfi srfi-1)
             (statprof))

(define texture #f)
(define batch #f)
(define start-time 0.0)
(define avg-frame-time 16)
(define num-sprites 5000)
(define sprites
  (list-tabulate num-sprites
                 (lambda (n)
                   (list (rect (* (random:uniform) 640.0)
                               (* (random:uniform) 480.0)
                               16.0 16.0)
                         (vec2 (* (- (random:uniform) 0.5) 0.5)
                               (* (- (random:uniform) 0.5) 0.5))))))
(define matrix (make-identity-matrix4))

(define (stats-message)
  (format #f "sprites: ~d   fps: ~1,2f"
          num-sprites
          (/ 1.0 avg-frame-time)))

(define stats-text (stats-message))

(define (load)
  (set! *random-state* (random-state-from-platform))
  (set! texture (load-image "images/shot.png"))
  (set! batch (make-sprite-batch texture #:capacity num-sprites))
  (script
   (forever
    (sleep 60)
    (set! stats-text (pk 'stats (stats-message))))))

(define stats-text-pos (vec2 4.0 464.0))
(define (draw alpha)
  (sprite-batch-clear! batch)
  (for-each (match-lambda
             ((r v)
              (set-rect-x! r (+ (rect-x r) (vec2-x v)))
              (set-rect-y! r (+ (rect-y r) (vec2-y v)))
              (sprite-batch-add* batch r matrix)))
            sprites)
  (draw-sprite-batch batch)
  (draw-text stats-text stats-text-pos #:color black)
  (let ((current-time (elapsed-time)))
    (set! avg-frame-time
          (+ (* (- current-time start-time) 0.1)
             (* avg-frame-time 0.9)))
    (set! start-time current-time)))

(define (update dt)
  (update-agenda 1))

(run-game #:load load #:draw draw #:update update
          #:window-title "sprite batch stress test")