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 translate-mob translate)
273 (define (get-current-mob-id)
276 (define* (run-mobs #:optional (mobs (get-active-mobs)))
277 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
280 (set! current-mob-id (m 'get-mob-id))
281 (glmatrix-block (m)))
283 (set! current-mob-id #f)))
288 (define mob-functions (make-hash-table))
290 (define (get-mob-function-name mob-name)
291 (let ((name (hash-ref mob-functions mob-name)))
294 (hash-set! mob-functions mob-name name)))
297 (define-macro (the-mob mob-name init-data)
298 `(let ((mob-id (gensym))
301 (mob-data ,init-data)
302 (saved-data ,init-data))
303 (lambda* (#:optional (option #f))
305 (let ((time (get-frame-time)))
306 (cond ((not (= time mob-time))
308 (set! saved-data mob-data)))))
315 (procedure-name ,mob-name))
320 (cond ((keyword? option)
321 (assoc-ref saved-data (keyword->symbol option)))
324 (let ((res (,mob-name mob-id mob-data)))
325 (set! mob-z-index (car res))
326 (set! mob-data (cadr res))))))))))
328 (define-macro (define-mob-function attr . body)
329 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
330 (mob-id-symbol (gensym))
331 (mob-id-z-index (gensym))
332 (data-symbol (gensym)))
333 `(lambda (,mob-id-symbol ,data-symbol)
334 (let ((,mob-id-z-index 0))
336 (hide-mob-hash ,mob-id-symbol))
337 (define* (translate x y #:optional (z 0))
339 (translate-mob x y z))
341 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
342 (translate-mob x y))))
346 `(let ((val (assoc-ref ,data-symbol ',(car a))))
347 (cond (val (set! ,(car a) val)))))
351 (lambda (key . args) #f))
352 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
354 (define-macro (define-mob mob-head . body)
355 (let* ((name (car mob-head))
356 (attr (cdr mob-head))
357 (make-fun-symbol (gensym))
358 (mob-fun-symbol (gensym))
359 (params-symbol (gensym)))
360 `(define (,name . ,params-symbol)
361 (define ,make-fun-symbol
362 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
363 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
364 (define ,mob-fun-symbol
365 (define-mob-function ,attr ,@body))
366 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
367 (apply ,make-fun-symbol ,params-symbol))
369 (apply ,mob-fun-symbol ,params-symbol))))))
371 (define-macro (lambda-mob attr . body)
372 (let ((fun-name (gensym)))
374 (define-mob-function ,(cons fun-name attr) ,@body)
375 (the-mob 'undefined '() ,fun-name))))
378 ;;; Functions for checking mobs (collisions and more)
380 (define (map-mobs fun type)
381 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
382 (map (lambda (m) (fun (m 'get-data))) mobs)))
384 (define-macro (define-checking-mobs head mob-def . body)
385 (let ((type (car mob-def)) (attr (cdr mob-def)))
389 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
396 (define-macro (define-scene name . body)