summaryrefslogtreecommitdiff
path: root/chickadee.scm
blob: eb984430ef788527c83e05f68104ab29c15dd5b0 (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
;;; Chickadee Game Toolkit
;;; Copyright © 2016, 2018 David Thompson <davet@gnu.org>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (chickadee)
  #:use-module (gl)
  #:use-module (sdl2)
  #:use-module (sdl2 events)
  #:use-module (sdl2 input text)
  #:use-module (chickadee math matrix)
  #:use-module (chickadee window)
  #:use-module (chickadee render)
  #:use-module (chickadee render color)
  #:use-module (chickadee render gl)
  #:use-module (chickadee render gpu)
  #:use-module (chickadee render viewport)
  #:export (run-game*
            abort-game
            run-game/sdl))


;;;
;;; Error handling
;;;

(define (display-game-loop-error stack key args)
  (let ((port (current-error-port)))
    (display "Backtrace:\n" port)
    (display-backtrace stack port)
    (newline port)
    (apply display-error (stack-ref stack 0) port args)
    (newline port)))

(define (call-with-error-handling handler thunk)
  (let ((stack #f))
    (catch #t
      thunk
      (lambda (key . args)
        (display-game-loop-error stack key args)
        (error stack key args))
      (lambda (key . args)
        (set! stack (make-stack #t 3))))))

(define-syntax-rule (with-error-handling handler body ...)
  (call-with-error-handling handler  (lambda () body ...)))


;;;
;;; Game loop core
;;;

(define game-loop-prompt-tag (make-prompt-tag 'game-loop))

(define (abort-game)
  (abort-to-prompt game-loop-prompt-tag #f))

(define* (run-game #:key update render time error (update-hz 60))
  (let ((timestep (round (/ 1000 update-hz))))
    (call-with-prompt game-loop-prompt-tag
      (lambda ()
        ;; Catch SIGINT and kill the loop.
        (sigaction SIGINT
          (lambda (signum)
            (abort-game)))
        ;; A simple analogy is that we are filling up a bucket
        ;; with water.  When the bucket fills up to a marked
        ;; line, we dump it out.  Our water is time, and each
        ;; time we dump the bucket we update the game.  Updating
        ;; the game on a fixed timestep like this yields a
        ;; stable simulation.
        (let loop ((previous-time (time))
                   (buffer 0))
          (let* ((current-time (time))
                 (delta (- current-time previous-time)))
            (let update-loop ((buffer (+ buffer delta)))
              (if (>= buffer timestep)
                  (begin
                    (with-error-handling error (update timestep))
                    (update-loop (- buffer timestep)))
                  (begin
                    ;; We render upon every iteration of the loop, and
                    ;; thus rendering is decoupled from updating.
                    ;; It's possible to render multiple times before
                    ;; an update is performed.
                    (with-error-handling error (render (/ buffer timestep)))
                    (loop current-time buffer)))))))
      (lambda (cont callback)
        #f))))


;;;
;;; Simple SDL + OpenGL engine
;;;

;; Good enough for simple games.

(define open-controller (@@ (chickadee input controller) open-controller))
(define close-controller (@@ (chickadee input controller) close-controller))
(define lookup-controller (@@ (chickadee input controller) lookup-controller))

(define* (run-game/sdl #:key
                       (window-title "Chickadee!")
                       (window-width 640)
                       (window-height 480)
                       window-fullscreen?
                       (update-hz 60)
                       (load (const #t))
                       (update (const #t))
                       (draw (const #t))
                       (quit abort-game)
                       (key-press (const #t))
                       (key-release (const #t))
                       (text-input (const #t))
                       (mouse-press (const #t))
                       (mouse-release (const #t))
                       (mouse-move (const #t))
                       (controller-add (const #t))
                       (controller-remove (const #t))
                       (controller-press (const #t))
                       (controller-release (const #t))
                       (controller-move (const #t))
                       (error (const #t)))
  (sdl-init)
  ((@@ (chickadee audio) enable-audio))
  (start-text-input)
  (let ((window (open-window #:title window-title
                             #:width window-width
                             #:height window-height
                             #:fullscreen? window-fullscreen?))
        (default-viewport (make-viewport 0 0 window-width window-height))
        (default-projection (orthographic-projection 0 window-width
                                                     window-height 0
                                                     0 1)))
    (define (invert-y y)
      ;; SDL's origin is the top-left, but our origin is the bottom
      ;; left so we need to invert Y coordinates that SDL gives us.
      (- window-height y))

    (define (input-sdl)
      (define (process-event event)
        (cond
         ((quit-event? event)
          (quit))
         ((keyboard-down-event? event)
          (key-press (keyboard-event-key event)
                     (keyboard-event-scancode event)
                     (keyboard-event-modifiers event)
                     (keyboard-event-repeat? event)))
         ((keyboard-up-event? event)
          (key-release (keyboard-event-key event)
                       (keyboard-event-scancode event)
                       (keyboard-event-modifiers event)))
         ((text-input-event? event)
          (text-input (text-input-event-text event)))
         ((mouse-button-down-event? event)
          (mouse-press (mouse-button-event-button event)
                       (mouse-button-event-clicks event)
                       (mouse-button-event-x event)
                       (invert-y (mouse-button-event-y event))))
         ((mouse-button-up-event? event)
          (mouse-release (mouse-button-event-button event)
                         (mouse-button-event-x event)
                         (invert-y (mouse-button-event-y event))))
         ((mouse-motion-event? event)
          (mouse-move (mouse-motion-event-x event)
                      (invert-y (mouse-motion-event-y event))
                      (mouse-motion-event-x-rel event)
                      (- (mouse-motion-event-y-rel event))
                      (mouse-motion-event-buttons event)))
         ((and (controller-device-event? event)
               (eq? (controller-device-event-action event) 'added))
          (controller-add
           (open-controller (controller-device-event-which event))))
         ((and (controller-device-event? event)
               (eq? (controller-device-event-action event) 'removed))
          (let ((controller (lookup-controller
                             (controller-device-event-which event))))
            (controller-remove controller)
            (close-controller controller)))
         ((controller-button-down-event? event)
          (controller-press (lookup-controller
                             (controller-button-event-which event))
                            (controller-button-event-button event)))
         ((controller-button-up-event? event)
          (controller-release (lookup-controller
                               (controller-button-event-which event))
                              (controller-button-event-button event)))
         ((controller-axis-event? event)
          (controller-move (lookup-controller
                            (controller-axis-event-which event))
                           (controller-axis-event-axis event)
                           (/ (controller-axis-event-value event) 32768.0)))))
      ;; Process all pending events.
      (let loop ((event (poll-event)))
        (when event
          (process-event event)
          (loop (poll-event)))))

    (define (update-sdl dt)
      (input-sdl)
      (update dt)
      ;; Free any GPU resources that have been GC'd.
      (gpu-reap!))

    (define (render-sdl-opengl alpha)
      ;; Switch to the null viewport to ensure that
      ;; the default viewport will be re-applied and
      ;; clear the screen.
      (gpu-state-set! *viewport-state* null-viewport)
      (with-viewport default-viewport
        (with-projection default-projection
          (draw alpha)))
      (swap-buffers window))
    (with-window window
      (load)
      (run-game #:update update-sdl
                #:render render-sdl-opengl
                #:error error
                #:time sdl-ticks
                #:update-hz update-hz))))