summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2017-01-20 20:14:11 -0500
committerDavid Thompson <dthompson2@worcester.edu>2017-01-20 20:14:50 -0500
commit2debe0d158a0192c7d665ad316d4bcf105e96537 (patch)
tree6cdbb06abfdb49b5ffd01ce854cd2820dc1e3054
parent15ee89a01eb02c988814df861544a6de86232de2 (diff)
Add game controller example.
-rw-r--r--Makefile.am3
-rw-r--r--examples/controller.scm56
2 files changed, 58 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index 4dbaf71..5e033aa 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -88,7 +88,8 @@ EXTRA_DIST += \
README \
guix.scm \
examples/hello.scm \
- examples/hello.bmp
+ examples/hello.bmp \
+ examples/controller.scm
publish: dist
gpg2 --sign --armor --yes guile-sdl2-$(VERSION).tar.gz && \
diff --git a/examples/controller.scm b/examples/controller.scm
new file mode 100644
index 0000000..7989e7c
--- /dev/null
+++ b/examples/controller.scm
@@ -0,0 +1,56 @@
+(use-modules (sdl2)
+ (sdl2 events)
+ (sdl2 video)
+ (sdl2 input game-controller)
+ (sdl2 input joystick))
+
+(sdl-init)
+
+(if (not (game-controller-index? 0))
+ (begin
+ (display "sorry, there are no controllers available!\n")
+ (exit 1))
+ (display "connecting to controller 0\n"))
+
+(define controller (open-game-controller 0))
+(define window (make-window #:opengl? #t))
+(define running? #t)
+(define last-power-check 0)
+
+(while running?
+ (let ((time (sdl-ticks)))
+ (when (and (zero? (modulo time 2000))
+ (not (= time last-power-check)))
+ (set! last-power-check time)
+ (display "controller power level: ")
+ (display (joystick-power-level (game-controller-joystick controller)))
+ (newline)))
+ (let ((event (poll-event)))
+ (when event
+ (cond
+ ((quit-event? event)
+ (display "bye!\n")
+ (set! running? #f))
+ ((controller-device-event? event)
+ (display "controller ")
+ (display (controller-device-event-which event))
+ (display " ")
+ (display (controller-device-event-action event))
+ (newline))
+ ((controller-button-event? event)
+ (display (if (controller-button-event-pressed? event)
+ "pressed"
+ "released"))
+ (display " button ")
+ (display (controller-button-event-button event))
+ (display " on controller ")
+ (display (controller-button-event-which event))
+ (newline))
+ ((controller-axis-event? event)
+ (display "moved ")
+ (display (controller-axis-event-axis event))
+ (display " to ")
+ (display (controller-axis-event-value event))
+ (display " on controller ")
+ (display (controller-axis-event-which event))
+ (newline))))))