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
54 #:re-export (get-current-color
81 (define resources-cache (make-weak-value-hash-table))
83 (define (from-cache key)
84 (hash-ref resources-cache key))
86 (define (into-cache key res)
87 (hash-set! resources-cache key res))
89 (define-macro (use-cache-with module proc)
90 (let ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
92 (define ,pwc (@ ,module ,proc))
93 (define (,proc . param)
95 (res (from-cache key)))
99 (set! res (apply ,pwc param))
103 (use-cache-with (gacela video) load-texture)
104 (use-cache-with (gacela video) load-font)
109 (define loop-flag #f)
110 (define game-code #f)
111 (define game-loop-thread #f)
113 (define-macro (run-in-game-loop proc)
114 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
115 (flag-symbol (gensym))
116 (value-symbol (gensym)))
119 (define (,proc . param)
120 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
121 (let ((,flag-symbol #f))
122 (define ,value-symbol)
126 (lambda () (set! ,value-symbol (apply ,pgl param)))
127 (lambda (key . args) #f))
128 (set! ,flag-symbol #t))
130 (while (not ,flag-symbol))
133 (apply ,pgl param)))))))
135 (run-in-game-loop load-texture)
136 (run-in-game-loop load-font)
137 (run-in-game-loop set-screen-bpp!)
138 (run-in-game-loop resize-screen)
140 (define-macro (game . code)
141 `(let ((game-function ,(if (null? code)
143 `(lambda () ,@code))))
144 (set-game-code game-function)
145 (cond ((not (game-running?))
148 (define (init-gacela)
149 (set! game-loop-thread (call-with-new-thread (lambda () (game))))
150 (while (not loop-flag))
153 (define (quit-gacela)
154 (set! game-loop-thread #f)
158 (refresh-active-mobs)
159 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
163 ; (check-connections)
165 (cond ((quit-signal?)
170 (refresh-active-mobs)
171 (if (procedure? game-code)
173 (lambda () (game-code))
174 (lambda (key . args) #f)))
180 (define (game-running?)
183 (define (set-game-code game-function)
184 (set! game-code game-function))
189 (define *title* "Gacela")
190 (define *width-screen* 640)
191 (define *height-screen* 480)
192 (define *bpp-screen* 32)
193 (define *frames-per-second* 20)
196 (define* (set-game-properties! #:key title width height bpp fps mode)
198 (set-screen-title! title))
200 (set-screen-bpp! bpp))
201 (if (or width height)
203 (if (not width) (set! width (get-screen-width)))
204 (if (not height) (set! height (get-screen-height)))
205 (resize-screen width height)))
207 (set-frames-per-second! fps))
209 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
210 (get-game-properties))
212 (define (get-game-properties)
213 `((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))))
218 (define mobs-table (make-hash-table))
219 (define active-mobs '())
220 (define mobs-changed #f)
222 (define (show-mob-hash mob)
223 (hash-set! mobs-table (mob 'get-mob-id) mob)
224 (set! mobs-changed #t))
226 (define (hide-mob-hash mob-id)
227 (hash-remove! mobs-table mob-id)
228 (set! mobs-changed #t))
230 (define (refresh-active-mobs)
232 (set! mobs-changed #f)
233 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
235 (define (get-active-mobs)
238 (define (hide-all-mobs)
239 (set! mobs-changed #t)
240 (hash-clear! mobs-table))
242 (define (mobs-changed?)
246 (define-macro (show-mob mob)
251 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
253 (define-macro (hide-mob mob)
256 (hide-mob-hash (m 'get-mob-id))))
258 `(hide-mob-hash (,mob 'get-mob-id)))))
260 (define current-mob-id #f)
262 (define (get-current-mob-id)
265 (define* (run-mobs #:optional (mobs (get-active-mobs)))
266 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
269 (set! current-mob-id (m 'get-mob-id))
270 (glmatrix-block (m)))
272 (set! current-mob-id #f)))
277 (define mob-functions (make-hash-table))
279 (define (get-mob-function-name mob-name)
280 (let ((name (hash-ref mob-functions mob-name)))
283 (hash-set! mob-functions mob-name name)))
286 (define-macro (the-mob type init-data fun-name)
287 `(let ((mob-id (gensym))
290 (mob-data ,init-data)
291 (saved-data ,init-data))
292 (lambda* (#:optional (option #f))
294 (let ((time (get-frame-time)))
295 (cond ((not (= time mob-time))
297 (set! saved-data mob-data)))))
310 (let ((res (,fun-name mob-id mob-data)))
311 (set! mob-z-index (car res))
312 (set! mob-data (cadr res))))))))
314 (define-macro (define-mob-function head . body)
315 (let ((fun-name (car head))
316 (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
317 (mob-id-symbol (gensym))
318 (mob-id-z-index (gensym))
319 (data-symbol (gensym)))
320 `(define (,fun-name ,mob-id-symbol ,data-symbol)
321 (let ((,mob-id-z-index 0))
323 (hide-mob-hash ,mob-id-symbol))
324 (define* (translate x y #:optional (z 0))
326 (translate-mob x y z))
328 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
329 (translate-mob x y))))
333 `(let ((val (assoc-ref ,data-symbol ',(car a))))
334 (cond (val (set! ,(car a) val)))))
338 (lambda (key . args) #f))
339 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
341 (define-macro (define-mob mob-head . body)
342 (let* ((name (car mob-head)) (attr (cdr mob-head))
343 (fun-name (get-mob-function-name name)))
345 (define-mob-function ,(cons fun-name attr) ,@body)
346 (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
347 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
348 (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
350 (define-macro (lambda-mob attr . body)
351 (let ((fun-name (gensym)))
353 (define-mob-function ,(cons fun-name attr) ,@body)
354 (the-mob 'undefined '() ,fun-name))))
357 ;;; Functions for checking mobs (collisions and more)
359 (define translate-mob translate)
361 (define (map-mobs fun type)
362 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
363 (map (lambda (m) (fun (m 'get-data))) mobs)))
365 (define-macro (define-checking-mobs head mob-def . body)
366 (let ((type (car mob-def)) (attr (cdr mob-def)))
370 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)