]> git.jsancho.org Git - gacela.git/blob - src/gacela_mobs.scm
09e296a4ccbecb9f2f5aedf45e5cc9a57578a836
[gacela.git] / src / gacela_mobs.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 (use-modules (srfi srfi-1))
19
20 ;;; Mob Factory
21
22 (define-macro (makemob name . methods)
23   `(define* (,name . args)
24      (let ((option (car args)))
25        ,(union
26          `(case option
27             (:on (mob-on ',name))
28             (:off (mob-off ',name)))
29          (define (options m)
30            (let ((option (car m)) (body (cadr m)))
31              (cond ((null? m) '())
32                    (else (cons (list option `(apply ,body (cdr args))) (options (cddr m)))))))
33          (options methods)))))
34
35
36 (define mob-on #f)
37 (define run-mobs #f)
38 (define mob-off #f)
39 (define refresh-running-mobs #f)
40 (define quit-all-mobs #f)
41
42 (let ((running-mobs '()) (mobs-to-add '()) (mobs-to-quit '()))
43   (set! mob-on
44         (lambda (mob)
45           (push mob mobs-to-add)))
46
47   (set! run-mobs
48         (lambda* (option #:key args function)
49           (define (run-mobs-rec mobs)
50             (cond ((null? mobs) #f)
51                   (else
52                    (cond (function (function)))
53                    (catch #t (lambda () (apply (car mobs) (cons option args))) (lambda (key . args) #f))
54                    (or #t (run-mobs-rec (cdr mobs))))))))
55
56   (set! mob-off
57         (lambda (mob)
58           (push mob mobs-to-quit)))
59
60   (set! refresh-running-mobs
61         (lambda ()
62           (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null? mob))
63             (push mob running-mobs)
64             (secure-block nil (mob #:init)))
65           (set! running-mobs (reverse (lset-difference eq? running-mobs mobs-to-quit)))
66           (set! mobs-to-quit '())))
67
68   (set! quit-all-mobs
69         (lambda ()
70           (set! running-mobs '())
71           (set! mobs-to-add '())
72           (set! mobs-to-quit '()))))
73
74
75 (define (logic-mobs)
76   (run-mobs #:logic))
77
78 (define (render-mobs)
79   (run-mobs #:render #:function (lambda () (glLoadIdentity))))