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