Add a bunch of window manipulation procedures.
[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 (chickadee config)
26 #:use-module (chickadee game-loop)
27 #:use-module (chickadee math matrix)
28 #:use-module (chickadee render)
29 #:use-module (chickadee render color)
30 #:use-module (chickadee render gl)
31 #:use-module (chickadee render gpu)
32 #:use-module (chickadee render viewport)
33 #:use-module (chickadee utils)
34 #:use-module (ice-9 match)
35 #:use-module (sdl2)
36 #:use-module (sdl2 events)
37 #:use-module ((sdl2 input game-controller) #:prefix sdl2:)
38 #:use-module (sdl2 input joystick)
39 #:use-module ((sdl2 input keyboard) #:prefix sdl2:)
40 #:use-module ((sdl2 input mouse) #:prefix sdl2:)
41 #:use-module (sdl2 input text)
42 #:use-module (sdl2 mixer)
43 #:use-module ((sdl2 video) #:prefix sdl2:)
44 #:use-module (srfi srfi-9)
45 #:export (current-window
46 window?
47 window-width
48 window-height
49 window-x
50 window-y
51 window-title
52 hide-window!
53 show-window!
54 maximize-window!
55 minimize-window!
56 raise-window!
57 restore-window!
58 set-window-border!
59 set-window-title!
60 set-window-size!
61 set-window-position!
62 set-window-fullscreen!
63 controller-button-pressed?
64 controller-axis
65 controller-name
66 key-pressed?
67 key-released?
68 mouse-x
69 mouse-y
70 mouse-button-pressed?
71 mouse-button-released?
72 run-game)
73 #:re-export (abort-game))
74
75 (define (key-pressed? key)
76 "Return #t if KEY is currently being pressed."
77 (sdl2:key-pressed? key))
78
79 (define (key-released? key)
80 "Return #t if KEY is not currently being pressed."
81 (sdl2:key-released? key))
82
83 (define (mouse-x)
84 "Return the current X coordinate of the mouse cursor."
85 (sdl2:mouse-x))
86
87 (define (mouse-y)
88 "Return the current Y coordinate of the mouse cursor."
89 (sdl2:mouse-y))
90
91 (define (mouse-button-pressed? button)
92 "Return #t if BUTTON is currently being pressed."
93 (sdl2:mouse-button-pressed? button))
94
95 (define (mouse-button-released? button)
96 "Return #t if BUTTON is not currently being pressed."
97 (sdl2:mouse-button-released? button))
98
99 (define *controllers* (make-hash-table))
100
101 (define (lookup-controller joystick-id)
102 (hashv-ref *controllers* joystick-id))
103
104 (define (add-controller joystick-index)
105 (let ((controller (sdl2:open-game-controller joystick-index)))
106 (hashv-set! *controllers*
107 (joystick-instance-id
108 (sdl2:game-controller-joystick controller))
109 controller)
110 controller))
111
112 (define (remove-controller joystick-id)
113 (hashv-remove! *controllers* joystick-id))
114
115 (define (controller-button-pressed? controller button)
116 "Return #t if BUTTON is currently being pressed on CONTROLLER."
117 (sdl2:game-controller-button-pressed? controller button))
118
119 (define (controller-axis controller axis)
120 "Return a floating point value in the range [-1, 1] corresponding to
121 how much AXIS is being pushed on CONTROLLER. 0 is returned if AXIS is
122 not being pushed at all."
123 (/ (sdl2:game-controller-axis controller axis)
124 32768.0))
125
126 (define controller-name
127 ;; Memoize to avoid repeated allocation of strings via
128 ;; pointer->string.
129 (memoize
130 (lambda (controller)
131 (sdl2:game-controller-name controller))))
132
133 (define-record-type <window>
134 (wrap-sdl-window sdl-window)
135 window?
136 (sdl-window unwrap-window))
137
138 (define current-window (make-parameter #f))
139
140 (define-syntax-rule (define-window-wrapper (name args ...) sdl-proc docstring)
141 (define (name window args ...)
142 docstring
143 (sdl-proc (unwrap-window window) args ...)))
144
145 (define-window-wrapper (window-title) sdl2:window-title
146 "Return the title of WINDOW.")
147
148 (define-window-wrapper (hide-window!) sdl2:hide-window!
149 "Hide WINDOW.")
150
151 (define-window-wrapper (show-window!) sdl2:show-window!
152 "Show WINDOW.")
153
154 (define-window-wrapper (maximize-window!) sdl2:maximize-window!
155 "Maximize WINDOW.")
156
157 (define-window-wrapper (minimize-window!) sdl2:minimize-window!
158 "Minimize WINDOW.")
159
160 (define-window-wrapper (raise-window!) sdl2:raise-window!
161 "Make WINDOW visible over all other windows.")
162
163 (define-window-wrapper (restore-window!) sdl2:restore-window!
164 "Restore the size and position of a minimized or maximized WINDOW.")
165
166 (define-window-wrapper (set-window-border! border?) sdl2:set-window-border!
167 "Enable/disable the border around WINDOW. If BORDER? is #f, the
168 border is disabled, otherwise it is enabled.")
169
170 (define-window-wrapper (set-window-title! title) sdl2:set-window-title!
171 "Set the title of WINDOW to TITLE.")
172
173 (define-window-wrapper (set-window-fullscreen! fullscreen?) sdl2:set-window-fullscreen!
174 "Enable or disable fullscreen mode for WINDOW. If FULLSCREEN? is
175 #f, fullscreen mode is disabled, otherwise it is enabled.")
176
177 (define (window-width window)
178 "Return the width of WINDOW."
179 (match (sdl2:window-size (unwrap-window window))
180 ((x _) x)))
181
182 (define (window-height window)
183 "Return the height of WINDOW."
184 (match (sdl2:window-size (unwrap-window window))
185 ((_ y) y)))
186
187 (define (window-x window)
188 "Return the X coordinate of the upper-left corner of WINDOW."
189 (match (sdl2:window-position (unwrap-window window))
190 ((x _) x)))
191
192 (define (window-y window)
193 "Return the Y coordinate of the upper-left corner of WINDOW."
194 (match (sdl2:window-position (unwrap-window window))
195 ((_ y) y)))
196
197 (define (set-window-size! window width height)
198 "Change the dimensions of WINDOW to WIDTH x HEIGHT pixels."
199 (sdl2:set-window-size! (unwrap-window window) (list width height)))
200
201 (define (set-window-position! window x y)
202 "Move the upper-left corner of WINDOW to pixel coordinates (X, Y)."
203 (sdl2:set-window-position! (unwrap-window window) (list x y)))
204
205 (define* (run-game #:key
206 (window-title "Chickadee!")
207 (window-width 640)
208 (window-height 480)
209 window-fullscreen?
210 (update-hz 60)
211 (load (const #t))
212 (update (const #t))
213 (draw (const #t))
214 (quit abort-game)
215 (key-press (const #t))
216 (key-release (const #t))
217 (text-input (const #t))
218 (mouse-press (const #t))
219 (mouse-release (const #t))
220 (mouse-move (const #t))
221 (controller-add (const #t))
222 (controller-remove (const #t))
223 (controller-press (const #t))
224 (controller-release (const #t))
225 (controller-move (const #t))
226 error)
227 (sdl-init)
228 (false-if-exception (mixer-init))
229 (open-audio)
230 (start-text-input)
231 (let* ((window (sdl2:make-window #:opengl? #t
232 #:title window-title
233 #:size (list window-width window-height)
234 #:fullscreen? window-fullscreen?))
235 (gl-context (sdl2:make-gl-context window))
236 (gpu (make-gpu gl-context))
237 (default-viewport (make-viewport 0 0 window-width window-height))
238 (default-projection (orthographic-projection 0 window-width
239 window-height 0
240 0 1)))
241 (define (invert-y y)
242 ;; SDL's origin is the top-left, but our origin is the bottom
243 ;; left so we need to invert Y coordinates that SDL gives us.
244 (- window-height y))
245 (define (input-sdl)
246 (define (process-event event)
247 (cond
248 ((quit-event? event)
249 (quit))
250 ((keyboard-down-event? event)
251 (key-press (keyboard-event-key event)
252 (keyboard-event-scancode event)
253 (keyboard-event-modifiers event)
254 (keyboard-event-repeat? event)))
255 ((keyboard-up-event? event)
256 (key-release (keyboard-event-key event)
257 (keyboard-event-scancode event)
258 (keyboard-event-modifiers event)))
259 ((text-input-event? event)
260 (text-input (text-input-event-text event)))
261 ((mouse-button-down-event? event)
262 (mouse-press (mouse-button-event-button event)
263 (mouse-button-event-clicks event)
264 (mouse-button-event-x event)
265 (invert-y (mouse-button-event-y event))))
266 ((mouse-button-up-event? event)
267 (mouse-release (mouse-button-event-button event)
268 (mouse-button-event-x event)
269 (invert-y (mouse-button-event-y event))))
270 ((mouse-motion-event? event)
271 (mouse-move (mouse-motion-event-x event)
272 (invert-y (mouse-motion-event-y event))
273 (mouse-motion-event-x-rel event)
274 (- (mouse-motion-event-y-rel event))
275 (mouse-motion-event-buttons event)))
276 ((and (controller-device-event? event)
277 (eq? (controller-device-event-action event) 'added))
278 (controller-add (add-controller
279 (controller-device-event-which event))))
280 ((and (controller-device-event? event)
281 (eq? (controller-device-event-action event) 'removed))
282 (let ((controller (lookup-controller
283 (controller-device-event-which event))))
284 (controller-remove controller)
285 (remove-controller (controller-device-event-which event))
286 (sdl2:close-game-controller controller)))
287 ((controller-button-down-event? event)
288 (controller-press (lookup-controller
289 (controller-button-event-which event))
290 (controller-button-event-button event)))
291 ((controller-button-up-event? event)
292 (controller-release (lookup-controller
293 (controller-button-event-which event))
294 (controller-button-event-button event)))
295 ((controller-axis-event? event)
296 (controller-move (lookup-controller
297 (controller-axis-event-which event))
298 (controller-axis-event-axis event)
299 (/ (controller-axis-event-value event) 32768.0)))))
300 ;; Process all pending events.
301 (let loop ((event (poll-event)))
302 (when event
303 (process-event event)
304 (loop (poll-event)))))
305 (define (update-sdl dt)
306 (input-sdl)
307 (update dt)
308 ;; Free any GPU resources that have been GC'd.
309 (gpu-reap!))
310 (define (render-sdl-opengl alpha)
311 ;; Switch to the null viewport to ensure that
312 ;; the default viewport will be re-applied and
313 ;; clear the screen.
314 (set-gpu-viewport! gpu null-viewport)
315 (with-viewport default-viewport
316 (with-projection default-projection
317 (draw alpha)))
318 (sdl2:swap-gl-window window))
319 (dynamic-wind
320 (const #t)
321 (lambda ()
322 (parameterize ((current-window (wrap-sdl-window window))
323 (current-gpu gpu))
324 ;; Attempt to activate vsync, if possible. Some systems do
325 ;; not support setting the OpenGL swap interval.
326 (catch #t
327 (lambda ()
328 (sdl2:set-gl-swap-interval! 'vsync))
329 (lambda args
330 (display "warning: could not enable vsync\n"
331 (current-error-port))))
332 (load)
333 (sdl2:load-game-controller-mappings!
334 (scope-datadir "gamecontrollerdb.txt"))
335 (run-game* #:update update-sdl
336 #:render render-sdl-opengl
337 #:error error
338 #:time sdl-ticks
339 #:update-hz update-hz)))
340 (lambda ()
341 (sdl2:delete-gl-context! gl-context)
342 (sdl2:close-window! window)))))