]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
(no commit message)
[gacela.git] / src / gacela.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 ;;; Default values for Gacela
19
20 (define *title* "Gacela")
21 (define *width-screen* 640)
22 (define *height-screen* 480)
23 (define *bpp-screen* 32)
24 (define *frames-per-second* 20)
25 (define *mode* '2d)
26
27
28 ;;; SDL Initialization Subsystem
29
30 (define init-sdl #f)
31 (define sdl-on? #f)
32 (define quit-sdl #f)
33
34 (let ((initialized #f))
35   (set! init-sdl
36         (lambda ()
37           (cond ((not initialized) (SDL_Init SDL_INIT_EVERYTHING) (set! initialized #t))
38                 (else initialized))))
39
40   (set! sdl-on?
41         (lambda ()
42           (if initialized #t #f)))
43
44   (set! quit-sdl
45         (lambda ()
46           (SDL_Quit)
47           (set! initialized #f))))
48
49
50 ;;; Video Subsystem
51
52 (define init-video-mode #f)
53 (define video-mode-on? #f)
54 (define resize-screen #f)
55 (define quit-video-mode #f)
56
57 (let ((screen #f) (flags 0))
58   (set! init-video-mode
59         (lambda ()
60           (cond ((not screen)
61                  (init-sdl)
62                  (let* ((props (get-game-properties))
63                         (width (assoc-ref props 'width)) (height (assoc-ref props 'height))
64                         (bpp (assoc-ref props 'bpp)) (title (assoc-ref props 'title))
65                         (mode (assoc-ref props 'mode))
66                         (info (SDL_GetVideoInfo)))
67                    (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
68                    (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
69                                   (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
70                                   (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
71                    (set! screen (SDL_SetVideoMode width height bpp flags))
72                    (SDL_WM_SetCaption title "")
73                    (init-gl)
74                    (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
75                 (else #t))))
76
77   (set! video-mode-on?
78         (lambda () (if screen #t #f)))
79
80   (set! resize-screen
81         (lambda* (width height #:optional (bpp current-bpp))
82           (cond (screen (set! screen (SDL_SetVideoMode width height bpp flags))
83                         (resize-screen-GL width height)))))
84
85   (set! quit-video-mode
86         (lambda ()
87           (SDL_FreeSurface screen)
88           (set! screen #f))))
89
90 (define (set-2d-mode)
91   (cond ((not (3d-mode?))
92          (init-video-mode)
93          (glDisable GL_DEPTH_TEST)
94          (apply-mode-change))))
95
96 (define (set-3d-mode)
97   (cond ((3d-mode?)
98          (init-video-mode)
99          (glClearDepth 1)
100          (glEnable GL_DEPTH_TEST)
101          (glDepthFunc GL_LEQUAL)
102          (apply-mode-change))))
103
104 (define (apply-mode-change)
105   (let* ((props (get-game-properties))
106          (width (assoc-ref props 'width)) (height (assoc-ref props 'height)))
107     (resize-screen-GL width height)))
108
109 (define (3d-mode?)
110   (eq? (assoc-ref (get-game-properties) 'mode) '3d))
111
112 (define (init-gl)
113   (glShadeModel GL_SMOOTH)
114   (glClearColor 0 0 0 0)
115 ;  (glClearDepth 1)
116 ;  (glDepthFunc GL_LEQUAL)
117 ;  (glEnable GL_BLEND)
118 ;  (glBlendFunc GL_SRC_ALPHA GL_ONE)
119   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
120   #t)
121
122 (define (init-lighting)
123   (init-video-mode)
124   (glEnable GL_LIGHTING))
125
126 (define (resize-screen-GL width height)
127   (glViewport 0 0 width height)
128   (glMatrixMode GL_PROJECTION)
129   (glLoadIdentity)
130   (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
131                       (gluPerspective 45 ratio 0.1 100))) ;0.1
132         (else (let* ((w (/ width 2)) (h (/ height 2)))
133                 (glOrtho (- w) w (- h) h 0 1))))
134   (glMatrixMode GL_MODELVIEW)
135   (glLoadIdentity)
136   #t)
137
138 (define get-current-color #f)
139 (define set-current-color #f)
140
141 (let ((current-color '(1 1 1 1)))
142   (set! get-current-color
143         (lambda ()
144           current-color))
145
146   (set! set-current-color
147         (lambda* (red green blue #:optional (alpha 1))
148           (set! current-color (list red green blue alpha))
149           (glColor4f red green blue alpha))))
150
151
152 ;;; Audio Subsystem
153
154 (define init-audio #f)
155 (define quit-audio #f)
156
157 (let ((audio #f))
158   (set! init-audio
159         (lambda ()
160           (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
161                 (else audio))))
162
163   (set! quit-audio
164         (lambda ()
165           (Mix_CloseAudio)
166           (set! audio #f))))
167
168
169 ;;; GaCeLa Functions
170
171 (define set-frames-per-second #f)
172 (define init-frame-time #f)
173 (define delay-frame #f)
174
175 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
176   (set! set-frames-per-second
177         (lambda (fps)
178           (set! time-per-frame (/ 1000.0 fps))))
179
180   (set! init-frame-time
181         (lambda ()
182           (set! time (SDL_GetTicks))))
183
184   (set! delay-frame
185         (lambda ()
186           (let ((frame-time (- (SDL_GetTicks) time)))
187             (cond ((< frame-time time-per-frame)
188                    (SDL_Delay (- time-per-frame frame-time))))))))
189
190
191 (define set-game-properties #f)
192 (define get-game-properties #f)
193
194 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
195   (set! set-game-properties
196         (lambda* (#:key title width height bpp fps mode)
197 ;         (init-video-mode)
198           (if title
199               (begin
200                 (set! ptitle title)
201                 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
202           (if (or width height bpp)
203               (begin
204                 (if width (set! pwidth width))
205                 (if height (set! pheight height))
206                 (if bpp (set! pbpp bpp))
207                 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
208           (if fps
209               (begin
210                 (set! pfps fps)
211                 (set-frames-per-second fps)))
212           (if mode
213               (begin
214                 (set! pmode mode)
215                 (if (video-mode-on?)
216                     (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
217           (get-game-properties)))
218
219   (set! get-game-properties
220         (lambda ()
221           `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
222
223
224 (define-macro (run-game . code)
225   `(let ((game-function ,(if (null? code)
226                              `(lambda () #f)
227                              `(lambda () ,@code))))
228      (init-video-mode)
229      (set-game-code game-function)
230      (cond ((not (game-running?))
231             (game-loop)))))
232
233 (define game-loop #f)
234 (define game-running? #f)
235 (define set-game-code #f)
236
237 (let ((running #f) (game-code #f) (mobs '()))
238   (set! game-loop
239         (lambda ()
240           (set! mobs (get-active-mobs))
241           (set! running #t)
242           (quit! #f)
243           (do () ((quit?))
244             (init-frame-time)
245             (check-connections)
246             (eval-from-clients)
247             (process-events)
248             (cond ((not (quit?))
249                    (cond ((video-mode-on?)
250                           (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
251                           (to-origin)))
252                    (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
253                    (if (procedure? game-code) (game-code))
254                    (run-mob-actions mobs)
255                    (cond ((video-mode-on?)
256                           (render-mobs mobs)
257                           (SDL_GL_SwapBuffers)))
258                    (delay-frame))))
259           (set! running #f)))
260
261   (set! game-running?
262         (lambda ()
263           running))
264
265   (set! set-game-code
266         (lambda (game-function)
267           (set! game-code game-function))))