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