9b225ba733b5fb34f02f9a60fbdba9dcc92d11a2
[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)
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 (open-game-controller joystick-index)))
84 (hashv-set! *controllers*
85 (joystick-instance-id
86 (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 (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 0.5))
111
112 (define current-window (make-parameter #f))
113
114 (define* (run-game #:key
115 (window-title "Chickadee!")
116 (window-width 640)
117 (window-height 480)
118 window-fullscreen?
119 (update-hz 60)
120 (load (const #t))
121 (update (const #t))
122 (draw (const #t))
123 (quit abort-game)
124 (key-press (const #t))
125 (key-release (const #t))
126 (text-input (const #t))
127 (mouse-press (const #t))
128 (mouse-release (const #t))
129 (mouse-move (const #t))
130 (controller-add (const #t))
131 (controller-remove (const #t))
132 (controller-press (const #t))
133 (controller-release (const #t))
134 (controller-move (const #t))
135 error)
136 (sdl-init)
137 (false-if-exception (mixer-init))
138 (open-audio)
139 (start-text-input)
140 (open-all-controllers)
141 (let* ((window (make-window #:opengl? #t
142 #:title window-title
143 #:size (list window-width window-height)
144 #:fullscreen? window-fullscreen?))
145 (gl-context (make-gl-context window))
146 (default-viewport (make-viewport 0 0 window-width window-height))
147 (default-projection (orthographic-projection 0 window-width
148 window-height 0
149 0 1)))
150 (define (invert-y y)
151 ;; SDL's origin is the top-left, but our origin is the bottom
152 ;; left so we need to invert Y coordinates that SDL gives us.
153 (- window-height y))
154 (define (input-sdl)
155 (define (process-event event)
156 (cond
157 ((quit-event? event)
158 (quit))
159 ((keyboard-down-event? event)
160 (key-press (keyboard-event-key event)
161 (keyboard-event-scancode event)
162 (keyboard-event-modifiers event)
163 (keyboard-event-repeat? event)))
164 ((keyboard-up-event? event)
165 (key-release (keyboard-event-key event)
166 (keyboard-event-scancode event)
167 (keyboard-event-modifiers event)))
168 ((text-input-event? event)
169 (text-input (text-input-event-text event)))
170 ((mouse-button-down-event? event)
171 (mouse-press (mouse-button-event-button event)
172 (mouse-button-event-clicks event)
173 (mouse-button-event-x event)
174 (invert-y (mouse-button-event-y event))))
175 ((mouse-button-up-event? event)
176 (mouse-release (mouse-button-event-button event)
177 (mouse-button-event-x event)
178 (invert-y (mouse-button-event-y event))))
179 ((mouse-motion-event? event)
180 (mouse-move (mouse-motion-event-x event)
181 (invert-y (mouse-motion-event-y event))
182 (mouse-motion-event-x-rel event)
183 (- (mouse-motion-event-y-rel event))
184 (mouse-motion-event-buttons event)))
185 ((and (controller-device-event? event)
186 (eq? (controller-device-event-action event) 'added))
187 (controller-add (add-controller
188 (controller-device-event-which event))))
189 ((and (controller-device-event? event)
190 (eq? (controller-device-event-action event) 'removed))
191 (let ((controller (lookup-controller
192 (controller-device-event-which event))))
193 (controller-remove controller)
194 (remove-controller (controller-device-event-which event))
195 (close-game-controller controller)))
196 ((controller-button-down-event? event)
197 (controller-press (lookup-controller
198 (controller-button-event-which event))
199 (controller-button-event-button event)))
200 ((controller-button-up-event? event)
201 (controller-release (lookup-controller
202 (controller-button-event-which event))
203 (controller-button-event-button event)))
204 ((controller-axis-event? event)
205 (controller-move (lookup-controller
206 (controller-axis-event-which event))
207 (controller-axis-event-axis event)
208 (/ (controller-axis-event-value event) 32768.0)))))
209 ;; Process all pending events.
210 (let loop ((event (poll-event)))
211 (when event
212 (process-event event)
213 (loop (poll-event)))))
214 (define (update-sdl dt)
215 (input-sdl)
216 (update dt)
217 ;; Free any GPU resources that have been GC'd.
218 (gpu-reap!))
219 (define (render-sdl-opengl alpha)
220 ;; Switch to the null viewport to ensure that
221 ;; the default viewport will be re-applied and
222 ;; clear the screen.
223 (gpu-state-set! *viewport-state* null-viewport)
224 (with-viewport default-viewport
225 (with-projection default-projection
226 (draw alpha)))
227 (swap-gl-window window))
228 (dynamic-wind
229 (const #t)
230 (lambda ()
231 (parameterize ((current-window window))
232 ;; Attempt to activate vsync, if possible. Some systems do
233 ;; not support setting the OpenGL swap interval.
234 (catch #t
235 (lambda ()
236 (set-gl-swap-interval! 'vsync))
237 (lambda args
238 (display "warning: could not enable vsync\n"
239 (current-error-port))))
240 (load)
241 ;; Notify about all controllers that were already connected
242 ;; when the game was launched because SDL will not create
243 ;; events for them.
244 (hash-for-each (lambda (key controller)
245 (controller-add controller))
246 *controllers*)
247 (run-game* #:update update-sdl
248 #:render render-sdl-opengl
249 #:error error
250 #:time sdl-ticks
251 #:update-hz update-hz)))
252 (lambda ()
253 (delete-gl-context! gl-context)
254 (close-window! window)))))