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
82 (define resources-cache (make-weak-value-hash-table))
84 (define (from-cache key)
85 (hash-ref resources-cache key))
87 (define (into-cache key res)
88 (hash-set! resources-cache key res))
90 (define-macro (use-cache-with module proc)
91 (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
93 (define ,pwc (@ ,module ,proc))
94 (define (,proc . param)
96 (res (from-cache key)))
100 (set! res (apply ,pwc param))
104 (use-cache-with (gacela video) load-texture)
105 (use-cache-with (gacela video) load-font)
110 (define loop-flag #f)
111 (define game-code #f)
112 (define game-loop-thread #f)
114 (define-macro (run-in-game-loop proc)
115 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
116 (flag-symbol (gensym))
117 (value-symbol (gensym)))
120 (define (,proc . param)
121 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
122 (let ((,flag-symbol #f))
123 (define ,value-symbol)
127 (lambda () (set! ,value-symbol (apply ,pgl param)))
128 (lambda (key . args) #f))
129 (set! ,flag-symbol #t))
131 (while (not ,flag-symbol))
134 (apply ,pgl param)))))))
136 (run-in-game-loop load-texture)
137 (run-in-game-loop load-font)
138 (run-in-game-loop set-screen-bpp!)
139 (run-in-game-loop resize-screen)
141 (define-macro (game . code)
142 `(let ((game-function ,(if (null? code)
144 `(lambda () ,@code))))
145 (set-game-code game-function)
146 (cond ((not (game-running?))
149 (define (init-gacela)
151 (set-game-code (lambda () #f))
152 (cond ((not game-loop-thread)
153 (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
154 (while (not loop-flag))
157 (define (quit-gacela)
159 (set-game-code (lambda () #f))
160 (set! game-loop-thread #f)
164 (refresh-active-mobs)
165 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
169 ; (check-connections)
171 (cond ((quit-signal?)
176 (refresh-active-mobs)
177 (if (procedure? game-code)
179 (lambda () (game-code))
180 (lambda (key . args) #f)))
186 (define (gacela-script args)
187 (while loop-flag (sleep 1)))
189 (define (game-running?)
192 (define (set-game-code game-function)
193 (set! game-code game-function))
198 (define *title* "Gacela")
199 (define *width-screen* 640)
200 (define *height-screen* 480)
201 (define *bpp-screen* 32)
202 (define *frames-per-second* 20)
205 (define* (set-game-properties! #:key title width height bpp fps mode)
207 (set-screen-title! title))
209 (set-screen-bpp! bpp))
210 (if (or width height)
212 (if (not width) (set! width (get-screen-width)))
213 (if (not height) (set! height (get-screen-height)))
214 (resize-screen width height)))
216 (set-frames-per-second! fps))
218 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
219 (get-game-properties))
221 (define (get-game-properties)
222 `((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))))
227 (define mobs-table (make-hash-table))
228 (define active-mobs '())
229 (define mobs-changed #f)
231 (define (show-mob-hash mob)
232 (hash-set! mobs-table (mob 'get-mob-id) mob)
233 (set! mobs-changed #t))
235 (define (hide-mob-hash mob-id)
236 (hash-remove! mobs-table mob-id)
237 (set! mobs-changed #t))
239 (define (refresh-active-mobs)
241 (set! mobs-changed #f)
242 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
244 (define (get-active-mobs)
247 (define (hide-all-mobs)
248 (set! mobs-changed #t)
249 (hash-clear! mobs-table))
251 (define (mobs-changed?)
255 (define-macro (show-mob mob)
260 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
262 (define-macro (hide-mob mob)
265 (hide-mob-hash (m 'get-mob-id))))
267 `(hide-mob-hash (,mob 'get-mob-id)))))
269 (define current-mob-id #f)
271 (define (get-current-mob-id)
274 (define* (run-mobs #:optional (mobs (get-active-mobs)))
275 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
278 (set! current-mob-id (m 'get-mob-id))
279 (glmatrix-block (m)))
281 (set! current-mob-id #f)))
286 (define mob-functions (make-hash-table))
288 (define (get-mob-function-name mob-name)
289 (let ((name (hash-ref mob-functions mob-name)))
292 (hash-set! mob-functions mob-name name)))
295 (define-macro (the-mob type init-data fun-name)
296 `(let ((mob-id (gensym))
299 (mob-data ,init-data)
300 (saved-data ,init-data))
301 (lambda* (#:optional (option #f))
303 (let ((time (get-frame-time)))
304 (cond ((not (= time mob-time))
306 (set! saved-data mob-data)))))
319 (let ((res (,fun-name mob-id mob-data)))
320 (set! mob-z-index (car res))
321 (set! mob-data (cadr res))))))))
323 (define-macro (define-mob-function head . body)
324 (let ((fun-name (car head))
325 (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
326 (mob-id-symbol (gensym))
327 (mob-id-z-index (gensym))
328 (data-symbol (gensym)))
329 `(define (,fun-name ,mob-id-symbol ,data-symbol)
330 (let ((,mob-id-z-index 0))
332 (hide-mob-hash ,mob-id-symbol))
333 (define* (translate x y #:optional (z 0))
335 (translate-mob x y z))
337 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
338 (translate-mob x y))))
342 `(let ((val (assoc-ref ,data-symbol ',(car a))))
343 (cond (val (set! ,(car a) val)))))
347 (lambda (key . args) #f))
348 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
350 (define-macro (define-mob mob-head . body)
351 (let* ((name (car mob-head)) (attr (cdr mob-head))
352 (fun-name (get-mob-function-name name)))
354 (define-mob-function ,(cons fun-name attr) ,@body)
355 (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
356 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
357 (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
359 (define-macro (lambda-mob attr . body)
360 (let ((fun-name (gensym)))
362 (define-mob-function ,(cons fun-name attr) ,@body)
363 (the-mob 'undefined '() ,fun-name))))
366 ;;; Functions for checking mobs (collisions and more)
368 (define translate-mob translate)
370 (define (map-mobs fun type)
371 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
372 (map (lambda (m) (fun (m 'get-data))) mobs)))
374 (define-macro (define-checking-mobs head mob-def . body)
375 (let ((type (car mob-def)) (attr (cdr mob-def)))
379 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)