summaryrefslogtreecommitdiff
path: root/examples/animation.scm
blob: d00a9df3e24c786e0ba8cb16bb22e1a049ef03cf (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
(use-modules (2d animation)
             (2d game-loop)
             (2d helpers)
             (2d sprite)
             (2d texture)
             (2d vector)
             (2d window))

(define window-width 800)
(define window-height 600)

;; Open the window.
(open-window window-width window-height)

(define (key-down key mod unicode)
  (cond ((any-equal? key 'escape 'q)
         (close-window)
         (quit))))

;; Draw our sprite
(define (render)
  (draw-sprite sprite))

;; Register callbacks.
(add-hook! on-render-hook (lambda () (render)))
(add-hook! on-key-down-hook (lambda (key mod unicode) (key-down key mod unicode)))

;; Load a texture, split it into 64x64 tiles, and build an animated
;; sprite out of it.
(define animation
  (let* ((tiles (split-texture (load-texture "images/princess.png") 64 64))
         (frames (vector (vector-ref tiles 19)
                         (vector-ref tiles 20)
                         (vector-ref tiles 21)
                         (vector-ref tiles 22)
                         (vector-ref tiles 23)
                         (vector-ref tiles 24)
                         (vector-ref tiles 25)
                         (vector-ref tiles 26))))
    (make-animation frames 6 #t)))

(define sprite
  (make-sprite animation
               #:position (vector (/ window-width 2)
                                  (/ window-height 2))))


(run-game-loop)