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