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
52 #:re-export (get-current-color
75 (define resources-cache (make-weak-value-hash-table))
77 (define (from-cache key)
78 (hash-ref resources-cache key))
80 (define (into-cache key res)
81 (hash-set! resources-cache key res))
83 (define-macro (use-cache-with module proc)
84 (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
86 (define ,pwc (@ ,module ,proc))
87 (define (,proc . param)
89 (res (from-cache key)))
93 (set! res (apply ,pwc param))
97 (use-cache-with (gacela video) load-texture)
98 (use-cache-with (gacela video) load-font)
103 (define loop-flag #f)
104 (define game-code #f)
105 (define game-loop-thread #f)
107 (define-macro (game . code)
108 `(let ((game-function ,(if (null? code)
110 `(lambda () ,@code))))
111 (set-game-code game-function)
112 (cond ((not (game-running?))
115 (define-macro (run-in-game-loop . code)
116 `(if game-loop-thread
117 (system-async-mark (lambda () ,@code) game-loop-thread)
120 (define (init-gacela)
121 (set! game-loop-thread (call-with-new-thread (lambda () (game)))))
123 (define (quit-gacela)
124 (set! game-loop-thread #f)
128 (refresh-active-mobs)
130 (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
133 ; (check-connections)
135 (cond ((quit-signal?)
140 (refresh-active-mobs)
141 (if (procedure? game-code)
143 (lambda () (game-code))
144 (lambda (key . args) #f)))
150 (define (game-running?)
153 (define (set-game-code game-function)
154 (set! game-code game-function))
159 (define *title* "Gacela")
160 (define *width-screen* 640)
161 (define *height-screen* 480)
162 (define *bpp-screen* 32)
163 (define *frames-per-second* 20)
166 (define* (set-game-properties! #:key title width height bpp fps mode)
168 (set-screen-title! title))
170 (run-in-game-loop (set-screen-bpp! bpp)))
171 (if (or width height)
173 (if (not width) (set! width (get-screen-width)))
174 (if (not height) (set! height (get-screen-height)))
175 (run-in-game-loop (resize-screen width height))))
177 (set-frames-per-second! fps))
179 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
181 (define (get-game-properties)
182 `((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))))
187 (define mobs-table (make-hash-table))
188 (define active-mobs '())
189 (define mobs-changed #f)
191 (define (show-mob-hash mob)
192 (hash-set! mobs-table (mob 'get-mob-id) mob)
193 (set! mobs-changed #t))
195 (define (hide-mob-hash mob-id)
196 (hash-remove! mobs-table mob-id)
197 (set! mobs-changed #t))
199 (define (refresh-active-mobs)
201 (set! mobs-changed #f)
202 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
204 (define (get-active-mobs)
207 (define (hide-all-mobs)
208 (set! mobs-changed #t)
209 (hash-clear! mobs-table))
211 (define (mobs-changed?)
215 (define-macro (show-mob mob)
220 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
222 (define-macro (hide-mob mob)
225 (hide-mob-hash (m 'get-mob-id))))
227 `(hide-mob-hash (,mob 'get-mob-id)))))
229 (define current-mob-id #f)
231 (define (get-current-mob-id)
234 (define* (run-mobs #:optional (mobs (get-active-mobs)))
237 (set! current-mob-id (m 'get-mob-id))
238 (glmatrix-block (m)))
240 (set! current-mob-id #f))
245 (define mob-functions (make-hash-table))
247 (define (get-mob-function-name mob-name)
248 (let ((name (hash-ref mob-functions mob-name)))
251 (hash-set! mob-functions mob-name name)))
254 (define-macro (the-mob type init-data fun-name)
255 `(let ((mob-id (gensym))
257 (mob-data ,init-data)
258 (saved-data ,init-data))
259 (lambda* (#:optional (option #f))
261 (let ((time (get-frame-time)))
262 (cond ((not (= time mob-time))
264 (set! saved-data mob-data)))))
265 ; (define (filter-mobs type fun)
267 ; (define (map-mobs fun type)
268 ; (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
269 ; (map (lambda (m) (fun (m 'get-data))) mobs)))
280 (set! mob-data (,fun-name mob-id mob-data)))))))
282 (define-macro (define-mob-function head . body)
283 (let ((fun-name (car head))
284 (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
285 (mob-id-symbol (gensym))
286 (data-symbol (gensym)))
287 `(define (,fun-name ,mob-id-symbol ,data-symbol)
289 (hide-mob-hash ,mob-id-symbol))
293 `(let ((val (assoc-ref ,data-symbol ',(car a))))
294 (cond (val (set! ,(car a) val)))))
298 (lambda (key . args) #f))
299 (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))
301 (define-macro (define-mob mob-head . body)
302 (let* ((name (car mob-head)) (attr (cdr mob-head))
303 (fun-name (get-mob-function-name name)))
305 (define-mob-function ,(cons fun-name attr) ,@body)
306 (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
307 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
308 (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
310 (define-macro (lambda-mob attr . body)
311 (let ((fun-name (gensym)))
313 (define-mob-function ,(cons fun-name attr) ,@body)
314 (the-mob 'undefined '() ,fun-name))))
317 ;;; Functions for checking mobs (collisions and more)
319 (define (map-mobs fun)
320 (let ((mobs (filter (lambda (m) (not (eq? (m 'get-mob-id) (get-current-mob-id)))) (get-active-mobs))))
323 (define-macro (define-checking-mobs head mob-attr . body)
326 ;; (define-macro (lambda-mob-data attr . body)
327 ;; `(lambda ,attr ,@body))
329 ;; (define-macro (define-collision-check name mobs . body)
330 ;; `(defmacro* ,name (#:optional m)
331 ;; `(let ,(cond (m `((mob-id (,m 'get-mob-id)) (mob-type (,m 'get-type))))