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 (translate
62 (define resources-cache (make-weak-value-hash-table))
64 (define (from-cache key)
65 (hash-ref resources-cache key))
67 (define (into-cache key res)
68 (hash-set! resources-cache key res))
70 (define-macro (use-cache-with module proc)
71 (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
73 (define ,pwc (@ ,module ,proc))
74 (define (,proc . param)
76 (res (from-cache key)))
80 (set! res (apply ,pwc param))
84 (use-cache-with (gacela video) load-texture)
85 (use-cache-with (gacela video) load-font)
92 (define game-loop-thread #f)
94 (define-macro (run-in-game-loop proc)
95 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
96 (flag-symbol (gensym))
97 (value-symbol (gensym)))
100 (define (,proc . param)
101 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
102 (let ((,flag-symbol #f))
103 (define ,value-symbol)
107 (lambda () (set! ,value-symbol (apply ,pgl param)))
108 (lambda (key . args) #f))
109 (set! ,flag-symbol #t))
111 (while (not ,flag-symbol))
114 (apply ,pgl param)))))))
116 (run-in-game-loop load-texture)
117 (run-in-game-loop load-font)
118 (run-in-game-loop set-screen-bpp!)
119 (run-in-game-loop resize-screen)
121 (define-macro (game . code)
122 `(let ((game-function ,(if (null? code)
124 `(lambda () ,@code))))
125 (set-game-code game-function)
126 (cond ((not (game-running?))
129 (define (init-gacela)
131 (set-game-code (lambda () #f))
132 (cond ((not game-loop-thread)
133 (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
134 (while (not loop-flag))
137 (define (quit-gacela)
139 (set-game-code (lambda () #f))
140 (set! game-loop-thread #f)
144 (refresh-active-mobs)
145 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
149 ; (check-connections)
151 (cond ((quit-signal?)
156 (refresh-active-mobs)
157 (if (procedure? game-code)
159 (lambda () (game-code))
160 (lambda (key . args) #f)))
166 (define (gacela-script args)
167 (while loop-flag (sleep 1)))
169 (define (game-running?)
172 (define (set-game-code game-function)
173 (set! game-code game-function))
178 (define *title* "Gacela")
179 (define *width-screen* 640)
180 (define *height-screen* 480)
181 (define *bpp-screen* 32)
182 (define *frames-per-second* 20)
185 (define* (set-game-properties! #:key title width height bpp fps mode)
187 (set-screen-title! title))
189 (set-screen-bpp! bpp))
190 (if (or width height)
192 (if (not width) (set! width (get-screen-width)))
193 (if (not height) (set! height (get-screen-height)))
194 (resize-screen width height)))
196 (set-frames-per-second! fps))
198 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
199 (get-game-properties))
201 (define (get-game-properties)
202 `((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))))
207 (define mobs-table (make-hash-table))
208 (define active-mobs '())
209 (define mobs-changed #f)
211 (define (show-mob-hash mob)
212 (hash-set! mobs-table (mob 'get-mob-id) mob)
213 (set! mobs-changed #t))
215 (define (hide-mob-hash mob-id)
216 (hash-remove! mobs-table mob-id)
217 (set! mobs-changed #t))
219 (define (refresh-active-mobs)
221 (set! mobs-changed #f)
222 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
224 (define (get-active-mobs)
227 (define (hide-all-mobs)
228 (set! mobs-changed #t)
229 (hash-clear! mobs-table))
231 (define (mobs-changed?)
235 (define-macro (show-mob mob)
240 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
242 (define-macro (hide-mob mob)
245 (hide-mob-hash (m 'get-mob-id))))
247 `(hide-mob-hash (,mob 'get-mob-id)))))
249 (define current-mob-id #f)
251 (define translate-mob translate)
253 (define (get-current-mob-id)
256 (define* (run-mobs #:optional (mobs (get-active-mobs)))
257 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
260 (set! current-mob-id (m 'get-mob-id))
261 (glmatrix-block (m)))
263 (set! current-mob-id #f)))
268 (define mob-functions (make-hash-table))
270 (define (get-mob-function-name mob-name)
271 (let ((name (hash-ref mob-functions mob-name)))
274 (hash-set! mob-functions mob-name name)))
277 (define-macro (the-mob mob-name init-data)
278 `(let ((mob-id (gensym))
281 (mob-data ,init-data)
282 (saved-data ,init-data))
283 (lambda* (#:optional (option #f))
285 (let ((time (get-frame-time)))
286 (cond ((not (= time mob-time))
288 (set! saved-data mob-data)))))
295 (procedure-name ,mob-name))
300 (cond ((keyword? option)
301 (assoc-ref saved-data (keyword->symbol option)))
304 (let ((res (,mob-name mob-id mob-data)))
305 (set! mob-z-index (car res))
306 (set! mob-data (cadr res))))))))))
308 (define-macro (define-mob-function attr . body)
309 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
310 (mob-id-symbol (gensym))
311 (mob-id-z-index (gensym))
312 (data-symbol (gensym)))
313 `(lambda (,mob-id-symbol ,data-symbol)
314 (let ((,mob-id-z-index 0))
316 (hide-mob-hash ,mob-id-symbol))
317 (define* (translate x y #:optional (z 0))
319 (translate-mob x y z))
321 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
322 (translate-mob x y))))
326 `(let ((val (assoc-ref ,data-symbol ',(car a))))
327 (cond (val (set! ,(car a) val)))))
331 (lambda (key . args) #f))
332 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
334 (define-macro (define-mob mob-head . body)
335 (let* ((name (car mob-head))
336 (attr (cdr mob-head))
337 (make-fun-symbol (gensym))
338 (mob-fun-symbol (gensym))
339 (params-symbol (gensym)))
340 `(define (,name . ,params-symbol)
341 (define ,make-fun-symbol
342 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
343 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
344 (define ,mob-fun-symbol
345 (define-mob-function ,attr ,@body))
346 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
347 (apply ,make-fun-symbol ,params-symbol))
349 (apply ,mob-fun-symbol ,params-symbol))))))
351 (define-macro (lambda-mob attr . body)
352 (let ((fun-name (gensym)))
354 (define-mob-function ,(cons fun-name attr) ,@body)
355 (the-mob 'undefined '() ,fun-name))))
358 ;;; Functions for checking mobs (collisions and more)
360 (define (map-mobs fun type)
361 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
362 (map (lambda (m) (fun (m 'get-data))) mobs)))
364 (define-macro (define-checking-mobs head mob-def . body)
365 (let ((type (car mob-def)) (attr (cdr mob-def)))
369 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
376 (define-macro (define-scene name . body)