Update a couple of examples to use new sprite batch API.
[chickadee.git] / examples / game-controller.scm
index fe1a681..77a0ac0 100644 (file)
@@ -1,10 +1,12 @@
 (use-modules (chickadee)
              (chickadee math vector)
+             (chickadee render color)
              (chickadee render font)
              (chickadee render sprite)
              (chickadee render texture)
              (ice-9 match))
 
+(define batch #f)
 (define controller #f)
 (define atlas #f)
 (define button-icons
@@ -23,6 +25,9 @@
     (right-stick ,(vec2 450.0 20.0) 13 31)
     (left-shoulder ,(vec2 110.0 390.0) 4 22)
     (right-shoulder ,(vec2 450.0 390.0) 5 23)))
+(define trigger-icons
+  `((trigger-left ,(vec2 10.0 390.0) 22)
+    (trigger-right ,(vec2 550.0 390.0) 23)))
 (define no-controller-msg "no controller detected   :'(")
 (define no-controller-msg-pos #f)
 (define controller-name-pos #f)
 
 (define (load)
   (set! no-controller-msg-pos (vec2 (center-text no-controller-msg) 240.0))
-  (set! atlas (split-texture (load-image "images/controller-buttons.png")
-                             80 80)))
+  (let ((texture (load-image "images/controller-buttons.png")))
+    (set! atlas (split-texture texture 80 80))
+    (set! batch (make-sprite-batch texture))))
 
 (define (controller-add new-controller)
   (set! controller (or controller new-controller))
   (set! controller-name-pos
         (vec2 (center-text (controller-name controller)) 440.0)))
-
-;; FIXME: I couldn't think of a decent way to display the analog
-;; left/right trigger buttons, so they are omitted.
 (define (draw alpha)
   (if controller
       (begin
+        (sprite-batch-clear! batch)
         (draw-text (controller-name controller) controller-name-pos)
         (for-each (match-lambda
-                    (((and (or 'left-stick 'right-stick) button)
-                      p released pressed)
-                     (let ((pressed? (controller-button-pressed? controller
-                                                                 button))
-                           (x (controller-axis controller
-                                               (if (eq? button 'left-stick)
-                                                   'left-x
-                                                   'right-x)))
-                           (y (controller-axis controller
-                                               (if (eq? button 'left-stick)
-                                                   'left-y
-                                                   'right-y))))
-                       (draw-sprite (texture-atlas-ref atlas released)
-                                    p)
-                       (when (or pressed? (not (zero? x)) (not (zero? y)))
-                         (draw-sprite (texture-atlas-ref atlas pressed)
-                                      (vec2+ p (vec2 (* x 16.0) (* y -16.0)))))))
-                    ((button p released pressed)
-                     (let ((pressed? (controller-button-pressed? controller
-                                                                 button)))
-                       (draw-sprite (texture-atlas-ref
-                                     atlas
-                                     (if pressed? pressed released))
-                                    p))))
-                  button-icons))
+                   ((axis p tile)
+                    (let ((alpha (controller-axis controller axis)))
+                      (sprite-batch-add!
+                       batch p
+                       #:texture-region (texture-atlas-ref atlas tile)
+                       #:tint (color 1.0 1.0 1.0 alpha)))))
+                 trigger-icons)
+       (for-each (match-lambda
+                   (((and (or 'left-stick 'right-stick) button)
+                     p released pressed)
+                    (let ((pressed? (controller-button-pressed? controller
+                                                                button))
+                          (x (controller-axis controller
+                                              (if (eq? button 'left-stick)
+                                                  'left-x
+                                                  'right-x)))
+                          (y (controller-axis controller
+                                              (if (eq? button 'left-stick)
+                                                  'left-y
+                                                  'right-y))))
+                      (sprite-batch-add! batch p
+                                         #:texture-region (texture-atlas-ref atlas released))
+                      (when (or pressed? (not (zero? x)) (not (zero? y)))
+                        (sprite-batch-add!
+                         batch
+                         (vec2+ p (vec2 (* x 16.0) (* y -16.0)))
+                         #:texture-region (texture-atlas-ref atlas pressed)))))
+                   ((button p released pressed)
+                    (let ((pressed? (controller-button-pressed? controller
+                                                                button)))
+                      (sprite-batch-add!
+                       batch p
+                       #:texture-region
+                       (texture-atlas-ref atlas
+                                          (if pressed? pressed released))))))
+                 button-icons)
+       (draw-sprite-batch batch))
       (draw-text no-controller-msg no-controller-msg-pos)))
 
 (run-game #:load load #:draw draw #:controller-add controller-add)