1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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
41 get-mob-function-name)
49 #:re-export (get-current-color
72 (define resources-cache (make-weak-value-hash-table))
74 (define (from-cache key)
75 (hash-ref resources-cache key))
77 (define (into-cache key res)
78 (hash-set! resources-cache key res))
80 (define-macro (use-cache-with module proc)
81 (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
83 (define ,pwc (@ ,module ,proc))
84 (define (,proc . param)
86 (res (from-cache key)))
90 (set! res (apply ,pwc param))
94 (use-cache-with (gacela video) load-texture)
95 (use-cache-with (gacela video) load-font)
100 (define loop-flag #f)
101 (define game-code #f)
102 (define game-loop-thread #f)
104 (define-macro (game . code)
105 `(let ((game-function ,(if (null? code)
107 `(lambda () ,@code))))
108 (set-game-code game-function)
109 (cond ((not (game-running?))
112 (define-macro (run-in-game-loop . code)
113 `(if game-loop-thread
114 (system-async-mark (lambda () ,@code) game-loop-thread)
117 (define (init-gacela)
118 (set! game-loop-thread (call-with-new-thread (lambda () (game)))))
120 (define (quit-gacela)
121 (set! game-loop-thread #f)
125 (refresh-active-mobs)
127 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
130 ; (check-connections)
132 (cond ((quit-signal?)
137 (refresh-active-mobs)
138 (if (procedure? game-code)
140 (lambda () (game-code))
141 (lambda (key . args) #f)))
147 (define (game-running?)
150 (define (set-game-code game-function)
151 (set! game-code game-function))
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)
163 (define* (set-game-properties! #:key title width height bpp fps mode)
165 (set-screen-title! title))
167 (run-in-game-loop (set-screen-bpp! bpp)))
168 (if (or width height)
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))))
174 (set-frames-per-second! fps))
176 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
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))))
184 (define mobs-table (make-hash-table))
185 (define active-mobs '())
186 (define mobs-changed #f)
188 (define (show-mob-hash mob)
189 (hash-set! mobs-table (mob 'get-mob-id) mob)
190 (set! mobs-changed #t))
192 (define (hide-mob-hash mob-id)
193 (hash-remove! mobs-table mob-id)
194 (set! mobs-changed #t))
196 (define (refresh-active-mobs)
198 (set! mobs-changed #f)
199 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
201 (define (get-active-mobs)
204 (define (hide-all-mobs)
205 (set! mobs-changed #t)
206 (hash-clear! mobs-table))
208 (define (mobs-changed?)
212 (define-macro (show-mob mob)
217 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
219 (define-macro (hide-mob mob)
222 (hide-mob-hash (m 'get-mob-id))))
224 `(hide-mob-hash (,mob 'get-mob-id)))))
226 (define* (run-mobs #:optional (mobs (get-active-mobs)))
229 (glmatrix-block (m)))
235 (define mob-functions (make-hash-table))
237 (define (get-mob-function-name mob-name)
238 (let ((name (hash-ref mob-functions mob-name)))
241 (hash-set! mob-functions mob-name name)))
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))
254 (lambda* (#:optional (option #f))
256 (hide-mob-hash ,mob-id-symbol))
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)))))))
264 (define (filter-mobs type fun)
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)))
279 (,fun-name 123)))))))
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))
287 (lambda (key . args) #f))))
288 `(define (,fun-name ,data-symbol)
292 `(let ((val (assoc-ref ,data-symbol ',(car a))))
293 (cond (val (set! ,(car a) val)))))
296 (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))
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)))
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))))))
307 (define-macro (lambda-mob attr . body)
308 (let ((fun-name (gensym)))
310 (define-mob-function ,(cons fun-name attr) ,@body)
311 (the-mob 'undefined ,attr '() ,fun-name))))
316 ;; (define-macro (lambda-mob-data attr . body)
317 ;; `(lambda ,attr ,@body))
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))))