summaryrefslogtreecommitdiff
path: root/catbird/kernel.scm
blob: 4ed642a45a754b4ce8db97a0d9b6264b7cc68e9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
(define-module (catbird kernel)
  #:use-module (catbird asset)
  #:use-module (catbird camera)
  #:use-module (catbird config)
  #:use-module (catbird input-map)
  #:use-module (catbird mixins)
  #:use-module (catbird mode)
  #:use-module (catbird region)
  #:use-module (catbird scene)
  #:use-module (chickadee)
  #:use-module (chickadee data array-list)
  #:use-module (chickadee math rect)
  #:use-module (ice-9 atomic)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (srfi srfi-1)
  #:use-module (system repl coop-server)
  #:export (all-regions
            create-full-region
            create-region
            find-region-by-name
            frames-per-second
            kill-region
            current-keyboard-focus
            take-keyboard-focus
            current-controller-focus
            take-controller-focus
            bind-input/global
            unbind-input/global
            run-catbird
            exit-catbird))


;;;
;;; Kernel
;;;

(define-root-class <kernel> ()
  (controllers #:getter controllers #:init-thunk make-array-list)
  (regions #:accessor regions #:init-value '())
  (input-map #:accessor input-map #:init-thunk make-input-map)
  (keyboard-focus #:accessor keyboard-focus #:init-value #f)
  (controller-focus #:getter controller-focus #:init-thunk make-hash-table)
  (repl #:accessor repl #:init-value #f)
  (frame-start-time #:accessor frame-start-time #:init-value 0.0)
  (average-frame-time #:accessor average-frame-time #:init-value 0.0))

(define-method (load* (kernel <kernel>))
  (when developer-mode?
    (set! (repl kernel) (spawn-coop-repl-server))))

;; Add the system notification and debugging overlay.
(define-method (add-overlay (kernel <kernel>))
  (let ((region (create-full-region #:name 'overlay #:rank 9999)))
    (set! (camera region)
          (make <camera-2d>
            #:width (rect-width (area region))
            #:height (rect-height (area region))))
    ;; Use resolve-module to avoid a circular dependency.
    (replace-scene region
                   ((module-ref (resolve-module '(catbird overlay))
                                'make-overlay)))))

(define-method (overlay-scene (kernel <kernel>))
  (scene (lookup-region kernel 'overlay)))

(define-method (notify (kernel <kernel>) message)
  (let ((notify (module-ref (resolve-module '(catbird overlay)) 'notify)))
    (notify (overlay-scene kernel) message)))

(define-method (update (kernel <kernel>) dt)
  (when developer-mode?
    (poll-coop-repl-server (repl kernel))
    (reload-modified-assets))
  (for-each (lambda (region) (update/around region dt))
            (regions kernel)))

(define-method (render (kernel <kernel>) alpha)
  (for-each (lambda (region)
              (render/around region alpha))
            (regions kernel))
  ;; Compute FPS.
  (let ((current-time (elapsed-time)))
    (set! (average-frame-time kernel)
          (+ (* (- current-time (frame-start-time kernel)) 0.1)
             (* (average-frame-time kernel) 0.9)))
    (set! (frame-start-time kernel) current-time)))

(define-method (lookup-region (kernel <kernel>) region-name)
  (find (lambda (region)
          (eq? (name region) region-name))
        (regions kernel)))

(define-method (add-region (kernel <kernel>) (region <region>))
  (let ((r (regions kernel)))
    ;; The first region added gets keyboard focus by default.
    (when (null? r)
      (set! (keyboard-focus kernel) region))
    (set! (regions kernel)
          (sort-by-rank/ascending (cons region (regions kernel))))))

(define-method (bind-input (kernel <kernel>) spec handler)
  (set! (input-map kernel) (add-input (input-map kernel) spec handler)))

(define-method (unbind-input (kernel <kernel>) spec)
  (set! (input-map kernel) (remove-input (input-map kernel) spec)))


;;;
;;; Keyboard
;;;

(define-method (on-key-press (kernel <kernel>) key modifiers)
  (or (let ((handler (key-press-handler (input-map kernel) key modifiers)))
        (and handler (handler)))
      (let* ((r (keyboard-focus kernel))
             (s (and r (scene r))))
        (and s (on-key-press s key modifiers)))))

(define-method (on-key-release (kernel <kernel>) key modifiers)
  (or (let ((handler (key-release-handler (input-map kernel) key modifiers)))
        (and handler (handler)))
      (let* ((r (keyboard-focus kernel))
             (s (and r (scene r))))
        (and s (on-key-release s key modifiers)))))

(define-method (on-text-input (kernel <kernel>) text)
  (or (let ((handler (text-input-handler (input-map kernel))))
        (and handler (handler text)))
      (let* ((r (keyboard-focus kernel))
             (s (and r (scene r))))
        (and s (on-text-input s text)))))


;;;
;;; Mouse
;;;

(define (mouse-search kernel proc)
  (let loop ((regions* (regions kernel)))
    (match regions*
      (() #f)
      ((r . rest)
       (or (loop rest)
           (let ((s (scene r)))
             (and s (proc s))))))))

(define-method (on-mouse-press (kernel <kernel>) button x y)
  (or (let ((handler (mouse-press-handler (input-map kernel) button)))
        (and handler (handler x y)))
      (mouse-search kernel
                    (lambda (s)
                      (on-mouse-press s button x y)))))

(define-method (on-mouse-release (kernel <kernel>) button x y)
  (or (let ((handler (mouse-release-handler (input-map kernel) button)))
        (and handler (handler x y)))
      (mouse-search kernel
                    (lambda (s)
                      (on-mouse-release s button x y)))))

(define-method (on-mouse-move (kernel <kernel>) x y x-rel y-rel buttons)
  (or (let ((handler (mouse-move-handler (input-map kernel) buttons)))
        (and handler (handler x y x-rel y-rel)))
      (mouse-search kernel
                    (lambda (s)
                      (on-mouse-move s x y x-rel y-rel buttons)))))

(define-method (on-mouse-wheel (kernel <kernel>) x y)
  (or (let ((handler (mouse-wheel-handler (input-map kernel))))
        (and handler (handler x y)))
      (mouse-search kernel
                    (lambda (s)
                      (on-mouse-wheel s x y)))))


;;;
;;; Controllers
;;;

(define-method (controller-focus (kernel <kernel>) slot)
  (hashv-ref (controller-focus kernel) (controller-slot-id slot)))

(define (make-controller-slot id)
  (vector id #f))

(define (controller-slot-id slot)
  (vector-ref slot 0))

(define (controller-slot-controller slot)
  (vector-ref slot 1))

(define (controller-slot-empty? slot)
  (not (controller-slot-controller slot)))

(define (fill-controller-slot! slot controller)
  (vector-set! slot 1 controller))

(define (clear-controller-slot! slot)
  (fill-controller-slot! slot #f))

(define-method (empty-controller-slot (kernel <kernel>))
  (let* ((c (controllers kernel))
         (n (array-list-size c)))
    (let loop ((i 0))
      (if (= i n)
          (let ((slot (make-controller-slot i)))
            (array-list-push! c slot)
            slot)
          (let ((slot (array-list-ref c i)))
            (if (controller-slot-empty? slot)
                slot
                (loop (+ i 1))))))))

(define-method (find-controller-slot (kernel <kernel>) controller)
  (let* ((c (controllers kernel))
         (n (array-list-size c)))
    (let loop ((i 0))
      (if (= i n)
          #f
          (let ((slot (array-list-ref c i)))
            (if (eq? (controller-slot-controller slot) controller)
                slot
                (loop (+ i 1))))))))

(define-method (on-controller-add (kernel <kernel>) controller)
  (let ((slot (empty-controller-slot kernel)))
    (notify kernel (string-append "Controller "
                                  (number->string
                                   (+ (controller-slot-id slot) 1))
                                  " connected: "
                                  (controller-name controller)))
    (fill-controller-slot! slot controller)))

(define-method (on-controller-remove (kernel <kernel>) controller)
  (let ((slot (find-controller-slot kernel controller)))
    (notify kernel (string-append "Controller "
                                  (number->string
                                   (+ (controller-slot-id slot) 1))
                                  " disconnected: "
                                  (controller-name controller)))
    (clear-controller-slot! (find-controller-slot kernel controller))))

(define-method (on-controller-press (kernel <kernel>) controller button)
  (let ((slot (find-controller-slot kernel controller)))
    (or (let ((handler (controller-press-handler (input-map kernel)
                                                 (controller-slot-id slot)
                                                 button)))
          (and handler (handler)))
        (let* ((r (controller-focus kernel slot))
               (s (and r (scene r))))
          (and r (on-controller-press s
                                      (controller-slot-id slot)
                                      button))))))

(define-method (on-controller-release (kernel <kernel>) controller button)
  (let ((slot (find-controller-slot kernel controller)))
    (or (let ((handler (controller-release-handler (input-map kernel)
                                                   (controller-slot-id slot)
                                                   button)))
          (and handler (handler)))
        (let* ((r (controller-focus kernel slot))
               (s (and r (scene r))))
          (and s (on-controller-release s
                                        (controller-slot-id slot)
                                        button))))))

(define-method (on-controller-move (kernel <kernel>) controller axis value)
  (let ((slot (find-controller-slot kernel controller)))
    (or (let ((handler (controller-move-handler (input-map kernel)
                                                (controller-slot-id slot)
                                                axis)))
          (and handler (handler value)))
        (let* ((r (controller-focus kernel slot))
               (s (and r (scene r))))
          (and s (on-controller-move s
                                     (controller-slot-id slot)
                                     axis
                                     value))))))


;;;
;;; Global kernel API
;;;

(define current-kernel (make-parameter #f))

(define (unique-region-name)
  (gensym "region-"))

(define* (create-region area #:key (rank 0) (name (unique-region-name)))
  (let ((region (make-region area name rank)))
    (add-region (current-kernel) region)
    region))

(define* (create-full-region #:key (rank 0) (name (unique-region-name)))
  (let ((w (window-width (current-window)))
        (h (window-height (current-window))))
    (create-region (make-rect 0.0 0.0 w h) #:rank rank #:name name)))

(define (kill-region region)
  (let ((k (current-kernel)))
    (set! (regions k) (delq region (regions k)))))

(define (all-regions)
  (regions (current-kernel)))

(define (find-region-by-name name)
  (lookup-region (current-kernel) name))

(define (current-keyboard-focus)
  (keyboard-focus (current-kernel)))

(define (take-keyboard-focus region)
  (set! (keyboard-focus (current-kernel)) region))

(define (current-controller-focus controller-id)
  (hashv-ref (controller-focus (current-kernel)) controller-id))

(define (take-controller-focus controller-id region)
  (hashv-set! (controller-focus (current-kernel)) controller-id region))

(define (bind-input/global spec handler)
  (bind-input (current-kernel) spec handler))

(define (unbind-input/global spec handler)
  (unbind-input (current-kernel) spec handler))

(define (frames-per-second)
  (/ 1.0 (average-frame-time (current-kernel))))

(define* (run-catbird thunk #:key (width 1366) (height 768)
                      (title "^~Catbird~^") (fullscreen? #f)
                      (resizable? #t) (update-hz 60))
  (let ((kernel (make <kernel>)))
    (parameterize ((current-kernel kernel))
      (run-game #:window-title title
                #:window-width width
                #:window-height height
                #:window-fullscreen? fullscreen?
                #:window-resizable? resizable?
                #:update-hz update-hz
                #:load
                (lambda ()
                  (load* kernel)
                  (thunk)
                  (add-overlay kernel))
                #:draw
                (lambda (alpha)
                  (render kernel alpha))
                #:update
                (lambda (dt)
                  (update kernel dt))
                #:key-press
                (lambda (key modifiers repeat?)
                  (on-key-press kernel key modifiers))
                #:key-release
                (lambda (key modifiers)
                  (on-key-release kernel key modifiers))
                #:text-input
                (lambda (text)
                  (on-text-input kernel text))
                #:mouse-press
                ;; TODO: Handle click counter?
                (lambda (button clicks x y)
                  (on-mouse-press kernel button x y))
                #:mouse-release
                (lambda (button x y)
                  (on-mouse-release kernel button x y))
                #:mouse-move
                (lambda (x y x-rel y-rel buttons)
                  (on-mouse-move kernel x y x-rel y-rel buttons))
                #:mouse-wheel
                (lambda (x y)
                  (on-mouse-wheel kernel x y))
                #:controller-add
                (lambda (controller)
                  (on-controller-add kernel controller))
                #:controller-remove
                (lambda (controller)
                  (on-controller-remove kernel controller))
                #:controller-press
                (lambda (controller button)
                  (on-controller-press kernel controller button))
                #:controller-release
                (lambda (controller button)
                  (on-controller-release kernel controller button))
                #:controller-move
                (lambda (controller axis value)
                  (on-controller-move kernel controller axis value))))))

(define (exit-catbird)
  "Stop the Catbird engine."
  (abort-game))