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