]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
Now mobs use let*
[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 (load-texture
24             load-font
25             *title*
26             *width-screen*
27             *height-screen*
28             *bpp-screen*
29             *frames-per-second*
30             *mode*
31             set-game-properties!
32             get-game-properties
33             init-gacela
34             quit-gacela
35             game-loop
36             gacela-script
37             game-running?
38             set-game-code
39             show-mob-hash
40             hide-mob-hash
41             get-active-mobs
42             hide-all-mobs
43             get-current-mob-id
44             get-mob-function-name
45             map-mobs
46             translate-mob)
47   #:export-syntax (game
48                    show-mob
49                    hide-mob
50                    the-mob
51                    define-mob-function
52                    define-mob
53                    lambda-mob
54                    define-checking-mobs)
55   #:re-export (get-current-color
56                get-texture-properties
57                set-current-color
58                with-color
59                progn-textures
60                draw
61                draw-texture
62                draw-line
63                draw-quad
64                draw-rectangle
65                draw-square
66                draw-cube
67                translate
68                rotate
69                to-origin
70                add-light
71                set-camera
72                camera-look
73                render-text
74                get-frame-time
75                key?
76                key-pressed?
77                key-released?
78                3d-mode?))
79
80
81 ;;; Resources Cache
82
83 (define resources-cache (make-weak-value-hash-table))
84
85 (define (from-cache key)
86   (hash-ref resources-cache key))
87
88 (define (into-cache key res)
89   (hash-set! resources-cache key res))
90
91 (define-macro (use-cache-with module proc)
92   (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
93     `(begin
94        (define ,pwc (@ ,module ,proc))
95        (define (,proc . param)
96          (let* ((key param)
97                 (res (from-cache key)))
98            (cond (res
99                   res)
100                  (else
101                   (set! res (apply ,pwc param))
102                   (into-cache key res)
103                   res)))))))
104
105 (use-cache-with (gacela video) load-texture)
106 (use-cache-with (gacela video) load-font)
107
108
109 ;;; Main Loop
110
111 (define loop-flag #f)
112 (define game-code #f)
113 (define game-loop-thread #f)
114
115 (define-macro (run-in-game-loop proc)
116   (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
117         (flag-symbol (gensym))
118         (value-symbol (gensym)))
119     `(begin
120        (define ,pgl ,proc)
121        (define (,proc . param)
122          (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
123                 (let ((,flag-symbol #f))
124                   (define ,value-symbol)
125                   (system-async-mark
126                    (lambda ()
127                      (catch #t
128                            (lambda () (set! ,value-symbol (apply ,pgl param)))
129                            (lambda (key . args) #f))
130                      (set! ,flag-symbol #t))
131                    game-loop-thread)
132                   (while (not ,flag-symbol))
133                   ,value-symbol))
134                (else
135                 (apply ,pgl param)))))))
136
137 (run-in-game-loop load-texture)
138 (run-in-game-loop load-font)
139 (run-in-game-loop set-screen-bpp!)
140 (run-in-game-loop resize-screen)
141
142 (define-macro (game . code)
143   `(let ((game-function ,(if (null? code)
144                              `(lambda () #f)
145                              `(lambda () ,@code))))
146      (set-game-code game-function)
147      (cond ((not (game-running?))
148             (game-loop)))))
149
150 (define (init-gacela)
151   (hide-all-mobs)
152   (set-game-code (lambda () #f))
153   (cond ((not game-loop-thread)
154          (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
155   (while (not loop-flag))
156   #t)
157
158 (define (quit-gacela)
159   (hide-all-mobs)
160   (set-game-code (lambda () #f))
161   (set! game-loop-thread #f)
162   (set! loop-flag #f))
163
164 (define (game-loop)
165   (refresh-active-mobs)
166   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
167   (set! loop-flag #t)
168   (while loop-flag
169          (init-frame-time)
170 ;           (check-connections)
171          (process-events)
172          (cond ((quit-signal?)
173                 (quit-gacela))
174                (else
175                 (clear-screen)
176                 (to-origin)
177                 (refresh-active-mobs)
178                 (if (procedure? game-code)
179                     (catch #t
180                            (lambda () (game-code))
181                            (lambda (key . args) #f)))
182                 (run-mobs)
183                 (flip-screen)
184                 (delay-frame))))
185   (quit-video))
186
187 (define (gacela-script args)
188   (while loop-flag (sleep 1)))
189
190 (define (game-running?)
191   loop-flag)
192
193 (define (set-game-code game-function)
194   (set! game-code game-function))
195
196
197 ;;; Game Properties
198
199 (define *title* "Gacela")
200 (define *width-screen* 640)
201 (define *height-screen* 480)
202 (define *bpp-screen* 32)
203 (define *frames-per-second* 20)
204 (define *mode* '2d)
205
206 (define* (set-game-properties! #:key title width height bpp fps mode)
207   (if title
208       (set-screen-title! title))
209   (if bpp
210       (set-screen-bpp! bpp))
211   (if (or width height)
212       (begin
213         (if (not width) (set! width (get-screen-width)))
214         (if (not height) (set! height (get-screen-height)))
215         (resize-screen width height)))
216   (if fps
217       (set-frames-per-second! fps))
218   (if mode
219       (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
220   (get-game-properties))
221
222 (define (get-game-properties)
223   `((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))))
224
225
226 ;;; Mobs Factory
227
228 (define mobs-table (make-hash-table))
229 (define active-mobs '())
230 (define mobs-changed #f)
231
232 (define (show-mob-hash mob)
233   (hash-set! mobs-table (mob 'get-mob-id) mob)
234   (set! mobs-changed #t))
235
236 (define (hide-mob-hash mob-id)
237   (hash-remove! mobs-table mob-id)
238   (set! mobs-changed #t))
239
240 (define (refresh-active-mobs)
241   (cond (mobs-changed
242          (set! mobs-changed #f)
243          (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
244
245 (define (get-active-mobs)
246   active-mobs)
247
248 (define (hide-all-mobs)
249   (set! mobs-changed #t)
250   (hash-clear! mobs-table))
251
252 (define (mobs-changed?)
253   mobs-changed)
254
255
256 (define-macro (show-mob mob)
257   (cond ((list? mob)
258          `(let ((m ,mob))
259             (show-mob-hash m)))
260         (else
261          `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
262
263 (define-macro (hide-mob mob)
264   (cond ((list? mob)
265          `(let ((m ,mob))
266             (hide-mob-hash (m 'get-mob-id))))
267         (else
268          `(hide-mob-hash (,mob 'get-mob-id)))))
269
270 (define current-mob-id #f)
271
272 (define translate-mob translate)
273
274 (define (get-current-mob-id)
275   current-mob-id)
276
277 (define* (run-mobs #:optional (mobs (get-active-mobs)))
278   (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
279     (for-each
280      (lambda (m)
281        (set! current-mob-id (m 'get-mob-id))
282        (glmatrix-block (m)))
283      sorted-mobs)
284     (set! current-mob-id #f)))
285
286
287 ;;; Making mobs
288
289 (define mob-functions (make-hash-table))
290
291 (define (get-mob-function-name mob-name)
292   (let ((name (hash-ref mob-functions mob-name)))
293     (cond ((not name)
294            (set! name (gensym))
295            (hash-set! mob-functions mob-name name)))
296     name))
297
298 (define-macro (the-mob mob-name init-data)
299   `(let ((mob-id (gensym))
300          (mob-z-index 0)
301          (mob-time 0)
302          (mob-data ,init-data)
303          (saved-data ,init-data))
304      (lambda* (#:optional (option #f))
305        (define (save-data)
306          (let ((time (get-frame-time)))
307            (cond ((not (= time mob-time))
308                   (set! mob-time time)
309                   (set! saved-data mob-data)))))
310        (case option
311          ((get-mob-id)
312           mob-id)
313          ((get-z-index)
314           mob-z-index)
315          ((get-type)
316           (procedure-name ,mob-name))
317          ((get-data)
318           (save-data)
319           saved-data)
320          (else
321           (cond ((keyword? option)
322                  (assoc-ref saved-data (keyword->symbol option)))
323                 (else
324                  (save-data)
325                  (let ((res (,mob-name mob-id mob-data)))
326                    (set! mob-z-index (car res))
327                    (set! mob-data (cadr res))))))))))
328
329 (define-macro (define-mob-function attr . body)
330   (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
331         (mob-id-symbol (gensym))
332         (mob-id-z-index (gensym))
333         (data-symbol (gensym)))
334     `(lambda (,mob-id-symbol ,data-symbol)
335        (let ((,mob-id-z-index 0))
336          (define (kill-me)
337            (hide-mob-hash ,mob-id-symbol))
338          (define* (translate x y #:optional (z 0))
339            (cond ((3d-mode?)
340                   (translate-mob x y z))
341                  (else
342                   (set! ,mob-id-z-index (+ ,mob-id-z-index z))
343                   (translate-mob x y))))
344          (let* ,attr
345            ,@(map
346               (lambda (a)
347                 `(let ((val (assoc-ref ,data-symbol ',(car a))))
348                    (cond (val (set! ,(car a) val)))))
349               attr)
350            (catch #t
351                   (lambda* () ,@body)
352                   (lambda (key . args) #f))
353            (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
354
355 (define-macro (define-mob mob-head . body)
356   (let* ((name (car mob-head))
357          (attr (cdr mob-head))
358          (make-fun-symbol (gensym))
359          (mob-fun-symbol (gensym))
360          (params-symbol (gensym)))
361     `(define (,name . ,params-symbol)
362        (define ,make-fun-symbol
363          (lambda* ,(if (null? attr) '() `(#:key ,@attr))
364            (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
365        (define ,mob-fun-symbol
366          (define-mob-function ,attr ,@body))
367        (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
368               (apply ,make-fun-symbol ,params-symbol))
369              (else
370               (apply ,mob-fun-symbol ,params-symbol))))))
371
372 (define-macro (lambda-mob attr . body)
373   (let ((fun-name (gensym)))
374     `(begin
375        (define-mob-function ,(cons fun-name attr) ,@body)
376        (the-mob 'undefined '() ,fun-name))))
377
378
379 ;;; Functions for checking mobs (collisions and more)
380
381 (define (map-mobs fun type)
382   (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
383     (map (lambda (m) (fun (m 'get-data))) mobs)))
384
385 (define-macro (define-checking-mobs head mob-def . body)
386   (let ((type (car mob-def)) (attr (cdr mob-def)))
387     `(define ,head
388        (map-mobs
389         (lambda (m)
390           (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
391             ,@body))
392         ',type))))
393
394
395 ;;; Scenes
396
397 (define-macro (define-scene name . body)
398   `(define (,name)
399      ,@body))