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 video) #:renamer (symbol-prefix-proc 'video:))
22 #:use-module (gacela audio)
23 #:use-module (ice-9 optargs)
53 #:re-export ;(translate
61 (define game-loop-thread #f)
63 (define-macro (run-in-game-loop proc)
64 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
65 (flag-symbol (gensym))
66 (value-symbol (gensym)))
69 (define (,proc . param)
70 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
71 (let ((,flag-symbol #f))
72 (define ,value-symbol)
76 (lambda () (set! ,value-symbol (apply ,pgl param)))
77 (lambda (key . args) #f))
78 (set! ,flag-symbol #t))
80 (while (not ,flag-symbol))
83 (apply ,pgl param)))))))
85 (run-in-game-loop load-texture)
86 (run-in-game-loop load-font)
87 (run-in-game-loop set-screen-bpp!)
88 (run-in-game-loop resize-screen)
90 (define-macro (game . code)
93 `(call-with-new-thread (lambda () ,@code))))
97 (cond ((not game-loop-thread)
98 (set! game-loop-thread (call-with-new-thread (lambda () (cond ((not (game-running?)) (game-loop))))))))
99 (while (not loop-flag))
102 (define (quit-gacela)
104 (set! game-loop-thread #f)
108 (refresh-active-mobs)
109 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
113 ; (check-connections)
115 (cond ((quit-signal?)
120 (refresh-active-mobs)
127 (define (gacela-script args)
128 (while loop-flag (sleep 1)))
130 (define (game-running?)
136 (define *title* "Gacela")
137 (define *width-screen* 640)
138 (define *height-screen* 480)
139 (define *bpp-screen* 32)
140 (define *frames-per-second* 20)
142 (define *fullscreen* 'off)
144 (define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
146 (set-screen-title! title))
148 (set-screen-bpp! bpp))
149 (if (or width height)
151 (if (not width) (set! width (get-screen-width)))
152 (if (not height) (set! height (get-screen-height)))
153 (resize-screen width height)))
155 (set-frames-per-second! fps))
157 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
159 (set-fullscreen! fullscreen))
160 (get-game-properties))
162 (define (get-game-properties)
163 `((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))))
168 (define mobs-table (make-hash-table))
169 (define active-mobs '())
170 (define mobs-changed #f)
172 (define (show-mob-hash mob)
173 (hash-set! mobs-table (mob 'get-mob-id) mob)
174 (set! mobs-changed #t))
176 (define (hide-mob-hash mob-id)
177 (hash-remove! mobs-table mob-id)
178 (set! mobs-changed #t))
180 (define (refresh-active-mobs)
182 (set! mobs-changed #f)
183 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
185 (define (get-active-mobs)
188 (define (hide-all-mobs)
189 (set! mobs-changed #t)
190 (hash-clear! mobs-table))
192 (define (mobs-changed?)
196 (define-macro (show-mob mob)
201 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
203 (define-macro (hide-mob mob)
206 (hide-mob-hash (m 'get-mob-id))))
208 `(hide-mob-hash (,mob 'get-mob-id)))))
210 (define current-mob-id #f)
212 (define translate-mob translate)
214 (define (get-current-mob-id)
217 (define* (run-mobs #:optional (mobs (get-active-mobs)))
218 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
221 (set! current-mob-id (m 'get-mob-id))
222 (glmatrix-block (m)))
224 (set! current-mob-id #f)))
229 (define mob-functions (make-hash-table))
231 (define (get-mob-function-name mob-name)
232 (let ((name (hash-ref mob-functions mob-name)))
235 (hash-set! mob-functions mob-name name)))
238 (define-macro (the-mob mob-name init-data)
239 `(let ((mob-id (gensym))
242 (mob-data ,init-data)
243 (saved-data ,init-data))
244 (lambda* (#:optional (option #f))
246 (let ((time (get-frame-time)))
247 (cond ((not (= time mob-time))
249 (set! saved-data mob-data)))))
256 (procedure-name ,mob-name))
261 (cond ((keyword? option)
262 (assoc-ref saved-data (keyword->symbol option)))
265 (let ((res (,mob-name mob-id mob-data)))
266 (set! mob-z-index (car res))
267 (set! mob-data (cadr res))))))))))
269 (define-macro (define-mob-function attr . body)
270 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
271 (mob-id-symbol (gensym))
272 (mob-id-z-index (gensym))
273 (data-symbol (gensym)))
274 `(lambda (,mob-id-symbol ,data-symbol)
275 (let ((,mob-id-z-index 0))
277 (hide-mob-hash ,mob-id-symbol))
278 (define* (translate x y #:optional (z 0))
280 (translate-mob x y z))
282 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
283 (translate-mob x y))))
287 `(let ((val (assoc-ref ,data-symbol ',(car a))))
288 (cond (val (set! ,(car a) val)))))
292 (lambda (key . args) #f))
293 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
295 (define-macro (define-mob mob-head . body)
296 (let* ((name (car mob-head))
297 (attr (cdr mob-head))
298 (make-fun-symbol (gensym))
299 (mob-fun-symbol (gensym))
300 (params-symbol (gensym)))
301 `(define (,name . ,params-symbol)
302 (define ,make-fun-symbol
303 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
304 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
305 (define ,mob-fun-symbol
306 (define-mob-function ,attr ,@body))
307 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
308 (apply ,make-fun-symbol ,params-symbol))
310 (apply ,mob-fun-symbol ,params-symbol))))))
312 (define-macro (lambda-mob attr . body)
313 (let ((fun-name (gensym)))
315 (define-mob-function ,(cons fun-name attr) ,@body)
316 (the-mob 'undefined '() ,fun-name))))
319 ;;; Functions for checking mobs (collisions and more)
321 (define (map-mobs fun type)
322 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
323 (map (lambda (m) (fun (m 'get-data))) mobs)))
325 (define-macro (define-checking-mobs head mob-def . body)
326 (let ((type (car mob-def)) (attr (cdr mob-def)))
330 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
337 (define-macro (define-scene name . body)
344 (define active-views (make-hash-table))
346 (define* (draw-views #:optional (views (hash-map->list (lambda (k v) v) active-views)))
347 (cond ((not (null? views))
349 (lambda* () ((car views)))
350 (lambda (key . args) #f))
351 (draw-views (cdr views)))))
353 (define-macro (define-view name content)
355 (hash-set! active-views ',name (lambda () (glmatrix-block ,content)))
361 (define-macro (translate x y view-or-z . view)
362 (let* ((z (if (null? view) 0 view-or-z))
363 (view (if (null? view) view-or-z (car view))))
365 (gltranslate ,x ,y ,z)
369 (module-map (lambda (sym var)
370 (if (not (eq? sym '%module-public-interface))
371 (module-export! (current-module) (list sym))))