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 video)
19 #:use-module (gacela sdl)
20 #:use-module (gacela gl)
21 #:use-module (gacela ftgl)
22 #:use-module (ice-9 optargs)
23 #:use-module (ice-9 receive)
59 (define init-video #f)
60 (define get-screen-height #f)
61 (define get-screen-width #f)
62 (define get-screen-bpp #f)
63 (define resize-screen #f)
64 (define quit-video #f)
66 (let ((screen #f) (flags 0))
68 (lambda* (width height bpp #:key (mode '2d) (title ""))
70 (SDL_Init SDL_INIT_VIDEO)
71 (let ((info (SDL_GetVideoInfo)))
72 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
73 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
74 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
75 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
76 (set! screen (SDL_SetVideoMode width height bpp flags))
77 (SDL_WM_SetCaption title "")
79 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))))
81 (set! get-screen-height
85 (set! get-screen-width
91 (surface-format-BytesPerPixel screen)))
94 (lambda (width height)
96 (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
97 (resize-screen-GL width height)))))
101 (SDL_FreeSurface screen)
105 (define (clear-screen)
106 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
108 (define (flip-screen)
109 (SDL_GL_SwapBuffers))
112 (define set-2d-mode #f)
113 (define set-3d-mode #f)
120 (glDisable GL_DEPTH_TEST)
121 (resize-screen-GL (get-screen-width) (get-screen-height))))
127 (glEnable GL_DEPTH_TEST)
128 (glDepthFunc GL_LEQUAL)
129 (resize-screen-GL (get-screen-width) (get-screen-height))))
137 (glShadeModel GL_SMOOTH)
138 (glClearColor 0 0 0 0)
140 (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
141 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
143 (define (resize-screen-GL width height)
144 (glViewport 0 0 width height)
145 (glMatrixMode GL_PROJECTION)
148 (let ((ratio (if (= height 0) width (/ width height))))
149 (gluPerspective 45 ratio 0.1 100)))
151 (let* ((w (/ width 2)) (h (/ height 2)))
152 (glOrtho (- w) w (- h) h 0 1))))
153 (glMatrixMode GL_MODELVIEW)
159 (define get-current-color #f)
160 (define set-current-color #f)
162 (let ((current-color '(1 1 1 1)))
163 (set! get-current-color
167 (set! set-current-color
168 (lambda* (red green blue #:optional (alpha 1))
169 (set! current-color (list red green blue alpha))
170 (glColor4f red green blue alpha))))
172 (define-macro (with-color color . code)
174 `(let ((original-color (get-current-color))
176 (apply set-current-color ,color)
177 (set! result (begin ,@code))
178 (apply set-current-color original-color)
180 (else `(begin ,@code))))
182 (define-macro (progn-textures . code)
184 (glEnable GL_TEXTURE_2D)
185 (set! result (begin ,@code))
186 (glDisable GL_TEXTURE_2D)
189 (define (draw . vertexes)
190 (begin-draw (length vertexes))
191 (draw-vertexes vertexes)
194 (define (begin-draw number-of-points)
195 (cond ((= number-of-points 2) (glBegin GL_LINES))
196 ((= number-of-points 3) (glBegin GL_TRIANGLES))
197 ((= number-of-points 4) (glBegin GL_QUADS))))
199 (define (draw-vertexes vertexes)
200 (cond ((not (null? vertexes))
201 (draw-vertex (car vertexes))
202 (draw-vertexes (cdr vertexes)))))
204 (define* (draw-vertex vertex #:key texture-coord)
205 (cond ((list? (car vertex))
206 (with-color (car vertex)
207 (apply simple-draw-vertex (cadr vertex))))
209 (cond (texture-coord (apply glTexCoord2f texture-coord)))
210 (apply simple-draw-vertex vertex))))
212 (define* (simple-draw-vertex x y #:optional (z 0))
213 (cond ((3d-mode?) (glVertex3f x y z))
214 (else (glVertex2f x y))))
216 (define (load-image filename)
217 (let ((image (IMG_Load filename)))
219 (SDL_DisplayFormatAlpha image)))))
221 (define (load-image-for-texture filename)
222 (let ((image (load-image filename)))
224 (let* ((width (surface-w image)) (height (surface-h image))
225 (power-2 (nearest-power-of-two (min width height)))
227 (cond ((and (= width power-2) (= height power-2)) (values image width height))
228 (else (set! resized-image (resize-surface image power-2 power-2))
229 (if resized-image (values resized-image width height))))))
233 (define (resize-surface surface width height)
234 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
235 (cond ((and (= width old-width) (= height old-height)) surface)
236 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
237 (zoomSurface surface zoomx zoomy 0))))))
239 (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
240 (let* ((key (list filename min-filter mag-filter))
241 (res (get-resource-from-cache key)))
246 (image real-w real-h) (load-image-for-texture filename)
248 (let ((width (surface-w image)) (height (surface-h image))
249 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
250 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
251 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
252 (texture (car (glGenTextures 1))))
254 (glBindTexture GL_TEXTURE_2D texture)
255 (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
256 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
257 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
258 (set-texture-size! texture real-w real-h)
259 (insert-resource-into-cache key texture)
262 (define* (draw-texture texture #:optional (zoom 1))
264 (let ((width (texture-w texture))
265 (height (texture-h texture)))
266 (draw-rectangle (* zoom width) (* zoom height) #:texture texture)))))
268 (define* (draw-line length #:optional color)
269 (let ((l (/ length 2)))
271 (with-color color (draw (list 0 l) (list 0 (- l)))))
273 (draw (list 0 l) (list 0 (- l)))))))
275 (define* (draw-quad v1 v2 v3 v4 #:key texture color)
278 (glBindTexture GL_TEXTURE_2D texture)
280 (draw-vertex v1 #:texture-coord '(0 0))
281 (draw-vertex v2 #:texture-coord '(1 0))
282 (draw-vertex v3 #:texture-coord '(1 1))
283 (draw-vertex v4 #:texture-coord '(0 1))
286 (with-color color (draw v1 v2 v3 v4)))
288 (draw v1 v2 v3 v4))))
290 (define* (draw-rectangle width height #:key texture color)
291 (let ((w (/ width 2)) (h (/ height 2)))
292 (draw-quad (list (- w) h 0)
299 (define* (draw-square #:key (size 1) texture color)
300 (draw-rectangle size size #:texture texture #:color color))
302 (define* (draw-cube #:key (size 1)
303 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
304 color color-1 color-2 color-3 color-4 color-5 color-6)
305 (let ((-size (- size)))
308 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) #:texture (or texture-1 texture) #:color (or color-1 color))
310 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) #:texture (or texture-2 texture) #:color (or color-2 color))
312 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) #:texture (or texture-3 texture) #:color (or color-3 color))
314 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture) #:color (or color-4 color))
316 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture) #:color (or color-5 color))
318 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture) #:color (or color-6 color)))))
320 (define* (translate x y #:optional (z 0))
321 (glTranslatef x y z))
323 (define* (rotate #:rest rot)
325 (apply 3d-rotate rot))
327 (apply 2d-rotate rot))))
329 (define (3d-rotate xrot yrot zrot)
330 (glRotatef xrot 1 0 0)
331 (glRotatef yrot 0 1 0)
332 (glRotatef zrot 0 0 1))
334 (define (2d-rotate rot)
335 (glRotatef rot 0 0 1))
339 (cond ((3d-mode?) (camera-look))))
344 (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
346 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
347 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
348 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
349 (and turn-on (glEnable id))
355 (define set-camera #f)
356 (define camera-look #f)
358 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
360 (lambda* (#:key eye center up)
361 (cond (eye (set! camera-eye eye)))
362 (cond (center (set! camera-center center)))
363 (cond (up (set! camera-up up)))))
367 (apply gluLookAt (append camera-eye camera-center camera-up)))))
372 (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
373 (let* ((key (list font-file))
374 (font (get-resource-from-cache key)))
376 (set! font (ftglCreateTextureFont font-file))
377 (insert-resource-into-cache key font)))
378 (ftglSetFontFaceSize font size 72)
379 (ftglSetFontCharMap font encoding)
382 (define* (render-text text font #:key (size #f))
383 (cond (size (ftglSetFontFaceSize font size 72)))
384 (ftglRenderFont font text FTGL_RENDER_ALL))