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