3c10880fdd71265b8e4a180be1ff7830429be750
[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 text)
30 #:use-module (sdl2 mixer)
31 #:use-module (sdl2 video)
32 #:use-module (chickadee game-loop)
33 #:use-module (chickadee math matrix)
34 #:use-module (chickadee render)
35 #:use-module (chickadee render color)
36 #:use-module (chickadee render gl)
37 #:use-module (chickadee render gpu)
38 #:use-module (chickadee render viewport)
39 #:export (current-window
40 run-game)
41 #:re-export (abort-game))
42
43 (define *controllers* (make-hash-table))
44
45 (define (lookup-controller joystick-id)
46 (hashv-ref *controllers* joystick-id))
47
48 (define (add-controller joystick-index)
49 (let ((controller (open-game-controller joystick-index)))
50 (hashv-set! *controllers*
51 (joystick-instance-id
52 (game-controller-joystick controller))
53 controller)
54 controller))
55
56 (define (remove-controller joystick-id)
57 (hashv-remove! *controllers* joystick-id))
58
59 (define (open-all-controllers)
60 (let loop ((i 0))
61 (when (< i (num-joysticks))
62 (when (game-controller-index? i)
63 (add-controller i))
64 (loop (+ i 1)))))
65
66 (define current-window (make-parameter #f))
67
68 (define* (run-game #:key
69 (window-title "Chickadee!")
70 (window-width 640)
71 (window-height 480)
72 window-fullscreen?
73 (update-hz 60)
74 (load (const #t))
75 (update (const #t))
76 (draw (const #t))
77 (quit abort-game)
78 (key-press (const #t))
79 (key-release (const #t))
80 (text-input (const #t))
81 (mouse-press (const #t))
82 (mouse-release (const #t))
83 (mouse-move (const #t))
84 (controller-add (const #t))
85 (controller-remove (const #t))
86 (controller-press (const #t))
87 (controller-release (const #t))
88 (controller-move (const #t))
89 error)
90 (sdl-init)
91 (false-if-exception (mixer-init))
92 (open-audio)
93 (start-text-input)
94 (open-all-controllers)
95 (let* ((window (make-window #:opengl? #t
96 #:title window-title
97 #:size (list window-width window-height)
98 #:fullscreen? window-fullscreen?))
99 (gl-context (make-gl-context window))
100 (default-viewport (make-viewport 0 0 window-width window-height))
101 (default-projection (orthographic-projection 0 window-width
102 window-height 0
103 0 1)))
104 (define (invert-y y)
105 ;; SDL's origin is the top-left, but our origin is the bottom
106 ;; left so we need to invert Y coordinates that SDL gives us.
107 (- window-height y))
108 (define (input-sdl)
109 (define (process-event event)
110 (cond
111 ((quit-event? event)
112 (quit))
113 ((keyboard-down-event? event)
114 (key-press (keyboard-event-key event)
115 (keyboard-event-scancode event)
116 (keyboard-event-modifiers event)
117 (keyboard-event-repeat? event)))
118 ((keyboard-up-event? event)
119 (key-release (keyboard-event-key event)
120 (keyboard-event-scancode event)
121 (keyboard-event-modifiers event)))
122 ((text-input-event? event)
123 (text-input (text-input-event-text event)))
124 ((mouse-button-down-event? event)
125 (mouse-press (mouse-button-event-button event)
126 (mouse-button-event-clicks event)
127 (mouse-button-event-x event)
128 (invert-y (mouse-button-event-y event))))
129 ((mouse-button-up-event? event)
130 (mouse-release (mouse-button-event-button event)
131 (mouse-button-event-x event)
132 (invert-y (mouse-button-event-y event))))
133 ((mouse-motion-event? event)
134 (mouse-move (mouse-motion-event-x event)
135 (invert-y (mouse-motion-event-y event))
136 (mouse-motion-event-x-rel event)
137 (- (mouse-motion-event-y-rel event))
138 (mouse-motion-event-buttons event)))
139 ((and (controller-device-event? event)
140 (eq? (controller-device-event-action event) 'added))
141 (controller-add (add-controller
142 (controller-device-event-which event))))
143 ((and (controller-device-event? event)
144 (eq? (controller-device-event-action event) 'removed))
145 (let ((controller (lookup-controller
146 (controller-device-event-which event))))
147 (controller-remove controller)
148 (remove-controller (controller-device-event-which event))
149 (close-game-controller controller)))
150 ((controller-button-down-event? event)
151 (controller-press (lookup-controller
152 (controller-button-event-which event))
153 (controller-button-event-button event)))
154 ((controller-button-up-event? event)
155 (controller-release (lookup-controller
156 (controller-button-event-which event))
157 (controller-button-event-button event)))
158 ((controller-axis-event? event)
159 (controller-move (lookup-controller
160 (controller-axis-event-which event))
161 (controller-axis-event-axis event)
162 (/ (controller-axis-event-value event) 32768.0)))))
163 ;; Process all pending events.
164 (let loop ((event (poll-event)))
165 (when event
166 (process-event event)
167 (loop (poll-event)))))
168 (define (update-sdl dt)
169 (input-sdl)
170 (update dt)
171 ;; Free any GPU resources that have been GC'd.
172 (gpu-reap!))
173 (define (render-sdl-opengl alpha)
174 ;; Switch to the null viewport to ensure that
175 ;; the default viewport will be re-applied and
176 ;; clear the screen.
177 (gpu-state-set! *viewport-state* null-viewport)
178 (with-viewport default-viewport
179 (with-projection default-projection
180 (draw alpha)))
181 (swap-gl-window window))
182 (dynamic-wind
183 (const #t)
184 (lambda ()
185 (parameterize ((current-window window))
186 ;; Attempt to activate vsync, if possible. Some systems do
187 ;; not support setting the OpenGL swap interval.
188 (catch #t
189 (lambda ()
190 (set-gl-swap-interval! 'vsync))
191 (lambda args
192 (display "warning: could not enable vsync\n"
193 (current-error-port))))
194 (load)
195 ;; Notify about all controllers that were already connected
196 ;; when the game was launched because SDL will not create
197 ;; events for them.
198 (hash-for-each (lambda (key controller)
199 (controller-add controller))
200 *controllers*)
201 (run-game* #:update update-sdl
202 #:render render-sdl-opengl
203 #:error error
204 #:time sdl-ticks
205 #:update-hz update-hz)))
206 (lambda ()
207 (delete-gl-context! gl-context)
208 (close-window! window)))))