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