]> 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   (glBlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
120   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
121   #t)
122
123 (define (init-lighting)
124   (init-video-mode)
125   (glEnable GL_LIGHTING))
126
127 (define (resize-screen-GL width height)
128   (glViewport 0 0 width height)
129   (glMatrixMode GL_PROJECTION)
130   (glLoadIdentity)
131   (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
132                       (gluPerspective 45 ratio 0.1 100))) ;0.1
133         (else (let* ((w (/ width 2)) (h (/ height 2)))
134                 (glOrtho (- w) w (- h) h 0 1))))
135   (glMatrixMode GL_MODELVIEW)
136   (glLoadIdentity)
137   #t)
138
139 (define get-current-color #f)
140 (define set-current-color #f)
141
142 (let ((current-color '(1 1 1 1)))
143   (set! get-current-color
144         (lambda ()
145           current-color))
146
147   (set! set-current-color
148         (lambda* (red green blue #:optional (alpha 1))
149           (set! current-color (list red green blue alpha))
150           (glColor4f red green blue alpha))))
151
152
153 ;;; Audio Subsystem
154
155 (define init-audio #f)
156 (define quit-audio #f)
157
158 (let ((audio #f))
159   (set! init-audio
160         (lambda ()
161           (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
162                 (else audio))))
163
164   (set! quit-audio
165         (lambda ()
166           (Mix_CloseAudio)
167           (set! audio #f))))
168
169
170 ;;; Resources Cache
171
172 (define resources-cache (make-weak-value-hash-table))
173
174
175 ;;; GaCeLa Functions
176
177 (define set-frames-per-second #f)
178 (define init-frame-time #f)
179 (define delay-frame #f)
180
181 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
182   (set! set-frames-per-second
183         (lambda (fps)
184           (set! time-per-frame (/ 1000.0 fps))))
185
186   (set! init-frame-time
187         (lambda ()
188           (set! time (SDL_GetTicks))))
189
190   (set! delay-frame
191         (lambda ()
192           (let ((frame-time (- (SDL_GetTicks) time)))
193             (cond ((< frame-time time-per-frame)
194                    (SDL_Delay (- time-per-frame frame-time))))))))
195
196
197 (define set-game-properties! #f)
198 (define get-game-properties #f)
199
200 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
201   (set! set-game-properties!
202         (lambda* (#:key title width height bpp fps mode)
203 ;         (init-video-mode)
204           (if title
205               (begin
206                 (set! ptitle title)
207                 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
208           (if (or width height bpp)
209               (begin
210                 (if width (set! pwidth width))
211                 (if height (set! pheight height))
212                 (if bpp (set! pbpp bpp))
213                 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
214           (if fps
215               (begin
216                 (set! pfps fps)
217                 (set-frames-per-second fps)))
218           (if mode
219               (begin
220                 (set! pmode mode)
221                 (if (video-mode-on?)
222                     (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
223           (get-game-properties)))
224
225   (set! get-game-properties
226         (lambda ()
227           `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
228
229
230 (define-macro (run-game . code)
231   `(let ((game-function ,(if (null? code)
232                              `(lambda () #f)
233                              `(lambda () ,@code))))
234      (init-video-mode)
235      (set-game-code game-function)
236      (cond ((not (game-running?))
237             (game-loop)))))
238
239 (define game-loop #f)
240 (define game-running? #f)
241 (define set-game-code #f)
242
243 (let ((running #f) (game-code #f) (mobs '()))
244   (set! game-loop
245         (lambda ()
246           (set! mobs (get-active-mobs))
247           (set! running #t)
248           (quit! #f)
249           (do () ((quit?))
250             (init-frame-time)
251             (check-connections)
252             (eval-from-clients)
253             (process-events)
254             (cond ((not (quit?))
255                    (cond ((video-mode-on?)
256                           (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
257                           (to-origin)))
258                    (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
259                    (if (procedure? game-code)
260                        (catch #t
261                               (lambda () (game-code))
262                               (lambda (key . args) #f)))
263                    (cond ((video-mode-on?)
264                           (run-mobs mobs)
265                           (SDL_GL_SwapBuffers)))
266                    (delay-frame))))
267           (set! running #f)))
268
269   (set! game-running?
270         (lambda ()
271           running))
272
273   (set! set-game-code
274         (lambda (game-function)
275           (set! game-code game-function))))