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 (gacela math)
23 #:use-module (gacela utils)
24 #:use-module (ice-9 optargs)
25 #:use-module (ice-9 receive)
41 set-frames-per-second!
53 load-texture-without-cache
54 get-texture-properties
68 load-font-without-texture
70 #:re-export (glPushMatrix
72 #:export-syntax (glmatrix-block))
81 (define* (init-video width height bpp #:key (mode '2d) (title "") (fps 20) (fullscreen 'off))
82 (SDL_Init SDL_INIT_VIDEO)
83 (let ((info (SDL_GetVideoInfo)))
84 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
85 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
86 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
87 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)
88 (if (eq? fullscreen 'on) SDL_FULLSCREEN 0)))
89 (set! screen (SDL_SetVideoMode width height bpp flags))
90 (set-screen-title! title)
91 (set-frames-per-second! fps)
92 (set-fullscreen! fullscreen #f)
94 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
96 (define (get-screen-height)
99 (define (get-screen-width)
102 (define (get-screen-bpp)
103 (* (surface-format-BytesPerPixel screen) 8))
105 (define (set-screen-bpp! bpp)
107 (set! screen (SDL_SetVideoMode (get-screen-width) (get-screen-height) (get-screen-bpp) flags)))))
109 (define (resize-screen width height)
111 (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
112 (resize-screen-GL width height))))
116 (SDL_FreeSurface screen)
120 (define (clear-screen)
121 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
123 (define (flip-screen)
124 (SDL_GL_SwapBuffers))
127 (define screen-title "")
129 (define (set-screen-title! title)
130 (set! screen-title title)
131 (SDL_WM_SetCaption title ""))
133 (define (get-screen-title)
139 (define (set-2d-mode)
141 (glDisable GL_DEPTH_TEST)
142 (resize-screen-GL (get-screen-width) (get-screen-height)))
144 (define (set-3d-mode)
147 (glEnable GL_DEPTH_TEST)
148 (glDepthFunc GL_LEQUAL)
149 (resize-screen-GL (get-screen-width) (get-screen-height)))
155 (define fullscreen 'off)
157 (define* (set-fullscreen! fs #:optional (toggle #t))
158 (cond ((or (and (eq? fullscreen 'on) (eq? fs 'off))
159 (and (eq? fullscreen 'off) (eq? fs 'on)))
162 (SDL_WM_ToggleFullScreen screen))))))
164 (define (get-fullscreen)
169 (glShadeModel GL_SMOOTH)
170 (glClearColor 0 0 0 0)
172 (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
173 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
175 (define (resize-screen-GL width height)
176 (glViewport 0 0 width height)
177 (glMatrixMode GL_PROJECTION)
180 (let ((ratio (if (= height 0) width (/ width height))))
181 (gluPerspective 45 ratio 0.1 100)))
183 (let* ((w (/ width 2)) (h (/ height 2)))
184 (glOrtho (- w) w (- h) h 0 1))))
185 (glMatrixMode GL_MODELVIEW)
189 ;;; Frames per second
192 (define frames-per-second 20)
193 (define time-per-frame 50) ;in ms
195 (define (get-frames-per-second)
198 (define (set-frames-per-second! fps)
199 (set! frames-per-second fps)
200 (set! time-per-frame (/ 1000.0 fps)))
202 (define (init-frame-time)
203 (set! time (SDL_GetTicks)))
205 (define (get-frame-time)
208 (define (delay-frame)
209 (let ((frame-time (- (SDL_GetTicks) time)))
210 (cond ((< frame-time time-per-frame)
211 (SDL_Delay (- time-per-frame frame-time))))))
216 (define current-color '(1 1 1 1))
218 (define (get-current-color)
221 (define* (set-current-color red green blue #:optional (alpha 1))
222 (set! current-color (list red green blue alpha))
223 (glColor4f red green blue alpha))
225 (define-macro (with-color color . code)
227 `(let ((original-color (get-current-color))
229 (apply set-current-color ,color)
230 (set! result (begin ,@code))
231 (apply set-current-color original-color)
233 (else `(begin ,@code))))
235 (define-macro (progn-textures . code)
237 (glEnable GL_TEXTURE_2D)
238 (set! result (begin ,@code))
239 (glDisable GL_TEXTURE_2D)
242 (define (draw . vertexes)
243 (begin-draw (length vertexes))
244 (draw-vertexes vertexes)
247 (define (begin-draw number-of-points)
248 (cond ((= number-of-points 2) (glBegin GL_LINES))
249 ((= number-of-points 3) (glBegin GL_TRIANGLES))
250 ((= number-of-points 4) (glBegin GL_QUADS))))
252 (define (draw-vertexes vertexes)
253 (cond ((not (null? vertexes))
254 (draw-vertex (car vertexes))
255 (draw-vertexes (cdr vertexes)))))
257 (define* (draw-vertex vertex #:key texture-coord)
258 (cond ((list? (car vertex))
259 (with-color (car vertex)
260 (apply simple-draw-vertex (cadr vertex))))
262 (cond (texture-coord (apply glTexCoord2f texture-coord)))
263 (apply simple-draw-vertex vertex))))
265 (define* (simple-draw-vertex x y #:optional (z 0))
266 (cond ((3d-mode?) (glVertex3f x y z))
267 (else (glVertex2f x y))))
269 (define (load-image filename)
270 (let ((image (IMG_Load filename)))
272 (SDL_DisplayFormatAlpha image)))))
274 (define (load-image-for-texture filename)
275 (let ((image (load-image filename)))
277 (let* ((width (surface-w image)) (height (surface-h image))
278 (power-2 (nearest-power-of-two (min width height)))
280 (cond ((and (= width power-2) (= height power-2)) (values image width height))
281 (else (set! resized-image (resize-surface image power-2 power-2))
282 (if resized-image (values resized-image width height))))))
286 (define (resize-surface surface width height)
287 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
288 (cond ((and (= width old-width) (= height old-height)) surface)
289 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
290 (zoomSurface surface zoomx zoomy 0))))))
292 (define* (load-texture-without-cache filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
295 (image real-w real-h) (load-image-for-texture filename)
297 (let ((width (surface-w image)) (height (surface-h image))
298 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
299 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
300 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
301 (texture (car (glGenTextures 1))))
303 (glBindTexture GL_TEXTURE_2D texture)
304 (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
305 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
306 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
307 (set-texture-size! texture real-w real-h)
310 (define load-texture (use-cache-with load-texture-without-cache))
312 (define (get-texture-properties texture)
313 `((width . ,(texture-w texture)) (height . ,(texture-h texture))))
315 (define* (draw-texture texture #:key (zoom 1) (sprite '((0 0) (1 1))))
317 (let ((width (texture-w texture))
318 (height (texture-h texture)))
319 (draw-rectangle (* zoom width (- (caadr sprite) (caar sprite)))
320 (* zoom height (- (cadadr sprite) (cadar sprite)))
322 #:texture-coord sprite)))))
324 (define* (draw-line length #:optional color)
325 (let ((l (/ length 2)))
327 (with-color color (draw (list 0 l) (list 0 (- l)))))
329 (draw (list 0 l) (list 0 (- l)))))))
331 (define* (draw-quad v1 v2 v3 v4 #:key texture color (texture-coord '((0 0) (1 1))))
334 (glBindTexture GL_TEXTURE_2D texture)
336 (draw-vertex v1 #:texture-coord (car texture-coord))
337 (draw-vertex v2 #:texture-coord (list (caadr texture-coord) (cadar texture-coord)))
338 (draw-vertex v3 #:texture-coord (cadr texture-coord))
339 (draw-vertex v4 #:texture-coord (list (caar texture-coord) (cadadr texture-coord)))
342 (with-color color (draw v1 v2 v3 v4)))
344 (draw v1 v2 v3 v4))))
346 (define* (draw-rectangle width height #:key texture color texture-coord)
347 (let ((w (/ width 2)) (h (/ height 2)))
348 (draw-quad (list (- w) h 0)
353 #:texture-coord texture-coord
356 (define* (draw-square #:key (size 1) texture color)
357 (draw-rectangle size size #:texture texture #:color color))
359 (define* (draw-cube #:key (size 1)
360 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
361 color color-1 color-2 color-3 color-4 color-5 color-6)
362 (let ((-size (- size)))
365 (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))
367 (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))
369 (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))
371 (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))
373 (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))
375 (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)))))
377 (define* (translate x y #:optional (z 0))
378 (glTranslatef x y z))
380 (define* (rotate #:rest rot)
382 (apply 3d-rotate rot))
384 (apply 2d-rotate rot))))
386 (define (3d-rotate xrot yrot zrot)
387 (glRotatef xrot 1 0 0)
388 (glRotatef yrot 0 1 0)
389 (glRotatef zrot 0 0 1))
391 (define (2d-rotate rot)
392 (glRotatef rot 0 0 1))
396 (cond ((3d-mode?) (camera-look))))
398 (define-macro (glmatrix-block . code)
401 (set! result (begin ,@code))
408 ;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
410 ;; (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
411 ;; (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
412 ;; (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
413 ;; (and turn-on (glEnable id))
419 (define camera-eye '(0 0 0))
420 (define camera-center '(0 0 -100))
421 (define camera-up '(0 1 0))
423 (define* (set-camera #:key eye center up)
424 (cond (eye (set! camera-eye eye)))
425 (cond (center (set! camera-center center)))
426 (cond (up (set! camera-up up))))
428 (define (camera-look)
429 (apply gluLookAt (append camera-eye camera-center camera-up)))
434 (define* (load-font-without-cache font-file #:key (size 40) (encoding ft_encoding_unicode))
435 (let ((font (ftglCreateTextureFont font-file size)))
436 (ftglSetFontFaceSize font size 72)
437 (ftglSetFontCharMap font encoding)
440 (define load-font (use-cache-with load-font-without-cache))
442 (define* (render-text text font #:key (size #f))
444 (cond ((not (= (ftglGetFontFaceSize font) size))
445 (ftglSetFontFaceSize font size 72))))
446 ((not (= (ftglGetFontFaceSize font) (font-size font)))
447 (ftglSetFontFaceSize font (font-size font) 72)))
448 (ftglRenderFont font text FTGL_RENDER_ALL))