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 (use-modules (srfi srfi-1))
22 (define-macro (define-action action-def . code)
23 (let ((name (car action-def)) (attr (cdr action-def)))
24 `(define (,name mob-attr)
25 (let ,(map attribute-definition attr)
27 ,(cons 'list (map attribute-result attr))))))
29 (define (attribute-definition attribute)
30 (let ((name (if (list? attribute) (car attribute) attribute))
31 (value (if (list? attribute) (cadr attribute) #f)))
32 `(,name (let ((v (assoc-ref mob-attr ',name))) (if v (cdr v) ,value)))))
34 (define (attribute-result attribute)
35 (let ((name (if (list? attribute) (car attribute) attribute)))
36 `(list ',name ,name)))
43 (define kill-all-mobs #f)
44 (define refresh-active-mobs #f)
45 (define action-mobs #f)
46 (define render-mobs #f)
48 (let ((active-mobs '()) (mobs-to-add '()) (mobs-to-kill '())
51 (pushnew mob mobs-to-add)))
55 (pushnew mob mobs-to-kill))
59 (set! active-mobs '())
60 (set! mobs-to-add '())
61 (set! mobs-to-kill '())))
63 (set! refresh-active-mobs
65 (define (add active to-add)
66 (cond ((null? to-add) active)
68 (pushnew (car to-add) active)
69 (add active (cdr to-add)))))
71 (add active-mobs mobs-to-add)
72 (set! mobs-to-add '())
73 (set! active-mobs (reverse (lset-difference eq? active-mobs mobs-to-kill)))
74 (set! mobs-to-kill '())))
78 (map (lambda (m) (m #:action)) active-mobs)))
82 (map (lambda (m) (m #:render)) active-mobs))))
85 (define-macro (makemob name . methods)
86 `(define* (,name . args)
87 (let ((option (car args)))
91 (:off (mob-off ',name)))
93 (let ((option (car m)) (body (cadr m)))
95 (else (cons (list option `(apply ,body (cdr args))) (options (cddr m)))))))
98 (define-macro (makemob name . methods)
100 (cond ((null? m) '((else #f)))
102 (let ((option (caar m)) (body (cadar m)))
103 (cons `((,option) (apply ,body (cdr args))) (options (cdr m)))))))
104 (let ((m (options methods)))
105 `(define (,name . args)
106 (let ((option (car args)))
108 ((#:on) (mob-on ',name))
109 ((#:off) (mob-off ',name))
116 (define refresh-running-mobs #f)
117 (define quit-all-mobs #f)
119 (let ((running-mobs '()) (mobs-to-add '()) (mobs-to-quit '()))
122 (push mob mobs-to-add)))
125 (lambda* (option #:key args function)
126 (define (run-mobs-rec mobs)
127 (cond ((null? mobs) #f)
129 (cond (function (function)))
130 (catch #t (lambda () (apply (car mobs) (cons option args))) (lambda (key . args) #f))
131 (or #t (run-mobs-rec (cdr mobs))))))
132 (run-mobs-rec running-mobs)))
136 (push mob mobs-to-quit)))
138 (set! refresh-running-mobs
140 (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null? mob))
141 (push mob running-mobs)
142 (catch #t (lambda () (mob #:init)) (lambda (key . args) #f)))
143 (set! running-mobs (reverse (lset-difference eq? running-mobs mobs-to-quit)))
144 (set! mobs-to-quit '())))
148 (set! running-mobs '())
149 (set! mobs-to-add '())
150 (set! mobs-to-quit '()))))
156 (define (render-mobs)
157 (run-mobs #:render #:function (lambda () (glLoadIdentity))))