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
55 #:re-export (get-current-color
56 get-texture-properties
83 (define resources-cache (make-weak-value-hash-table))
85 (define (from-cache key)
86 (hash-ref resources-cache key))
88 (define (into-cache key res)
89 (hash-set! resources-cache key res))
91 (define-macro (use-cache-with module proc)
92 (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
94 (define ,pwc (@ ,module ,proc))
95 (define (,proc . param)
97 (res (from-cache key)))
101 (set! res (apply ,pwc param))
105 (use-cache-with (gacela video) load-texture)
106 (use-cache-with (gacela video) load-font)
111 (define loop-flag #f)
112 (define game-code #f)
113 (define game-loop-thread #f)
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)))
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)
128 (lambda () (set! ,value-symbol (apply ,pgl param)))
129 (lambda (key . args) #f))
130 (set! ,flag-symbol #t))
132 (while (not ,flag-symbol))
135 (apply ,pgl param)))))))
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)
142 (define-macro (game . code)
143 `(let ((game-function ,(if (null? code)
145 `(lambda () ,@code))))
146 (set-game-code game-function)
147 (cond ((not (game-running?))
150 (define (init-gacela)
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))
158 (define (quit-gacela)
160 (set-game-code (lambda () #f))
161 (set! game-loop-thread #f)
165 (refresh-active-mobs)
166 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
170 ; (check-connections)
172 (cond ((quit-signal?)
177 (refresh-active-mobs)
178 (if (procedure? game-code)
180 (lambda () (game-code))
181 (lambda (key . args) #f)))
187 (define (gacela-script args)
188 (while loop-flag (sleep 1)))
190 (define (game-running?)
193 (define (set-game-code game-function)
194 (set! game-code game-function))
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)
206 (define* (set-game-properties! #:key title width height bpp fps mode)
208 (set-screen-title! title))
210 (set-screen-bpp! bpp))
211 (if (or width height)
213 (if (not width) (set! width (get-screen-width)))
214 (if (not height) (set! height (get-screen-height)))
215 (resize-screen width height)))
217 (set-frames-per-second! fps))
219 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
220 (get-game-properties))
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))))
228 (define mobs-table (make-hash-table))
229 (define active-mobs '())
230 (define mobs-changed #f)
232 (define (show-mob-hash mob)
233 (hash-set! mobs-table (mob 'get-mob-id) mob)
234 (set! mobs-changed #t))
236 (define (hide-mob-hash mob-id)
237 (hash-remove! mobs-table mob-id)
238 (set! mobs-changed #t))
240 (define (refresh-active-mobs)
242 (set! mobs-changed #f)
243 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
245 (define (get-active-mobs)
248 (define (hide-all-mobs)
249 (set! mobs-changed #t)
250 (hash-clear! mobs-table))
252 (define (mobs-changed?)
256 (define-macro (show-mob mob)
261 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
263 (define-macro (hide-mob mob)
266 (hide-mob-hash (m 'get-mob-id))))
268 `(hide-mob-hash (,mob 'get-mob-id)))))
270 (define current-mob-id #f)
272 (define translate-mob translate)
274 (define (get-current-mob-id)
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))))))
281 (set! current-mob-id (m 'get-mob-id))
282 (glmatrix-block (m)))
284 (set! current-mob-id #f)))
289 (define mob-functions (make-hash-table))
291 (define (get-mob-function-name mob-name)
292 (let ((name (hash-ref mob-functions mob-name)))
295 (hash-set! mob-functions mob-name name)))
298 (define-macro (the-mob mob-name init-data)
299 `(let ((mob-id (gensym))
302 (mob-data ,init-data)
303 (saved-data ,init-data))
304 (lambda* (#:optional (option #f))
306 (let ((time (get-frame-time)))
307 (cond ((not (= time mob-time))
309 (set! saved-data mob-data)))))
316 (procedure-name ,mob-name))
321 (cond ((keyword? option)
322 (assoc-ref saved-data (keyword->symbol option)))
325 (let ((res (,mob-name mob-id mob-data)))
326 (set! mob-z-index (car res))
327 (set! mob-data (cadr res))))))))))
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))
337 (hide-mob-hash ,mob-id-symbol))
338 (define* (translate x y #:optional (z 0))
340 (translate-mob x y z))
342 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
343 (translate-mob x y))))
347 `(let ((val (assoc-ref ,data-symbol ',(car a))))
348 (cond (val (set! ,(car a) val)))))
352 (lambda (key . args) #f))
353 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
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))
370 (apply ,mob-fun-symbol ,params-symbol))))))
372 (define-macro (lambda-mob attr . body)
373 (let ((fun-name (gensym)))
375 (define-mob-function ,(cons fun-name attr) ,@body)
376 (the-mob 'undefined '() ,fun-name))))
379 ;;; Functions for checking mobs (collisions and more)
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)))
385 (define-macro (define-checking-mobs head mob-def . body)
386 (let ((type (car mob-def)) (attr (cdr mob-def)))
390 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
397 (define-macro (define-scene name . body)