summaryrefslogtreecommitdiff
path: root/examples/particles.scm
blob: 4fd7ea3f35bd934f44585bc7c65e9a0277db0eba (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
(use-modules (chickadee)
             (chickadee math rect)
             (chickadee math vector)
             (chickadee render)
             (chickadee render font)
             (chickadee render particles)
             (chickadee render texture)
             (chickadee scripting)
             (ice-9 format)
             ((sdl2) #:select (sdl-ticks)))

(define particles #f)
(define texture #f)
(define start-time (sdl-ticks))
(define avg-frame-time 0)
(define stats-text "")

(define (load)
  (set! *random-state* (random-state-from-platform))
  (set! texture (load-image "images/explosion.png"))
  (set! particles (make-particles 1000
                                  #:texture texture
                                  #:animation-columns 12
                                  #:speed-range (vec2 1.0 10.0)
                                  #:lifetime 50
                                  #:sort 'young))
  (add-particle-emitter particles
                        (make-particle-emitter (make-rect 320.0 240.0 0.0 0.0)
                                               8))
  (script
   (forever
    (sleep 60)
    (set! stats-text (format #f "particles: ~d   fps: ~1,2f"
                             (particles-size particles)
                             (/ 1000.0 avg-frame-time))))))

(define stats-text-pos (vec2 4.0 464.0))
(define (draw alpha)
  (draw-particles particles)
  (draw-text stats-text stats-text-pos)
  (let ((current-time (sdl-ticks)))
    (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)
  (update-particles particles))

(run-game #:load load #:draw draw #:update update)