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)
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 (assoc-ref properties (car params)))
67 (set! properties (assoc-set! properties (car params) (cadr params)))))))))
69 (define* (show mesh #:optional (view default-view))
70 (let ((id (mesh 'inner-property 'id)))
71 (if (not (hash-ref view id))
72 (hash-set! view id mesh))))
74 (define* (hide mesh #:optional (view default-view))
75 (hash-remove! view (mesh 'inner-property 'id)))
77 (define* (translate mesh x y #:optional (z 0))
78 (mesh 'translate x y z)
81 (define (turn mesh . params)
82 (if (>= (length params) 3)
83 (apply mesh (cons 'turn params))
84 (mesh 'turn 0 0 (car params)))
87 (define (rotate mesh . params)
88 (if (>= (length params) 3)
89 (apply mesh (cons 'rotate params))
90 (mesh 'rotate 0 0 (car params)))
93 (define-macro (define-primitives . symbols)
94 (cond ((null? symbols)
97 (let ((origin (caar symbols))
98 (dest (cadar symbols)))
100 ,(if (and (list? origin) (list? dest))
101 `(define* ,origin #f)
102 `(define (,origin . params) (mesh (lambda () (apply ,dest params)))))
103 (define-primitives ,@(cdr symbols)))))))
106 (rectangle video:draw-rectangle)
107 (square video:draw-square))
110 (module-map (lambda (sym var)
111 (if (not (eq? sym '%module-public-interface))
112 (module-export! (current-module) (list sym))))