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