Fix normalization of game controller axis values.
[chickadee.git] / chickadee.scm
1 ;;; Chickadee Game Toolkit
2 ;;; Copyright © 2018 David Thompson <davet@gnu.org>
3 ;;;
4 ;;; Chickadee is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published
6 ;;; by the Free Software Foundation, either version 3 of the License,
7 ;;; or (at your option) any later version.
8 ;;;
9 ;;; Chickadee is distributed in the hope that it will be useful, but
10 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;; General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see
16 ;;; <http://www.gnu.org/licenses/>.
17
18 ;;; Commentary:
19 ;;
20 ;; Simple SDL + OpenGL game loop implementation.
21 ;;
22 ;;; Code:
23
24 (define-module (chickadee)
25 #:use-module (sdl2)
26 #:use-module (sdl2 events)
27 #:use-module ((sdl2 input game-controller) #:prefix sdl2:)
28 #:use-module (sdl2 input joystick)
29 #:use-module ((sdl2 input keyboard) #:prefix sdl2:)
30 #:use-module ((sdl2 input mouse) #:prefix sdl2:)
31 #:use-module (sdl2 input text)
32 #:use-module (sdl2 mixer)
33 #:use-module (sdl2 video)
34 #:use-module (chickadee game-loop)
35 #:use-module (chickadee math matrix)
36 #:use-module (chickadee render)
37 #:use-module (chickadee render color)
38 #:use-module (chickadee render gl)
39 #:use-module (chickadee render gpu)
40 #:use-module (chickadee render viewport)
41 #:export (current-window
42 controller-button-pressed?
43 controller-axis
44 key-pressed?
45 key-released?
46 mouse-x
47 mouse-y
48 mouse-button-pressed?
49 mouse-button-released?
50 run-game)
51 #:re-export (abort-game))
52
53 (define (key-pressed? key)
54 "Return #t if KEY is currently being pressed."
55 (sdl2:key-pressed? key))
56
57 (define (key-released? key)
58 "Return #t if KEY is not currently being pressed."
59 (sdl2:key-released? key))
60
61 (define (mouse-x)
62 "Return the current X coordinate of the mouse cursor."
63 (sdl2:mouse-x))
64
65 (define (mouse-y)
66 "Return the current Y coordinate of the mouse cursor."
67 (sdl2:mouse-y))
68
69 (define (mouse-button-pressed? button)
70 "Return #t if BUTTON is currently being pressed."
71 (sdl2:mouse-button-pressed? button))
72
73 (define (mouse-button-released? button)
74 "Return #t if BUTTON is not currently being pressed."
75 (sdl2:mouse-button-released? button))
76
77 (define *controllers* (make-hash-table))
78
79 (define (lookup-controller joystick-id)
80 (hashv-ref *controllers* joystick-id))
81
82 (define (add-controller joystick-index)
83 (let ((controller (sdl2:open-game-controller joystick-index)))
84 (hashv-set! *controllers*
85 (joystick-instance-id
86 (sdl2:game-controller-joystick controller))
87 controller)
88 controller))
89
90 (define (remove-controller joystick-id)
91 (hashv-remove! *controllers* joystick-id))
92
93 (define (open-all-controllers)
94 (let loop ((i 0))
95 (when (< i (num-joysticks))
96 (when (sdl2:game-controller-index? i)
97 (add-controller i))
98 (loop (+ i 1)))))
99
100 (define (controller-button-pressed? controller button)
101 "Return #t if BUTTON is currently being pressed on CONTROLLER."
102 (sdl2:game-controller-button-pressed? controller button))
103
104 (define (controller-axis controller axis)
105 "Return a floating point value in the range [-1, 1] corresponding to
106 how much AXIS is being pushed on CONTROLLER. 0 is returned if AXIS is
107 not being pushed at all."
108 (/ (sdl2:game-controller-axis controller axis)
109 32768.0))
110
111 (define current-window (make-parameter #f))
112
113 (define* (run-game #:key
114 (window-title "Chickadee!")
115 (window-width 640)
116 (window-height 480)
117 window-fullscreen?
118 (update-hz 60)
119 (load (const #t))
120 (update (const #t))
121 (draw (const #t))
122 (quit abort-game)
123 (key-press (const #t))
124 (key-release (const #t))
125 (text-input (const #t))
126 (mouse-press (const #t))
127 (mouse-release (const #t))
128 (mouse-move (const #t))
129 (controller-add (const #t))
130 (controller-remove (const #t))
131 (controller-press (const #t))
132 (controller-release (const #t))
133 (controller-move (const #t))
134 error)
135 (sdl-init)
136 (false-if-exception (mixer-init))
137 (open-audio)
138 (start-text-input)
139 (open-all-controllers)
140 (let* ((window (make-window #:opengl? #t
141 #:title window-title
142 #:size (list window-width window-height)
143 #:fullscreen? window-fullscreen?))
144 (gl-context (make-gl-context window))
145 (default-viewport (make-viewport 0 0 window-width window-height))
146 (default-projection (orthographic-projection 0 window-width
147 window-height 0
148 0 1)))
149 (define (invert-y y)
150 ;; SDL's origin is the top-left, but our origin is the bottom
151 ;; left so we need to invert Y coordinates that SDL gives us.
152 (- window-height y))
153 (define (input-sdl)
154 (define (process-event event)
155 (cond
156 ((quit-event? event)
157 (quit))
158 ((keyboard-down-event? event)
159 (key-press (keyboard-event-key event)
160 (keyboard-event-scancode event)
161 (keyboard-event-modifiers event)
162 (keyboard-event-repeat? event)))
163 ((keyboard-up-event? event)
164 (key-release (keyboard-event-key event)
165 (keyboard-event-scancode event)
166 (keyboard-event-modifiers event)))
167 ((text-input-event? event)
168 (text-input (text-input-event-text event)))
169 ((mouse-button-down-event? event)
170 (mouse-press (mouse-button-event-button event)
171 (mouse-button-event-clicks event)
172 (mouse-button-event-x event)
173 (invert-y (mouse-button-event-y event))))
174 ((mouse-button-up-event? event)
175 (mouse-release (mouse-button-event-button event)
176 (mouse-button-event-x event)
177 (invert-y (mouse-button-event-y event))))
178 ((mouse-motion-event? event)
179 (mouse-move (mouse-motion-event-x event)
180 (invert-y (mouse-motion-event-y event))
181 (mouse-motion-event-x-rel event)
182 (- (mouse-motion-event-y-rel event))
183 (mouse-motion-event-buttons event)))
184 ((and (controller-device-event? event)
185 (eq? (controller-device-event-action event) 'added))
186 (controller-add (add-controller
187 (controller-device-event-which event))))
188 ((and (controller-device-event? event)
189 (eq? (controller-device-event-action event) 'removed))
190 (let ((controller (lookup-controller
191 (controller-device-event-which event))))
192 (controller-remove controller)
193 (remove-controller (controller-device-event-which event))
194 (sdl2:close-game-controller controller)))
195 ((controller-button-down-event? event)
196 (controller-press (lookup-controller
197 (controller-button-event-which event))
198 (controller-button-event-button event)))
199 ((controller-button-up-event? event)
200 (controller-release (lookup-controller
201 (controller-button-event-which event))
202 (controller-button-event-button event)))
203 ((controller-axis-event? event)
204 (controller-move (lookup-controller
205 (controller-axis-event-which event))
206 (controller-axis-event-axis event)
207 (/ (controller-axis-event-value event) 32768.0)))))
208 ;; Process all pending events.
209 (let loop ((event (poll-event)))
210 (when event
211 (process-event event)
212 (loop (poll-event)))))
213 (define (update-sdl dt)
214 (input-sdl)
215 (update dt)
216 ;; Free any GPU resources that have been GC'd.
217 (gpu-reap!))
218 (define (render-sdl-opengl alpha)
219 ;; Switch to the null viewport to ensure that
220 ;; the default viewport will be re-applied and
221 ;; clear the screen.
222 (gpu-state-set! *viewport-state* null-viewport)
223 (with-viewport default-viewport
224 (with-projection default-projection
225 (draw alpha)))
226 (swap-gl-window window))
227 (dynamic-wind
228 (const #t)
229 (lambda ()
230 (parameterize ((current-window window))
231 ;; Attempt to activate vsync, if possible. Some systems do
232 ;; not support setting the OpenGL swap interval.
233 (catch #t
234 (lambda ()
235 (set-gl-swap-interval! 'vsync))
236 (lambda args
237 (display "warning: could not enable vsync\n"
238 (current-error-port))))
239 (load)
240 ;; Notify about all controllers that were already connected
241 ;; when the game was launched because SDL will not create
242 ;; events for them.
243 (hash-for-each (lambda (key controller)
244 (controller-add controller))
245 *controllers*)
246 (run-game* #:update update-sdl
247 #:render render-sdl-opengl
248 #:error error
249 #:time sdl-ticks
250 #:update-hz update-hz)))
251 (lambda ()
252 (delete-gl-context! gl-context)
253 (close-window! window)))))