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)
54 #:re-export (translate
63 (define game-loop-thread #f)
65 (define-macro (run-in-game-loop proc)
66 (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
67 (flag-symbol (gensym))
68 (value-symbol (gensym)))
71 (define (,proc . param)
72 (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
73 (let ((,flag-symbol #f))
74 (define ,value-symbol)
78 (lambda () (set! ,value-symbol (apply ,pgl param)))
79 (lambda (key . args) #f))
80 (set! ,flag-symbol #t))
82 (while (not ,flag-symbol))
85 (apply ,pgl param)))))))
87 (run-in-game-loop load-texture)
88 (run-in-game-loop load-font)
89 (run-in-game-loop set-screen-bpp!)
90 (run-in-game-loop resize-screen)
92 (define-macro (game . code)
93 `(let ((game-function ,(if (null? code)
95 `(lambda () ,@code))))
96 (set-game-code game-function)
97 (cond ((not (game-running?))
100 (define (init-gacela)
102 (set-game-code (lambda () #f))
103 (cond ((not game-loop-thread)
104 (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
105 (while (not loop-flag))
108 (define (quit-gacela)
110 (set-game-code (lambda () #f))
111 (set! game-loop-thread #f)
115 (refresh-active-mobs)
116 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
120 ; (check-connections)
122 (cond ((quit-signal?)
127 (refresh-active-mobs)
128 (if (procedure? game-code)
130 (lambda () (game-code))
131 (lambda (key . args) #f)))
138 (define (gacela-script args)
139 (while loop-flag (sleep 1)))
141 (define (game-running?)
144 (define (set-game-code game-function)
145 (set! game-code game-function))
150 (define *title* "Gacela")
151 (define *width-screen* 640)
152 (define *height-screen* 480)
153 (define *bpp-screen* 32)
154 (define *frames-per-second* 20)
156 (define *fullscreen* 'off)
158 (define* (set-game-properties! #:key title width height bpp fps mode fullscreen)
160 (set-screen-title! title))
162 (set-screen-bpp! bpp))
163 (if (or width height)
165 (if (not width) (set! width (get-screen-width)))
166 (if (not height) (set! height (get-screen-height)))
167 (resize-screen width height)))
169 (set-frames-per-second! fps))
171 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
173 (set-fullscreen! fullscreen))
174 (get-game-properties))
176 (define (get-game-properties)
177 `((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))))
182 (define mobs-table (make-hash-table))
183 (define active-mobs '())
184 (define mobs-changed #f)
186 (define (show-mob-hash mob)
187 (hash-set! mobs-table (mob 'get-mob-id) mob)
188 (set! mobs-changed #t))
190 (define (hide-mob-hash mob-id)
191 (hash-remove! mobs-table mob-id)
192 (set! mobs-changed #t))
194 (define (refresh-active-mobs)
196 (set! mobs-changed #f)
197 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
199 (define (get-active-mobs)
202 (define (hide-all-mobs)
203 (set! mobs-changed #t)
204 (hash-clear! mobs-table))
206 (define (mobs-changed?)
210 (define-macro (show-mob mob)
215 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
217 (define-macro (hide-mob mob)
220 (hide-mob-hash (m 'get-mob-id))))
222 `(hide-mob-hash (,mob 'get-mob-id)))))
224 (define current-mob-id #f)
226 (define translate-mob translate)
228 (define (get-current-mob-id)
231 (define* (run-mobs #:optional (mobs (get-active-mobs)))
232 (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
235 (set! current-mob-id (m 'get-mob-id))
236 (glmatrix-block (m)))
238 (set! current-mob-id #f)))
243 (define mob-functions (make-hash-table))
245 (define (get-mob-function-name mob-name)
246 (let ((name (hash-ref mob-functions mob-name)))
249 (hash-set! mob-functions mob-name name)))
252 (define-macro (the-mob mob-name init-data)
253 `(let ((mob-id (gensym))
256 (mob-data ,init-data)
257 (saved-data ,init-data))
258 (lambda* (#:optional (option #f))
260 (let ((time (get-frame-time)))
261 (cond ((not (= time mob-time))
263 (set! saved-data mob-data)))))
270 (procedure-name ,mob-name))
275 (cond ((keyword? option)
276 (assoc-ref saved-data (keyword->symbol option)))
279 (let ((res (,mob-name mob-id mob-data)))
280 (set! mob-z-index (car res))
281 (set! mob-data (cadr res))))))))))
283 (define-macro (define-mob-function attr . body)
284 (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
285 (mob-id-symbol (gensym))
286 (mob-id-z-index (gensym))
287 (data-symbol (gensym)))
288 `(lambda (,mob-id-symbol ,data-symbol)
289 (let ((,mob-id-z-index 0))
291 (hide-mob-hash ,mob-id-symbol))
292 (define* (translate x y #:optional (z 0))
294 (translate-mob x y z))
296 (set! ,mob-id-z-index (+ ,mob-id-z-index z))
297 (translate-mob x y))))
301 `(let ((val (assoc-ref ,data-symbol ',(car a))))
302 (cond (val (set! ,(car a) val)))))
306 (lambda (key . args) #f))
307 (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
309 (define-macro (define-mob mob-head . body)
310 (let* ((name (car mob-head))
311 (attr (cdr mob-head))
312 (make-fun-symbol (gensym))
313 (mob-fun-symbol (gensym))
314 (params-symbol (gensym)))
315 `(define (,name . ,params-symbol)
316 (define ,make-fun-symbol
317 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
318 (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
319 (define ,mob-fun-symbol
320 (define-mob-function ,attr ,@body))
321 (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
322 (apply ,make-fun-symbol ,params-symbol))
324 (apply ,mob-fun-symbol ,params-symbol))))))
326 (define-macro (lambda-mob attr . body)
327 (let ((fun-name (gensym)))
329 (define-mob-function ,(cons fun-name attr) ,@body)
330 (the-mob 'undefined '() ,fun-name))))
333 ;;; Functions for checking mobs (collisions and more)
335 (define (map-mobs fun type)
336 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
337 (map (lambda (m) (fun (m 'get-data))) mobs)))
339 (define-macro (define-checking-mobs head mob-def . body)
340 (let ((type (car mob-def)) (attr (cdr mob-def)))
344 (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
351 (define-macro (define-scene name . body)
358 (define active-bricks '())
360 (define* (draw-bricks #:optional (bricks active-bricks))
361 (cond ((not (null? bricks))
363 (draw-bricks (cdr bricks)))))
365 (define (show-brick brick)
366 (set! active-bricks (cons brick active-bricks)))
368 (define-macro (simple-brick brick-code)
369 (let ((name (gensym)))
379 (define (draw-square . args)
380 (simple-brick (apply video:draw-square args)))
383 (module-map (lambda (sym var)
384 (if (not (eq? sym '%module-public-interface))
385 (module-export! (current-module) (list sym))))