Add particles example.
authorDavid Thompson <dthompson2@worcester.edu>
Fri, 14 Dec 2018 13:37:05 +0000 (08:37 -0500)
committerDavid Thompson <dthompson2@worcester.edu>
Fri, 14 Dec 2018 13:37:05 +0000 (08:37 -0500)
* examples/images/explosion.png: New file.
* examples/particles.scm: New file.
* Makefile.am (EXTRA_DIST): Add them.

Makefile.am
examples/images/explosion.png [new file with mode: 0644]
examples/particles.scm [new file with mode: 0644]

index d80831a..493f415 100644 (file)
@@ -84,11 +84,13 @@ EXTRA_DIST +=                                       \
   examples/lines.scm                           \
   examples/text.scm                            \
   examples/nine-patch.scm                      \
+  examples/particles.scm                       \
   examples/tiled.scm                           \
   examples/grid.scm                            \
   examples/images/AUTHORS                      \
   examples/images/chickadee.png                        \
   examples/images/dialog-box.png               \
+  examples/images/explosion.png                        \
   examples/images/tiles.png                    \
   examples/maps/example.tmx
 
diff --git a/examples/images/explosion.png b/examples/images/explosion.png
new file mode 100644 (file)
index 0000000..d64f54b
Binary files /dev/null and b/examples/images/explosion.png differ
diff --git a/examples/particles.scm b/examples/particles.scm
new file mode 100644 (file)
index 0000000..4fd7ea3
--- /dev/null
@@ -0,0 +1,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)