summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2019-01-06 23:19:46 -0500
committerDavid Thompson <dthompson2@worcester.edu>2019-01-06 23:19:46 -0500
commit6b4f2d5ebf81d7eea262ce45ae189e79b103cb09 (patch)
tree217b35dc2813e94afcf76f7154d7c35efc477118 /examples
parenta9671d07fd97dd4fe010647c84bc2e6a87d6687c (diff)
examples: game-controller: Add left/right trigger rendering.
Diffstat (limited to 'examples')
-rw-r--r--examples/game-controller.scm68
1 files changed, 38 insertions, 30 deletions
diff --git a/examples/game-controller.scm b/examples/game-controller.scm
index fe1a681..252f368 100644
--- a/examples/game-controller.scm
+++ b/examples/game-controller.scm
@@ -1,5 +1,6 @@
(use-modules (chickadee)
(chickadee math vector)
+ (chickadee render color)
(chickadee render font)
(chickadee render sprite)
(chickadee render texture)
@@ -23,6 +24,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)
@@ -40,38 +44,42 @@
(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
- (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))
+ (with-batched-sprites
+ (draw-text (controller-name controller) controller-name-pos)
+ (for-each (match-lambda
+ ((axis p tile)
+ (let ((alpha (controller-axis controller axis)))
+ (draw-sprite (texture-atlas-ref atlas tile) p
+ #: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))))
+ (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))
(draw-text no-controller-msg no-controller-msg-pos)))
(run-game #:load load #:draw draw #:controller-add controller-add)