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 (ice-9 optargs)
24 #:use-module (ice-9 receive)
67 (define (init-video width height bpp #:key (mode '2d) (title ""))
69 (SDL_Init SDL_INIT_VIDEO)
70 (let ((info (SDL_GetVideoInfo)))
71 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
72 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
73 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
74 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
75 (set! screen (SDL_SetVideoMode width height bpp flags))
76 (SDL_WM_SetCaption title "")
78 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))))
80 (define (get-screen-height)
83 (define (get-screen-width)
86 (define (get-screen-bpp)
87 (surface-format-BytesPerPixel screen))
89 (define (resize-screen width height)
91 (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
92 (resize-screen-GL width height))))
96 (SDL_FreeSurface screen)
100 (define (clear-screen)
101 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
103 (define (flip-screen)
104 (SDL_GL_SwapBuffers))
109 (define (set-2d-mode)
111 (glDisable GL_DEPTH_TEST)
112 (resize-screen-GL (get-screen-width) (get-screen-height)))
114 (define (set-3d-mode)
117 (glEnable GL_DEPTH_TEST)
118 (glDepthFunc GL_LEQUAL)
119 (resize-screen-GL (get-screen-width) (get-screen-height)))
126 (glShadeModel GL_SMOOTH)
127 (glClearColor 0 0 0 0)
129 (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
130 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
132 (define (resize-screen-GL width height)
133 (glViewport 0 0 width height)
134 (glMatrixMode GL_PROJECTION)
137 (let ((ratio (if (= height 0) width (/ width height))))
138 (gluPerspective 45 ratio 0.1 100)))
140 (let* ((w (/ width 2)) (h (/ height 2)))
141 (glOrtho (- w) w (- h) h 0 1))))
142 (glMatrixMode GL_MODELVIEW)
146 ;;; Frames per second
148 (define set-frames-per-second #f)
149 (define init-frame-time #f)
150 (define get-frame-time #f)
151 (define delay-frame #f)
153 ;; (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
154 ;; (set! set-frames-per-second
156 ;; (set! time-per-frame (/ 1000.0 fps))))
158 ;; (set! init-frame-time
160 ;; (set! time (SDL_GetTicks))))
162 ;; (set! get-frame-time
168 ;; (let ((frame-time (- (SDL_GetTicks) time)))
169 ;; (cond ((< frame-time time-per-frame)
170 ;; (SDL_Delay (- time-per-frame frame-time))))))))
175 (define current-color '(1 1 1 1))
177 (define (get-current-color)
180 (define* (set-current-color red green blue #:optional (alpha 1))
181 (set! current-color (list red green blue alpha))
182 (glColor4f red green blue alpha))
184 (define-macro (with-color color . code)
186 `(let ((original-color (get-current-color))
188 (apply set-current-color ,color)
189 (set! result (begin ,@code))
190 (apply set-current-color original-color)
192 (else `(begin ,@code))))
194 (define-macro (progn-textures . code)
196 (glEnable GL_TEXTURE_2D)
197 (set! result (begin ,@code))
198 (glDisable GL_TEXTURE_2D)
201 (define (draw . vertexes)
202 (begin-draw (length vertexes))
203 (draw-vertexes vertexes)
206 (define (begin-draw number-of-points)
207 (cond ((= number-of-points 2) (glBegin GL_LINES))
208 ((= number-of-points 3) (glBegin GL_TRIANGLES))
209 ((= number-of-points 4) (glBegin GL_QUADS))))
211 (define (draw-vertexes vertexes)
212 (cond ((not (null? vertexes))
213 (draw-vertex (car vertexes))
214 (draw-vertexes (cdr vertexes)))))
216 (define* (draw-vertex vertex #:key texture-coord)
217 (cond ((list? (car vertex))
218 (with-color (car vertex)
219 (apply simple-draw-vertex (cadr vertex))))
221 (cond (texture-coord (apply glTexCoord2f texture-coord)))
222 (apply simple-draw-vertex vertex))))
224 (define* (simple-draw-vertex x y #:optional (z 0))
225 (cond ((3d-mode?) (glVertex3f x y z))
226 (else (glVertex2f x y))))
228 (define (load-image filename)
229 (let ((image (IMG_Load filename)))
231 (SDL_DisplayFormatAlpha image)))))
233 (define (load-image-for-texture filename)
234 (let ((image (load-image filename)))
236 (let* ((width (surface-w image)) (height (surface-h image))
237 (power-2 (nearest-power-of-two (min width height)))
239 (cond ((and (= width power-2) (= height power-2)) (values image width height))
240 (else (set! resized-image (resize-surface image power-2 power-2))
241 (if resized-image (values resized-image width height))))))
245 (define (resize-surface surface width height)
246 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
247 (cond ((and (= width old-width) (= height old-height)) surface)
248 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
249 (zoomSurface surface zoomx zoomy 0))))))
251 (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
254 (image real-w real-h) (load-image-for-texture filename)
256 (let ((width (surface-w image)) (height (surface-h image))
257 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
258 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
259 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
260 (texture (car (glGenTextures 1))))
262 (glBindTexture GL_TEXTURE_2D texture)
263 (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
264 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
265 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
266 (set-texture-size! texture real-w real-h)
269 (define* (draw-texture texture #:optional (zoom 1))
271 (let ((width (texture-w texture))
272 (height (texture-h texture)))
273 (draw-rectangle (* zoom width) (* zoom height) #:texture texture)))))
275 (define* (draw-line length #:optional color)
276 (let ((l (/ length 2)))
278 (with-color color (draw (list 0 l) (list 0 (- l)))))
280 (draw (list 0 l) (list 0 (- l)))))))
282 (define* (draw-quad v1 v2 v3 v4 #:key texture color)
285 (glBindTexture GL_TEXTURE_2D texture)
287 (draw-vertex v1 #:texture-coord '(0 0))
288 (draw-vertex v2 #:texture-coord '(1 0))
289 (draw-vertex v3 #:texture-coord '(1 1))
290 (draw-vertex v4 #:texture-coord '(0 1))
293 (with-color color (draw v1 v2 v3 v4)))
295 (draw v1 v2 v3 v4))))
297 (define* (draw-rectangle width height #:key texture color)
298 (let ((w (/ width 2)) (h (/ height 2)))
299 (draw-quad (list (- w) h 0)
306 (define* (draw-square #:key (size 1) texture color)
307 (draw-rectangle size size #:texture texture #:color color))
309 (define* (draw-cube #:key (size 1)
310 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
311 color color-1 color-2 color-3 color-4 color-5 color-6)
312 (let ((-size (- size)))
315 (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))
317 (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))
319 (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))
321 (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))
323 (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))
325 (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)))))
327 (define* (translate x y #:optional (z 0))
328 (glTranslatef x y z))
330 (define* (rotate #:rest rot)
332 (apply 3d-rotate rot))
334 (apply 2d-rotate rot))))
336 (define (3d-rotate xrot yrot zrot)
337 (glRotatef xrot 1 0 0)
338 (glRotatef yrot 0 1 0)
339 (glRotatef zrot 0 0 1))
341 (define (2d-rotate rot)
342 (glRotatef rot 0 0 1))
346 (cond ((3d-mode?) (camera-look))))
351 (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
353 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
354 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
355 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
356 (and turn-on (glEnable id))
362 (define camera-eye '(0 0 0))
363 (define camera-center '(0 0 -100))
364 (define camera-up '(0 1 0))
366 (define* (set-camera #:key eye center up)
367 (cond (eye (set! camera-eye eye)))
368 (cond (center (set! camera-center center)))
369 (cond (up (set! camera-up up))))
371 (define (camera-look)
372 (apply gluLookAt (append camera-eye camera-center camera-up)))
377 (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
378 (let ((font (ftglCreateTextureFont font-file)))
379 (ftglSetFontFaceSize font size 72)
380 (ftglSetFontCharMap font encoding)
383 (define* (render-text text font #:key (size #f))
384 (cond (size (ftglSetFontFaceSize font size 72)))
385 (ftglRenderFont font text FTGL_RENDER_ALL))