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 (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))
28 (make-record-type "view"
29 '(id body meshes priority)
31 (format port "#<view: ~a meshes>"
32 (length (view-meshes record))))))
34 (define (make-view body meshes) ((record-constructor view-type) (gensym) body meshes 0))
35 (define view? (record-predicate view-type))
36 (define view-id (record-accessor view-type 'id))
37 (define view-body (record-accessor view-type 'body))
38 (define view-meshes (record-accessor view-type 'meshes))
39 (define view-priority (record-accessor view-type 'priority))
41 (define-macro (define-view meshes . body)
43 ,(cond ((null? body) `(lambda () #f))
44 (else `(lambda () ,@body)))
46 `(,(mesh-inner-property m 'id) . ,m))
49 (define activated-views '())
50 (define (sort-views views-alist)
53 (< (view-priority (cdr v1)) (view-priority (cdr v2))))))
55 (define (activate-view view)
57 (sort-views (assoc-set! activated-views (view-id view) view)))
60 (define (view-actived? view)
61 (and (assoc (view-id view) activated-views) #t))
63 (define (view-priority-set! view priority)
64 ((record-modifier view-type 'priority) view priority)
65 (set! activated-views (sort-views activated-views)))
67 (define current-view #f)
69 (define* (run-views #:optional (views activated-views))
70 (cond ((not (null? views))
71 (set! current-view (cdar views))
73 (draw-meshes (view-meshes current-view))
74 (run-views (cdr views)))))
76 (define (draw-meshes meshes)
77 (cond ((not (null? meshes))
79 (lambda () (mesh-draw (cdar meshes)))
80 (lambda (key . args) #f))
81 (draw-meshes (cdr meshes)))))
84 (define default-view (activate-view (define-view '())))
90 (make-record-type "mesh"
91 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
93 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
95 (cond (((@ (gacela utils) bound?) (cdr x))
96 (format port " ~a" x))))
97 (mesh-properties record))
101 (define mesh? (record-predicate mesh-type))
103 (define* (make-mesh proc #:optional type)
105 (record-constructor mesh-type)
106 (let ((px 0) (py 0) (pz 0)
111 (let ((inner-properties
113 `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
117 (video:glmatrix-block
118 (video:rotate ax ay az)
119 (video:translate px py pz)
120 (video:rotate rx ry rz)
142 (assoc-ref (inner-properties) prop-name))
146 (lambda (new-properties)
148 (set! properties new-properties))
151 (assoc-ref properties prop-name))
152 (lambda (prop-name value)
154 (set! properties (assoc-set! properties prop-name value))))))))
156 (define (mesh-draw mesh)
157 (((record-accessor mesh-type 'draw) mesh)))
159 (define (mesh-inner-properties mesh)
160 (((record-accessor mesh-type 'inner-properties) mesh)))
162 (define (mesh-inner-property mesh prop-name)
163 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
165 (define (mesh-properties mesh)
166 (((record-accessor mesh-type 'properties) mesh)))
168 (define (mesh-properties-set! mesh new-properties)
169 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
171 (define (mesh-property mesh prop-name)
172 (((record-accessor mesh-type 'property) mesh) prop-name))
174 (define (mesh-property-set! mesh prop-name value)
175 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
177 (define* (show mesh #:optional (view current-view))
178 (let ((id (mesh-inner-property mesh 'id))
179 (table (view-meshes view)))
180 (if (not (hash-ref table id))
181 (hash-set! table id mesh))))
183 (define* (hide mesh #:optional (view current-view))
184 (let ((table (view-meshes view)))
185 (hash-remove! table (mesh-inner-property mesh 'id))))
187 (define* (translate mesh x y #:optional (z 0))
188 (((record-accessor mesh-type 'translate) mesh) x y z)
191 (define (turn mesh . params)
192 (apply ((record-accessor mesh-type 'turn) mesh)
193 (if (>= (length params) 3)
195 (list 0 0 (car params))))
198 (define (rotate mesh . params)
199 (apply ((record-accessor mesh-type 'rotate) mesh)
200 (if (>= (length params) 3)
202 (list 0 0 (car params))))
208 (defmacro* define-primitive (proc #:optional type)
210 (let ((m (make-mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
211 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
214 (define-macro (define-primitives . symbols)
215 (cond ((null? symbols)
218 (let ((origin (caar symbols))
219 (dest (cadar symbols)))
221 (define ,origin (define-primitive ,dest ',origin))
222 (define-primitives ,@(cdr symbols)))))))
225 (rectangle video:draw-rectangle)
226 (square video:draw-square))
229 ;;; Adding extensions to the main loop
230 (add-extension! run-views 10)
233 (module-map (lambda (sym var)
234 (if (not (eq? sym '%module-public-interface))
235 (module-export! (current-module) (list sym))))