1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
18 ;;; Default values for Gacela
20 (define *width-screen* 640)
21 (define *height-screen* 480)
22 (define *bpp-screen* 32)
23 (define *frames-per-second* 20)
26 ;;; SDL Initialization Subsystem
32 (let ((initialized #f))
35 (cond ((not initialized) (SDL_Init SDL_INIT_EVERYTHING) (set! initialized #t))
40 (if initialized #t #f)))
45 (set! initialized #f))))
50 (define init-video-mode #f)
51 (define video-mode-on? #f)
52 (define resize-screen #f)
53 (define quit-video-mode #f)
55 (let ((screen #f) (flags 0))
60 (let* ((props (get-game-properties))
61 (width (assoc-ref props 'width)) (height (assoc-ref props 'height))
62 (bpp (assoc-ref props 'bpp)) (title (assoc-ref props 'title))
63 (mode (assoc-ref props 'mode))
64 (info (SDL_GetVideoInfo)))
65 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
66 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
67 (if (= (assoc-ref info 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
68 (if (= (assoc-ref info 'blit_hw) 0) 0 SDL_HWACCEL)))
69 (set! screen (SDL_SetVideoMode width height bpp flags))
70 (SDL_WM_SetCaption title "")
72 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
76 (lambda () (if screen #t #f)))
79 (lambda* (width height #:optional (bpp current-bpp))
80 (cond (screen (set! screen (SDL_SetVideoMode width height bpp flags))
81 (resize-screen-GL width height)))))
84 (lambda () (set! screen #f))))
87 (cond ((not (3d-mode?))
89 (glDisable GL_DEPTH_TEST)
90 (apply-mode-change))))
96 (glEnable GL_DEPTH_TEST)
97 (glDepthFunc GL_LEQUAL)
98 (apply-mode-change))))
100 (define (apply-mode-change)
101 (let* ((props (get-game-properties))
102 (width (assoc-ref props 'width)) (height (assoc-ref props 'height)))
103 (resize-screen-GL width height)))
106 (eq? (assoc-ref (get-game-properties) 'mode) '3d))
109 (glShadeModel GL_SMOOTH)
110 (glClearColor 0 0 0 0)
112 ; (glDepthFunc GL_LEQUAL)
113 ; (glEnable GL_BLEND)
114 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
115 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
118 (define (init-lighting)
120 (glEnable GL_LIGHTING))
122 (define (resize-screen-GL width height)
123 (glViewport 0 0 width height)
124 (glMatrixMode GL_PROJECTION)
126 (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
127 (gluPerspective 45 ratio 0.1 100))) ;0.1
128 (else (let* ((w (/ width 2)) (h (/ height 2)))
129 (glOrtho (- w) w (- h) h 0 1))))
130 (glMatrixMode GL_MODELVIEW)
134 (define get-current-color #f)
135 (define set-current-color #f)
137 (let ((current-color '(1 1 1 1)))
138 (set! get-current-color
142 (set! set-current-color
143 (lambda* (red green blue #:optional (alpha 1))
144 (set! current-color (list red green blue alpha))
145 (glColor4f red green blue alpha))))
150 (define init-audio #f)
151 (define quit-audio #f)
156 (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
167 (define (init-gacela)
172 (define set-frames-per-second #f)
173 (define init-frame-time #f)
174 (define delay-frame #f)
176 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
177 (set! set-frames-per-second
179 (set! time-per-frame (/ 1000.0 fps))))
181 (set! init-frame-time
183 (set! time (SDL_GetTicks))))
187 (let ((frame-time (- (SDL_GetTicks) time)))
188 (cond ((< frame-time time-per-frame)
189 (SDL_Delay (- time-per-frame frame-time))))))))
192 (define set-game-properties #f)
193 (define get-game-properties #f)
195 (let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
196 (set! set-game-properties
197 (lambda* (#:key title width height bpp fps mode)
202 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
203 (if (or width height bpp)
205 (if width (set! pwidth width))
206 (if height (set! pheight height))
207 (if bpp (set! pbpp bpp))
208 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
212 (set-frames-per-second fps)))
217 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
218 (get-game-properties)))
220 (set! get-game-properties
222 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
225 (define-macro (run-game . code)
226 `(let ((game-function ,(if (null? code)
228 `(lambda () ,@code))))
230 (set-game-code game-function)
231 (cond ((not (game-running?))
234 (define game-loop #f)
235 (define game-running? #f)
236 (define set-game-code #f)
238 (let ((running #f) (game-code #f) (mobs '()))
241 (set! mobs (get-active-mobs))
250 (cond ((video-mode-on?)
251 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
253 (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
254 (if (procedure? game-code) (game-code))
255 (run-mob-actions mobs)
256 (cond ((video-mode-on?)
258 (SDL_GL_SwapBuffers)))
267 (lambda (game-function)
268 (set! game-code game-function))))