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 #:export-syntax (glmatrix-block))
79 (define* (init-video width height bpp #:key (mode '2d) (title "") (fps 20) (fullscreen 'off))
80 (SDL_Init SDL_INIT_VIDEO)
81 (let ((info (SDL_GetVideoInfo)))
82 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
83 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
84 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
85 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)
86 (if (eq? fullscreen 'on) SDL_FULLSCREEN 0)))
87 (set! screen (SDL_SetVideoMode width height bpp flags))
88 (set-screen-title! title)
89 (set-frames-per-second! fps)
90 (set-fullscreen! fullscreen #f)
92 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
94 (define (get-screen-height)
97 (define (get-screen-width)
100 (define (get-screen-bpp)
101 (* (surface-format-BytesPerPixel screen) 8))
103 (define (set-screen-bpp! bpp)
105 (set! screen (SDL_SetVideoMode (get-screen-width) (get-screen-height) (get-screen-bpp) flags)))))
107 (define (resize-screen width height)
109 (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
110 (resize-screen-GL width height))))
114 (SDL_FreeSurface screen)
118 (define (clear-screen)
119 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
121 (define (flip-screen)
122 (SDL_GL_SwapBuffers))
125 (define screen-title "")
127 (define (set-screen-title! title)
128 (set! screen-title title)
129 (SDL_WM_SetCaption title ""))
131 (define (get-screen-title)
137 (define (set-2d-mode)
139 (glDisable GL_DEPTH_TEST)
140 (resize-screen-GL (get-screen-width) (get-screen-height)))
142 (define (set-3d-mode)
145 (glEnable GL_DEPTH_TEST)
146 (glDepthFunc GL_LEQUAL)
147 (resize-screen-GL (get-screen-width) (get-screen-height)))
153 (define fullscreen 'off)
155 (define* (set-fullscreen! fs #:optional (toggle #t))
156 (cond ((or (and (eq? fullscreen 'on) (eq? fs 'off))
157 (and (eq? fullscreen 'off) (eq? fs 'on)))
160 (SDL_WM_ToggleFullScreen screen))))))
162 (define (get-fullscreen)
167 (glShadeModel GL_SMOOTH)
168 (glClearColor 0 0 0 0)
170 (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
171 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
173 (define (resize-screen-GL width height)
174 (glViewport 0 0 width height)
175 (glMatrixMode GL_PROJECTION)
178 (let ((ratio (if (= height 0) width (/ width height))))
179 (gluPerspective 45 ratio 0.1 100)))
181 (let* ((w (/ width 2)) (h (/ height 2)))
182 (glOrtho (- w) w (- h) h 0 1))))
183 (glMatrixMode GL_MODELVIEW)
187 ;;; Frames per second
190 (define frames-per-second 20)
191 (define time-per-frame 50) ;in ms
193 (define (get-frames-per-second)
196 (define (set-frames-per-second! fps)
197 (set! frames-per-second fps)
198 (set! time-per-frame (/ 1000.0 fps)))
200 (define (init-frame-time)
201 (set! time (SDL_GetTicks)))
203 (define (get-frame-time)
206 (define (delay-frame)
207 (let ((frame-time (- (SDL_GetTicks) time)))
208 (cond ((< frame-time time-per-frame)
209 (SDL_Delay (- time-per-frame frame-time))))))
214 (define current-color '(1 1 1 1))
216 (define (get-current-color)
219 (define* (set-current-color red green blue #:optional (alpha 1))
220 (set! current-color (list red green blue alpha))
221 (glColor4f red green blue alpha))
223 (define-macro (with-color color . code)
225 `(let ((original-color (get-current-color))
227 (apply set-current-color ,color)
228 (set! result (begin ,@code))
229 (apply set-current-color original-color)
231 (else `(begin ,@code))))
233 (define-macro (progn-textures . code)
235 (glEnable GL_TEXTURE_2D)
236 (set! result (begin ,@code))
237 (glDisable GL_TEXTURE_2D)
240 (define (draw . vertexes)
241 (begin-draw (length vertexes))
242 (draw-vertexes vertexes)
245 (define (begin-draw number-of-points)
246 (cond ((= number-of-points 2) (glBegin GL_LINES))
247 ((= number-of-points 3) (glBegin GL_TRIANGLES))
248 ((= number-of-points 4) (glBegin GL_QUADS))))
250 (define (draw-vertexes vertexes)
251 (cond ((not (null? vertexes))
252 (draw-vertex (car vertexes))
253 (draw-vertexes (cdr vertexes)))))
255 (define* (draw-vertex vertex #:key texture-coord)
256 (cond ((list? (car vertex))
257 (with-color (car vertex)
258 (apply simple-draw-vertex (cadr vertex))))
260 (cond (texture-coord (apply glTexCoord2f texture-coord)))
261 (apply simple-draw-vertex vertex))))
263 (define* (simple-draw-vertex x y #:optional (z 0))
264 (cond ((3d-mode?) (glVertex3f x y z))
265 (else (glVertex2f x y))))
267 (define (load-image filename)
268 (let ((image (IMG_Load filename)))
270 (SDL_DisplayFormatAlpha image)))))
272 (define (load-image-for-texture filename)
273 (let ((image (load-image filename)))
275 (let* ((width (surface-w image)) (height (surface-h image))
276 (power-2 (nearest-power-of-two (min width height)))
278 (cond ((and (= width power-2) (= height power-2)) (values image width height))
279 (else (set! resized-image (resize-surface image power-2 power-2))
280 (if resized-image (values resized-image width height))))))
284 (define (resize-surface surface width height)
285 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
286 (cond ((and (= width old-width) (= height old-height)) surface)
287 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
288 (zoomSurface surface zoomx zoomy 0))))))
290 (define* (load-texture-without-cache filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
293 (image real-w real-h) (load-image-for-texture filename)
295 (let ((width (surface-w image)) (height (surface-h image))
296 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
297 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
298 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
299 (texture (car (glGenTextures 1))))
301 (glBindTexture GL_TEXTURE_2D texture)
302 (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
303 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
304 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
305 (set-texture-size! texture real-w real-h)
308 (define load-texture (use-cache-with load-texture-without-cache))
310 (define (get-texture-properties texture)
311 `((width . ,(texture-w texture)) (height . ,(texture-h texture))))
313 (define* (draw-texture texture #:key (zoom 1) (sprite '((0 0) (1 1))))
315 (let ((width (texture-w texture))
316 (height (texture-h texture)))
317 (draw-rectangle (* zoom width (- (caadr sprite) (caar sprite)))
318 (* zoom height (- (cadadr sprite) (cadar sprite)))
320 #:texture-coord sprite)))))
322 (define* (draw-line length #:optional color)
323 (let ((l (/ length 2)))
325 (with-color color (draw (list 0 l) (list 0 (- l)))))
327 (draw (list 0 l) (list 0 (- l)))))))
329 (define* (draw-quad v1 v2 v3 v4 #:key texture color (texture-coord '((0 0) (1 1))))
332 (glBindTexture GL_TEXTURE_2D texture)
334 (draw-vertex v1 #:texture-coord (car texture-coord))
335 (draw-vertex v2 #:texture-coord (list (caadr texture-coord) (cadar texture-coord)))
336 (draw-vertex v3 #:texture-coord (cadr texture-coord))
337 (draw-vertex v4 #:texture-coord (list (caar texture-coord) (cadadr texture-coord)))
340 (with-color color (draw v1 v2 v3 v4)))
342 (draw v1 v2 v3 v4))))
344 (define* (draw-rectangle width height #:key texture color texture-coord)
345 (let ((w (/ width 2)) (h (/ height 2)))
346 (draw-quad (list (- w) h 0)
351 #:texture-coord texture-coord
354 (define* (draw-square #:key (size 1) texture color)
355 (draw-rectangle size size #:texture texture #:color color))
357 (define* (draw-cube #:key (size 1)
358 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
359 color color-1 color-2 color-3 color-4 color-5 color-6)
360 (let ((-size (- size)))
363 (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))
365 (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))
367 (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))
369 (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))
371 (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))
373 (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)))))
375 (define* (translate x y #:optional (z 0))
376 (glTranslatef x y z))
378 (define* (rotate #:rest rot)
380 (apply 3d-rotate rot))
382 (apply 2d-rotate rot))))
384 (define (3d-rotate xrot yrot zrot)
385 (glRotatef xrot 1 0 0)
386 (glRotatef yrot 0 1 0)
387 (glRotatef zrot 0 0 1))
389 (define (2d-rotate rot)
390 (glRotatef rot 0 0 1))
394 (cond ((3d-mode?) (camera-look))))
396 (define-macro (glmatrix-block . code)
399 (set! result (begin ,@code))
406 ;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
408 ;; (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
409 ;; (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
410 ;; (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
411 ;; (and turn-on (glEnable id))
417 (define camera-eye '(0 0 0))
418 (define camera-center '(0 0 -100))
419 (define camera-up '(0 1 0))
421 (define* (set-camera #:key eye center up)
422 (cond (eye (set! camera-eye eye)))
423 (cond (center (set! camera-center center)))
424 (cond (up (set! camera-up up))))
426 (define (camera-look)
427 (apply gluLookAt (append camera-eye camera-center camera-up)))
432 (define* (load-font-without-cache font-file #:key (size 40) (encoding ft_encoding_unicode))
433 (let ((font (ftglCreateTextureFont font-file size)))
434 (ftglSetFontFaceSize font size 72)
435 (ftglSetFontCharMap font encoding)
438 (define load-font (use-cache-with load-font-without-cache))
440 (define* (render-text text font #:key (size #f))
442 (cond ((not (= (ftglGetFontFaceSize font) size))
443 (ftglSetFontFaceSize font size 72))))
444 ((not (= (ftglGetFontFaceSize font) (font-size font)))
445 (ftglSetFontFaceSize font (font-size font) 72)))
446 (ftglRenderFont font text FTGL_RENDER_ALL))