examples: Update particle example.
authorDavid Thompson <dthompson2@worcester.edu>
Wed, 24 Jul 2019 20:41:07 +0000 (16:41 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Wed, 24 Jul 2019 20:41:07 +0000 (16:41 -0400)
examples/particles.scm

index 4fd7ea3..c51f62f 100644 (file)
@@ -2,31 +2,49 @@
              (chickadee math rect)
              (chickadee math vector)
              (chickadee render)
+             (chickadee render color)
              (chickadee render font)
              (chickadee render particles)
+             (chickadee render sprite)
              (chickadee render texture)
              (chickadee scripting)
              (ice-9 format)
              ((sdl2) #:select (sdl-ticks)))
 
+(define window-width 800)
+(define window-height 600)
 (define particles #f)
-(define texture #f)
+(define emitter #f)
+(define particle-texture #f)
+(define sprite-texture #f)
 (define start-time (sdl-ticks))
 (define avg-frame-time 0)
 (define stats-text "")
 
+(define (center-x w)
+  (- (/ window-width 2.0) (/ w 2.0)))
+
+(define (center-y h)
+  (- (/ window-height 2.0) (/ h 2.0)))
+
 (define (load)
   (set! *random-state* (random-state-from-platform))
-  (set! texture (load-image "images/explosion.png"))
-  (set! particles (make-particles 1000
-                                  #:texture texture
+  (set! particle-texture (load-image "images/explosion.png"))
+  (set! sprite-texture (load-image "images/chickadee.png"))
+  (set! particles (make-particles 2000
+                                  #:texture particle-texture
+                                  #:end-color (make-color 1.0 1.0 1.0 0.8)
+                                  #:end-color (make-color 1.0 1.0 1.0 0.0)
+                                  #:speed-range (vec2 1.0 5.0)
+                                  ;;#:acceleration-range (vec2 -0.1 -0.2)
+                                  #:lifetime 40
                                   #: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))
+  (set! emitter (make-particle-emitter (make-rect (center-x 0.0)
+                                                  (center-y 0.0)
+                                                  0.0 0.0)
+                                       8))
+  (add-particle-emitter particles emitter)
   (script
    (forever
     (sleep 60)
                              (particles-size particles)
                              (/ 1000.0 avg-frame-time))))))
 
-(define stats-text-pos (vec2 4.0 464.0))
+(define stats-text-pos (vec2 4.0 (- window-height 16.0)))
+(define sprite-position (vec2 (center-x 128.0) (center-y 128.0)))
 (define (draw alpha)
   (draw-particles particles)
+  (draw-sprite sprite-texture sprite-position)
   (draw-text stats-text stats-text-pos)
   (let ((current-time (sdl-ticks)))
     (set! avg-frame-time
   (update-agenda 1)
   (update-particles particles))
 
-(run-game #:load load #:draw draw #:update update)
+(define (mouse-move x y dx dy buttons)
+  (let ((area (particle-emitter-spawn-area emitter)))
+    (set-rect-x! area x)
+    (set-rect-y! area y)
+    (set-vec2! sprite-position (- x 64.0) (- y 64.0))))
+
+(run-game #:load load
+          #:draw draw
+          #:update update
+          #:mouse-move mouse-move
+          #:window-width window-width
+          #:window-height window-height)