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)
52 #:re-export (translate
60 (define game-loop-thread #f)
62 (define-macro (run-in-game-loop proc)
63 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
64 (flag-symbol (gensym))
65 (value-symbol (gensym)))
68 (define (,proc . param)
69 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
70 (let ((,flag-symbol #f))
71 (define ,value-symbol)
75 (lambda () (set! ,value-symbol (apply ,pgl param)))
76 (lambda (key . args) #f))
77 (set! ,flag-symbol #t))
79 (while (not ,flag-symbol))
82 (apply ,pgl param)))))))
84 (run-in-game-loop load-texture)
85 (run-in-game-loop load-font)
86 (run-in-game-loop set-screen-bpp!)
87 (run-in-game-loop resize-screen)
89 (define-macro (game . code)
92 `(call-with-new-thread (lambda () ,@code))))
96 (cond ((not game-loop-thread)
97 (set! game-loop-thread (call-with-new-thread (lambda () (cond ((not (game-running?)) (game-loop))))))))
98 (while (not loop-flag))
101 (define (quit-gacela)
103 (set! game-loop-thread #f)
107 (refresh-active-mobs)
108 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
112 ; (check-connections)
114 (cond ((quit-signal?)
119 (refresh-active-mobs)
126 (define (gacela-script args)
127 (while loop-flag (sleep 1)))
129 (define (game-running?)
135 (define *title* "Gacela")
136 (define *width-screen* 640)
137 (define *height-screen* 480)
138 (define *bpp-screen* 32)
139 (define *frames-per-second* 20)
141 (define *fullscreen* 'off)
143 (define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
145 (set-screen-title! title))
147 (set-screen-bpp! bpp))
148 (if (or width height)
150 (if (not width) (set! width (get-screen-width)))
151 (if (not height) (set! height (get-screen-height)))
152 (resize-screen width height)))
154 (set-frames-per-second! fps))
156 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
158 (set-fullscreen! fullscreen))
159 (get-game-properties))
161 (define (get-game-properties)
162 `((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))))
167 (define mobs-table (make-hash-table))
168 (define active-mobs '())
169 (define mobs-changed #f)
171 (define (show-mob-hash mob)
172 (hash-set! mobs-table (mob 'get-mob-id) mob)
173 (set! mobs-changed #t))
175 (define (hide-mob-hash mob-id)
176 (hash-remove! mobs-table mob-id)
177 (set! mobs-changed #t))
179 (define (refresh-active-mobs)
181 (set! mobs-changed #f)
182 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
184 (define (get-active-mobs)
187 (define (hide-all-mobs)
188 (set! mobs-changed #t)
189 (hash-clear! mobs-table))
191 (define (mobs-changed?)
195 (define-macro (show-mob mob)
200 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
202 (define-macro (hide-mob mob)
205 (hide-mob-hash (m 'get-mob-id))))
207 `(hide-mob-hash (,mob 'get-mob-id)))))
209 (define current-mob-id #f)
211 (define translate-mob translate)
213 (define (get-current-mob-id)
216 (define* (run-mobs #:optional (mobs (get-active-mobs)))
217 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
220 (set! current-mob-id (m 'get-mob-id))
221 (glmatrix-block (m)))
223 (set! current-mob-id #f)))
228 (define mob-functions (make-hash-table))
230 (define (get-mob-function-name mob-name)
231 (let ((name (hash-ref mob-functions mob-name)))
234 (hash-set! mob-functions mob-name name)))
237 (define-macro (the-mob mob-name init-data)
238 `(let ((mob-id (gensym))
241 (mob-data ,init-data)
242 (saved-data ,init-data))
243 (lambda* (#:optional (option #f))
245 (let ((time (get-frame-time)))
246 (cond ((not (= time mob-time))
248 (set! saved-data mob-data)))))
255 (procedure-name ,mob-name))
260 (cond ((keyword? option)
261 (assoc-ref saved-data (keyword->symbol option)))
264 (let ((res (,mob-name mob-id mob-data)))
265 (set! mob-z-index (car res))
266 (set! mob-data (cadr res))))))))))
268 (define-macro (define-mob-function attr . body)
269 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
270 (mob-id-symbol (gensym))
271 (mob-id-z-index (gensym))
272 (data-symbol (gensym)))
273 `(lambda (,mob-id-symbol ,data-symbol)
274 (let ((,mob-id-z-index 0))
276 (hide-mob-hash ,mob-id-symbol))
277 (define* (translate x y #:optional (z 0))
279 (translate-mob x y z))
281 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
282 (translate-mob x y))))
286 `(let ((val (assoc-ref ,data-symbol ',(car a))))
287 (cond (val (set! ,(car a) val)))))
291 (lambda (key . args) #f))
292 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
294 (define-macro (define-mob mob-head . body)
295 (let* ((name (car mob-head))
296 (attr (cdr mob-head))
297 (make-fun-symbol (gensym))
298 (mob-fun-symbol (gensym))
299 (params-symbol (gensym)))
300 `(define (,name . ,params-symbol)
301 (define ,make-fun-symbol
302 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
303 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
304 (define ,mob-fun-symbol
305 (define-mob-function ,attr ,@body))
306 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
307 (apply ,make-fun-symbol ,params-symbol))
309 (apply ,mob-fun-symbol ,params-symbol))))))
311 (define-macro (lambda-mob attr . body)
312 (let ((fun-name (gensym)))
314 (define-mob-function ,(cons fun-name attr) ,@body)
315 (the-mob 'undefined '() ,fun-name))))
318 ;;; Functions for checking mobs (collisions and more)
320 (define (map-mobs fun type)
321 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
322 (map (lambda (m) (fun (m 'get-data))) mobs)))
324 (define-macro (define-checking-mobs head mob-def . body)
325 (let ((type (car mob-def)) (attr (cdr mob-def)))
329 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
336 (define-macro (define-scene name . body)
343 (define default-view (make-hash-table))
345 (define* (draw-meshes #:optional (meshes (hash-map->list (lambda (k v) v) default-view)))
346 (cond ((not (null? meshes))
348 (lambda () ((car meshes) 'draw))
349 (lambda (key . args) #f))
350 (draw-meshes (cdr meshes)))))
353 (module-map (lambda (sym var)
354 (if (not (eq? sym '%module-public-interface))
355 (module-export! (current-module) (list sym))))