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)
53 #:re-export (translate
62 (define game-loop-thread #f)
64 (define-macro (run-in-game-loop proc)
65 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
66 (flag-symbol (gensym))
67 (value-symbol (gensym)))
70 (define (,proc . param)
71 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
72 (let ((,flag-symbol #f))
73 (define ,value-symbol)
77 (lambda () (set! ,value-symbol (apply ,pgl param)))
78 (lambda (key . args) #f))
79 (set! ,flag-symbol #t))
81 (while (not ,flag-symbol))
84 (apply ,pgl param)))))))
86 (run-in-game-loop load-texture)
87 (run-in-game-loop load-font)
88 (run-in-game-loop set-screen-bpp!)
89 (run-in-game-loop resize-screen)
91 (define-macro (game . code)
92 `(let ((game-function ,(if (null? code)
94 `(lambda () ,@code))))
95 (set-game-code game-function)
96 (cond ((not (game-running?))
101 (set-game-code (lambda () #f))
102 (cond ((not game-loop-thread)
103 (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
104 (while (not loop-flag))
107 (define (quit-gacela)
109 (set-game-code (lambda () #f))
110 (set! game-loop-thread #f)
114 (refresh-active-mobs)
115 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
119 ; (check-connections)
121 (cond ((quit-signal?)
126 (refresh-active-mobs)
127 (if (procedure? game-code)
129 (lambda () (game-code))
130 (lambda (key . args) #f)))
136 (define (gacela-script args)
137 (while loop-flag (sleep 1)))
139 (define (game-running?)
142 (define (set-game-code game-function)
143 (set! game-code game-function))
148 (define *title* "Gacela")
149 (define *width-screen* 640)
150 (define *height-screen* 480)
151 (define *bpp-screen* 32)
152 (define *frames-per-second* 20)
154 (define *fullscreen* 'off)
156 (define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
158 (set-screen-title! title))
160 (set-screen-bpp! bpp))
161 (if (or width height)
163 (if (not width) (set! width (get-screen-width)))
164 (if (not height) (set! height (get-screen-height)))
165 (resize-screen width height)))
167 (set-frames-per-second! fps))
169 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
171 (set-fullscreen! fullscreen))
172 (get-game-properties))
174 (define (get-game-properties)
175 `((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)) (fullscreen . ,(get-fullscreen))))
180 (define mobs-table (make-hash-table))
181 (define active-mobs '())
182 (define mobs-changed #f)
184 (define (show-mob-hash mob)
185 (hash-set! mobs-table (mob 'get-mob-id) mob)
186 (set! mobs-changed #t))
188 (define (hide-mob-hash mob-id)
189 (hash-remove! mobs-table mob-id)
190 (set! mobs-changed #t))
192 (define (refresh-active-mobs)
194 (set! mobs-changed #f)
195 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
197 (define (get-active-mobs)
200 (define (hide-all-mobs)
201 (set! mobs-changed #t)
202 (hash-clear! mobs-table))
204 (define (mobs-changed?)
208 (define-macro (show-mob mob)
213 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
215 (define-macro (hide-mob mob)
218 (hide-mob-hash (m 'get-mob-id))))
220 `(hide-mob-hash (,mob 'get-mob-id)))))
222 (define current-mob-id #f)
224 (define translate-mob translate)
226 (define (get-current-mob-id)
229 (define* (run-mobs #:optional (mobs (get-active-mobs)))
230 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
233 (set! current-mob-id (m 'get-mob-id))
234 (glmatrix-block (m)))
236 (set! current-mob-id #f)))
241 (define mob-functions (make-hash-table))
243 (define (get-mob-function-name mob-name)
244 (let ((name (hash-ref mob-functions mob-name)))
247 (hash-set! mob-functions mob-name name)))
250 (define-macro (the-mob mob-name init-data)
251 `(let ((mob-id (gensym))
254 (mob-data ,init-data)
255 (saved-data ,init-data))
256 (lambda* (#:optional (option #f))
258 (let ((time (get-frame-time)))
259 (cond ((not (= time mob-time))
261 (set! saved-data mob-data)))))
268 (procedure-name ,mob-name))
273 (cond ((keyword? option)
274 (assoc-ref saved-data (keyword->symbol option)))
277 (let ((res (,mob-name mob-id mob-data)))
278 (set! mob-z-index (car res))
279 (set! mob-data (cadr res))))))))))
281 (define-macro (define-mob-function attr . body)
282 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
283 (mob-id-symbol (gensym))
284 (mob-id-z-index (gensym))
285 (data-symbol (gensym)))
286 `(lambda (,mob-id-symbol ,data-symbol)
287 (let ((,mob-id-z-index 0))
289 (hide-mob-hash ,mob-id-symbol))
290 (define* (translate x y #:optional (z 0))
292 (translate-mob x y z))
294 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
295 (translate-mob x y))))
299 `(let ((val (assoc-ref ,data-symbol ',(car a))))
300 (cond (val (set! ,(car a) val)))))
304 (lambda (key . args) #f))
305 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
307 (define-macro (define-mob mob-head . body)
308 (let* ((name (car mob-head))
309 (attr (cdr mob-head))
310 (make-fun-symbol (gensym))
311 (mob-fun-symbol (gensym))
312 (params-symbol (gensym)))
313 `(define (,name . ,params-symbol)
314 (define ,make-fun-symbol
315 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
316 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
317 (define ,mob-fun-symbol
318 (define-mob-function ,attr ,@body))
319 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
320 (apply ,make-fun-symbol ,params-symbol))
322 (apply ,mob-fun-symbol ,params-symbol))))))
324 (define-macro (lambda-mob attr . body)
325 (let ((fun-name (gensym)))
327 (define-mob-function ,(cons fun-name attr) ,@body)
328 (the-mob 'undefined '() ,fun-name))))
331 ;;; Functions for checking mobs (collisions and more)
333 (define (map-mobs fun type)
334 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
335 (map (lambda (m) (fun (m 'get-data))) mobs)))
337 (define-macro (define-checking-mobs head mob-def . body)
338 (let ((type (car mob-def)) (attr (cdr mob-def)))
342 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
349 (define-macro (define-scene name . body)