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)))
29 (define (mesh primitive)
30 (let ((x 0) (y 0) (z 0)
35 (let ((inner-properties
37 `((id . ,id) (x . ,x) (y . ,y) (z . ,z) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
38 (lambda (option . params)
42 (video:rotate ax ay az)
43 (video:translate x y z)
44 (video:rotate rx ry rz)
45 (primitive properties)))
47 (set! x (+ x (car params)))
48 (set! y (+ y (cadr params)))
49 (set! z (+ z (caddr params))))
51 (set! ax (+ ax (car params)))
52 (set! ay (+ ay (cadr params)))
53 (set! az (+ az (caddr params))))
55 (set! rx (+ rx (car params)))
56 (set! ry (+ ry (cadr params)))
57 (set! rz (+ rz (caddr params))))
61 (assoc-ref (inner-properties) (car params)))
65 (set! properties (car params)))
67 (assoc-ref properties (car params)))
69 (set! properties (assoc-set! properties (car params) (cadr params)))))))))
71 (define* (show mesh #:optional (view default-view))
72 (let ((id (mesh 'inner-property 'id)))
73 (if (not (hash-ref view id))
74 (hash-set! view id mesh))))
76 (define* (hide mesh #:optional (view default-view))
77 (hash-remove! view (mesh 'inner-property 'id)))
79 (define* (translate mesh x y #:optional (z 0))
80 (mesh 'translate x y z)
83 (define (turn mesh . params)
84 (if (>= (length params) 3)
85 (apply mesh (cons 'turn params))
86 (mesh 'turn 0 0 (car params)))
89 (define (rotate mesh . params)
90 (if (>= (length params) 3)
91 (apply mesh (cons 'rotate params))
92 (mesh 'rotate 0 0 (car params)))
98 (define-macro (primitive proc)
100 (let ((m (mesh (lambda (props) (apply ,proc ((@ (gacela utils) arguments-apply) ,proc props))))))
101 (m 'properties-set! ((@ (gacela utils) arguments-calling) ,proc params))
104 (define-macro (define-primitives . symbols)
105 (cond ((null? symbols)
108 (let ((origin (caar symbols))
109 (dest (cadar symbols)))
111 (define ,origin (primitive ,dest))
112 (define-primitives ,@(cdr symbols)))))))
115 (rectangle video:draw-rectangle)
116 (square video:draw-square))
119 (module-map (lambda (sym var)
120 (if (not (eq? sym '%module-public-interface))
121 (module-export! (current-module) (list sym))))