58513d46d99dac33cc9e50c7cc7676116e6f4f2d
[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 config)
35 #:use-module (chickadee game-loop)
36 #:use-module (chickadee math matrix)
37 #:use-module (chickadee render)
38 #:use-module (chickadee render color)
39 #:use-module (chickadee render gl)
40 #:use-module (chickadee render gpu)
41 #:use-module (chickadee render viewport)
42 #:use-module (chickadee utils)
43 #:export (current-window
44 controller-button-pressed?
45 controller-axis
46 controller-name
47 key-pressed?
48 key-released?
49 mouse-x
50 mouse-y
51 mouse-button-pressed?
52 mouse-button-released?
53 run-game)
54 #:re-export (abort-game))
55
56 (define (key-pressed? key)
57 "Return #t if KEY is currently being pressed."
58 (sdl2:key-pressed? key))
59
60 (define (key-released? key)
61 "Return #t if KEY is not currently being pressed."
62 (sdl2:key-released? key))
63
64 (define (mouse-x)
65 "Return the current X coordinate of the mouse cursor."
66 (sdl2:mouse-x))
67
68 (define (mouse-y)
69 "Return the current Y coordinate of the mouse cursor."
70 (sdl2:mouse-y))
71
72 (define (mouse-button-pressed? button)
73 "Return #t if BUTTON is currently being pressed."
74 (sdl2:mouse-button-pressed? button))
75
76 (define (mouse-button-released? button)
77 "Return #t if BUTTON is not currently being pressed."
78 (sdl2:mouse-button-released? button))
79
80 (define *controllers* (make-hash-table))
81
82 (define (lookup-controller joystick-id)
83 (hashv-ref *controllers* joystick-id))
84
85 (define (add-controller joystick-index)
86 (let ((controller (sdl2:open-game-controller joystick-index)))
87 (hashv-set! *controllers*
88 (joystick-instance-id
89 (sdl2:game-controller-joystick controller))
90 controller)
91 controller))
92
93 (define (remove-controller joystick-id)
94 (hashv-remove! *controllers* joystick-id))
95
96 (define (controller-button-pressed? controller button)
97 "Return #t if BUTTON is currently being pressed on CONTROLLER."
98 (sdl2:game-controller-button-pressed? controller button))
99
100 (define (controller-axis controller axis)
101 "Return a floating point value in the range [-1, 1] corresponding to
102 how much AXIS is being pushed on CONTROLLER. 0 is returned if AXIS is
103 not being pushed at all."
104 (/ (sdl2:game-controller-axis controller axis)
105 32768.0))
106
107 (define controller-name
108 ;; Memoize to avoid repeated allocation of strings via
109 ;; pointer->string.
110 (memoize
111 (lambda (controller)
112 (sdl2:game-controller-name controller))))
113
114 (define current-window (make-parameter #f))
115
116 (define* (run-game #:key
117 (window-title "Chickadee!")
118 (window-width 640)
119 (window-height 480)
120 window-fullscreen?
121 (update-hz 60)
122 (load (const #t))
123 (update (const #t))
124 (draw (const #t))
125 (quit abort-game)
126 (key-press (const #t))
127 (key-release (const #t))
128 (text-input (const #t))
129 (mouse-press (const #t))
130 (mouse-release (const #t))
131 (mouse-move (const #t))
132 (controller-add (const #t))
133 (controller-remove (const #t))
134 (controller-press (const #t))
135 (controller-release (const #t))
136 (controller-move (const #t))
137 error)
138 (sdl-init)
139 (false-if-exception (mixer-init))
140 (open-audio)
141 (start-text-input)
142 (let* ((window (make-window #:opengl? #t
143 #:title window-title
144 #:size (list window-width window-height)
145 #:fullscreen? window-fullscreen?))
146 (gl-context (make-gl-context window))
147 (gpu (make-gpu gl-context))
148 (default-viewport (make-viewport 0 0 window-width window-height))
149 (default-projection (orthographic-projection 0 window-width
150 window-height 0
151 0 1)))
152 (define (invert-y y)
153 ;; SDL's origin is the top-left, but our origin is the bottom
154 ;; left so we need to invert Y coordinates that SDL gives us.
155 (- window-height y))
156 (define (input-sdl)
157 (define (process-event event)
158 (cond
159 ((quit-event? event)
160 (quit))
161 ((keyboard-down-event? event)
162 (key-press (keyboard-event-key event)
163 (keyboard-event-scancode event)
164 (keyboard-event-modifiers event)
165 (keyboard-event-repeat? event)))
166 ((keyboard-up-event? event)
167 (key-release (keyboard-event-key event)
168 (keyboard-event-scancode event)
169 (keyboard-event-modifiers event)))
170 ((text-input-event? event)
171 (text-input (text-input-event-text event)))
172 ((mouse-button-down-event? event)
173 (mouse-press (mouse-button-event-button event)
174 (mouse-button-event-clicks event)
175 (mouse-button-event-x event)
176 (invert-y (mouse-button-event-y event))))
177 ((mouse-button-up-event? event)
178 (mouse-release (mouse-button-event-button event)
179 (mouse-button-event-x event)
180 (invert-y (mouse-button-event-y event))))
181 ((mouse-motion-event? event)
182 (mouse-move (mouse-motion-event-x event)
183 (invert-y (mouse-motion-event-y event))
184 (mouse-motion-event-x-rel event)
185 (- (mouse-motion-event-y-rel event))
186 (mouse-motion-event-buttons event)))
187 ((and (controller-device-event? event)
188 (eq? (controller-device-event-action event) 'added))
189 (controller-add (add-controller
190 (controller-device-event-which event))))
191 ((and (controller-device-event? event)
192 (eq? (controller-device-event-action event) 'removed))
193 (let ((controller (lookup-controller
194 (controller-device-event-which event))))
195 (controller-remove controller)
196 (remove-controller (controller-device-event-which event))
197 (sdl2:close-game-controller controller)))
198 ((controller-button-down-event? event)
199 (controller-press (lookup-controller
200 (controller-button-event-which event))
201 (controller-button-event-button event)))
202 ((controller-button-up-event? event)
203 (controller-release (lookup-controller
204 (controller-button-event-which event))
205 (controller-button-event-button event)))
206 ((controller-axis-event? event)
207 (controller-move (lookup-controller
208 (controller-axis-event-which event))
209 (controller-axis-event-axis event)
210 (/ (controller-axis-event-value event) 32768.0)))))
211 ;; Process all pending events.
212 (let loop ((event (poll-event)))
213 (when event
214 (process-event event)
215 (loop (poll-event)))))
216 (define (update-sdl dt)
217 (input-sdl)
218 (update dt)
219 ;; Free any GPU resources that have been GC'd.
220 (gpu-reap!))
221 (define (render-sdl-opengl alpha)
222 ;; Switch to the null viewport to ensure that
223 ;; the default viewport will be re-applied and
224 ;; clear the screen.
225 (set-gpu-viewport! gpu null-viewport)
226 (with-viewport default-viewport
227 (with-projection default-projection
228 (draw alpha)))
229 (swap-gl-window window))
230 (dynamic-wind
231 (const #t)
232 (lambda ()
233 (parameterize ((current-window window)
234 (current-gpu gpu))
235 ;; Attempt to activate vsync, if possible. Some systems do
236 ;; not support setting the OpenGL swap interval.
237 (catch #t
238 (lambda ()
239 (set-gl-swap-interval! 'vsync))
240 (lambda args
241 (display "warning: could not enable vsync\n"
242 (current-error-port))))
243 (load)
244 (sdl2:load-game-controller-mappings!
245 (scope-datadir "gamecontrollerdb.txt"))
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)))))