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))
27 (define default-view (make-hash-table))
29 (define* (draw-meshes #:optional (meshes (hash-map->list (lambda (k v) v) default-view)))
30 (cond ((not (null? meshes))
32 (lambda () (mesh-draw (car meshes)))
33 (lambda (key . args) #f))
34 (draw-meshes (cdr meshes)))))
36 (define-macro (define-view name content)
38 (hash-set! active-views ',name (lambda () (video:glmatrix-block ,content)))
45 (make-record-type "mesh"
46 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
48 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
50 (cond (((@ (gacela utils) bound?) (cdr x))
51 (format port " ~a" x))))
52 (mesh-properties record))
56 (define mesh? (record-predicate mesh-type))
58 (define* (mesh proc #:optional mesh-type)
60 (record-constructor mesh-type)
61 (let ((px 0) (py 0) (pz 0)
64 (id (gensym)) (type mesh-type)
66 (let ((inner-properties
68 `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
73 (video:rotate ax ay az)
74 (video:translate px py pz)
75 (video:rotate rx ry rz)
97 (assoc-ref (inner-properties) prop-name))
101 (lambda (new-properties)
103 (set! properties new-properties))
106 (assoc-ref properties prop-name))
107 (lambda (prop-name value)
109 (set! properties (assoc-set! properties prop-name value))))))))
111 (define (mesh-draw mesh)
112 (((record-accessor mesh-type 'draw) mesh)))
114 (define (mesh-inner-properties mesh)
115 (((record-accessor mesh-type 'inner-properties) mesh)))
117 (define (mesh-inner-property mesh prop-name)
118 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
120 (define (mesh-properties mesh)
121 (((record-accessor mesh-type 'properties) mesh)))
123 (define (mesh-properties-set! mesh new-properties)
124 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
126 (define (mesh-property mesh prop-name)
127 (((record-accessor mesh-type 'property) mesh) prop-name))
129 (define (mesh-property-set! mesh prop-name value)
130 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
132 (define* (show mesh #:optional (view default-view))
133 (let ((id (mesh-inner-property mesh 'id)))
134 (if (not (hash-ref view id))
135 (hash-set! view id mesh))))
137 (define* (hide mesh #:optional (view default-view))
138 (hash-remove! view (mesh-inner-property mesh 'id)))
140 (define* (translate mesh x y #:optional (z 0))
141 (((record-accessor mesh-type 'translate) mesh) x y z)
144 (define (turn mesh . params)
145 (apply ((record-accessor mesh-type 'turn) mesh)
146 (if (>= (length params) 3)
148 (list 0 0 (car params))))
151 (define (rotate mesh . params)
152 (apply ((record-accessor mesh-type 'rotate) mesh)
153 (if (>= (length params) 3)
155 (list 0 0 (car params))))
161 (defmacro* primitive (proc #:optional type)
163 (let ((m (mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
164 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
167 (define-macro (define-primitives . symbols)
168 (cond ((null? symbols)
171 (let ((origin (caar symbols))
172 (dest (cadar symbols)))
174 (define ,origin (primitive ,dest ',origin))
175 (define-primitives ,@(cdr symbols)))))))
178 (rectangle video:draw-rectangle)
179 (square video:draw-square))
182 (module-map (lambda (sym var)
183 (if (not (eq? sym '%module-public-interface))
184 (module-export! (current-module) (list sym))))
188 ;;; Adding extensions to the main loop
189 (add-extension! draw-meshes 50)