examples: Update particle example.
[chickadee.git] / examples / particles.scm
CommitLineData
86a3254c
DT
1(use-modules (chickadee)
2 (chickadee math rect)
3 (chickadee math vector)
4 (chickadee render)
e2ead85d 5 (chickadee render color)
86a3254c
DT
6 (chickadee render font)
7 (chickadee render particles)
e2ead85d 8 (chickadee render sprite)
86a3254c
DT
9 (chickadee render texture)
10 (chickadee scripting)
11 (ice-9 format)
12 ((sdl2) #:select (sdl-ticks)))
13
e2ead85d
DT
14(define window-width 800)
15(define window-height 600)
86a3254c 16(define particles #f)
e2ead85d
DT
17(define emitter #f)
18(define particle-texture #f)
19(define sprite-texture #f)
86a3254c
DT
20(define start-time (sdl-ticks))
21(define avg-frame-time 0)
22(define stats-text "")
23
e2ead85d
DT
24(define (center-x w)
25 (- (/ window-width 2.0) (/ w 2.0)))
26
27(define (center-y h)
28 (- (/ window-height 2.0) (/ h 2.0)))
29
86a3254c
DT
30(define (load)
31 (set! *random-state* (random-state-from-platform))
e2ead85d
DT
32 (set! particle-texture (load-image "images/explosion.png"))
33 (set! sprite-texture (load-image "images/chickadee.png"))
34 (set! particles (make-particles 2000
35 #:texture particle-texture
36 #:end-color (make-color 1.0 1.0 1.0 0.8)
37 #:end-color (make-color 1.0 1.0 1.0 0.0)
38 #:speed-range (vec2 1.0 5.0)
39 ;;#:acceleration-range (vec2 -0.1 -0.2)
40 #:lifetime 40
86a3254c 41 #:animation-columns 12
86a3254c 42 #:sort 'young))
e2ead85d
DT
43 (set! emitter (make-particle-emitter (make-rect (center-x 0.0)
44 (center-y 0.0)
45 0.0 0.0)
46 8))
47 (add-particle-emitter particles emitter)
86a3254c
DT
48 (script
49 (forever
50 (sleep 60)
51 (set! stats-text (format #f "particles: ~d fps: ~1,2f"
52 (particles-size particles)
53 (/ 1000.0 avg-frame-time))))))
54
e2ead85d
DT
55(define stats-text-pos (vec2 4.0 (- window-height 16.0)))
56(define sprite-position (vec2 (center-x 128.0) (center-y 128.0)))
86a3254c
DT
57(define (draw alpha)
58 (draw-particles particles)
e2ead85d 59 (draw-sprite sprite-texture sprite-position)
86a3254c
DT
60 (draw-text stats-text stats-text-pos)
61 (let ((current-time (sdl-ticks)))
62 (set! avg-frame-time
63 (+ (* (- current-time start-time) 0.1)
64 (* avg-frame-time 0.9)))
65 (set! start-time current-time)))
66
67(define (update dt)
68 (update-agenda 1)
69 (update-particles particles))
70
e2ead85d
DT
71(define (mouse-move x y dx dy buttons)
72 (let ((area (particle-emitter-spawn-area emitter)))
73 (set-rect-x! area x)
74 (set-rect-y! area y)
75 (set-vec2! sprite-position (- x 64.0) (- y 64.0))))
76
77(run-game #:load load
78 #:draw draw
79 #:update update
80 #:mouse-move mouse-move
81 #:window-width window-width
82 #:window-height window-height)