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 (add-extension! draw-meshes 50)
36 (define-macro (define-view name content)
38 (hash-set! active-views ',name (lambda () (video:glmatrix-block ,content)))
42 (make-record-type "mesh"
43 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)))
45 (define mesh-constructor (record-constructor mesh-type))
46 (define mesh? (record-predicate mesh-type))
51 (let ((px 0) (py 0) (pz 0)
56 (let ((inner-properties
58 `((id . ,id) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
63 (video:rotate ax ay az)
64 (video:translate px py pz)
65 (video:rotate rx ry rz)
87 (assoc-ref (inner-properties) prop-name))
91 (lambda (new-properties)
93 (set! properties new-properties))
96 (assoc-ref properties prop-name))
97 (lambda (prop-name value)
99 (set! properties (assoc-set! properties prop-name value))))))))
101 (define (mesh-draw mesh)
102 (((record-accessor mesh-type 'draw) mesh)))
104 (define (mesh-inner-property mesh prop-name)
105 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
107 (define (mesh-properties mesh)
108 (((record-accessor mesh-type 'properties) mesh)))
110 (define (mesh-properties-set! mesh new-properties)
111 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
113 (define* (show mesh #:optional (view default-view))
114 (let ((id (mesh-inner-property mesh 'id)))
115 (if (not (hash-ref view id))
116 (hash-set! view id mesh))))
118 (define* (hide mesh #:optional (view default-view))
119 (hash-remove! view (mesh 'inner-property 'id)))
121 (define* (translate mesh x y #:optional (z 0))
122 (mesh 'translate x y z)
125 (define (turn mesh . params)
126 (if (>= (length params) 3)
127 (apply mesh (cons 'turn params))
128 (mesh 'turn 0 0 (car params)))
131 (define (rotate mesh . params)
132 (if (>= (length params) 3)
133 (apply mesh (cons 'rotate params))
134 (mesh 'rotate 0 0 (car params)))
140 (define-macro (primitive proc)
142 (let ((m (mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))))))
143 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
146 (define-macro (define-primitives . symbols)
147 (cond ((null? symbols)
150 (let ((origin (caar symbols))
151 (dest (cadar symbols)))
153 (define ,origin (primitive ,dest))
154 (define-primitives ,@(cdr symbols)))))))
157 (rectangle video:draw-rectangle)
158 (square video:draw-square))
161 (module-map (lambda (sym var)
162 (if (not (eq? sym '%module-public-interface))
163 (module-export! (current-module) (list sym))))