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