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 controllers meshes priority)
31 (format port "#<view: ~a controllers / ~a meshes>"
32 (length (hash-map->list (lambda x x) (view-controllers record)))
33 (length (hash-map->list (lambda x x) (view-meshes record)))))))
35 (define* (make-view #:optional (priority 0)) ((record-constructor view-type) (gensym) (make-hash-table) (make-hash-table) priority))
36 (define view? (record-predicate view-type))
37 (define view-id (record-accessor view-type 'id))
38 (define view-controllers (record-accessor view-type 'controllers))
39 (define view-meshes (record-accessor view-type 'meshes))
40 (define view-priority (record-accessor view-type 'priority))
42 (define activated-views '())
43 (define (sort-views views-alist)
46 (< (view-priority (cdr v1)) (view-priority (cdr v2))))))
48 (define (activate-view view)
50 (sort-views (assoc-set! activated-views (view-id view) view)))
53 (define (view-actived? view)
54 (and (assoc (view-id view) activated-views) #t))
56 (define (view-priority-set! view priority)
57 ((record-modifier view-type 'priority) view priority)
58 (set! activated-views (sort-views activated-views)))
60 (define* (run-views #:optional (views activated-views))
61 (cond ((not (null? views))
63 (draw-meshes (hash-map->list (lambda (k v) v) (view-meshes (cdar views))))
64 (run-views (cdr views)))))
66 (define (draw-meshes meshes)
67 (cond ((not (null? meshes))
69 (lambda () (mesh-draw (car meshes)))
70 (lambda (key . args) #f))
71 (draw-meshes (cdr meshes)))))
74 (define default-view (activate-view (make-view)))
80 (make-record-type "mesh"
81 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
83 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
85 (cond (((@ (gacela utils) bound?) (cdr x))
86 (format port " ~a" x))))
87 (mesh-properties record))
91 (define mesh? (record-predicate mesh-type))
93 (define* (make-mesh proc #:optional type)
95 (record-constructor mesh-type)
96 (let ((px 0) (py 0) (pz 0)
101 (let ((inner-properties
103 `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
107 (video:glmatrix-block
108 (video:rotate ax ay az)
109 (video:translate px py pz)
110 (video:rotate rx ry rz)
132 (assoc-ref (inner-properties) prop-name))
136 (lambda (new-properties)
138 (set! properties new-properties))
141 (assoc-ref properties prop-name))
142 (lambda (prop-name value)
144 (set! properties (assoc-set! properties prop-name value))))))))
146 (define (mesh-draw mesh)
147 (((record-accessor mesh-type 'draw) mesh)))
149 (define (mesh-inner-properties mesh)
150 (((record-accessor mesh-type 'inner-properties) mesh)))
152 (define (mesh-inner-property mesh prop-name)
153 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
155 (define (mesh-properties mesh)
156 (((record-accessor mesh-type 'properties) mesh)))
158 (define (mesh-properties-set! mesh new-properties)
159 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
161 (define (mesh-property mesh prop-name)
162 (((record-accessor mesh-type 'property) mesh) prop-name))
164 (define (mesh-property-set! mesh prop-name value)
165 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
167 (define* (show mesh #:optional (view default-view))
168 (let ((id (mesh-inner-property mesh 'id))
169 (table (view-meshes view)))
170 (if (not (hash-ref table id))
171 (hash-set! table id mesh))))
173 (define* (hide mesh #:optional (view default-view))
174 (let ((table (view-meshes view)))
175 (hash-remove! table (mesh-inner-property mesh 'id))))
177 (define* (translate mesh x y #:optional (z 0))
178 (((record-accessor mesh-type 'translate) mesh) x y z)
181 (define (turn mesh . params)
182 (apply ((record-accessor mesh-type 'turn) mesh)
183 (if (>= (length params) 3)
185 (list 0 0 (car params))))
188 (define (rotate mesh . params)
189 (apply ((record-accessor mesh-type 'rotate) mesh)
190 (if (>= (length params) 3)
192 (list 0 0 (car params))))
198 (defmacro* define-primitive (proc #:optional type)
200 (let ((m (make-mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
201 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
204 (define-macro (define-primitives . symbols)
205 (cond ((null? symbols)
208 (let ((origin (caar symbols))
209 (dest (cadar symbols)))
211 (define ,origin (define-primitive ,dest ',origin))
212 (define-primitives ,@(cdr symbols)))))))
215 (rectangle video:draw-rectangle)
216 (square video:draw-square))
219 (module-map (lambda (sym var)
220 (if (not (eq? sym '%module-public-interface))
221 (module-export! (current-module) (list sym))))
225 ;;; Adding extensions to the main loop
226 (add-extension! run-views 10)