]> git.jsancho.org Git - gacela.git/blob - src/video.scm
Gacela as Guile modules.
[gacela.git] / src / video.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
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)
25   #:export (init-video
26             get-screen-height
27             get-screen-width
28             get-screen-bpp
29             resize-screen
30             quit-video
31             clear-screen
32             flip-screen
33             set-2d-mode
34             set-3d-mode
35             3d-mode?
36             set-frames-per-second
37             init-frame-time
38             get-frame-time
39             delay-frame
40             get-current-color
41             set-current-color
42             with-color
43             progn-textures
44             draw
45             load-texture
46             draw-texture
47             draw-line
48             draw-quad
49             draw-rectangle
50             draw-square
51             draw-cube
52             translate
53             rotate
54             to-origin
55             add-light
56             set-camera
57             camera-look
58             load-font
59             render-text))
60
61
62 ;;; Screen
63
64 (define init-video #f)
65 (define get-screen-height #f)
66 (define get-screen-width #f)
67 (define get-screen-bpp #f)
68 (define resize-screen #f)
69 (define quit-video #f)
70
71 (let ((screen #f) (flags 0))
72   (set! init-video
73         (lambda* (width height bpp #:key (mode '2d) (title ""))
74           (cond ((not screen)
75                  (SDL_Init SDL_INIT_VIDEO)
76                  (let ((info (SDL_GetVideoInfo)))
77                    (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
78                    (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
79                                   (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
80                                   (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
81                    (set! screen (SDL_SetVideoMode width height bpp flags))
82                    (SDL_WM_SetCaption title "")
83                    (init-gl)
84                    (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))))
85
86   (set! get-screen-height
87         (lambda ()
88           (surface-h screen)))
89
90   (set! get-screen-width
91         (lambda ()
92           (surface-w screen)))
93
94   (set! get-screen-bpp
95         (lambda ()
96           (surface-format-BytesPerPixel screen)))
97
98   (set! resize-screen
99         (lambda (width height)
100           (cond (screen
101                  (set! screen (SDL_SetVideoMode width height (get-screen-bpp) flags))
102                  (resize-screen-GL width height)))))
103
104   (set! quit-video
105         (lambda ()
106           (cond (screen
107                  (SDL_FreeSurface screen)
108                  (set! screen #f)
109                  (SDL_Quit))))))
110
111 (define (clear-screen)
112   (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)))
113
114 (define (flip-screen)
115   (SDL_GL_SwapBuffers))
116
117
118 (define set-2d-mode #f)
119 (define set-3d-mode #f)
120 (define 3d-mode? #f)
121
122 (let ((mode '2d))
123   (set! set-2d-mode
124         (lambda ()
125           (set! mode '2d)
126           (glDisable GL_DEPTH_TEST)
127           (resize-screen-GL (get-screen-width) (get-screen-height))))
128
129   (set! set-3d-mode
130         (lambda ()
131           (set! mode '3d)
132           (glClearDepth 1)
133           (glEnable GL_DEPTH_TEST)
134           (glDepthFunc GL_LEQUAL)
135           (resize-screen-GL (get-screen-width) (get-screen-height))))
136
137   (set! 3d-mode?
138         (lambda ()
139           (eq? mode '3d))))
140
141
142 (define (init-gl)
143   (glShadeModel GL_SMOOTH)
144   (glClearColor 0 0 0 0)
145   (glEnable GL_BLEND)
146   (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
147   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST))
148
149 (define (resize-screen-GL width height)
150   (glViewport 0 0 width height)
151   (glMatrixMode GL_PROJECTION)
152   (glLoadIdentity)
153   (cond ((3d-mode?)
154          (let ((ratio (if (= height 0) width (/ width height))))
155            (gluPerspective 45 ratio 0.1 100)))
156         (else
157          (let* ((w (/ width 2)) (h (/ height 2)))
158            (glOrtho (- w) w (- h) h 0 1))))
159   (glMatrixMode GL_MODELVIEW)
160   (glLoadIdentity))
161
162
163 ;;; Frames per second
164
165 (define set-frames-per-second #f)
166 (define init-frame-time #f)
167 (define get-frame-time #f)
168 (define delay-frame #f)
169
170 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
171   (set! set-frames-per-second
172         (lambda (fps)
173           (set! time-per-frame (/ 1000.0 fps))))
174
175   (set! init-frame-time
176         (lambda ()
177           (set! time (SDL_GetTicks))))
178
179   (set! get-frame-time
180         (lambda ()
181           time))
182
183   (set! delay-frame
184         (lambda ()
185           (let ((frame-time (- (SDL_GetTicks) time)))
186             (cond ((< frame-time time-per-frame)
187                    (SDL_Delay (- time-per-frame frame-time))))))))
188
189
190 ;;; Drawing
191
192 (define get-current-color #f)
193 (define set-current-color #f)
194
195 (let ((current-color '(1 1 1 1)))
196   (set! get-current-color
197         (lambda ()
198           current-color))
199
200   (set! set-current-color
201         (lambda* (red green blue #:optional (alpha 1))
202           (set! current-color (list red green blue alpha))
203           (glColor4f red green blue alpha))))
204
205 (define-macro (with-color color . code)
206   (cond (color
207          `(let ((original-color (get-current-color))
208                 (result #f))
209             (apply set-current-color ,color)
210             (set! result (begin ,@code))
211             (apply set-current-color original-color)
212             result))
213         (else `(begin ,@code))))
214
215 (define-macro (progn-textures . code)
216   `(let ((result #f))
217      (glEnable GL_TEXTURE_2D)
218      (set! result (begin ,@code))
219      (glDisable GL_TEXTURE_2D)
220      result))
221
222 (define (draw . vertexes)
223   (begin-draw (length vertexes))
224   (draw-vertexes vertexes)
225   (glEnd))
226
227 (define (begin-draw number-of-points)
228   (cond ((= number-of-points 2) (glBegin GL_LINES))
229         ((= number-of-points 3) (glBegin GL_TRIANGLES))
230         ((= number-of-points 4) (glBegin GL_QUADS))))
231
232 (define (draw-vertexes vertexes)
233   (cond ((not (null? vertexes))
234          (draw-vertex (car vertexes))
235          (draw-vertexes (cdr vertexes)))))
236
237 (define* (draw-vertex vertex #:key texture-coord)
238   (cond ((list? (car vertex))
239          (with-color (car vertex)
240                      (apply simple-draw-vertex (cadr vertex))))
241         (else
242          (cond (texture-coord (apply glTexCoord2f texture-coord)))
243          (apply simple-draw-vertex vertex))))
244
245 (define* (simple-draw-vertex x y #:optional (z 0))
246   (cond ((3d-mode?) (glVertex3f x y z))
247         (else (glVertex2f x y))))
248
249 (define (load-image filename)
250   (let ((image (IMG_Load filename)))
251     (cond (image
252            (SDL_DisplayFormatAlpha image)))))
253   
254 (define (load-image-for-texture filename)
255   (let ((image (load-image filename)))
256     (cond (image
257            (let* ((width (surface-w image)) (height (surface-h image))
258                   (power-2 (nearest-power-of-two (min width height)))
259                   (resized-image #f))
260              (cond ((and (= width power-2) (= height power-2)) (values image width height))
261                    (else (set! resized-image (resize-surface image power-2 power-2))
262                          (if resized-image (values resized-image width height))))))
263           (else
264            (values #f 0 0)))))
265
266 (define (resize-surface surface width height)
267   (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
268     (cond ((and (= width old-width) (= height old-height)) surface)
269           (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
270                (zoomSurface surface zoomx zoomy 0))))))
271
272 (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
273   (progn-textures
274    (receive
275     (image real-w real-h) (load-image-for-texture filename)
276     (cond (image
277            (let ((width (surface-w image)) (height (surface-h image))
278                  (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
279                                 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
280                                 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
281                  (texture (car (glGenTextures 1))))
282
283              (glBindTexture GL_TEXTURE_2D texture)
284              (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
285              (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
286              (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
287              (set-texture-size! texture real-w real-h)
288              texture))))))
289
290 (define* (draw-texture texture #:optional (zoom 1))
291   (cond (texture
292          (let ((width (texture-w texture))
293                (height (texture-h texture)))
294            (draw-rectangle (* zoom width) (* zoom height) #:texture texture)))))
295
296 (define* (draw-line length #:optional color)
297   (let ((l (/ length 2)))
298     (cond (color
299            (with-color color (draw (list 0 l) (list 0 (- l)))))
300           (else
301            (draw (list 0 l) (list 0 (- l)))))))
302
303 (define* (draw-quad v1 v2 v3 v4 #:key texture color)
304   (cond (texture
305          (progn-textures
306           (glBindTexture GL_TEXTURE_2D texture)
307           (begin-draw 4)
308           (draw-vertex v1 #:texture-coord '(0 0))
309           (draw-vertex v2 #:texture-coord '(1 0))
310           (draw-vertex v3 #:texture-coord '(1 1))
311           (draw-vertex v4 #:texture-coord '(0 1))
312           (glEnd)))
313         (color
314          (with-color color (draw v1 v2 v3 v4)))
315         (else
316          (draw v1 v2 v3 v4))))
317
318 (define* (draw-rectangle width height #:key texture color)
319   (let ((w (/ width 2)) (h (/ height 2)))
320     (draw-quad (list (- w) h 0)
321                (list w h 0)
322                (list w (- h) 0)
323                (list (- w) (- h) 0)
324                #:texture texture
325                #:color color)))
326
327 (define* (draw-square #:key (size 1) texture color)
328   (draw-rectangle size size #:texture texture #:color color))
329
330 (define* (draw-cube #:key (size 1)
331                    texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
332                    color color-1 color-2 color-3 color-4 color-5 color-6)
333   (let ((-size (- size)))
334     (progn-textures
335      (glNormal3f 0 0 1)
336      (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))
337      (glNormal3f 0 0 -1)
338      (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))
339      (glNormal3f 0 1 0)
340      (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))
341      (glNormal3f 0 -1 0)
342      (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))
343      (glNormal3f 1 0 0)
344      (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))
345      (glNormal3f -1 0 0)
346      (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)))))
347
348 (define* (translate x y #:optional (z 0))
349   (glTranslatef x y z))
350
351 (define* (rotate #:rest rot)
352   (cond ((3d-mode?)
353          (apply 3d-rotate rot))
354         (else
355          (apply 2d-rotate rot))))
356
357 (define (3d-rotate xrot yrot zrot)
358   (glRotatef xrot 1 0 0)
359   (glRotatef yrot 0 1 0)
360   (glRotatef zrot 0 0 1))
361
362 (define (2d-rotate rot)
363   (glRotatef rot 0 0 1))
364
365 (define (to-origin)
366   (glLoadIdentity)
367   (cond ((3d-mode?) (camera-look))))
368
369
370 ;;; Lights
371
372 (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
373   (init-lighting)
374   (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
375   (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
376   (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
377   (and turn-on (glEnable id))
378   id)
379
380
381 ;;; Camera
382
383 (define set-camera #f)
384 (define camera-look #f)
385
386 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
387   (set! set-camera
388         (lambda* (#:key eye center up)
389           (cond (eye (set! camera-eye eye)))
390           (cond (center (set! camera-center center)))
391           (cond (up (set! camera-up up)))))
392
393   (set! camera-look
394         (lambda ()
395           (apply gluLookAt (append camera-eye camera-center camera-up)))))
396
397
398 ;;; Text and fonts
399
400 (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
401   (let ((font (ftglCreateTextureFont font-file)))
402     (ftglSetFontFaceSize font size 72)
403     (ftglSetFontCharMap font encoding)
404     font))
405
406 (define* (render-text text font #:key (size #f))
407   (cond (size (ftglSetFontFaceSize font size 72)))
408   (ftglRenderFont font text FTGL_RENDER_ALL))