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-macro (with-color color . code)
20 `(let ((original-color (get-current-color))
22 (apply set-current-color ,color)
23 (set! result (begin ,@code))
24 (apply set-current-color original-color)
26 (else `(begin ,@code))))
28 (define-macro (progn-textures . code)
31 (glEnable GL_TEXTURE_2D)
32 (set! values (multiple-value-list (begin ,@code)))
33 (glDisable GL_TEXTURE_2D)
34 (apply values values)))
36 (define (draw . vertexes)
37 (begin-draw (length vertexes))
38 (draw-vertexes vertexes)
41 (define (begin-draw number-of-points)
42 (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
43 ((= number-of-points 4) (glBegin GL_QUADS))))
45 (define (draw-vertexes vertexes)
46 (cond ((not (null? vertexes))
47 (draw-vertex (car vertexes))
48 (draw-vertexes (cdr vertexes)))))
50 (define* (draw-vertex vertex #:key texture-coord)
51 (cond ((list? (car vertex))
52 (with-color (car vertex)
53 (apply simple-draw-vertex (cadr vertex))))
55 (cond (texture-coord (apply glTexCoord2f texture-coord)))
56 (apply simple-draw-vertex vertex))))
58 (define* (simple-draw-vertex x y #:optional (z 0))
59 (cond ((3d-mode?) (glVertex3f x y z))
60 (else (glVertex2f x y))))
62 (define (load-image-for-texture filename)
64 (let ((image (IMG_Load filename)))
65 (cond ((not (= image 0))
66 (let* ((width (surface-w image)) (height (surface-h image))
67 (power-2 (nearest-power-of-two (min width height)))
69 (cond ((and (= width power-2) (= height power-2)) (values image width height))
70 (t (setq resized-image (resize-surface image power-2 power-2))
71 (SDL_FreeSurface image)
72 (cond ((/= resized-image 0) (values resized-image width height))))))))))
74 (defun resize-surface (surface width height)
75 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
76 (cond ((and (= width old-width) (= height old-height)) surface)
77 (t (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
78 (zoomSurface surface zoomx zoomy 0))))))
80 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
81 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
82 (cond ((get-resource key) key)
83 (t (true-load-texture filename min-filter mag-filter static)))))
85 (defun true-load-texture (filename min-filter mag-filter static)
86 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
89 (image real-w real-h) (load-image-for-texture filename)
91 (let ((width (surface-w image)) (height (surface-h image))
92 (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
93 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
94 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
95 (texture (car (glGenTextures 1))))
97 (glBindTexture GL_TEXTURE_2D texture)
98 (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
99 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
100 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
101 (SDL_FreeSurface image)
103 `(:id-texture ,texture :width ,real-w :height ,real-h)
104 (lambda () (true-load-texture filename min-filter mag-filter static))
105 (lambda () (glDeleteTextures 1 `(,texture)))
109 (defun draw-image (filename &ptional (zoom 1))
110 (let ((texture (load-texture filename)))
111 (cond (texture (draw-texture texture zoom)))))
113 (defun draw-texture (texture &optional (zoom 1))
115 (let ((width (getf (get-resource texture) :width))
116 (height (getf (get-resource texture) :height)))
117 (draw-rectangle (* zoom width) (* zoom height) :texture texture)))))
119 (defun draw-quad (v1 v2 v3 v4 &key texture)
120 (let ((id-texture (getf (get-resource texture) :id-texture)))
123 (glBindTexture GL_TEXTURE_2D id-texture)
125 (draw-vertex v1 :texture-coord '(0 0))
126 (draw-vertex v2 :texture-coord '(1 0))
127 (draw-vertex v3 :texture-coord '(1 1))
128 (draw-vertex v4 :texture-coord '(0 1))
130 ((consp texture) (with-color texture (draw v1 v2 v3 v4)))
131 (t (draw v1 v2 v3 v4)))))
133 (defun draw-rectangle (width height &key texture)
134 (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
135 (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
137 (defun draw-square (&key (size 1) texture)
138 (draw-rectangle size size :texture texture))
140 (defun draw-cube (&key (size 1) texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6)
141 (let ((-size (neg size)))
144 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture (or texture-1 texture))
146 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture (or texture-2 texture))
148 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture (or texture-3 texture))
150 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture))
152 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture))
154 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture)))))
156 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
158 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
159 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
160 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
161 (and turn-on (glEnable id))
164 (defun translate (x y &optional (z 0))
165 (glTranslatef x y z))
167 (defun rotate (&rest rot)
168 (cond ((3d-mode?) (apply #'3d-rotate rot))
169 (t (apply #'2d-rotate rot))))
171 (defun 3d-rotate (xrot yrot zrot)
172 (glRotatef xrot 1 0 0)
173 (glRotatef yrot 0 1 0)
174 (glRotatef zrot 0 0 1))
176 (defun 2d-rotate (rot)
177 (glRotatef rot 0 0 1))
181 (cond ((3d-mode?) (camera-look))))
183 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
184 (defun set-camera (&key eye center up)
185 (cond (eye (setq camera-eye eye)))
186 (cond (center (setq camera-center center)))
187 (cond (up (setq camera-up up))))
189 (defun camera-look ()
190 (apply #'gluLookAt (concatenate 'list camera-eye camera-center camera-up))))