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)
23 #:export (load-texture
38 #:export-syntax (game)
39 #:re-export (get-current-color
61 (define resources-cache (make-weak-value-hash-table))
63 (define (from-cache key)
64 (hash-ref resources-cache key))
66 (define (into-cache key res)
67 (hash-set! resources-cache key res))
69 (define-macro (use-cache-with module proc)
70 (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
72 (define ,pwc (@ ,module ,proc))
73 (define (,proc . param)
75 (res (from-cache key)))
79 (set! res (apply ,pwc param))
83 (use-cache-with (gacela video) load-texture)
84 (use-cache-with (gacela video) load-font)
91 (define game-loop-thread #f)
93 (define-macro (game . code)
94 `(let ((game-function ,(if (null? code)
96 `(lambda () ,@code))))
97 (set-game-code game-function)
98 (cond ((not (game-running?))
101 (define-macro (run-in-game-loop . code)
102 `(if game-loop-thread
103 (system-async-mark (lambda () ,@code) game-loop-thread)
106 (define (init-gacela)
107 (set! game-loop-thread (call-with-new-thread (lambda () (game)))))
109 (define (quit-gacela)
110 (set! game-loop-thread #f)
114 ; (refresh-active-mobs)
116 (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 (game-running?)
139 (define (set-game-code game-function)
140 (set! game-code game-function))
145 (define *title* "Gacela")
146 (define *width-screen* 640)
147 (define *height-screen* 480)
148 (define *bpp-screen* 32)
149 (define *frames-per-second* 20)
152 (define* (set-game-properties! #:key title width height bpp fps mode)
154 (set-screen-title! title))
156 (run-in-game-loop (set-screen-bpp! bpp)))
157 (if (or width height)
159 (if (not width) (set! width (get-screen-width)))
160 (if (not height) (set! height (get-screen-height)))
161 (run-in-game-loop (resize-screen width height))))
163 (set-frames-per-second! fps))
165 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
167 (define (get-game-properties)
168 `((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))))
173 (define mobs-table (make-hash-table))
174 (define active-mobs '())
177 (define (show-mob-hash mob)
178 (hash-set! mobs-table (mob 'get-mob-id) mob)
181 (define (hide-mob-hash mob-id)
182 (hash-remove! mobs-table mob-id)
185 (define (refresh-active-mobs)
188 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
190 (define (get-active-mobs)
193 (define (hide-all-mobs)
195 (hash-clear! mobs-table))
197 (define (mobs-changed?)
201 (define-macro (show-mob mob)
206 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
208 (define-macro (hide-mob mob)
211 (hide-mob-hash (m 'get-mob-id))))
213 `(hide-mob-hash (,mob 'get-mob-id)))))
215 (define* (run-mobs #:optional (mobs (get-active-mobs)))
226 (define-macro (define-mob mob-head . body)
227 (let ((name (car mob-head)) (attr (cdr mob-head)))
228 `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
229 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
230 (the-mob ',name () ,attr ,@body)))))
232 (define-macro (the-mob type attr publish . body)
233 (let ((mob-id-symbol (gensym))
234 (type-symbol (gensym))
235 (time-symbol (gensym))
236 (data-symbol (gensym)))
237 `(let ((,mob-id-symbol (gensym))
242 (lambda* (#:optional (option #f))
244 (hide-mob-hash ,mob-id-symbol))
246 (let ((time (get-frame-time)))
247 (cond ((not (= time ,time-symbol))
248 (set! ,time-symbol time)
249 (set! ,data-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))))))
252 (define (filter-mobs type fun)
254 (define (map-mobs fun type)
255 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
256 (map (lambda (m) (fun (m 'get-data))) mobs)))
269 (lambda (key . args) #f))))))))
271 (define-macro (lambda-mob attr . body)
272 `(the-mob 'undefined ,attr '() ,@body))
277 ;; (define-macro (lambda-mob-data attr . body)
278 ;; `(lambda ,attr ,@body))
280 ;; (define-macro (define-collision-check name mobs . body)
281 ;; `(defmacro* ,name (#:optional m)
282 ;; `(let ,(cond (m `((mob-id (,m 'get-mob-id)) (mob-type (,m 'get-type))))