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