]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
b9d13160dccac4bce3695d60fb0922e657d3393f
[gacela.git] / src / gacela.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU 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 <http://www.gnu.org/licenses/>.
16
17
18 (define-module (gacela gacela)
19   #:use-module (gacela events)
20   #:use-module (gacela video)
21   #:use-module (gacela audio)
22   #:use-module (ice-9 optargs)
23   #:export (*title*
24             *width-screen*
25             *height-screen*
26             *bpp-screen*
27             *frames-per-second*
28             *mode*
29             set-game-properties!
30             get-game-properties
31             init-gacela
32             quit-gacela
33             game-loop
34             gacela-script
35             game-running?
36             show-mob-hash
37             hide-mob-hash
38             get-active-mobs
39             hide-all-mobs
40             get-current-mob-id
41             get-mob-function-name
42             map-mobs
43             translate-mob)
44   #:export-syntax (game
45                    show-mob
46                    hide-mob
47                    the-mob
48                    define-mob-function
49                    define-mob
50                    lambda-mob
51                    define-checking-mobs)
52   #:re-export (get-frame-time
53                3d-mode?))
54
55
56 ;;; Main Loop
57
58 (define game-loop-flag #f)
59 (define game-loop-thread #f)
60 (define game-loop-procedure #f)
61
62 (define-macro (run-in-game-loop proc)
63   (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
64         (flag-symbol (gensym))
65         (value-symbol (gensym)))
66     `(begin
67        (define ,pgl ,proc)
68        (define (,proc . param)
69          (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
70                 (let ((,flag-symbol #f))
71                   (define ,value-symbol)
72                   (system-async-mark
73                    (lambda ()
74                      (catch #t
75                            (lambda () (set! ,value-symbol (apply ,pgl param)))
76                            (lambda (key . args) #f))
77                      (set! ,flag-symbol #t))
78                    game-loop-thread)
79                   (while (not ,flag-symbol))
80                   ,value-symbol))
81                (else
82                 (apply ,pgl param)))))))
83
84 (run-in-game-loop load-texture)
85 (run-in-game-loop load-font)
86 (run-in-game-loop set-screen-bpp!)
87 (run-in-game-loop resize-screen)
88
89 (define-macro (game . code)
90   `(set! game-loop-procedure
91     ,(if (null? code)
92          `#f
93          `(lambda (game-elements) ,@code))))
94
95 (define (init-gacela)
96 ;  (hide-all-mobs)
97   (cond ((not game-loop-thread)
98          (set! game-loop-thread (call-with-new-thread (lambda () (cond ((not (game-running?)) (game-loop))))))))
99   (while (not game-loop-flag))
100   #t)
101
102 (define (quit-gacela)
103 ;  (hide-all-mobs)
104   (set! game-loop-thread #f)
105   (set! game-loop-flag #f)
106   (quit-video))
107
108 (define (game-loop)
109 ;  (refresh-active-mobs)
110   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
111   (set! game-loop-flag #t)
112   (let loop ((game-elements '()))
113     (cond (game-loop-flag
114            (init-frame-time)
115 ;           (check-connections)
116            (process-events)
117            (cond ((quit-signal?)
118                   (quit-gacela))
119                  (else
120                   (clear-screen)
121                   (to-origin)
122 ;                 (refresh-active-mobs)
123 ;                 (run-mobs)
124 ;                 (run-extensions)
125                   (if game-loop-procedure
126                       (catch #t
127                              (lambda () (set! game-elements (game-loop-procedure game-elements)))
128                              (lambda (key . args) #f)))
129                   (process-game-elements game-elements)
130                   (flip-screen)
131                   (delay-frame)
132                   (loop game-elements)))))))
133
134 (define (game-running?)
135   game-loop-flag)
136
137 (define (process-game-elements elements)
138   (cond ((not (list? elements))
139          (process-game-elements (list elements)))
140         (else
141          (draw-meshes (filter (lambda (e) (mesh? e)) elements)))))
142
143 (define (draw-meshes meshes)
144   (cond ((null? meshes) #t)
145         (else
146          (catch #t
147                 (lambda () (mesh-draw (car meshes)))
148                 (lambda (key . args) #f))
149          (draw-meshes (cdr meshes)))))
150
151 ;;; Extensions to main loop
152
153 (define extensions '())
154
155 (define (add-extension! proc pri)
156   "Add an extension with a priority to the main loop"
157   (set! extensions
158         (sort (assoc-set! extensions proc pri)
159               (lambda (a b)
160                 (< (cdr a) (cdr b))))))
161
162 (define (remove-extension! proc)
163   "Remove an extension from the main loop"
164   (set! extensions
165         (assoc-remove! extensions proc)))
166
167 (define (run-extensions)
168   (for-each (lambda (x) ((car x))) extensions))
169
170
171 ;;; Game Properties
172
173 (define *title* "Gacela")
174 (define *width-screen* 640)
175 (define *height-screen* 480)
176 (define *bpp-screen* 32)
177 (define *frames-per-second* 20)
178 (define *mode* '2d)
179 (define *fullscreen* 'off)
180
181 (define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
182   (if title
183       (set-screen-title! title))
184   (if bpp
185       (set-screen-bpp! bpp))
186   (if (or width height)
187       (begin
188         (if (not width) (set! width (get-screen-width)))
189         (if (not height) (set! height (get-screen-height)))
190         (resize-screen width height)))
191   (if fps
192       (set-frames-per-second! fps))
193   (if mode
194       (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
195   (if fullscreen
196       (set-fullscreen! fullscreen))
197   (get-game-properties))
198
199 (define (get-game-properties)
200   `((title . ,(get-screen-title)) (width . ,(get-screen-width)) (height . ,(get-screen-height)) (bpp . ,(get-screen-bpp)) (fps . ,(get-frames-per-second)) (mode . ,(if (3d-mode?) '3d '2d)) (fullscreen . ,(get-fullscreen))))
201
202
203 ;;; Mobs Factory
204
205 (define mobs-table (make-hash-table))
206 (define active-mobs '())
207 (define mobs-changed #f)
208
209 (define (show-mob-hash mob)
210   (hash-set! mobs-table (mob 'get-mob-id) mob)
211   (set! mobs-changed #t))
212
213 (define (hide-mob-hash mob-id)
214   (hash-remove! mobs-table mob-id)
215   (set! mobs-changed #t))
216
217 (define (refresh-active-mobs)
218   (cond (mobs-changed
219          (set! mobs-changed #f)
220          (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
221
222 (define (get-active-mobs)
223   active-mobs)
224
225 (define (hide-all-mobs)
226   (set! mobs-changed #t)
227   (hash-clear! mobs-table))
228
229 (define (mobs-changed?)
230   mobs-changed)
231
232
233 (define-macro (show-mob mob)
234   (cond ((list? mob)
235          `(let ((m ,mob))
236             (show-mob-hash m)))
237         (else
238          `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
239
240 (define-macro (hide-mob mob)
241   (cond ((list? mob)
242          `(let ((m ,mob))
243             (hide-mob-hash (m 'get-mob-id))))
244         (else
245          `(hide-mob-hash (,mob 'get-mob-id)))))
246
247 (define current-mob-id #f)
248
249 (define translate-mob translate)
250
251 (define (get-current-mob-id)
252   current-mob-id)
253
254 (define* (run-mobs #:optional (mobs (get-active-mobs)))
255   (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
256     (for-each
257      (lambda (m)
258        (set! current-mob-id (m 'get-mob-id))
259        (glmatrix-block (m)))
260      sorted-mobs)
261     (set! current-mob-id #f)))
262
263
264 ;;; Making mobs
265
266 (define mob-functions (make-hash-table))
267
268 (define (get-mob-function-name mob-name)
269   (let ((name (hash-ref mob-functions mob-name)))
270     (cond ((not name)
271            (set! name (gensym))
272            (hash-set! mob-functions mob-name name)))
273     name))
274
275 (define-macro (the-mob mob-name init-data)
276   `(let ((mob-id (gensym))
277          (mob-z-index 0)
278          (mob-time 0)
279          (mob-data ,init-data)
280          (saved-data ,init-data))
281      (lambda* (#:optional (option #f))
282        (define (save-data)
283          (let ((time (get-frame-time)))
284            (cond ((not (= time mob-time))
285                   (set! mob-time time)
286                   (set! saved-data mob-data)))))
287        (case option
288          ((get-mob-id)
289           mob-id)
290          ((get-z-index)
291           mob-z-index)
292          ((get-type)
293           (procedure-name ,mob-name))
294          ((get-data)
295           (save-data)
296           saved-data)
297          (else
298           (cond ((keyword? option)
299                  (assoc-ref saved-data (keyword->symbol option)))
300                 (else
301                  (save-data)
302                  (let ((res (,mob-name mob-id mob-data)))
303                    (set! mob-z-index (car res))
304                    (set! mob-data (cadr res))))))))))
305
306 (define-macro (define-mob-function attr . body)
307   (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
308         (mob-id-symbol (gensym))
309         (mob-id-z-index (gensym))
310         (data-symbol (gensym)))
311     `(lambda (,mob-id-symbol ,data-symbol)
312        (let ((,mob-id-z-index 0))
313          (define (kill-me)
314            (hide-mob-hash ,mob-id-symbol))
315          (define* (translate x y #:optional (z 0))
316            (cond ((3d-mode?)
317                   (translate-mob x y z))
318                  (else
319                   (set! ,mob-id-z-index (+ ,mob-id-z-index z))
320                   (translate-mob x y))))
321          (let* ,attr
322            ,@(map
323               (lambda (a)
324                 `(let ((val (assoc-ref ,data-symbol ',(car a))))
325                    (cond (val (set! ,(car a) val)))))
326               attr)
327            (catch #t
328                   (lambda* () ,@body)
329                   (lambda (key . args) #f))
330            (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
331
332 (define-macro (define-mob mob-head . body)
333   (let* ((name (car mob-head))
334          (attr (cdr mob-head))
335          (make-fun-symbol (gensym))
336          (mob-fun-symbol (gensym))
337          (params-symbol (gensym)))
338     `(define (,name . ,params-symbol)
339        (define ,make-fun-symbol
340          (lambda* ,(if (null? attr) '() `(#:key ,@attr))
341            (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
342        (define ,mob-fun-symbol
343          (define-mob-function ,attr ,@body))
344        (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
345               (apply ,make-fun-symbol ,params-symbol))
346              (else
347               (apply ,mob-fun-symbol ,params-symbol))))))
348
349 (define-macro (lambda-mob attr . body)
350   (let ((fun-name (gensym)))
351     `(begin
352        (define-mob-function ,(cons fun-name attr) ,@body)
353        (the-mob 'undefined '() ,fun-name))))
354
355
356 ;;; Functions for checking mobs (collisions and more)
357
358 (define (map-mobs fun type)
359   (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
360     (map (lambda (m) (fun (m 'get-data))) mobs)))
361
362 (define-macro (define-checking-mobs head mob-def . body)
363   (let ((type (car mob-def)) (attr (cdr mob-def)))
364     `(define ,head
365        (map-mobs
366         (lambda (m)
367           (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
368             ,@body))
369         ',type))))
370
371
372 ;;; Scenes
373
374 (define-macro (define-scene name . body)
375   `(define (,name)
376      ,@body))
377
378
379 (module-map (lambda (sym var)
380               (if (not (eq? sym '%module-public-interface))
381                   (module-export! (current-module) (list sym))))
382             (current-module))