summaryrefslogtreecommitdiff
path: root/chickadee/cli/play.scm
blob: e66d063a6b53f6c21a044f49388c00379e940cff (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
;;; Chickadee Game Toolkit
;;; Copyright © 2021 David Thompson <dthompson2@worcester.edu>
;;;
;;; 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 cli play)
  #:declarative? #f
  #:use-module (chickadee)
  #:use-module (chickadee async-repl)
  #:use-module (chickadee cli)
  #:use-module (chickadee config)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics viewport)
  #:use-module (ice-9 exceptions)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-37)
  #:use-module (system base compile)
  #:use-module (system repl command)
  #:use-module (system repl debug)
  #:use-module (system repl coop-server)
  #:use-module (system repl server)
  #:export (chickadee-play
            %default-modules))

(define (display-help-and-exit)
  (format #t "Usage: chickadee play [OPTIONS] FILE~%
Play the game defined in FILE.~%")
  (display "
  --help                 display this help and exit")
  (display "
  -t, --title=TITLE      set window title to TITLE")
  (display "
  -w, --width=WIDTH      set window width to WIDTH")
  (display "
  -h, --height=HEIGHT    set window height to HEIGHT")
  (display "
  -f, --fullscreen       fullscreen mode")
  (display "
  -r, --resizable        allow window to be resized")
  (display "
  -u, --update-hz=N      set update rate to N times per second")
  (display "
  -c, --clear-color=RGB  set the clear color to hex color code RGB")
  (display "
 --repl                  start REPL in this terminal")
  (display "
 --repl-server=[PORT]    start REPL server on PORT or 37146 by default")
  (display "
 --language=LANG         change language; default: scheme")
  (display "
 -x EXTENSION            add EXTENSION to the front of the load extensions")
  (newline)
  (exit 1))

(define %options
  (list (option '("help") #f #f
                (lambda (opt name arg result)
                  (display-help-and-exit)))
        (option '(#\t "title") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'title arg result)))
        (option '(#\w "width") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'width (string->number arg) result)))
        (option '(#\h "height") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'height (string->number arg) result)))
        (option '(#\f "fullscreen") #f #f
                (lambda (opt name arg result)
                  (alist-cons 'fullscreen? #t result)))
        (option '(#\r "resizable") #f #f
                (lambda (opt name arg result)
                  (alist-cons 'resizable? #f result)))
        (option '(#\u "update-hz") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'update-hz (string->number arg) result)))
        (option '(#\c "clear-color") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'clear-color (string->color arg) result)))
        (option '("repl") #f #f
                (lambda (opt name arg result)
                  (alist-cons 'repl #t result)))
        (option '("repl-server") #f #t
                (lambda (opt name arg result)
                  (alist-cons 'repl
                              (if arg
                                  (string->number arg)
                                  37146)
                              result)))
        (option '("language") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'language (string->symbol arg) result)))
        (option '(#\x) #t #f
                (lambda (opt name arg result)
                  (alist-cons 'extension arg result)))))

(define %default-options
  `((title . "chickadee")
    (width . 640)
    (height . 480)
    (fullscreen? . #f)
    (resizable? . #f)
    (update-hz . 60)
    (clear-color . ,%default-clear-color)
    (repl . #f)
    (language . scheme)))

(define %default-modules
  '((chickadee)
    (chickadee audio)
    (chickadee graphics color)
    (chickadee graphics engine)
    (chickadee graphics text)
    (chickadee graphics texture)
    (chickadee math)
    (chickadee math matrix)
    (chickadee math rect)
    (chickadee math vector)
    (chickadee scripting)))

(define (make-user-module)
  (let ((module (resolve-module '(chickadee-user) #f)))
    (beautify-user-module! module)
    ;; Automatically load commonly used modules for
    ;; maximum convenience.
    (for-each (lambda (name)
                (module-use! module (resolve-interface name)))
              %default-modules)
    (module-define! module 'quit-game (lambda () (abort-game)))
    module))

(define-record-type <game-debugger>
  (make-game-debugger)
  game-debugger?
  (debug game-debugger-debug set-game-debugger-debug!)
  (debugging? game-debugger-debugging? set-game-debugger-debugging!))

(define *debugger* (make-game-debugger))

(define (launch-game file-name opts)
  (let ((module (make-user-module))
        (repl #f)
        (debug #f))
    (define-meta-command ((debug-game chickadee) r)
      "debug-game
Enter a debugger for the current game loop error."
      (if debug
          (begin
            (set-async-repl-debug! repl debug))
          (begin
            (display "nothing to debug")
            (newline))))
    (define-meta-command ((resume-game chickadee) r)
      "resume-game
Resume the game loop without entering a debugger."
      (if debug
          (set! debug #f)
          (begin
            (display "not currently debugging")
            (newline))))
    (define-syntax-rule (trampoline name args ...)
      (lambda (args ...)
        (let ((proc (false-if-exception (module-ref module 'name))))
          (when (procedure? proc)
            (proc args ...)))))
    (define (load-game)
      (save-module-excursion
       (lambda ()
         (let ((dir (dirname file-name)))
           (set-current-module module)
           (chdir dir)
           (add-to-load-path dir)
           (set! %load-extensions
                 (append (filter-map (match-lambda
                                       (('extension . ext) ext)
                                       (_ #f))
                                     opts)
                         %load-extensions))
           (load-compiled
            (compile-file (basename file-name)
                          #:from (assq-ref opts 'language)
                          #:env module))
           (parameterize ((current-language (assq-ref opts 'language)))
             (let ((repl-opt (assq-ref opts 'repl)))
               (cond
                ((number? repl-opt)
                 (set! repl (spawn-coop-repl-server
                             (make-tcp-server-socket #:port repl-opt))))
                (repl-opt
                 (set! repl (make-async-repl))
                 (start-async-repl repl abort-game)))))))))
    (define (handle-error e stack)
      ;; Setup the REPL debug object.
      (let* ((frame (stack-ref stack 0))
             (error-string
              (format #f "~a: In procedure: ~a:~%In procedure: ~a: ~a~%"
                      (match (frame-source frame)
                        ((_ file-name line . column)
                         (format #f "~a:~a:~a"
                                 (if file-name
                                     (basename file-name)
                                     "unknown file")
                                 line column))
                        (_ "unknown"))
                      (or (frame-procedure-name frame)
                          "unknown")
                      (or (and (exception-with-origin? e)
                               (exception-origin e))
                          "unknown")
                      (if (and (exception-with-message? e)
                               (exception-with-irritants? e))
                          (apply format #f (exception-message e)
                                 (exception-irritants e))
                          ""))))
        (set! debug (make-debug (narrow-stack->vector stack 0) 0 error-string))
        ;; Just update the REPL endlessly until the developer says to
        ;; resume.
        (let* ((window (current-window))
               (old-title (window-title (current-window))))
          (set-window-title! window
                             "*** ERROR: Run ,debug-game in REPL for details ***")
          (while debug
            (update-repl)
            (usleep 1000))
          (set-window-title! window old-title))))
    (define (update-repl)
      (cond
       ((async-repl? repl)
        (update-async-repl repl))
       (repl
        (poll-coop-repl-server repl))))
    ;; Run game loop, deferring all event handlers to those defined
    ;; in the user's Scheme file.
    (run-game #:window-title (assq-ref opts 'title)
              #:window-width (assq-ref opts 'width)
              #:window-height (assq-ref opts 'height)
              #:window-fullscreen? (assq-ref opts 'fullscreen?)
              #:window-resizable? (assq-ref opts 'resizable?)
              #:update-hz (assq-ref opts 'update-hz)
              #:clear-color (assq-ref opts 'clear-color)
              #:load load-game
              #:update (let ((update* (trampoline update dt)))
                         (lambda (dt)
                           (update-repl)
                           (update* dt)))
              #:draw (trampoline draw alpha)
              #:quit (trampoline quit-game)
              #:key-press (trampoline key-press key modifiers repeat?)
              #:key-release (trampoline key-release key modifiers)
              #:text-input (trampoline text-input text)
              #:mouse-press (trampoline mouse-press button clicks x y)
              #:mouse-release (trampoline mouse-release button x y)
              #:mouse-move (trampoline mouse-move x y x-rel y-rel buttons)
              #:mouse-wheel (trampoline mouse-wheel x y)
              #:controller-add (trampoline controller-add controller)
              #:controller-remove (trampoline controller-remove controller)
              #:controller-press (trampoline controller-press controller
                                             button)
              #:controller-release (trampoline controller-release controller
                                               button)
              #:controller-move (trampoline controller-move controller axis
                                            value)
              #:error (if (or (assq-ref opts 'repl)
                              (assq-ref opts 'repl-server))
                          handle-error
                          #f))
    (when (async-repl? repl)
      (close-async-repl repl))))

(define (chickadee-play . args)
  (let ((opts (simple-args-fold args %options %default-options)))
    (match (operands opts)
      (()
       (leave "you must specify a Scheme file to load"))
      ((file-name)
       (launch-game file-name opts))
      (_
       (leave "too many arguments specified. just pass a Scheme file name.")))))