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))
25 (define default-view (make-hash-table))
27 (define* (draw-meshes #:optional (meshes (hash-map->list (lambda (k v) v) default-view)))
28 (cond ((not (null? meshes))
30 (lambda () (mesh-draw (car meshes)))
31 (lambda (key . args) #f))
32 (draw-meshes (cdr meshes)))))
34 (define-macro (define-view name content)
36 (hash-set! active-views ',name (lambda () (video:glmatrix-block ,content)))
43 (make-record-type "mesh"
44 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)
46 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
48 (cond (((@ (gacela utils) bound?) (cdr x))
49 (format port " ~a" x))))
50 (mesh-properties record))
54 (define mesh-constructor (record-constructor mesh-type))
55 (define mesh? (record-predicate mesh-type))
57 (define* (mesh proc #:optional mesh-type)
60 (let ((px 0) (py 0) (pz 0)
63 (id (gensym)) (type mesh-type)
65 (let ((inner-properties
67 `((id . ,id) (type . ,type) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
72 (video:rotate ax ay az)
73 (video:translate px py pz)
74 (video:rotate rx ry rz)
96 (assoc-ref (inner-properties) prop-name))
100 (lambda (new-properties)
102 (set! properties new-properties))
105 (assoc-ref properties prop-name))
106 (lambda (prop-name value)
108 (set! properties (assoc-set! properties prop-name value))))))))
110 (define (mesh-draw mesh)
111 (((record-accessor mesh-type 'draw) mesh)))
113 (define (mesh-inner-properties mesh)
114 (((record-accessor mesh-type 'inner-properties) mesh)))
116 (define (mesh-inner-property mesh prop-name)
117 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
119 (define (mesh-properties mesh)
120 (((record-accessor mesh-type 'properties) mesh)))
122 (define (mesh-properties-set! mesh new-properties)
123 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
125 (define (mesh-property mesh prop-name)
126 (((record-accessor mesh-type 'property) mesh) prop-name))
128 (define (mesh-property-set! mesh prop-name value)
129 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
131 (define* (show mesh #:optional (view default-view))
132 (let ((id (mesh-inner-property mesh 'id)))
133 (if (not (hash-ref view id))
134 (hash-set! view id mesh))))
136 (define* (hide mesh #:optional (view default-view))
137 (hash-remove! view (mesh-inner-property mesh 'id)))
139 (define* (translate mesh x y #:optional (z 0))
140 (((record-accessor mesh-type 'translate) mesh) x y z)
143 (define (turn mesh . params)
144 (apply ((record-accessor mesh-type 'turn) mesh)
145 (if (>= (length params) 3)
147 (list 0 0 (car params))))
150 (define (rotate mesh . params)
151 (apply ((record-accessor mesh-type 'rotate) mesh)
152 (if (>= (length params) 3)
154 (list 0 0 (car params))))
160 (defmacro* primitive (proc #:optional type)
162 (let ((m (mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))) ,type)))
163 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
166 (define-macro (define-primitives . symbols)
167 (cond ((null? symbols)
170 (let ((origin (caar symbols))
171 (dest (cadar symbols)))
173 (define ,origin (primitive ,dest ',origin))
174 (define-primitives ,@(cdr symbols)))))))
177 (rectangle video:draw-rectangle)
178 (square video:draw-square))
181 (module-map (lambda (sym var)
182 (if (not (eq? sym '%module-public-interface))
183 (module-export! (current-module) (list sym))))
187 ;;; Adding extensions to the main loop
188 (add-extension! draw-meshes 50)