]> git.jsancho.org Git - gacela.git/blob - src/views.scm
Defining meshes as closures.
[gacela.git] / src / views.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 views)
19   #:use-module (gacela gacela)
20   #:use-module ((gacela video) #:renamer (symbol-prefix-proc 'video:))
21   #:use-module ((gacela gl) #:select (glPushMatrix glPopMatrix))
22   #:use-module (ice-9 optargs))
23
24 (define-macro (define-view name content)
25   `(begin
26      (hash-set! active-views ',name (lambda () (video:glmatrix-block ,content)))
27      ',name))
28
29 (define (mesh primitive)
30   (let ((x 0) (y 0) (z 0)
31         (ax 0) (ay 0) (az 0)
32         (rx 0) (ry 0) (rz 0))
33     (lambda (option . params)
34       (case option
35         ((draw)
36          (video:glatrix-block
37           (video:rotate rx ry rz)
38           (video:translate x y z)
39           (video:rotate ax ay az)
40           (primitive)))
41         ((get-properties)
42          `((x . ,x) (y . ,y) (z . ,z) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))))
43
44 (define-macro (define-mob mob-head . body)
45   (let* ((name (car mob-head))
46          (attr (cdr mob-head))
47          (make-fun-symbol (gensym))
48          (mob-fun-symbol (gensym))
49          (params-symbol (gensym)))
50     `(define (,name . ,params-symbol)
51        (define ,make-fun-symbol
52          (lambda* ,(if (null? attr) '() `(#:key ,@attr))
53            (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
54        (define ,mob-fun-symbol
55          (define-mob-function ,attr ,@body))
56        (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
57               (apply ,make-fun-symbol ,params-symbol))
58              (else
59               (apply ,mob-fun-symbol ,params-symbol))))))
60
61
62 (define-macro (define-mesh name . mesh)
63   (let* ((make-fun-symbol (gensym))
64          (mesh-fun-symbol (gensym))
65          (params-symbol (gensym)))
66     `(define ,name
67        (let ((,make-fun-symbol
68               (lambda ()))
69              (,mesh-fun-symbol
70               (lambda ())))
71          (lambda (. ,params-symbol)
72            (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
73                   (apply ,make-fun-symbol ,params-symbol))
74                  (else
75                   (apply ,mesh-fun-symbol ,params-symbol))))))))
76          
77
78 (define-macro (define-primitives . symbols)
79   (cond ((null? symbols)
80          `#t)
81         (else
82          `(begin
83             (define (,(caar symbols) . params) (mesh (lambda () (apply ,(cadar symbols) params))))
84             (define-primitives ,@(cdr symbols))))))
85
86 ; (define-macro (,(caar symbols) . params) (let ((f ',(cadar symbols))) `(mesh (lambda () (apply ,f ',params)))))
87
88 (define-primitives
89   (rectangle video:draw-rectangle)
90   (square video:draw-square))
91
92
93 (module-map (lambda (sym var)
94               (if (not (eq? sym '%module-public-interface))
95                   (module-export! (current-module) (list sym))))
96             (current-module))