/
input.lisp
71 lines (58 loc) · 2.3 KB
/
input.lisp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; indent-tabs-mode: nil -*-
;;;; This file is part of Until It Dies
;;;; input.lisp
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :until-it-dies)
;;;
;;; Callbacks for GLFW events
;;;
(cffi:defcallback key-hook :void ((key :int) (action uid-glfw:key/button-state))
"Invokes KEY-DOWN or KEY-UP on the active engine, for control keys."
(unless (<= key 255)
(continuable
(funcall (case action
(:press 'key-down)
(:release 'key-up))
*engine* (translate-glfw-control-key key)))))
(cffi:defcallback char-hook :void ((key :int) (action uid-glfw:key/button-state))
"Invokes KEY-DOWN or KEY-UP on the active engine, for character input."
(continuable
(funcall (case action
(:press 'key-down)
(:release 'key-up))
*engine* (code-char key))))
;;;
;;; Joystick support
;;;
;;; low-level stuff
(defun glfw-available-joysticks ()
(loop for i below 16
for joystick-present-p = (uid-glfw:get-joystick-param i :present)
when (= 1 joystick-present-p)
collect i))
(defun glfw-joystick-num-buttons (joystick-number)
(uid-glfw:get-joystick-param joystick-number :buttons))
(defun glfw-joystick-num-axes (joystick-number)
(uid-glfw:get-joystick-param joystick-number :axes))
(defun glfw-joystick-axis-positions (joystick-number num-axes)
(uid-glfw:get-joystick-pos joystick-number num-axes))
(defun glfw-joystick-button-states (joystick-number num-buttons)
(uid-glfw:get-joystick-buttons joystick-number num-buttons))
;;; Interface
(defproto =joystick= ()
((joystick-number 0)
(num-axes 0) (num-buttons 0)
(axis-positions nil) (button-states nil)))
(defun build-joystick (joynum)
(let ((num-axes (glfw-joystick-num-axes joynum))
(num-buttons (glfw-joystick-num-buttons joynum)))
(defobject =joystick=
((joystick-number joynum)
(num-axes num-axes)
(num-buttons num-buttons)
(axis-positions (glfw-joystick-axis-positions joynum num-axes))
(button-states (glfw-joystick-button-states joynum num-buttons))))))
(defun available-joysticks ()
(loop for joystick-num in (glfw-available-joysticks)
collect (build-joystick joystick-num)))