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
41 get-mob-function-name)
49 #:re-export (get-current-color
72 (define resources-cache (make-weak-value-hash-table))
74 (define (from-cache key)
75 (hash-ref resources-cache key))
77 (define (into-cache key res)
78 (hash-set! resources-cache key res))
80 (define-macro (use-cache-with module proc)
81 (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
83 (define ,pwc (@ ,module ,proc))
84 (define (,proc . param)
86 (res (from-cache key)))
90 (set! res (apply ,pwc param))
94 (use-cache-with (gacela video) load-texture)
95 (use-cache-with (gacela video) load-font)
100 (define loop-flag #f)
101 (define game-code #f)
102 (define game-loop-thread #f)
104 (define-macro (game . code)
105 `(let ((game-function ,(if (null? code)
107 `(lambda () ,@code))))
108 (set-game-code game-function)
109 (cond ((not (game-running?))
112 (define-macro (run-in-game-loop . code)
113 `(if game-loop-thread
114 (system-async-mark (lambda () ,@code) game-loop-thread)
117 (define (init-gacela)
118 (set! game-loop-thread (call-with-new-thread (lambda () (game)))))
120 (define (quit-gacela)
121 (set! game-loop-thread #f)
125 (refresh-active-mobs)
127 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
130 ; (check-connections)
132 (cond ((quit-signal?)
137 (refresh-active-mobs)
138 (if (procedure? game-code)
140 (lambda () (game-code))
141 (lambda (key . args) #f)))
147 (define (game-running?)
150 (define (set-game-code game-function)
151 (set! game-code game-function))
156 (define *title* "Gacela")
157 (define *width-screen* 640)
158 (define *height-screen* 480)
159 (define *bpp-screen* 32)
160 (define *frames-per-second* 20)
163 (define* (set-game-properties! #:key title width height bpp fps mode)
165 (set-screen-title! title))
167 (run-in-game-loop (set-screen-bpp! bpp)))
168 (if (or width height)
170 (if (not width) (set! width (get-screen-width)))
171 (if (not height) (set! height (get-screen-height)))
172 (run-in-game-loop (resize-screen width height))))
174 (set-frames-per-second! fps))
176 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
178 (define (get-game-properties)
179 `((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))))
184 (define mobs-table (make-hash-table))
185 (define active-mobs '())
186 (define mobs-changed #f)
188 (define (show-mob-hash mob)
189 (hash-set! mobs-table (mob 'get-mob-id) mob)
190 (set! mobs-changed #t))
192 (define (hide-mob-hash mob-id)
193 (hash-remove! mobs-table mob-id)
194 (set! mobs-changed #t))
196 (define (refresh-active-mobs)
198 (set! mobs-changed #f)
199 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
201 (define (get-active-mobs)
204 (define (hide-all-mobs)
205 (set! mobs-changed #t)
206 (hash-clear! mobs-table))
208 (define (mobs-changed?)
212 (define-macro (show-mob mob)
217 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
219 (define-macro (hide-mob mob)
222 (hide-mob-hash (m 'get-mob-id))))
224 `(hide-mob-hash (,mob 'get-mob-id)))))
226 (define* (run-mobs #:optional (mobs (get-active-mobs)))
229 (glmatrix-block (m)))
235 (define mob-functions (make-hash-table))
237 (define (get-mob-function-name mob-name)
238 (let ((name (hash-ref mob-functions mob-name)))
241 (hash-set! mob-functions mob-name name)))
244 (define-macro (the-mob type init-data fun-name)
245 `(let ((mob-id (gensym))
247 (mob-data ,init-data)
248 (saved-data ,init-data))
249 (lambda* (#:optional (option #f))
251 (let ((time (get-frame-time)))
252 (cond ((not (= time mob-time))
254 (set! saved-data mob-data)))))
255 ; (define (filter-mobs type fun)
257 ; (define (map-mobs fun type)
258 ; (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
259 ; (map (lambda (m) (fun (m 'get-data))) mobs)))
270 (set! mob-data (,fun-name mob-id mob-data)))))))
272 (define-macro (define-mob-function head . body)
273 (let ((fun-name (car head))
274 (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
275 (mob-id-symbol (gensym))
276 (data-symbol (gensym)))
277 `(define (,fun-name ,mob-id-symbol ,data-symbol)
279 (hide-mob-hash ,mob-id-symbol))
283 `(let ((val (assoc-ref ,data-symbol ',(car a))))
284 (cond (val (set! ,(car a) val)))))
288 (lambda (key . args) #f))
289 (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))
291 (define-macro (define-mob mob-head . body)
292 (let* ((name (car mob-head)) (attr (cdr mob-head))
293 (fun-name (get-mob-function-name name)))
295 (define-mob-function ,(cons fun-name attr) ,@body)
296 (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
297 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
298 (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
300 (define-macro (lambda-mob attr . body)
301 (let ((fun-name (gensym)))
303 (define-mob-function ,(cons fun-name attr) ,@body)
304 (the-mob 'undefined '() ,fun-name))))
309 ;; (define-macro (lambda-mob-data attr . body)
310 ;; `(lambda ,attr ,@body))
312 ;; (define-macro (define-collision-check name mobs . body)
313 ;; `(defmacro* ,name (#:optional m)
314 ;; `(let ,(cond (m `((mob-id (,m 'get-mob-id)) (mob-type (,m 'get-type))))