summaryrefslogtreecommitdiff
path: root/examples/particles.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2018-12-14 08:37:05 -0500
committerDavid Thompson <dthompson2@worcester.edu>2018-12-14 08:37:05 -0500
commit86a3254c8ed0f29be6e07be5538d9c3b3222cb76 (patch)
tree5e7a1e6b0b302424397d44d74fb47fe26ec4682e /examples/particles.scm
parentf2f90e8992420c3c3dd0b3ffa5e17ced4e9a4589 (diff)
Add particles example.
* examples/images/explosion.png: New file. * examples/particles.scm: New file. * Makefile.am (EXTRA_DIST): Add them.
Diffstat (limited to 'examples/particles.scm')
-rw-r--r--examples/particles.scm51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/particles.scm b/examples/particles.scm
new file mode 100644
index 0000000..4fd7ea3
--- /dev/null
+++ b/examples/particles.scm
@@ -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)