1 ;;; Gacela, a GNU Common Lisp 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/>.
19 (defmacro with-color (color &body code)
21 `(let ((original-color (get-current-color)))
22 (apply #'set-current-color ,color)
24 (apply #'set-current-color original-color)))
29 (defmacro progn-textures (&body code)
32 (glEnable GL_TEXTURE_2D)
33 (setq values (multiple-value-list (progn ,@code)))
34 (glDisable GL_TEXTURE_2D)
35 (apply #'values values)))
37 (defun draw (&rest vertexes)
38 (begin-draw (length vertexes))
39 (draw-vertexes vertexes)
42 (defun begin-draw (number-of-points)
43 (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
44 ((= number-of-points 4) (glBegin GL_QUADS))))
46 (defun draw-vertexes (vertexes)
47 (cond ((null vertexes) nil)
48 (t (draw-vertex (car vertexes))
49 (draw-vertexes (cdr vertexes)))))
51 (defun draw-vertex (vertex &key texture-coord)
52 (cond ((consp (car vertex))
53 (with-color (car vertex)
54 (apply #'simple-draw-vertex (cadr vertex))))
55 (t (cond (texture-coord (apply #'glTexCoord2f texture-coord)))
56 (apply #'simple-draw-vertex vertex))))
58 (defun simple-draw-vertex (x y &optional (z 0))
59 (cond ((3d-mode?) (glVertex3f x y z))
60 (t (glVertex2f x y))))
62 (defun load-image-for-texture (filename)
64 (let ((image (IMG_Load filename)))
66 (let* ((width (surface-w image)) (height (surface-h image))
67 (power-2 (nearest-power-of-two (min width height)))
68 (zoomx (/ power-2 width)) (zoomy (/ power-2 height))
70 (cond ((and (= zoomx 1) (= zoomy 1)) (values image width height))
71 (t (setq zoomed-image (zoomSurface image zoomx zoomy 0))
72 (SDL_FreeSurface image)
73 (cond ((/= zoomed-image 0) (values zoomed-image width height))))))))))
75 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
76 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
77 (cond ((get-resource key) key)
78 (t (true-load-texture filename min-filter mag-filter static)))))
80 (defun true-load-texture (filename min-filter mag-filter static)
81 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
84 (image real-w real-h) (load-image-for-texture filename)
86 (let ((width (surface-w image)) (height (surface-h image))
87 (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
88 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)
89 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)))
90 (texture (car (glGenTextures 1))))
91 (glBindTexture GL_TEXTURE_2D texture)
92 (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
93 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
94 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
95 (SDL_FreeSurface image)
97 `(:id-texture ,texture :width ,real-w :height ,real-h)
98 (lambda () (true-load-texture filename min-filter mag-filter static))
99 (lambda () (glDeleteTextures 1 `(,texture)))
103 (defun draw-image-function (filename)
104 (let ((texture (load-texture filename)))
105 (lambda (&optional (f 1))
107 (let ((width (getf (get-resource texture) :width))
108 (height (getf (get-resource texture) :height)))
109 (draw-rectangle (* f width) (* f height) :texture texture)))))))
111 (defun draw-quad (v1 v2 v3 v4 &key texture)
112 (cond ((consp texture) (with-color texture (draw v1 v2 v3 v4)))
115 (glBindTexture GL_TEXTURE_2D (getf (get-resource texture) :id-texture))
117 (draw-vertex v1 :texture-coord '(0 0))
118 (draw-vertex v2 :texture-coord '(1 0))
119 (draw-vertex v3 :texture-coord '(1 1))
120 (draw-vertex v4 :texture-coord '(0 1))
122 (t (draw v1 v2 v3 v4))))
124 (defun draw-rectangle (width height &key texture)
125 (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
126 (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
128 (defun draw-square (&key (size 1) texture)
129 (draw-rectangle size size :texture texture))
131 (defun draw-cube (&key (size 1) texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6)
132 (let ((-size (neg size)))
135 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture (or texture-1 texture))
137 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture (or texture-2 texture))
139 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture (or texture-3 texture))
141 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture))
143 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture))
145 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture)))))
147 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
149 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
150 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
151 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
152 (and turn-on (glEnable id))
155 (defun translate (x y &optional (z 0))
156 (glTranslatef x y z))
158 (defun rotate (&rest rot)
159 (cond ((3d-mode?) (apply #'3d-rotate rot))
160 (t (apply #'2d-rotate rot))))
162 (defun 3d-rotate (xrot yrot zrot)
163 (glRotatef xrot 1 0 0)
164 (glRotatef yrot 0 1 0)
165 (glRotatef zrot 0 0 1))
167 (defun 2d-rotate (rot)
168 (glRotatef rot 0 0 1))