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)
20 `(let ((original-color (get-current-color)))
21 (apply #'set-current-color ,color)
23 (apply #'set-current-color original-color)))
25 (defmacro progn-textures (&body code)
28 (glEnable GL_TEXTURE_2D)
29 (setq values (multiple-value-list (progn ,@code)))
30 (glDisable GL_TEXTURE_2D)
31 (apply #'values values)))
33 (defun draw (&rest vertexes)
34 (begin-draw (length vertexes))
35 (draw-vertexes vertexes)
38 (defun begin-draw (number-of-points)
39 (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
40 ((= number-of-points 4) (glBegin GL_QUADS))))
42 (defun draw-vertexes (vertexes)
43 (cond ((null vertexes) nil)
44 (t (draw-vertex (car vertexes))
45 (draw-vertexes (cdr vertexes)))))
47 (defun draw-vertex (vertex &key texture-coord)
48 (cond ((consp (car vertex))
49 (with-color (car vertex)
50 (apply #'simple-draw-vertex (cadr vertex))))
51 (t (cond (texture-coord (apply #'glTexCoord2f texture-coord)))
52 (apply #'simple-draw-vertex vertex))))
54 (defun simple-draw-vertex (x y &optional (z 0))
55 (cond ((3d-mode?) (glVertex3f x y z))
56 (t (glVertex2f x y))))
58 (defun load-image-for-texture (filename)
60 (let ((image (IMG_Load filename)))
62 (let* ((width (surface-w image)) (height (surface-h image))
63 (power-2 (nearest-power-of-two (min width height)))
64 (zoomx (/ power-2 width)) (zoomy (/ power-2 height))
66 (cond ((and (= zoomx 1) (= zoomy 1)) (values image width height))
67 (t (setq zoomed-image (zoomSurface image zoomx zoomy 0))
68 (SDL_FreeSurface image)
69 (cond ((/= zoomed-image 0) (values zoomed-image width height))))))))))
71 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
72 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
73 (cond ((get-resource key) key)
74 (t (true-load-texture filename min-filter mag-filter static)))))
76 (defun true-load-texture (filename min-filter mag-filter static)
77 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
80 (image real-w real-h) (load-image-for-texture filename)
82 (let ((width (surface-w image)) (height (surface-h image))
83 (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
84 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)
85 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)))
86 (texture (car (glGenTextures 1))))
87 (glBindTexture GL_TEXTURE_2D texture)
88 (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
89 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
90 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
91 (SDL_FreeSurface image)
93 `(:id-texture ,texture :width ,real-w :height ,real-h)
94 (lambda () (true-load-texture filename min-filter mag-filter static))
95 (lambda () (glDeleteTextures 1 `(,texture)))
99 (defun draw-image-function (filename)
100 (let ((texture (load-texture filename)))
101 (lambda (&optional (f 1))
103 (let ((width (getf (get-resource texture) :width))
104 (height (getf (get-resource texture) :height)))
105 (draw-rectangle (* f width) (* f height) :texture texture)))))))
107 (defun draw-quad (v1 v2 v3 v4 &key texture)
108 (cond (texture (progn-textures
109 (glBindTexture GL_TEXTURE_2D (getf (get-resource texture) :id-texture))
111 (draw-vertex v1 :texture-coord '(0 0))
112 (draw-vertex v2 :texture-coord '(1 0))
113 (draw-vertex v3 :texture-coord '(1 1))
114 (draw-vertex v4 :texture-coord '(0 1))
116 (t (draw v1 v2 v3 v4))))
118 (defun draw-rectangle (width height &key texture)
119 (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
120 (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
122 (defun draw-square (&key (size 1) texture)
123 (draw-rectangle size size :texture texture))
125 (defun draw-cube (&key size texture)
126 (let ((-size (neg size)))
129 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture texture)
131 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture texture)
133 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture texture)
135 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture texture)
137 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture texture)
139 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture texture))))
141 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
143 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
144 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
145 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
146 (and turn-on (glEnable id))
149 (defun translate (x y &optional (z 0))
150 (glTranslatef x y z))
152 (defun rotate (xrot yrot zrot)
153 (glRotatef xrot 1 0 0)
154 (glRotatef yrot 0 1 0)
155 (glRotatef zrot 0 0 1))
157 (defun 2d-rotate (rot)