From 2debe0d158a0192c7d665ad316d4bcf105e96537 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 20 Jan 2017 20:14:11 -0500 Subject: Add game controller example. --- Makefile.am | 3 ++- examples/controller.scm | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 examples/controller.scm 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)))))) -- cgit v1.2.3