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)
70 (define* (init-video width height bpp #:key (mode '2d) (title ""))
72 (SDL_Init SDL_INIT_VIDEO)
73 (let ((info (SDL_GetVideoInfo)))
74 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
75 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
76 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
77 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
78 (set! screen (SDL_SetVideoMode width height bpp flags))
79 (set-screen-title! title)
81 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))))
83 (define (get-screen-height)
86 (define (get-screen-width)
89 (define (get-screen-bpp)
90 (surface-format-BytesPerPixel screen))
92 (define (set-screen-bpp! bpp)
94 (set! screen (SDL_SetVideoMode (get-screen-width) (get-screen-height) bpp flags)))))
96 (define (resize-screen width height)
98 (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
99 (resize-screen-GL width height))))
103 (SDL_FreeSurface screen)
107 (define (clear-screen)
108 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
110 (define (flip-screen)
111 (SDL_GL_SwapBuffers))
114 (define screen-title "")
116 (define (set-screen-title! title)
117 (set! screen-title title)
118 (SDL_WM_SetCaption title ""))
120 (define (get-screen-title)
126 (define (set-2d-mode)
128 (glDisable GL_DEPTH_TEST)
129 (resize-screen-GL (get-screen-width) (get-screen-height)))
131 (define (set-3d-mode)
134 (glEnable GL_DEPTH_TEST)
135 (glDepthFunc GL_LEQUAL)
136 (resize-screen-GL (get-screen-width) (get-screen-height)))
143 (glShadeModel GL_SMOOTH)
144 (glClearColor 0 0 0 0)
146 (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
147 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
149 (define (resize-screen-GL width height)
150 (glViewport 0 0 width height)
151 (glMatrixMode GL_PROJECTION)
154 (let ((ratio (if (= height 0) width (/ width height))))
155 (gluPerspective 45 ratio 0.1 100)))
157 (let* ((w (/ width 2)) (h (/ height 2)))
158 (glOrtho (- w) w (- h) h 0 1))))
159 (glMatrixMode GL_MODELVIEW)
163 ;;; Frames per second
166 (define time-per-frame 50) ;in ms
168 (define (set-frames-per-second fps)
169 (set! time-per-frame (/ 1000.0 fps)))
171 (define (init-frame-time)
172 (set! time (SDL_GetTicks)))
174 (define (get-frame-time)
177 (define (delay-frame)
178 (let ((frame-time (- (SDL_GetTicks) time)))
179 (cond ((< frame-time time-per-frame)
180 (SDL_Delay (- time-per-frame frame-time))))))
185 (define current-color '(1 1 1 1))
187 (define (get-current-color)
190 (define* (set-current-color red green blue #:optional (alpha 1))
191 (set! current-color (list red green blue alpha))
192 (glColor4f red green blue alpha))
194 (define-macro (with-color color . code)
196 `(let ((original-color (get-current-color))
198 (apply set-current-color ,color)
199 (set! result (begin ,@code))
200 (apply set-current-color original-color)
202 (else `(begin ,@code))))
204 (define-macro (progn-textures . code)
206 (glEnable GL_TEXTURE_2D)
207 (set! result (begin ,@code))
208 (glDisable GL_TEXTURE_2D)
211 (define (draw . vertexes)
212 (begin-draw (length vertexes))
213 (draw-vertexes vertexes)
216 (define (begin-draw number-of-points)
217 (cond ((= number-of-points 2) (glBegin GL_LINES))
218 ((= number-of-points 3) (glBegin GL_TRIANGLES))
219 ((= number-of-points 4) (glBegin GL_QUADS))))
221 (define (draw-vertexes vertexes)
222 (cond ((not (null? vertexes))
223 (draw-vertex (car vertexes))
224 (draw-vertexes (cdr vertexes)))))
226 (define* (draw-vertex vertex #:key texture-coord)
227 (cond ((list? (car vertex))
228 (with-color (car vertex)
229 (apply simple-draw-vertex (cadr vertex))))
231 (cond (texture-coord (apply glTexCoord2f texture-coord)))
232 (apply simple-draw-vertex vertex))))
234 (define* (simple-draw-vertex x y #:optional (z 0))
235 (cond ((3d-mode?) (glVertex3f x y z))
236 (else (glVertex2f x y))))
238 (define (load-image filename)
239 (let ((image (IMG_Load filename)))
241 (SDL_DisplayFormatAlpha image)))))
243 (define (load-image-for-texture filename)
244 (let ((image (load-image filename)))
246 (let* ((width (surface-w image)) (height (surface-h image))
247 (power-2 (nearest-power-of-two (min width height)))
249 (cond ((and (= width power-2) (= height power-2)) (values image width height))
250 (else (set! resized-image (resize-surface image power-2 power-2))
251 (if resized-image (values resized-image width height))))))
255 (define (resize-surface surface width height)
256 (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
257 (cond ((and (= width old-width) (= height old-height)) surface)
258 (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
259 (zoomSurface surface zoomx zoomy 0))))))
261 (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
264 (image real-w real-h) (load-image-for-texture filename)
266 (let ((width (surface-w image)) (height (surface-h image))
267 (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
268 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
269 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
270 (texture (car (glGenTextures 1))))
272 (glBindTexture GL_TEXTURE_2D texture)
273 (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
274 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
275 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
276 (set-texture-size! texture real-w real-h)
279 (define* (draw-texture texture #:optional (zoom 1))
281 (let ((width (texture-w texture))
282 (height (texture-h texture)))
283 (draw-rectangle (* zoom width) (* zoom height) #:texture texture)))))
285 (define* (draw-line length #:optional color)
286 (let ((l (/ length 2)))
288 (with-color color (draw (list 0 l) (list 0 (- l)))))
290 (draw (list 0 l) (list 0 (- l)))))))
292 (define* (draw-quad v1 v2 v3 v4 #:key texture color)
295 (glBindTexture GL_TEXTURE_2D texture)
297 (draw-vertex v1 #:texture-coord '(0 0))
298 (draw-vertex v2 #:texture-coord '(1 0))
299 (draw-vertex v3 #:texture-coord '(1 1))
300 (draw-vertex v4 #:texture-coord '(0 1))
303 (with-color color (draw v1 v2 v3 v4)))
305 (draw v1 v2 v3 v4))))
307 (define* (draw-rectangle width height #:key texture color)
308 (let ((w (/ width 2)) (h (/ height 2)))
309 (draw-quad (list (- w) h 0)
316 (define* (draw-square #:key (size 1) texture color)
317 (draw-rectangle size size #:texture texture #:color color))
319 (define* (draw-cube #:key (size 1)
320 texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
321 color color-1 color-2 color-3 color-4 color-5 color-6)
322 (let ((-size (- size)))
325 (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))
327 (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))
329 (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))
331 (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))
333 (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))
335 (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)))))
337 (define* (translate x y #:optional (z 0))
338 (glTranslatef x y z))
340 (define* (rotate #:rest rot)
342 (apply 3d-rotate rot))
344 (apply 2d-rotate rot))))
346 (define (3d-rotate xrot yrot zrot)
347 (glRotatef xrot 1 0 0)
348 (glRotatef yrot 0 1 0)
349 (glRotatef zrot 0 0 1))
351 (define (2d-rotate rot)
352 (glRotatef rot 0 0 1))
356 (cond ((3d-mode?) (camera-look))))
361 ;; (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
363 ;; (and light (glLightfv id GL_DIFFUSE (car light) (cadr light) (caddr light) (cadddr light)))
364 ;; (and light position (glLightfv GL_POSITION (car position) (cadr position) (caddr position) (cadddr position)))
365 ;; (and ambient (glLightfv id GL_AMBIENT (car ambient) (cadr ambient) (caddr ambient) (cadddr ambient)))
366 ;; (and turn-on (glEnable id))
372 (define camera-eye '(0 0 0))
373 (define camera-center '(0 0 -100))
374 (define camera-up '(0 1 0))
376 (define* (set-camera #:key eye center up)
377 (cond (eye (set! camera-eye eye)))
378 (cond (center (set! camera-center center)))
379 (cond (up (set! camera-up up))))
381 (define (camera-look)
382 (apply gluLookAt (append camera-eye camera-center camera-up)))
387 (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
388 (let ((font (ftglCreateTextureFont font-file)))
389 (ftglSetFontFaceSize font size 72)
390 (ftglSetFontCharMap font encoding)
393 (define* (render-text text font #:key (size #f))
394 (cond (size (ftglSetFontFaceSize font size 72)))
395 (ftglRenderFont font text FTGL_RENDER_ALL))