]> git.jsancho.org Git - gacela.git/blob - src/video.scm
Gacela as Guile modules.
[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 (ice-9 optargs)
23   #:use-module (ice-9 receive)
24   #:export (with-color
25             progn-textures
26             draw
27             load-image
28             resize-surface
29             load-texture
30             draw-texture
31             draw-line
32             draw-quad
33             draw-rectangle
34             draw-square
35             draw-cube
36             add-light
37             translate
38             rotate
39             to-origin
40             set-camera
41             camera-look
42             load-font
43             render-text))
44
45
46 (define get-current-color #f)
47 (define set-current-color #f)
48
49 (let ((current-color '(1 1 1 1)))
50   (set! get-current-color
51         (lambda ()
52           current-color))
53
54   (set! set-current-color
55         (lambda* (red green blue #:optional (alpha 1))
56           (set! current-color (list red green blue alpha))
57           (glColor4f red green blue alpha))))
58
59 (define-macro (with-color color . code)
60   (cond (color
61          `(let ((original-color (get-current-color))
62                 (result #f))
63             (apply set-current-color ,color)
64             (set! result (begin ,@code))
65             (apply set-current-color original-color)
66             result))
67         (else `(begin ,@code))))
68
69 (define-macro (progn-textures . code)
70   `(let ((result #f))
71      (glEnable GL_TEXTURE_2D)
72      (set! result (begin ,@code))
73      (glDisable GL_TEXTURE_2D)
74      result))
75
76 (define (draw . vertexes)
77   (begin-draw (length vertexes))
78   (draw-vertexes vertexes)
79   (glEnd))
80
81 (define (begin-draw number-of-points)
82   (cond ((= number-of-points 2) (glBegin GL_LINES))
83         ((= number-of-points 3) (glBegin GL_TRIANGLES))
84         ((= number-of-points 4) (glBegin GL_QUADS))))
85
86 (define (draw-vertexes vertexes)
87   (cond ((not (null? vertexes))
88          (draw-vertex (car vertexes))
89          (draw-vertexes (cdr vertexes)))))
90
91 (define* (draw-vertex vertex #:key texture-coord)
92   (cond ((list? (car vertex))
93          (with-color (car vertex)
94                      (apply simple-draw-vertex (cadr vertex))))
95         (else
96          (cond (texture-coord (apply glTexCoord2f texture-coord)))
97          (apply simple-draw-vertex vertex))))
98
99 (define* (simple-draw-vertex x y #:optional (z 0))
100   (cond ((3d-mode?) (glVertex3f x y z))
101         (else (glVertex2f x y))))
102
103 (define (load-image filename)
104   (let ((image (IMG_Load filename)))
105     (cond (image
106            (SDL_DisplayFormatAlpha image)))))
107   
108 (define (load-image-for-texture filename)
109   (init-sdl)
110   (let ((image (load-image filename)))
111     (cond (image
112            (let* ((width (surface-w image)) (height (surface-h image))
113                   (power-2 (nearest-power-of-two (min width height)))
114                   (resized-image #f))
115              (cond ((and (= width power-2) (= height power-2)) (values image width height))
116                    (else (set! resized-image (resize-surface image power-2 power-2))
117                          (if resized-image (values resized-image width height))))))
118           (else
119            (values #f 0 0)))))
120
121 (define (resize-surface surface width height)
122   (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
123     (cond ((and (= width old-width) (= height old-height)) surface)
124           (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
125                (zoomSurface surface zoomx zoomy 0))))))
126
127 (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
128   (let* ((key (list filename min-filter mag-filter))
129          (res (get-resource-from-cache key)))
130     (cond (res res)
131           (else
132            (progn-textures
133             (receive
134              (image real-w real-h) (load-image-for-texture filename)
135              (cond (image
136                     (let ((width (surface-w image)) (height (surface-h image))
137                           (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
138                                          (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
139                                          (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
140                           (texture (car (glGenTextures 1))))
141
142                       (glBindTexture GL_TEXTURE_2D texture)
143                       (glTexImage2D GL_TEXTURE_2D 0 4 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
144                       (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
145                       (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
146                       (set-texture-size! texture real-w real-h)
147                       (insert-resource-into-cache key texture)
148                       texture)))))))))
149
150 ;; (define* (draw-image filename #:optional (zoom 1))
151 ;;   (let ((texture (load-texture filename)))
152 ;;     (cond (texture (draw-texture texture zoom)))))
153
154 (define* (draw-texture texture #:optional (zoom 1))
155   (cond (texture
156          (let ((width (texture-w texture))
157                (height (texture-h texture)))
158            (draw-rectangle (* zoom width) (* zoom height) #:texture texture)))))
159
160 (define* (draw-line length #:optional color)
161   (let ((l (/ length 2)))
162     (cond (color
163            (with-color color (draw (list 0 l) (list 0 (- l)))))
164           (else
165            (draw (list 0 l) (list 0 (- l)))))))
166
167 (define* (draw-quad v1 v2 v3 v4 #:key texture color)
168   (cond (texture
169          (progn-textures
170           (glBindTexture GL_TEXTURE_2D texture)
171           (begin-draw 4)
172           (draw-vertex v1 #:texture-coord '(0 0))
173           (draw-vertex v2 #:texture-coord '(1 0))
174           (draw-vertex v3 #:texture-coord '(1 1))
175           (draw-vertex v4 #:texture-coord '(0 1))
176           (glEnd)))
177         (color
178          (with-color color (draw v1 v2 v3 v4)))
179         (else
180          (draw v1 v2 v3 v4))))
181
182 (define* (draw-rectangle width height #:key texture color)
183   (let ((w (/ width 2)) (h (/ height 2)))
184     (draw-quad (list (- w) h 0)
185                (list w h 0)
186                (list w (- h) 0)
187                (list (- w) (- h) 0)
188                #:texture texture
189                #:color color)))
190
191 (define* (draw-square #:key (size 1) texture color)
192   (draw-rectangle size size #:texture texture #:color color))
193
194 (define* (draw-cube #:key (size 1)
195                    texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
196                    color color-1 color-2 color-3 color-4 color-5 color-6)
197   (let ((-size (- size)))
198     (progn-textures
199      (glNormal3f 0 0 1)
200      (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))
201      (glNormal3f 0 0 -1)
202      (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))
203      (glNormal3f 0 1 0)
204      (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))
205      (glNormal3f 0 -1 0)
206      (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))
207      (glNormal3f 1 0 0)
208      (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))
209      (glNormal3f -1 0 0)
210      (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)))))
211
212 (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
213   (init-lighting)
214   (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
215   (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
216   (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
217   (and turn-on (glEnable id))
218   id)
219
220 (define* (translate x y #:optional (z 0))
221   (glTranslatef x y z))
222
223 (define* (rotate #:rest rot)
224   (cond ((3d-mode?) (apply 3d-rotate rot))
225         (else (apply 2d-rotate rot))))
226
227 (define (3d-rotate xrot yrot zrot)
228   (glRotatef xrot 1 0 0)
229   (glRotatef yrot 0 1 0)
230   (glRotatef zrot 0 0 1))
231
232 (define (2d-rotate rot)
233   (glRotatef rot 0 0 1))
234
235 (define (to-origin)
236   (glLoadIdentity)
237   (cond ((3d-mode?) (camera-look))))
238
239 (define set-camera #f)
240 (define camera-look #f)
241
242 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
243   (set! set-camera
244         (lambda* (#:key eye center up)
245           (cond (eye (set! camera-eye eye)))
246           (cond (center (set! camera-center center)))
247           (cond (up (set! camera-up up)))))
248
249   (set! camera-look
250         (lambda ()
251           (apply gluLookAt (append camera-eye camera-center camera-up)))))
252
253
254 ;;; Text and fonts
255
256 (define* (load-font font-file #:key (size 40) (encoding ft_encoding_unicode))
257   (let* ((key (list font-file))
258          (font (get-resource-from-cache key)))
259     (cond ((not font)
260            (set! font (ftglCreateTextureFont font-file))
261            (insert-resource-into-cache key font)))
262     (ftglSetFontFaceSize font size 72)
263     (ftglSetFontCharMap font encoding)
264     font))
265
266 (define* (render-text text font #:key (size #f))
267   (cond (size (ftglSetFontFaceSize font size 72)))
268   (ftglRenderFont font text FTGL_RENDER_ALL))