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/>.
18 (when (not (find-package 'gacela))
19 (make-package 'gacela :nicknames '(gg) :use '(lisp)))
21 (eval-when (eval) (in-package 'gacela :nicknames '(gg) :use '(lisp)))
23 (defmacro with-color (color &body code)
25 `(let ((original-color (get-current-color)))
26 (apply #'set-current-color ,color)
28 (apply #'set-current-color original-color)))
33 (defmacro progn-textures (&body code)
36 (glEnable GL_TEXTURE_2D)
37 (setq values (multiple-value-list (progn ,@code)))
38 (glDisable GL_TEXTURE_2D)
39 (apply #'values values)))
41 (defun draw (&rest vertexes)
42 (begin-draw (length vertexes))
43 (draw-vertexes vertexes)
46 (defun begin-draw (number-of-points)
47 (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
48 ((= number-of-points 4) (glBegin GL_QUADS))))
50 (defun draw-vertexes (vertexes)
51 (cond ((null vertexes) nil)
52 (t (draw-vertex (car vertexes))
53 (draw-vertexes (cdr vertexes)))))
55 (defun draw-vertex (vertex &key texture-coord)
56 (cond ((consp (car vertex))
57 (with-color (car vertex)
58 (apply #'simple-draw-vertex (cadr vertex))))
59 (t (cond (texture-coord (apply #'glTexCoord2f texture-coord)))
60 (apply #'simple-draw-vertex vertex))))
62 (defun simple-draw-vertex (x y &optional (z 0))
63 (cond ((3d-mode?) (glVertex3f x y z))
64 (t (glVertex2f x y))))
66 (defun load-image-for-texture (filename)
68 (let ((image (IMG_Load filename)))
70 (let* ((width (surface-w image)) (height (surface-h image))
71 (power-2 (nearest-power-of-two (min width height)))
72 (zoomx (/ power-2 width)) (zoomy (/ power-2 height))
74 (cond ((and (= zoomx 1) (= zoomy 1)) (values image width height))
75 (t (setq zoomed-image (zoomSurface image zoomx zoomy 0))
76 (SDL_FreeSurface image)
77 (cond ((/= zoomed-image 0) (values zoomed-image width height))))))))))
79 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
80 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
81 (cond ((get-resource key) key)
82 (t (true-load-texture filename min-filter mag-filter static)))))
84 (defun true-load-texture (filename min-filter mag-filter static)
85 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
88 (image real-w real-h) (load-image-for-texture filename)
90 (let ((width (surface-w image)) (height (surface-h image))
91 (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
92 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)
93 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)))
94 (texture (car (glGenTextures 1))))
95 (glBindTexture GL_TEXTURE_2D texture)
96 (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
97 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
98 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
99 (SDL_FreeSurface image)
101 `(:id-texture ,texture :width ,real-w :height ,real-h)
102 (lambda () (true-load-texture filename min-filter mag-filter static))
103 (lambda () (glDeleteTextures 1 `(,texture)))
107 (defun draw-image-function (filename)
108 (let ((texture (load-texture filename)))
109 (lambda (&optional (f 1))
111 (let ((width (getf (get-resource texture) :width))
112 (height (getf (get-resource texture) :height)))
113 (draw-rectangle (* f width) (* f height) :texture texture)))))))
115 (defun draw-quad (v1 v2 v3 v4 &key texture)
116 (cond ((consp texture) (with-color texture (draw v1 v2 v3 v4)))
119 (glBindTexture GL_TEXTURE_2D (getf (get-resource texture) :id-texture))
121 (draw-vertex v1 :texture-coord '(0 0))
122 (draw-vertex v2 :texture-coord '(1 0))
123 (draw-vertex v3 :texture-coord '(1 1))
124 (draw-vertex v4 :texture-coord '(0 1))
126 (t (draw v1 v2 v3 v4))))
128 (defun draw-rectangle (width height &key texture)
129 (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
130 (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
132 (defun draw-square (&key (size 1) texture)
133 (draw-rectangle size size :texture texture))
135 (defun draw-cube (&key (size 1) texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6)
136 (let ((-size (neg size)))
139 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture (or texture-1 texture))
141 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture (or texture-2 texture))
143 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture (or texture-3 texture))
145 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture))
147 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture))
149 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture)))))
151 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
153 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
154 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
155 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
156 (and turn-on (glEnable id))
159 (defun translate (x y &optional (z 0))
160 (glTranslatef x y z))
162 (defun rotate (&rest rot)
163 (cond ((3d-mode?) (apply #'3d-rotate rot))
164 (t (apply #'2d-rotate rot))))
166 (defun 3d-rotate (xrot yrot zrot)
167 (glRotatef xrot 1 0 0)
168 (glRotatef yrot 0 1 0)
169 (glRotatef zrot 0 0 1))
171 (defun 2d-rotate (rot)
172 (glRotatef rot 0 0 1))
176 (cond ((3d-mode?) (camera-look))))
178 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
179 (defun set-camera (&key eye center up)
180 (cond (eye (setq camera-eye eye)))
181 (cond (center (setq camera-center center)))
182 (cond (up (setq camera-up up))))
184 (defun camera-look ()
185 (apply #'gluLookAt (concatenate 'list camera-eye camera-center camera-up))))