]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
Improved cache system for resources and more things.
[gacela.git] / src / gacela.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
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 (*title*
24             *width-screen*
25             *height-screen*
26             *bpp-screen*
27             *frames-per-second*
28             *mode*
29             set-game-properties!
30             get-game-properties
31             init-gacela
32             quit-gacela
33             game-loop
34             gacela-script
35             game-running?
36             set-game-code
37             show-mob-hash
38             hide-mob-hash
39             get-active-mobs
40             hide-all-mobs
41             get-current-mob-id
42             get-mob-function-name
43             map-mobs
44             translate-mob)
45   #:export-syntax (game
46                    show-mob
47                    hide-mob
48                    the-mob
49                    define-mob-function
50                    define-mob
51                    lambda-mob
52                    define-checking-mobs)
53   #:re-export (translate
54                get-frame-time
55                3d-mode?))
56
57
58 ;;; Main Loop
59
60 (define loop-flag #f)
61 (define game-code #f)
62 (define game-loop-thread #f)
63
64 (define-macro (run-in-game-loop proc)
65   (let ((pgl (string->symbol (string-concatenate (list (symbol->string proc) "-in-game-loop"))))
66         (flag-symbol (gensym))
67         (value-symbol (gensym)))
68     `(begin
69        (define ,pgl ,proc)
70        (define (,proc . param)
71          (cond ((and game-loop-thread (not (eq? game-loop-thread (current-thread))))
72                 (let ((,flag-symbol #f))
73                   (define ,value-symbol)
74                   (system-async-mark
75                    (lambda ()
76                      (catch #t
77                            (lambda () (set! ,value-symbol (apply ,pgl param)))
78                            (lambda (key . args) #f))
79                      (set! ,flag-symbol #t))
80                    game-loop-thread)
81                   (while (not ,flag-symbol))
82                   ,value-symbol))
83                (else
84                 (apply ,pgl param)))))))
85
86 (run-in-game-loop load-texture)
87 (run-in-game-loop load-font)
88 (run-in-game-loop set-screen-bpp!)
89 (run-in-game-loop resize-screen)
90
91 (define-macro (game . code)
92   `(let ((game-function ,(if (null? code)
93                              `(lambda () #f)
94                              `(lambda () ,@code))))
95      (set-game-code game-function)
96      (cond ((not (game-running?))
97             (game-loop)))))
98
99 (define (init-gacela)
100   (hide-all-mobs)
101   (set-game-code (lambda () #f))
102   (cond ((not game-loop-thread)
103          (set! game-loop-thread (call-with-new-thread (lambda () (game))))))
104   (while (not loop-flag))
105   #t)
106
107 (define (quit-gacela)
108   (hide-all-mobs)
109   (set-game-code (lambda () #f))
110   (set! game-loop-thread #f)
111   (set! loop-flag #f))
112
113 (define (game-loop)
114   (refresh-active-mobs)
115   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
116   (set! loop-flag #t)
117   (while loop-flag
118          (init-frame-time)
119 ;           (check-connections)
120          (process-events)
121          (cond ((quit-signal?)
122                 (quit-gacela))
123                (else
124                 (clear-screen)
125                 (to-origin)
126                 (refresh-active-mobs)
127                 (if (procedure? game-code)
128                     (catch #t
129                            (lambda () (game-code))
130                            (lambda (key . args) #f)))
131                 (run-mobs)
132                 (flip-screen)
133                 (delay-frame))))
134   (quit-video))
135
136 (define (gacela-script args)
137   (while loop-flag (sleep 1)))
138
139 (define (game-running?)
140   loop-flag)
141
142 (define (set-game-code game-function)
143   (set! game-code game-function))
144
145
146 ;;; Game Properties
147
148 (define *title* "Gacela")
149 (define *width-screen* 640)
150 (define *height-screen* 480)
151 (define *bpp-screen* 32)
152 (define *frames-per-second* 20)
153 (define *mode* '2d)
154
155 (define* (set-game-properties! #:key title width height bpp fps mode)
156   (if title
157       (set-screen-title! title))
158   (if bpp
159       (set-screen-bpp! bpp))
160   (if (or width height)
161       (begin
162         (if (not width) (set! width (get-screen-width)))
163         (if (not height) (set! height (get-screen-height)))
164         (resize-screen width height)))
165   (if fps
166       (set-frames-per-second! fps))
167   (if mode
168       (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
169   (get-game-properties))
170
171 (define (get-game-properties)
172   `((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
174
175 ;;; Mobs Factory
176
177 (define mobs-table (make-hash-table))
178 (define active-mobs '())
179 (define mobs-changed #f)
180
181 (define (show-mob-hash mob)
182   (hash-set! mobs-table (mob 'get-mob-id) mob)
183   (set! mobs-changed #t))
184
185 (define (hide-mob-hash mob-id)
186   (hash-remove! mobs-table mob-id)
187   (set! mobs-changed #t))
188
189 (define (refresh-active-mobs)
190   (cond (mobs-changed
191          (set! mobs-changed #f)
192          (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
193
194 (define (get-active-mobs)
195   active-mobs)
196
197 (define (hide-all-mobs)
198   (set! mobs-changed #t)
199   (hash-clear! mobs-table))
200
201 (define (mobs-changed?)
202   mobs-changed)
203
204
205 (define-macro (show-mob mob)
206   (cond ((list? mob)
207          `(let ((m ,mob))
208             (show-mob-hash m)))
209         (else
210          `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
211
212 (define-macro (hide-mob mob)
213   (cond ((list? mob)
214          `(let ((m ,mob))
215             (hide-mob-hash (m 'get-mob-id))))
216         (else
217          `(hide-mob-hash (,mob 'get-mob-id)))))
218
219 (define current-mob-id #f)
220
221 (define translate-mob translate)
222
223 (define (get-current-mob-id)
224   current-mob-id)
225
226 (define* (run-mobs #:optional (mobs (get-active-mobs)))
227   (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
228     (for-each
229      (lambda (m)
230        (set! current-mob-id (m 'get-mob-id))
231        (glmatrix-block (m)))
232      sorted-mobs)
233     (set! current-mob-id #f)))
234
235
236 ;;; Making mobs
237
238 (define mob-functions (make-hash-table))
239
240 (define (get-mob-function-name mob-name)
241   (let ((name (hash-ref mob-functions mob-name)))
242     (cond ((not name)
243            (set! name (gensym))
244            (hash-set! mob-functions mob-name name)))
245     name))
246
247 (define-macro (the-mob mob-name init-data)
248   `(let ((mob-id (gensym))
249          (mob-z-index 0)
250          (mob-time 0)
251          (mob-data ,init-data)
252          (saved-data ,init-data))
253      (lambda* (#:optional (option #f))
254        (define (save-data)
255          (let ((time (get-frame-time)))
256            (cond ((not (= time mob-time))
257                   (set! mob-time time)
258                   (set! saved-data mob-data)))))
259        (case option
260          ((get-mob-id)
261           mob-id)
262          ((get-z-index)
263           mob-z-index)
264          ((get-type)
265           (procedure-name ,mob-name))
266          ((get-data)
267           (save-data)
268           saved-data)
269          (else
270           (cond ((keyword? option)
271                  (assoc-ref saved-data (keyword->symbol option)))
272                 (else
273                  (save-data)
274                  (let ((res (,mob-name mob-id mob-data)))
275                    (set! mob-z-index (car res))
276                    (set! mob-data (cadr res))))))))))
277
278 (define-macro (define-mob-function attr . body)
279   (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
280         (mob-id-symbol (gensym))
281         (mob-id-z-index (gensym))
282         (data-symbol (gensym)))
283     `(lambda (,mob-id-symbol ,data-symbol)
284        (let ((,mob-id-z-index 0))
285          (define (kill-me)
286            (hide-mob-hash ,mob-id-symbol))
287          (define* (translate x y #:optional (z 0))
288            (cond ((3d-mode?)
289                   (translate-mob x y z))
290                  (else
291                   (set! ,mob-id-z-index (+ ,mob-id-z-index z))
292                   (translate-mob x y))))
293          (let* ,attr
294            ,@(map
295               (lambda (a)
296                 `(let ((val (assoc-ref ,data-symbol ',(car a))))
297                    (cond (val (set! ,(car a) val)))))
298               attr)
299            (catch #t
300                   (lambda* () ,@body)
301                   (lambda (key . args) #f))
302            (list ,mob-id-z-index (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))))
303
304 (define-macro (define-mob mob-head . body)
305   (let* ((name (car mob-head))
306          (attr (cdr mob-head))
307          (make-fun-symbol (gensym))
308          (mob-fun-symbol (gensym))
309          (params-symbol (gensym)))
310     `(define (,name . ,params-symbol)
311        (define ,make-fun-symbol
312          (lambda* ,(if (null? attr) '() `(#:key ,@attr))
313            (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
314        (define ,mob-fun-symbol
315          (define-mob-function ,attr ,@body))
316        (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
317               (apply ,make-fun-symbol ,params-symbol))
318              (else
319               (apply ,mob-fun-symbol ,params-symbol))))))
320
321 (define-macro (lambda-mob attr . body)
322   (let ((fun-name (gensym)))
323     `(begin
324        (define-mob-function ,(cons fun-name attr) ,@body)
325        (the-mob 'undefined '() ,fun-name))))
326
327
328 ;;; Functions for checking mobs (collisions and more)
329
330 (define (map-mobs fun type)
331   (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
332     (map (lambda (m) (fun (m 'get-data))) mobs)))
333
334 (define-macro (define-checking-mobs head mob-def . body)
335   (let ((type (car mob-def)) (attr (cdr mob-def)))
336     `(define ,head
337        (map-mobs
338         (lambda (m)
339           (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
340             ,@body))
341         ',type))))
342
343
344 ;;; Scenes
345
346 (define-macro (define-scene name . body)
347   `(define (,name)
348      ,@body))