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
53 #:re-export (get-current-color
79 (define resources-cache (make-weak-value-hash-table))
81 (define (from-cache key)
82 (hash-ref resources-cache key))
84 (define (into-cache key res)
85 (hash-set! resources-cache key res))
87 (define-macro (use-cache-with module proc)
88 (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
90 (define ,pwc (@ ,module ,proc))
91 (define (,proc . param)
93 (res (from-cache key)))
97 (set! res (apply ,pwc param))
101 (use-cache-with (gacela video) load-texture)
102 (use-cache-with (gacela video) load-font)
107 (define loop-flag #f)
108 (define game-code #f)
109 (define game-loop-thread #f)
111 (define-macro (run-in-game-loop proc)
112 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
113 (flag-symbol (gensym))
114 (value-symbol (gensym)))
117 (define (,proc . param)
118 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
119 (let ((,flag-symbol #f))
120 (define ,value-symbol)
124 (lambda () (set! ,value-symbol (apply ,pgl param)))
125 (lambda (key . args) #f))
126 (set! ,flag-symbol #t))
128 (while (not ,flag-symbol))
131 (apply ,pgl param)))))))
133 (run-in-game-loop load-texture)
134 (run-in-game-loop load-font)
135 (run-in-game-loop set-screen-bpp!)
136 (run-in-game-loop resize-screen)
138 (define-macro (game . code)
139 `(let ((game-function ,(if (null? code)
141 `(lambda () ,@code))))
142 (set-game-code game-function)
143 (cond ((not (game-running?))
146 (define (init-gacela)
147 (set! game-loop-thread (call-with-new-thread (lambda () (game))))
148 (while (not loop-flag))
151 (define (quit-gacela)
152 (set! game-loop-thread #f)
156 (refresh-active-mobs)
157 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
161 ; (check-connections)
163 (cond ((quit-signal?)
168 (refresh-active-mobs)
169 (if (procedure? game-code)
171 (lambda () (game-code))
172 (lambda (key . args) #f)))
178 (define (game-running?)
181 (define (set-game-code game-function)
182 (set! game-code game-function))
187 (define *title* "Gacela")
188 (define *width-screen* 640)
189 (define *height-screen* 480)
190 (define *bpp-screen* 32)
191 (define *frames-per-second* 20)
194 (define* (set-game-properties! #:key title width height bpp fps mode)
196 (set-screen-title! title))
198 (set-screen-bpp! bpp))
199 (if (or width height)
201 (if (not width) (set! width (get-screen-width)))
202 (if (not height) (set! height (get-screen-height)))
203 (resize-screen width height)))
205 (set-frames-per-second! fps))
207 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
208 (get-game-properties))
210 (define (get-game-properties)
211 `((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))))
216 (define mobs-table (make-hash-table))
217 (define active-mobs '())
218 (define mobs-changed #f)
220 (define (show-mob-hash mob)
221 (hash-set! mobs-table (mob 'get-mob-id) mob)
222 (set! mobs-changed #t))
224 (define (hide-mob-hash mob-id)
225 (hash-remove! mobs-table mob-id)
226 (set! mobs-changed #t))
228 (define (refresh-active-mobs)
230 (set! mobs-changed #f)
231 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
233 (define (get-active-mobs)
236 (define (hide-all-mobs)
237 (set! mobs-changed #t)
238 (hash-clear! mobs-table))
240 (define (mobs-changed?)
244 (define-macro (show-mob mob)
249 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
251 (define-macro (hide-mob mob)
254 (hide-mob-hash (m 'get-mob-id))))
256 `(hide-mob-hash (,mob 'get-mob-id)))))
258 (define current-mob-id #f)
260 (define (get-current-mob-id)
263 (define* (run-mobs #:optional (mobs (get-active-mobs)))
266 (set! current-mob-id (m 'get-mob-id))
267 (glmatrix-block (m)))
269 (set! current-mob-id #f))
274 (define mob-functions (make-hash-table))
276 (define (get-mob-function-name mob-name)
277 (let ((name (hash-ref mob-functions mob-name)))
280 (hash-set! mob-functions mob-name name)))
283 (define-macro (the-mob type init-data fun-name)
284 `(let ((mob-id (gensym))
286 (mob-data ,init-data)
287 (saved-data ,init-data))
288 (lambda* (#:optional (option #f))
290 (let ((time (get-frame-time)))
291 (cond ((not (= time mob-time))
293 (set! saved-data mob-data)))))
304 (set! mob-data (,fun-name mob-id mob-data)))))))
306 (define-macro (define-mob-function head . body)
307 (let ((fun-name (car head))
308 (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
309 (mob-id-symbol (gensym))
310 (data-symbol (gensym)))
311 `(define (,fun-name ,mob-id-symbol ,data-symbol)
313 (hide-mob-hash ,mob-id-symbol))
317 `(let ((val (assoc-ref ,data-symbol ',(car a))))
318 (cond (val (set! ,(car a) val)))))
322 (lambda (key . args) #f))
323 (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))
325 (define-macro (define-mob mob-head . body)
326 (let* ((name (car mob-head)) (attr (cdr mob-head))
327 (fun-name (get-mob-function-name name)))
329 (define-mob-function ,(cons fun-name attr) ,@body)
330 (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
331 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
332 (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
334 (define-macro (lambda-mob attr . body)
335 (let ((fun-name (gensym)))
337 (define-mob-function ,(cons fun-name attr) ,@body)
338 (the-mob 'undefined '() ,fun-name))))
341 ;;; Functions for checking mobs (collisions and more)
343 (define (map-mobs fun type)
344 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
345 (map (lambda (m) (fun (m 'get-data))) mobs)))
347 (define-macro (define-checking-mobs head mob-def . body)
348 (let ((type (car mob-def)) (attr (cdr mob-def)))
352 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)