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!
51 load-texture-without-cache
52 get-texture-properties
66 load-font-without-texture
68 #:export-syntax (glmatrix-block))
77 (define* (init-video width height bpp #:key (mode '2d) (title "") (fps 20))
78 (SDL_Init SDL_INIT_VIDEO)
79 (let ((info (SDL_GetVideoInfo)))
80 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
81 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
82 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
83 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
84 (set! screen (SDL_SetVideoMode width height bpp flags))
85 (set-screen-title! title)
86 (set-frames-per-second! fps)
88 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
90 (define (get-screen-height)
93 (define (get-screen-width)
96 (define (get-screen-bpp)
97 (* (surface-format-BytesPerPixel screen) 8))
99 (define (set-screen-bpp! bpp)
101 (set! screen (SDL_SetVideoMode (get-screen-width) (get-screen-height) bpp flags)))))
103 (define (resize-screen width height)
105 (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
106 (resize-screen-GL width height))))
110 (SDL_FreeSurface screen)
114 (define (clear-screen)
115 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
117 (define (flip-screen)
118 (SDL_GL_SwapBuffers))
121 (define screen-title "")
123 (define (set-screen-title! title)
124 (set! screen-title title)
125 (SDL_WM_SetCaption title ""))
127 (define (get-screen-title)
133 (define (set-2d-mode)
135 (glDisable GL_DEPTH_TEST)
136 (resize-screen-GL (get-screen-width) (get-screen-height)))
138 (define (set-3d-mode)
141 (glEnable GL_DEPTH_TEST)
142 (glDepthFunc GL_LEQUAL)
143 (resize-screen-GL (get-screen-width) (get-screen-height)))
150 (glShadeModel GL_SMOOTH)
151 (glClearColor 0 0 0 0)
153 (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
154 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
156 (define (resize-screen-GL width height)
157 (glViewport 0 0 width height)
158 (glMatrixMode GL_PROJECTION)
161 (let ((ratio (if (= height 0) width (/ width height))))
162 (gluPerspective 45 ratio 0.1 100)))
164 (let* ((w (/ width 2)) (h (/ height 2)))
165 (glOrtho (- w) w (- h) h 0 1))))
166 (glMatrixMode GL_MODELVIEW)
170 ;;; Frames per second
173 (define frames-per-second 20)
174 (define time-per-frame 50) ;in ms
176 (define (get-frames-per-second)
179 (define (set-frames-per-second! fps)
180 (set! frames-per-second fps)
181 (set! time-per-frame (/ 1000.0 fps)))
183 (define (init-frame-time)
184 (set! time (SDL_GetTicks)))
186 (define (get-frame-time)
189 (define (delay-frame)
190 (let ((frame-time (- (SDL_GetTicks) time)))
191 (cond ((< frame-time time-per-frame)
192 (SDL_Delay (- time-per-frame frame-time))))))
197 (define current-color '(1 1 1 1))
199 (define (get-current-color)
202 (define* (set-current-color red green blue #:optional (alpha 1))
203 (set! current-color (list red green blue alpha))
204 (glColor4f red green blue alpha))
206 (define-macro (with-color color . code)
208 `(let ((original-color (get-current-color))
210 (apply set-current-color ,color)
211 (set! result (begin ,@code))
212 (apply set-current-color original-color)
214 (else `(begin ,@code))))
216 (define-macro (progn-textures . code)
218 (glEnable GL_TEXTURE_2D)
219 (set! result (begin ,@code))
220 (glDisable GL_TEXTURE_2D)
223 (define (draw . vertexes)
224 (begin-draw (length vertexes))
225 (draw-vertexes vertexes)
228 (define (begin-draw number-of-points)
229 (cond ((= number-of-points 2) (glBegin GL_LINES))
230 ((= number-of-points 3) (glBegin GL_TRIANGLES))
231 ((= number-of-points 4) (glBegin GL_QUADS))))
233 (define (draw-vertexes vertexes)
234 (cond ((not (null? vertexes))
235 (draw-vertex (car vertexes))
236 (draw-vertexes (cdr vertexes)))))
238 (define* (draw-vertex vertex #:key texture-coord)
239 (cond ((list? (car vertex))
240 (with-color (car vertex)
241 (apply simple-draw-vertex (cadr vertex))))
243 (cond (texture-coord (apply glTexCoord2f texture-coord)))
244 (apply simple-draw-vertex vertex))))
246 (define* (simple-draw-vertex x y #:optional (z 0))
247 (cond ((3d-mode?) (glVertex3f x y z))
248 (else (glVertex2f x y))))
250 (define (load-image filename)
251 (let ((image (IMG_Load filename)))
253 (SDL_DisplayFormatAlpha image)))))
255 (define (load-image-for-texture filename)
256 (let ((image (load-image filename)))
258 (let* ((width (surface-w image)) (height (surface-h image))
259 (power-2 (nearest-power-of-two (min width height)))
261 (cond ((and (= width power-2) (= height power-2)) (values image width height))
262 (else (set! resized-image (resize-surface image power-2 power-2))
263 (if resized-image (values resized-image width height))))))
267 (define (resize-surface surface width height)
268 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
269 (cond ((and (= width old-width) (= height old-height)) surface)
270 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
271 (zoomSurface surface zoomx zoomy 0))))))
273 (define* (load-texture-without-cache filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
276 (image real-w real-h) (load-image-for-texture filename)
278 (let ((width (surface-w image)) (height (surface-h image))
279 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
280 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
281 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
282 (texture (car (glGenTextures 1))))
284 (glBindTexture GL_TEXTURE_2D texture)
285 (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
286 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
287 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
288 (set-texture-size! texture real-w real-h)
291 (define load-texture (use-cache-with load-texture-without-cache))
293 (define (get-texture-properties texture)
294 `((width . ,(texture-w texture)) (height . ,(texture-h texture))))
296 (define* (draw-texture texture #:key (zoom 1) (sprite '((0 0) (1 1))))
298 (let ((width (texture-w texture))
299 (height (texture-h texture)))
300 (draw-rectangle (* zoom width (- (caadr sprite) (caar sprite)))
301 (* zoom height (- (cadadr sprite) (cadar sprite)))
303 #:texture-coord sprite)))))
305 (define* (draw-line length #:optional color)
306 (let ((l (/ length 2)))
308 (with-color color (draw (list 0 l) (list 0 (- l)))))
310 (draw (list 0 l) (list 0 (- l)))))))
312 (define* (draw-quad v1 v2 v3 v4 #:key texture color (texture-coord '((0 0) (1 1))))
315 (glBindTexture GL_TEXTURE_2D texture)
317 (draw-vertex v1 #:texture-coord (car texture-coord))
318 (draw-vertex v2 #:texture-coord (list (caadr texture-coord) (cadar texture-coord)))
319 (draw-vertex v3 #:texture-coord (cadr texture-coord))
320 (draw-vertex v4 #:texture-coord (list (caar texture-coord) (cadadr texture-coord)))
323 (with-color color (draw v1 v2 v3 v4)))
325 (draw v1 v2 v3 v4))))
327 (define* (draw-rectangle width height #:key texture color texture-coord)
328 (let ((w (/ width 2)) (h (/ height 2)))
329 (draw-quad (list (- w) h 0)
334 #:texture-coord texture-coord
337 (define* (draw-square #:key (size 1) texture color)
338 (draw-rectangle size size #:texture texture #:color color))
340 (define* (draw-cube #:key (size 1)
341 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
342 color color-1 color-2 color-3 color-4 color-5 color-6)
343 (let ((-size (- size)))
346 (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))
348 (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))
350 (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))
352 (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))
354 (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))
356 (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)))))
358 (define* (translate x y #:optional (z 0))
359 (glTranslatef x y z))
361 (define* (rotate #:rest rot)
363 (apply 3d-rotate rot))
365 (apply 2d-rotate rot))))
367 (define (3d-rotate xrot yrot zrot)
368 (glRotatef xrot 1 0 0)
369 (glRotatef yrot 0 1 0)
370 (glRotatef zrot 0 0 1))
372 (define (2d-rotate rot)
373 (glRotatef rot 0 0 1))
377 (cond ((3d-mode?) (camera-look))))
379 (define-macro (glmatrix-block . code)
382 (set! result (begin ,@code))
389 ;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
391 ;; (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
392 ;; (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
393 ;; (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
394 ;; (and turn-on (glEnable id))
400 (define camera-eye '(0 0 0))
401 (define camera-center '(0 0 -100))
402 (define camera-up '(0 1 0))
404 (define* (set-camera #:key eye center up)
405 (cond (eye (set! camera-eye eye)))
406 (cond (center (set! camera-center center)))
407 (cond (up (set! camera-up up))))
409 (define (camera-look)
410 (apply gluLookAt (append camera-eye camera-center camera-up)))
415 (define* (load-font-without-cache font-file #:key (size 40) (encoding ft_encoding_unicode))
416 (let ((font (ftglCreateTextureFont font-file size)))
417 (ftglSetFontFaceSize font size 72)
418 (ftglSetFontCharMap font encoding)
421 (define load-font (use-cache-with load-font-without-cache))
423 (define* (render-text text font #:key (size #f))
425 (cond ((not (= (ftglGetFontFaceSize font) size))
426 (ftglSetFontFaceSize font size 72))))
427 ((not (= (ftglGetFontFaceSize font) (font-size font)))
428 (ftglSetFontFaceSize font (font-size font) 72)))
429 (ftglRenderFont font text FTGL_RENDER_ALL))