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))
24 (define-macro (define-view name content)
26 (hash-set! active-views ',name (lambda () (video:glmatrix-block ,content)))
30 (make-record-type "mesh"
31 '(draw translate turn rotate inner-properties inner-property properties properties-set! property property-set!)))
33 (define mesh-constructor (record-constructor mesh-type))
34 (define mesh? (record-predicate mesh-type))
39 (let ((px 0) (py 0) (pz 0)
44 (let ((inner-properties
46 `((id . ,id) (x . ,px) (y . ,py) (z . ,pz) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
51 (video:rotate ax ay az)
52 (video:translate px py pz)
53 (video:rotate rx ry rz)
75 (assoc-ref (inner-properties) prop-name))
79 (lambda (new-properties)
81 (set! properties new-properties))
84 (assoc-ref properties prop-name))
85 (lambda (prop-name value)
87 (set! properties (assoc-set! properties prop-name value))))))))
89 (define mesh-properties-set!
90 (let ((f (record-accessor mesh-type 'properties-set!)))
91 (lambda (mesh new-properties)
92 ((f mesh) new-properties))))
94 (define* (show mesh #:optional (view default-view))
95 (let ((id (mesh 'inner-property 'id)))
96 (if (not (hash-ref view id))
97 (hash-set! view id mesh))))
99 (define* (hide mesh #:optional (view default-view))
100 (hash-remove! view (mesh 'inner-property 'id)))
102 (define* (translate mesh x y #:optional (z 0))
103 (mesh 'translate x y z)
106 (define (turn mesh . params)
107 (if (>= (length params) 3)
108 (apply mesh (cons 'turn params))
109 (mesh 'turn 0 0 (car params)))
112 (define (rotate mesh . params)
113 (if (>= (length params) 3)
114 (apply mesh (cons 'rotate params))
115 (mesh 'rotate 0 0 (car params)))
121 (define-macro (primitive proc)
123 (let ((m (mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))))))
124 (mesh-properties-set! m ((@ (gacela utils) arguments-calling) ,proc params))
127 (define-macro (define-primitives . symbols)
128 (cond ((null? symbols)
131 (let ((origin (caar symbols))
132 (dest (cadar symbols)))
134 (define ,origin (primitive ,dest))
135 (define-primitives ,@(cdr symbols)))))))
138 (rectangle video:draw-rectangle)
139 (square video:draw-square))
142 (module-map (lambda (sym var)
143 (if (not (eq? sym '%module-public-interface))
144 (module-export! (current-module) (list sym))))