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 (get-frame-time
58 (define game-loop-flag #f)
59 (define game-loop-thread #f)
60 (define game-loop-procedure #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)
90 `(set! game-loop-procedure
93 `(lambda (game-elements) ,@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 game-loop-flag))
102 (define (quit-gacela)
104 (set! game-loop-thread #f)
105 (set! game-loop-flag #f)
109 ; (refresh-active-mobs)
110 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
111 (set! game-loop-flag #t)
112 (let loop ((game-elements '()))
113 (cond (game-loop-flag
115 ; (check-connections)
117 (cond ((quit-signal?)
122 ; (refresh-active-mobs)
125 (if game-loop-procedure
126 (set! game-elements (game-loop-procedure game-elements)))
127 (process-game-elements game-elements)
132 (define (game-running?)
135 (define (process-game-elements elements)
136 (cond ((not (list? elements))
137 (process-game-elements (list elements)))
139 (draw-meshes (filter (lambda (e) (mesh? e)) elements))
142 ;;; Extensions to main loop
144 (define extensions '())
146 (define (add-extension! proc pri)
147 "Add an extension with a priority to the main loop"
149 (sort (assoc-set! extensions proc pri)
151 (< (cdr a) (cdr b))))))
153 (define (remove-extension! proc)
154 "Remove an extension from the main loop"
156 (assoc-remove! extensions proc)))
158 (define (run-extensions)
159 (for-each (lambda (x) ((car x))) extensions))
164 (define *title* "Gacela")
165 (define *width-screen* 640)
166 (define *height-screen* 480)
167 (define *bpp-screen* 32)
168 (define *frames-per-second* 20)
170 (define *fullscreen* 'off)
172 (define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
174 (set-screen-title! title))
176 (set-screen-bpp! bpp))
177 (if (or width height)
179 (if (not width) (set! width (get-screen-width)))
180 (if (not height) (set! height (get-screen-height)))
181 (resize-screen width height)))
183 (set-frames-per-second! fps))
185 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
187 (set-fullscreen! fullscreen))
188 (get-game-properties))
190 (define (get-game-properties)
191 `((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))))
196 (define mobs-table (make-hash-table))
197 (define active-mobs '())
198 (define mobs-changed #f)
200 (define (show-mob-hash mob)
201 (hash-set! mobs-table (mob 'get-mob-id) mob)
202 (set! mobs-changed #t))
204 (define (hide-mob-hash mob-id)
205 (hash-remove! mobs-table mob-id)
206 (set! mobs-changed #t))
208 (define (refresh-active-mobs)
210 (set! mobs-changed #f)
211 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
213 (define (get-active-mobs)
216 (define (hide-all-mobs)
217 (set! mobs-changed #t)
218 (hash-clear! mobs-table))
220 (define (mobs-changed?)
224 (define-macro (show-mob mob)
229 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
231 (define-macro (hide-mob mob)
234 (hide-mob-hash (m 'get-mob-id))))
236 `(hide-mob-hash (,mob 'get-mob-id)))))
238 (define current-mob-id #f)
240 (define translate-mob translate)
242 (define (get-current-mob-id)
245 (define* (run-mobs #:optional (mobs (get-active-mobs)))
246 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
249 (set! current-mob-id (m 'get-mob-id))
250 (glmatrix-block (m)))
252 (set! current-mob-id #f)))
257 (define mob-functions (make-hash-table))
259 (define (get-mob-function-name mob-name)
260 (let ((name (hash-ref mob-functions mob-name)))
263 (hash-set! mob-functions mob-name name)))
266 (define-macro (the-mob mob-name init-data)
267 `(let ((mob-id (gensym))
270 (mob-data ,init-data)
271 (saved-data ,init-data))
272 (lambda* (#:optional (option #f))
274 (let ((time (get-frame-time)))
275 (cond ((not (= time mob-time))
277 (set! saved-data mob-data)))))
284 (procedure-name ,mob-name))
289 (cond ((keyword? option)
290 (assoc-ref saved-data (keyword->symbol option)))
293 (let ((res (,mob-name mob-id mob-data)))
294 (set! mob-z-index (car res))
295 (set! mob-data (cadr res))))))))))
297 (define-macro (define-mob-function attr . body)
298 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
299 (mob-id-symbol (gensym))
300 (mob-id-z-index (gensym))
301 (data-symbol (gensym)))
302 `(lambda (,mob-id-symbol ,data-symbol)
303 (let ((,mob-id-z-index 0))
305 (hide-mob-hash ,mob-id-symbol))
306 (define* (translate x y #:optional (z 0))
308 (translate-mob x y z))
310 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
311 (translate-mob x y))))
315 `(let ((val (assoc-ref ,data-symbol ',(car a))))
316 (cond (val (set! ,(car a) val)))))
320 (lambda (key . args) #f))
321 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
323 (define-macro (define-mob mob-head . body)
324 (let* ((name (car mob-head))
325 (attr (cdr mob-head))
326 (make-fun-symbol (gensym))
327 (mob-fun-symbol (gensym))
328 (params-symbol (gensym)))
329 `(define (,name . ,params-symbol)
330 (define ,make-fun-symbol
331 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
332 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
333 (define ,mob-fun-symbol
334 (define-mob-function ,attr ,@body))
335 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
336 (apply ,make-fun-symbol ,params-symbol))
338 (apply ,mob-fun-symbol ,params-symbol))))))
340 (define-macro (lambda-mob attr . body)
341 (let ((fun-name (gensym)))
343 (define-mob-function ,(cons fun-name attr) ,@body)
344 (the-mob 'undefined '() ,fun-name))))
347 ;;; Functions for checking mobs (collisions and more)
349 (define (map-mobs fun type)
350 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
351 (map (lambda (m) (fun (m 'get-data))) mobs)))
353 (define-macro (define-checking-mobs head mob-def . body)
354 (let ((type (car mob-def)) (attr (cdr mob-def)))
358 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
365 (define-macro (define-scene name . body)
370 (module-map (lambda (sym var)
371 (if (not (eq? sym '%module-public-interface))
372 (module-export! (current-module) (list sym))))