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 (make-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-meshes (record-accessor view-type 'meshes))
40 (define view-priority (record-accessor view-type 'priority))
42 (define-macro (define-view body)
43 `(make-view (lambda () ,body)))
45 (define activated-views '())
46 (define (sort-views views-alist)
49 (< (view-priority (cdr v1)) (view-priority (cdr v2))))))
51 (define (activate-view view)
53 (sort-views (assoc-set! activated-views (view-id view) view)))
56 (define (view-actived? view)
57 (and (assoc (view-id view) activated-views) #t))
59 (define (view-priority-set! view priority)
60 ((record-modifier view-type 'priority) view priority)
61 (set! activated-views (sort-views activated-views)))
63 (define current-view #f)
65 (define* (run-views #:optional (views activated-views))
66 (cond ((not (null? views))
67 (set! current-view (cdar views))
69 (draw-meshes (view-meshes current-view))
70 (run-views (cdr views)))))
72 (define (draw-meshes meshes)
73 (cond ((not (null? meshes))
75 (lambda () (mesh-draw (cdar meshes)))
76 (lambda (key . args) #f))
77 (draw-meshes (cdr meshes)))))
80 (define default-view (activate-view (define-view '())))
86 (make-record-type "mesh"
87 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
89 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
91 (cond (((@ (gacela utils) bound?) (cdr x))
92 (format port " ~a" x))))
93 (mesh-properties record))
97 (define mesh? (record-predicate mesh-type))
99 (define* (make-mesh proc #:optional type)
101 (record-constructor mesh-type)
102 (let ((px 0) (py 0) (pz 0)
107 (let ((inner-properties
109 `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
113 (video:glmatrix-block
114 (video:rotate ax ay az)
115 (video:translate px py pz)
116 (video:rotate rx ry rz)
138 (assoc-ref (inner-properties) prop-name))
142 (lambda (new-properties)
144 (set! properties new-properties))
147 (assoc-ref properties prop-name))
148 (lambda (prop-name value)
150 (set! properties (assoc-set! properties prop-name value))))))))
152 (define (mesh-draw mesh)
153 (((record-accessor mesh-type 'draw) mesh)))
155 (define (mesh-inner-properties mesh)
156 (((record-accessor mesh-type 'inner-properties) mesh)))
158 (define (mesh-inner-property mesh prop-name)
159 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
161 (define (mesh-properties mesh)
162 (((record-accessor mesh-type 'properties) mesh)))
164 (define (mesh-properties-set! mesh new-properties)
165 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
167 (define (mesh-property mesh prop-name)
168 (((record-accessor mesh-type 'property) mesh) prop-name))
170 (define (mesh-property-set! mesh prop-name value)
171 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
173 (define* (show mesh #:optional (view current-view))
174 (let ((id (mesh-inner-property mesh 'id))
175 (table (view-meshes view)))
176 (if (not (hash-ref table id))
177 (hash-set! table id mesh))))
179 (define* (hide mesh #:optional (view current-view))
180 (let ((table (view-meshes view)))
181 (hash-remove! table (mesh-inner-property mesh 'id))))
183 (define* (translate mesh x y #:optional (z 0))
184 (((record-accessor mesh-type 'translate) mesh) x y z)
187 (define (turn mesh . params)
188 (apply ((record-accessor mesh-type 'turn) mesh)
189 (if (>= (length params) 3)
191 (list 0 0 (car params))))
194 (define (rotate mesh . params)
195 (apply ((record-accessor mesh-type 'rotate) mesh)
196 (if (>= (length params) 3)
198 (list 0 0 (car params))))
204 (defmacro* define-primitive (proc #:optional type)
206 (let ((m (make-mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
207 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
210 (define-macro (define-primitives . symbols)
211 (cond ((null? symbols)
214 (let ((origin (caar symbols))
215 (dest (cadar symbols)))
217 (define ,origin (define-primitive ,dest ',origin))
218 (define-primitives ,@(cdr symbols)))))))
221 (rectangle video:draw-rectangle)
222 (square video:draw-square))
225 ;;; Adding extensions to the main loop
226 (add-extension! run-views 10)
229 (module-map (lambda (sym var)
230 (if (not (eq? sym '%module-public-interface))
231 (module-export! (current-module) (list sym))))