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 (figl gl)
22 #:use-module (figl glu)
23 #:use-module (gacela ftgl)
24 #:use-module (gacela math)
25 #:use-module (gacela utils)
26 #:use-module (ice-9 optargs)
27 #:use-module (ice-9 receive)
43 set-frames-per-second!
55 load-texture-without-cache
56 get-texture-properties
70 load-font-without-texture
72 #:re-export (with-gl-push-matrix))
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 (gl-clear (clear-buffer-mask color-buffer depth-buffer)))
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 (gl-disable (enable-cap depth-test))
142 (resize-screen-GL (get-screen-width) (get-screen-height)))
144 (define (set-3d-mode)
146 (set-gl-clear-depth 1)
147 (gl-enable (enable-cap depth-test))
148 (set-gl-depth-function (depth-function 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 (set-gl-shade-model (shading-model smooth))
170 (set-gl-clear-color 0 0 0 0)
171 (gl-enable (enable-cap blend))
172 (set-gl-blend-function (blending-factor-dest src-alpha) (blending-factor-dest one-minus-src-alpha))
173 (set-gl-hint (hint-target perspective-correction-hint) (hint-mode nicest)))
175 (define (resize-screen-GL width height)
176 (gl-viewport 0 0 width height)
177 (set-gl-matrix-mode (matrix-mode projection))
180 (let ((ratio (if (= height 0) width (/ width height))))
181 (glu-perspective 45 ratio 0.1 100)))
183 (let* ((w (/ width 2)) (h (/ height 2)))
184 (gl-ortho (- w) w (- h) h 0 1))))
185 (set-gl-matrix-mode (matrix-mode 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 (gl-color 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 (begin-textures . code)
237 (gl-enable (oes-framebuffer-object texture-2d))
238 (set! result (begin ,@code))
239 (gl-disable (oes-framebuffer-object texture-2d))
242 (define (draw . vertexes)
244 (let ((number-of-points (length vertexes)))
245 (cond ((= number-of-points 2) (begin-mode lines))
246 ((= number-of-points 3) (begin-mode triangles))
247 ((= number-of-points 4) (begin-mode quads))
248 ((> number-of-points 4) (begin-mode polygon))))
249 (draw-vertexes vertexes)))
251 (define (draw-vertexes vertexes)
252 (cond ((not (null? vertexes))
253 (apply draw-vertex (if (list? (caar vertexes)) (car vertexes) (list (car vertexes))))
254 (draw-vertexes (cdr vertexes)))))
256 (define* (draw-vertex vertex #:key texture-coord)
257 (cond (texture-coord (apply glTexCoord2f texture-coord)))
258 (apply gl-vertex vertex))
260 (define (load-image filename)
261 (let ((image (IMG_Load filename)))
263 (SDL_DisplayFormatAlpha image)))))
265 (define (load-image-for-texture filename)
266 (let ((image (load-image filename)))
268 (let* ((width (surface-w image)) (height (surface-h image))
269 (power-2 (nearest-power-of-two (min width height)))
271 (cond ((and (= width power-2) (= height power-2)) (values image width height))
272 (else (set! resized-image (resize-surface image power-2 power-2))
273 (if resized-image (values resized-image width height))))))
277 (define (resize-surface surface width height)
278 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
279 (cond ((and (= width old-width) (= height old-height)) surface)
280 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
281 (zoomSurface surface zoomx zoomy 0))))))
283 (define* (load-texture-without-cache filename #:key (min-filter (texture-min-filter linear)) (mag-filter (texture-mag-filter linear)))
286 (image real-w real-h) (load-image-for-texture filename)
288 (let ((width (surface-w image)) (height (surface-h image))
289 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
290 (if (= (surface-format-BytesPerPixel image) 3) (ext-bgra bgr-ext) (ext-bgra bgra-ext))
291 (if (= (surface-format-BytesPerPixel image) 3) (pixel-format rgb) (pixel-format rgba))))
292 (texture (car (glGenTextures 1))))
294 (glBindTexture (oes-framebuffer-object texture-2d) texture)
295 (glTexImage2D (oes-framebuffer-object texture-2d) 0 4 width height 0 byteorder (data-type unsigned-byte) (surface-pixels image))
296 (glTexParameteri (oes-framebuffer-object texture-2d) (texture-parameter-name texture-min-filter) min-filter)
297 (glTexParameteri (oes-framebuffer-object texture-2d) (texture-parameter-name texture-mag-filter) mag-filter)
298 (set-texture-size! texture real-w real-h)
301 (define load-texture (use-cache-with load-texture-without-cache))
303 (define (get-texture-properties texture)
304 `((width . ,(texture-w texture)) (height . ,(texture-h texture))))
306 (define* (draw-texture texture #:key (zoom 1) (sprite '((0 0) (1 1))))
308 (let ((width (texture-w texture))
309 (height (texture-h texture)))
310 (draw-rectangle (* zoom width (- (caadr sprite) (caar sprite)))
311 (* zoom height (- (cadadr sprite) (cadar sprite)))
313 #:texture-coord sprite)))))
315 (define* (draw-line length)
316 (let ((l (/ length 2)))
317 (draw (list 0 l) (list 0 (- l)))))
319 (define (draw-circle radius)
324 (let ((a (degrees-to-radians i)))
325 (draw-vertex (list (* radius (cos a)) (* radius (sin a))))))))
327 (define* (draw-quad v1 v2 v3 v4 #:key texture (texture-coord '((0 0) (1 1))))
330 (glBindTexture (oes-framebuffer-object texture-2d) texture)
331 (draw (list v1 #:texture-coord (car texture-coord))
332 (list v2 #:texture-coord (list (caadr texture-coord) (cadar texture-coord)))
333 (list v3 #:texture-coord (cadr texture-coord))
334 (list v4 #:texture-coord (list (caar texture-coord) (cadadr texture-coord))))))
336 (draw v1 v2 v3 v4))))
338 (define* (draw-rectangle width height #:key texture texture-coord)
339 (let ((w (/ width 2)) (h (/ height 2)))
340 (draw-quad (list (- w) h 0)
345 #:texture-coord texture-coord)))
347 (define* (draw-square size #:key texture)
348 (draw-rectangle size size #:texture texture))
350 (define* (draw-cube #:key (size 1)
351 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
352 color-1 color-2 color-3 color-4 color-5 color-6)
353 (let ((-size (- size)))
356 (with-color color-1 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) #:texture (or texture-1 texture)))
358 (with-color color-2 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) #:texture (or texture-2 texture)))
360 (with-color color-3 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) #:texture (or texture-3 texture)))
362 (with-color color-4 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) #:texture (or texture-4 texture)))
364 (with-color color-5 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) #:texture (or texture-5 texture)))
366 (with-color color-6 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) #:texture (or texture-6 texture))))))
368 (define* (gtranslate x y #:optional (z 0))
369 (gl-translate x y z))
371 (define (grotate . rot)
373 (apply 3d-rotate rot))
375 (2d-rotate (car (last-pair rot))))))
377 (define (3d-rotate xrot yrot zrot)
378 (gl-rotate xrot 1 0 0)
379 (gl-rotate yrot 0 1 0)
380 (gl-rotate zrot 0 0 1))
382 (define (2d-rotate rot)
383 (gl-rotate rot 0 0 1))
387 (cond ((3d-mode?) (camera-look))))
392 ;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
394 ;; (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
395 ;; (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
396 ;; (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
397 ;; (and turn-on (gl-enable id))
403 (define camera-eye '(0 0 0))
404 (define camera-center '(0 0 -100))
405 (define camera-up '(0 1 0))
407 (define* (set-camera #:key eye center up)
408 (cond (eye (set! camera-eye eye)))
409 (cond (center (set! camera-center center)))
410 (cond (up (set! camera-up up))))
412 (define (camera-look)
413 (apply glu-look-at (append camera-eye camera-center camera-up)))
418 (define* (load-font-without-cache font-file #:key (size 40) (encoding ft_encoding_unicode))
419 (let ((font (ftglCreateTextureFont font-file size)))
420 (ftglSetFontFaceSize font size 72)
421 (ftglSetFontCharMap font encoding)
424 (define load-font (use-cache-with load-font-without-cache))
426 (define* (render-text text font #:key (size #f))
428 (cond ((not (= (ftglGetFontFaceSize font) size))
429 (ftglSetFontFaceSize font size 72))))
430 ((not (= (ftglGetFontFaceSize font) (font-size font)))
431 (ftglSetFontFaceSize font (font-size font) 72)))
432 (ftglRenderFont font text FTGL_RENDER_ALL))
438 (make-record-type "mesh"
439 '(draw translate turn rotate color inner-properties inner-property properties properties-set! property property-set!)
440 (lambda (record port)
441 (format port "#<mesh: ~a" (mesh-inner-property record 'type))
442 (for-each (lambda (x) (format port " ~a" x))
443 (mesh-properties record))
444 (display ">" port))))
446 (define mesh? (record-predicate mesh-type))
448 (define* (make-mesh type proc)
450 (record-constructor mesh-type)
451 (let ((px 0) (py 0) (pz 0)
456 (let ((inner-properties
458 `((type . ,type) (color . ,color)
459 (x . ,px) (y . ,py) (z . ,pz)
460 (ax . ,ax) (ay . ,ay) (az . ,az)
461 (rx . ,rx) (ry . ,ry) (rz . ,rz)))))
467 (gtranslate px py pz)
469 (with-color color (proc properties))))
493 (assoc-ref (inner-properties) prop-name))
497 (lambda (new-properties)
499 (set! properties new-properties))
502 (assoc-ref properties prop-name))
503 (lambda (prop-name value)
505 (set! properties (assoc-set! properties prop-name value))))))))
507 (define (mesh-draw mesh)
508 (((record-accessor mesh-type 'draw) mesh)))
510 (define (mesh-inner-properties mesh)
511 (((record-accessor mesh-type 'inner-properties) mesh)))
513 (define (mesh-inner-property mesh prop-name)
514 (((record-accessor mesh-type 'inner-property) mesh) prop-name))
516 (define (mesh-properties mesh)
517 (((record-accessor mesh-type 'properties) mesh)))
519 (define (mesh-properties-set! mesh new-properties)
520 (((record-accessor mesh-type 'properties-set!) mesh) new-properties))
522 (define (mesh-property mesh prop-name)
523 (((record-accessor mesh-type 'property) mesh) prop-name))
525 (define (mesh-property-set! mesh prop-name value)
526 (((record-accessor mesh-type 'property-set!) mesh) prop-name value))
528 (define* (translate mesh x y #:optional (z 0))
529 (((record-accessor mesh-type 'translate) mesh) x y z)
532 (define (turn mesh . params)
533 (apply ((record-accessor mesh-type 'turn) mesh)
534 (if (>= (length params) 3)
536 (list 0 0 (car params))))
539 (define (rotate mesh . params)
540 (apply ((record-accessor mesh-type 'rotate) mesh)
541 (if (>= (length params) 3)
543 (list 0 0 (car params))))
546 (define (color mesh c)
547 (((record-accessor mesh-type 'color) mesh) c)
553 (define (mesh-join . meshes)
557 (for-each (lambda (m) (with-gl-push-matrix (mesh-draw m))) meshes))))
562 (define-macro (primitive header . body)
563 (let* ((type (car header))
565 (list-args (names-arguments args)))
570 (apply (lambda* ,(cons #:key list-args) ,@body)
572 ,@(let get-params ((l list-args))
573 (cond ((null? l) '())
575 (cons (symbol->keyword (car l))
576 (cons `(assoc-ref props ',(car l))
577 (get-params (cdr l)))))))))))))
578 (mesh-properties-set! m (list ,@(map (lambda (a) `(cons ',a ,a)) list-args)))
581 (define-macro (define-primitive header . body)
582 `(define ,(car header) (primitive ,header ,@body)))
585 ;;; Primitives definition
587 (define-primitive (square size #:key texture)
588 (draw-square size #:texture texture))
590 (define-primitive (rectangle width height #:key texture texture-coord)
591 (draw-rectangle width height #:texture texture #:texture-coord texture-coord))
593 (define-primitive (circle radius)
594 (draw-circle radius))
596 (define-primitive (picture filename #:key (min-filter (texture-min-filter linear)) (mag-filter (texture-mag-filter linear)) (zoom 1) (sprite '((0 0) (1 1))))
597 (draw-texture (load-texture filename #:min-filter min-filter #:mag-filter mag-filter) #:zoom zoom #:sprite sprite))
600 (module-map (lambda (sym var)
601 (if (not (eq? sym '%module-public-interface))
602 (module-export! (current-module) (list sym))))