summaryrefslogtreecommitdiff
path: root/examples/controller.scm
blob: 7989e7c234099a30119e8128c2cd92b63c2a934d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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))))))