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