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 constructor body meshes priority)
31 (format port "#<view: ~a meshes>"
32 (length (view-meshes record))))))
34 (define (build-view constructor) ((record-constructor view-type) (gensym) constructor #f '() 0))
35 (define view? (record-predicate view-type))
36 (define view-id (record-accessor view-type 'id))
37 (define view-constructor (record-accessor view-type 'constructor))
38 (define view-body (record-accessor view-type 'body))
39 (define view-body-set! (record-modifier view-type 'body))
40 (define view-meshes (record-accessor view-type 'meshes))
41 (define view-meshes-set! (record-modifier view-type 'meshes))
42 (define view-priority (record-accessor view-type 'priority))
44 (define-macro (make-view body)
45 `(build-view (lambda () ,body)))
47 (define activated-views '())
49 (define (sort-views views-alist)
52 (< (view-priority (cdr v1)) (view-priority (cdr v2))))))
54 (define (reset-view! view)
55 (view-body-set! view ((view-constructor view)))
56 (view-meshes-set! view '())
57 (view-priority-set! view 0))
59 (define (activate-view view)
62 (sort-views (assoc-set! activated-views (view-id view) view)))
65 (define (view-actived? view)
66 (and (assoc (view-id view) activated-views) #t))
68 (define (view-priority-set! view priority)
69 ((record-modifier view-type 'priority) view priority)
70 (if (view-actived? view)
71 (set! activated-views (sort-views activated-views))))
73 (define current-view #f)
75 (define* (run-views #:optional (views activated-views))
76 (cond ((not (null? views))
77 (set! current-view (cdar views))
78 ((view-body current-view))
79 (draw-meshes (view-meshes current-view))
80 (run-views (cdr views)))))
82 (define (draw-meshes meshes)
83 (cond ((not (null? meshes))
85 (lambda () (mesh-draw (cdar meshes)))
86 (lambda (key . args) #f))
87 (draw-meshes (cdr meshes)))))
90 (define default-view (activate-view (make-view (lambda () #f))))
96 (make-record-type "mesh"
97 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
99 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
100 (for-each (lambda (x)
101 (cond (((@ (gacela utils) bound?) (cdr x))
102 (format port " ~a" x))))
103 (mesh-properties record))
104 (display ">" port))))
107 (define mesh? (record-predicate mesh-type))
109 (define* (make-mesh proc #:optional type)
111 (record-constructor mesh-type)
112 (let ((px 0) (py 0) (pz 0)
117 (let ((inner-properties
119 `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
123 (video:glmatrix-block
124 (video:rotate ax ay az)
125 (video:translate px py pz)
126 (video:rotate rx ry rz)
148 (assoc-ref (inner-properties) prop-name))
152 (lambda (new-properties)
154 (set! properties new-properties))
157 (assoc-ref properties prop-name))
158 (lambda (prop-name value)
160 (set! properties (assoc-set! properties prop-name value))))))))
162 (define (mesh-draw mesh)
163 (((record-accessor mesh-type 'draw) mesh)))
165 (define (mesh-inner-properties mesh)
166 (((record-accessor mesh-type 'inner-properties) mesh)))
168 (define (mesh-inner-property mesh prop-name)
169 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
171 (define (mesh-properties mesh)
172 (((record-accessor mesh-type 'properties) mesh)))
174 (define (mesh-properties-set! mesh new-properties)
175 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
177 (define (mesh-property mesh prop-name)
178 (((record-accessor mesh-type 'property) mesh) prop-name))
180 (define (mesh-property-set! mesh prop-name value)
181 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
183 (define* (show mesh #:optional (view current-view))
184 (let ((id (mesh-inner-property mesh 'id))
185 (table (view-meshes view)))
186 (if (not (assoc-ref table id))
187 (view-meshes-set! view (assoc-set! table id mesh))))
190 (define* (hide mesh #:optional (view current-view))
191 (let ((id (mesh-inner-property mesh 'id))
192 (table (view-meshes view)))
193 (if (assoc-ref table id)
194 (view-meshes-set! view (assoc-remove! table id))))
197 (define* (translate mesh x y #:optional (z 0))
198 (((record-accessor mesh-type 'translate) mesh) x y z)
201 (define (turn mesh . params)
202 (apply ((record-accessor mesh-type 'turn) mesh)
203 (if (>= (length params) 3)
205 (list 0 0 (car params))))
208 (define (rotate mesh . params)
209 (apply ((record-accessor mesh-type 'rotate) mesh)
210 (if (>= (length params) 3)
212 (list 0 0 (car params))))
218 (defmacro* define-primitive (proc #:optional type)
220 (let ((m (make-mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
221 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
224 (define-macro (define-primitives . symbols)
225 (cond ((null? symbols)
228 (let ((origin (caar symbols))
229 (dest (cadar symbols)))
231 (define ,origin (define-primitive ,dest ',origin))
232 (define-primitives ,@(cdr symbols)))))))
235 (rectangle video:draw-rectangle)
236 (square video:draw-square))
239 ;;; Adding extensions to the main loop
240 (add-extension! run-views 10)
243 (module-map (lambda (sym var)
244 (if (not (eq? sym '%module-public-interface))
245 (module-export! (current-module) (list sym))))