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
31 (let ((initialized #f))
34 (cond ((not initialized) (SDL_Init SDL_INIT_EVERYTHING) (set! initialized #t))
40 (set! initialized #f))))
45 (define init-video-mode #f)
46 (define video-mode-on? #f)
47 (define resize-screen #f)
48 (define quit-video-mode #f)
50 (let ((screen #f) (flags 0))
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 "")
67 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
71 (lambda () (if screen #t #f)))
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)))))
79 (lambda () (set! screen #f))))
82 (cond ((not (3d-mode?))
84 (glDisable GL_DEPTH_TEST)
85 (apply-mode-change))))
91 (glEnable GL_DEPTH_TEST)
92 (glDepthFunc GL_LEQUAL)
93 (apply-mode-change))))
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)))
101 (eq? (assoc-ref (get-game-properties) 'mode) '3d))
104 (glShadeModel GL_SMOOTH)
105 (glClearColor 0 0 0 0)
107 ; (glDepthFunc GL_LEQUAL)
108 ; (glEnable GL_BLEND)
109 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
110 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
113 (define (init-lighting)
115 (glEnable GL_LIGHTING))
117 (define (resize-screen-GL width height)
118 (glViewport 0 0 width height)
119 (glMatrixMode GL_PROJECTION)
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)
129 (define get-current-color #f)
130 (define set-current-color #f)
132 (let ((current-color '(1 1 1 1)))
133 (set! get-current-color
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))))
145 (define init-audio #f)
146 (define quit-audio #f)
151 (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
162 (define (init-gacela)
167 (define set-frames-per-second #f)
168 (define init-frame-time #f)
169 (define delay-frame #f)
171 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
172 (set! set-frames-per-second
174 (set! time-per-frame (/ 1000.0 fps))))
176 (set! init-frame-time
178 (set! time (SDL_GetTicks))))
182 (let ((frame-time (- (SDL_GetTicks) time)))
183 (cond ((< frame-time time-per-frame)
184 (SDL_Delay (- time-per-frame frame-time))))))))
187 (define set-game-properties #f)
188 (define get-game-properties #f)
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)
197 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
198 (if (or width height bpp)
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))))
207 (set-frames-per-second fps)))
212 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
213 (get-game-properties)))
215 (set! get-game-properties
217 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
220 (define-macro (run-game . code)
221 `(let ((game-function ,(if (null? code)
223 `(lambda () ,@code))))
225 (set-game-code game-function)
226 (cond ((not (game-running?))
229 (define game-loop #f)
230 (define game-running? #f)
231 (define set-game-code #f)
233 (let ((running #f) (game-code #f) (mobs '()))
236 (set! mobs (get-active-mobs))
241 ; (check-connections)
242 ; (eval-from-clients)
245 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
247 (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
248 (if (procedure? game-code) (game-code))
259 (lambda (game-function)
260 (set! game-code game-function))))