]> git.jsancho.org Git - gacela.git/blob - src/views.scm
Functions for playing with arguments when apply to procedures.
[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         (id (gensym))
34         (properties '()))
35     (let ((inner-properties
36            (lambda ()
37              `((id . ,id) (x . ,x) (y . ,y) (z . ,z) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
38       (lambda (option . params)
39         (case option
40           ((draw)
41            (video:glmatrix-block
42             (video:rotate ax ay az)
43             (video:translate x y z)
44             (video:rotate rx ry rz)
45             (primitive (assoc-ref properties 'args))))
46           ((translate)
47            (set! x (+ x (car params)))
48            (set! y (+ y (cadr params)))
49            (set! z (+ z (caddr params))))
50           ((turn)
51            (set! ax (+ ax (car params)))
52            (set! ay (+ ay (cadr params)))
53            (set! az (+ az (caddr params))))
54           ((rotate)
55            (set! rx (+ rx (car params)))
56            (set! ry (+ ry (cadr params)))
57            (set! rz (+ rz (caddr params))))
58           ((inner-properties)
59            (inner-properties))
60           ((inner-property)
61            (assoc-ref (inner-properties) (car params)))
62           ((properties)
63            properties)
64           ((property)
65            (assoc-ref properties (car params)))
66           ((property-set!)
67            (set! properties (assoc-set! properties (car params) (cadr params)))))))))
68
69 (define* (show mesh #:optional (view default-view))
70   (let ((id (mesh 'inner-property 'id)))
71     (if (not (hash-ref view id))
72         (hash-set! view id mesh))))
73
74 (define* (hide mesh #:optional (view default-view))
75   (hash-remove! view (mesh 'inner-property 'id)))
76
77 (define* (translate mesh x y #:optional (z 0))
78   (mesh 'translate x y z)
79   mesh)
80
81 (define (turn mesh . params)
82   (if (>= (length params) 3)
83       (apply mesh (cons 'turn params))
84       (mesh 'turn 0 0 (car params)))
85   mesh)
86
87 (define (rotate mesh . params)
88   (if (>= (length params) 3)
89       (apply mesh (cons 'rotate params))
90       (mesh 'rotate 0 0 (car params)))
91   mesh)
92
93
94 ;;; Primitives
95
96 (define-macro (primitive proc)
97   `(lambda (. params)
98      (let ((m (mesh (lambda (args) (apply ,proc args)))))
99        (m 'property-set! 'args params)
100        m)))
101
102 (define-macro (define-primitives . symbols)
103   (cond ((null? symbols)
104          `#t)
105         (else
106          (let ((origin (caar symbols))
107                (dest (cadar symbols)))
108            `(begin
109               ,(if (and (list? origin) (list? dest))
110                    `(define* ,origin #f)
111                    `(define (,origin . params) (mesh (lambda () (apply ,dest params)))))
112               (define-primitives ,@(cdr symbols)))))))
113
114 (define-primitives
115   (rectangle video:draw-rectangle)
116   (square video:draw-square))
117
118
119 (module-map (lambda (sym var)
120               (if (not (eq? sym '%module-public-interface))
121                   (module-export! (current-module) (list sym))))
122             (current-module))