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