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