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