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