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)
36 `((id . ,id) (x . ,x) (y . ,y) (z . ,z) (ax . ,ax) (ay . ,ay) (az . ,az) (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
37 (lambda (option . params)
41 (video:rotate ax ay az)
42 (video:translate x y z)
43 (video:rotate rx ry rz)
46 (set! x (+ x (car params)))
47 (set! y (+ y (cadr params)))
48 (set! z (+ z (caddr params))))
50 (set! ax (+ ax (car params)))
51 (set! ay (+ ay (cadr params)))
52 (set! az (+ az (caddr params))))
54 (set! rx (+ rx (car params)))
55 (set! ry (+ ry (cadr params)))
56 (set! rz (+ rz (caddr params))))
60 (assoc-ref (get-properties) (car params))))))))
62 (define* (show mesh #:optional (view default-view))
63 (let ((id (mesh 'get-property 'id)))
64 (if (not (hash-ref view id))
65 (hash-set! view id mesh))))
67 (define* (hide mesh #:optional (view default-view))
68 (hash-remove! view (mesh 'get-property 'id)))
70 (define* (translate mesh x y #:optional (z 0))
71 (mesh 'translate x y z)
74 (define (turn mesh . params)
75 (if (>= (length params) 3)
76 (apply mesh (cons 'turn params))
77 (mesh 'turn 0 0 (car params)))
80 (define (rotate mesh . params)
81 (if (>= (length params) 3)
82 (apply mesh (cons 'rotate params))
83 (mesh 'rotate 0 0 (car params)))
86 (define-macro (define-primitives . symbols)
87 (cond ((null? symbols)
91 (define (,(caar symbols) . params) (mesh (lambda () (apply ,(cadar symbols) params))))
92 (define-primitives ,@(cdr symbols))))))
95 (rectangle video:draw-rectangle)
96 (square video:draw-square))
99 (module-map (lambda (sym var)
100 (if (not (eq? sym '%module-public-interface))
101 (module-export! (current-module) (list sym))))