]> git.jsancho.org Git - gacela.git/blob - src/views.scm
We have stable meshes with properties, translate, rotate, turn and more.
[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
25 (define default-view (make-hash-table))
26
27 (define* (draw-meshes #:optional (meshes (hash-map->list (lambda (k v) v) default-view)))
28   (cond ((not (null? meshes))
29          (catch #t
30                   (lambda () (mesh-draw (car meshes)))
31                   (lambda (key . args) #f))
32          (draw-meshes (cdr meshes)))))
33
34 (define-macro (define-view name content)
35   `(begin
36      (hash-set! active-views ',name (lambda () (video:glmatrix-block ,content)))
37      ',name))
38
39
40 ;;; Meshes
41
42 (define mesh-type
43   (make-record-type "mesh" 
44                     '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
45                     (lambda (record port)
46                       (format port "#<mesh: ~a" (mesh-inner-property record 'type))
47                       (for-each (lambda (x)
48                                   (cond (((@ (gacela utils) bound?) (cdr x))
49                                          (format port " ~a" x))))
50                                 (mesh-properties record))
51                       (display ">" port))))
52                       
53
54 (define mesh-constructor (record-constructor mesh-type))
55 (define mesh? (record-predicate mesh-type))
56
57 (define* (mesh proc #:optional mesh-type)
58   (apply
59    mesh-constructor
60    (let ((px 0) (py 0) (pz 0)
61          (ax 0) (ay 0) (az 0)
62          (rx 0) (ry 0) (rz 0)
63          (id (gensym)) (type mesh-type)
64          (properties '()))
65      (let ((inner-properties
66             (lambda ()
67               `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
68        (list
69         (lambda ()
70           "draw"
71           (video:glmatrix-block
72            (video:rotate ax ay az)
73            (video:translate px py pz)
74            (video:rotate rx ry rz)
75            (proc properties)))
76         (lambda (x y z)
77           "translate"
78           (set! px (+ px x))
79           (set! py (+ py y))
80           (set! pz (+ pz z)))
81         (lambda (x y z)
82           "turn"
83           (set! ax (+ ax x))
84           (set! ay (+ ay y))
85           (set! az (+ az z)))
86         (lambda (x y z)
87           "rotate"
88           (set! rx (+ rx x))
89           (set! ry (+ ry y))
90           (set! rz (+ rz z)))
91         (lambda ()
92           "inner-properties"
93           (inner-properties))
94         (lambda (prop-name)
95           "inner-property"
96           (assoc-ref (inner-properties) prop-name))
97         (lambda ()
98           "properties"
99           properties)
100         (lambda (new-properties)
101           "properties-set!"
102           (set! properties new-properties))
103         (lambda (prop-name)
104           "property"
105           (assoc-ref properties prop-name))
106         (lambda (prop-name value)
107           "property-set!"
108           (set! properties (assoc-set! properties prop-name value))))))))
109
110 (define (mesh-draw mesh)
111   (((record-accessor mesh-type 'draw) mesh)))
112
113 (define (mesh-inner-properties mesh)
114   (((record-accessor mesh-type 'inner-properties) mesh)))
115
116 (define (mesh-inner-property mesh prop-name)
117   (((record-accessor mesh-type 'inner-property) mesh) prop-name))
118
119 (define (mesh-properties mesh)
120   (((record-accessor mesh-type 'properties) mesh)))
121
122 (define (mesh-properties-set! mesh new-properties)
123   (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
124
125 (define (mesh-property mesh prop-name)
126   (((record-accessor mesh-type 'property) mesh) prop-name))
127
128 (define (mesh-property-set! mesh prop-name value)
129   (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
130
131 (define* (show mesh #:optional (view default-view))
132   (let ((id (mesh-inner-property mesh 'id)))
133     (if (not (hash-ref view id))
134         (hash-set! view id mesh))))
135
136 (define* (hide mesh #:optional (view default-view))
137   (hash-remove! view (mesh-inner-property mesh 'id)))
138
139 (define* (translate mesh x y #:optional (z 0))
140   (((record-accessor mesh-type 'translate) mesh) x y z)
141   mesh)
142
143 (define (turn mesh . params)
144   (apply ((record-accessor mesh-type 'turn) mesh)
145          (if (>= (length params) 3)
146              params
147              (list 0 0 (car params))))
148   mesh)
149
150 (define (rotate mesh . params)
151   (apply ((record-accessor mesh-type 'rotate) mesh)
152          (if (>= (length params) 3)
153              params
154              (list 0 0 (car params))))
155   mesh)
156
157
158 ;;; Primitives
159
160 (defmacro* primitive (proc #:optional type)
161   `(lambda (. params)
162      (let ((m (mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
163        (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
164        m)))
165
166 (define-macro (define-primitives . symbols)
167   (cond ((null? symbols)
168          `#t)
169         (else
170          (let ((origin (caar symbols))
171                (dest (cadar symbols)))
172            `(begin
173               (define ,origin (primitive ,dest ',origin))
174               (define-primitives ,@(cdr symbols)))))))
175
176 (define-primitives
177   (rectangle video:draw-rectangle)
178   (square video:draw-square))
179
180
181 (module-map (lambda (sym var)
182               (if (not (eq? sym '%module-public-interface))
183                   (module-export! (current-module) (list sym))))
184             (current-module))
185
186
187 ;;; Adding extensions to the main loop
188 (add-extension! draw-meshes 50)