Add particles example.
[chickadee.git] / examples / particles.scm
1 (use-modules (chickadee)
2 (chickadee math rect)
3 (chickadee math vector)
4 (chickadee render)
5 (chickadee render font)
6 (chickadee render particles)
7 (chickadee render texture)
8 (chickadee scripting)
9 (ice-9 format)
10 ((sdl2) #:select (sdl-ticks)))
11
12 (define particles #f)
13 (define texture #f)
14 (define start-time (sdl-ticks))
15 (define avg-frame-time 0)
16 (define stats-text "")
17
18 (define (load)
19 (set! *random-state* (random-state-from-platform))
20 (set! texture (load-image "images/explosion.png"))
21 (set! particles (make-particles 1000
22 #:texture texture
23 #:animation-columns 12
24 #:speed-range (vec2 1.0 10.0)
25 #:lifetime 50
26 #:sort 'young))
27 (add-particle-emitter particles
28 (make-particle-emitter (make-rect 320.0 240.0 0.0 0.0)
29 8))
30 (script
31 (forever
32 (sleep 60)
33 (set! stats-text (format #f "particles: ~d fps: ~1,2f"
34 (particles-size particles)
35 (/ 1000.0 avg-frame-time))))))
36
37 (define stats-text-pos (vec2 4.0 464.0))
38 (define (draw alpha)
39 (draw-particles particles)
40 (draw-text stats-text stats-text-pos)
41 (let ((current-time (sdl-ticks)))
42 (set! avg-frame-time
43 (+ (* (- current-time start-time) 0.1)
44 (* avg-frame-time 0.9)))
45 (set! start-time current-time)))
46
47 (define (update dt)
48 (update-agenda 1)
49 (update-particles particles))
50
51 (run-game #:load load #:draw draw #:update update)