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 *title* "Gacela")
21 (define *width-screen* 640)
22 (define *height-screen* 480)
23 (define *bpp-screen* 32)
24 (define *frames-per-second* 20)
28 ;;; SDL Initialization Subsystem
34 (let ((initialized #f))
37 (cond ((not initialized) (SDL_Init SDL_INIT_EVERYTHING) (set! initialized #t))
42 (if initialized #t #f)))
47 (set! initialized #f))))
52 (define init-video-mode #f)
53 (define video-mode-on? #f)
54 (define resize-screen #f)
55 (define quit-video-mode #f)
57 (let ((screen #f) (flags 0))
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 "")
74 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
78 (lambda () (if screen #t #f)))
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)))))
87 (SDL_FreeSurface screen)
91 (cond ((not (3d-mode?))
93 (glDisable GL_DEPTH_TEST)
94 (apply-mode-change))))
100 (glEnable GL_DEPTH_TEST)
101 (glDepthFunc GL_LEQUAL)
102 (apply-mode-change))))
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)))
110 (eq? (assoc-ref (get-game-properties) 'mode) '3d))
113 (glShadeModel GL_SMOOTH)
114 (glClearColor 0 0 0 0)
116 ; (glDepthFunc GL_LEQUAL)
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)
123 (define (init-lighting)
125 (glEnable GL_LIGHTING))
127 (define (resize-screen-GL width height)
128 (glViewport 0 0 width height)
129 (glMatrixMode GL_PROJECTION)
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)
142 (define init-audio #f)
143 (define quit-audio #f)
148 (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
159 (define resources-cache (make-weak-value-hash-table))
161 (define get-resource-from-cache #f)
162 (define insert-resource-into-cache #f)
165 (set! get-resource-from-cache
167 (hash-ref resources-cache key)))
169 (set! insert-resource-into-cache
171 (hash-set! resources-cache key res))))
175 (define set-frames-per-second #f)
176 (define init-frame-time #f)
177 (define get-frame-time #f)
178 (define delay-frame #f)
180 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
181 (set! set-frames-per-second
183 (set! time-per-frame (/ 1000.0 fps))))
185 (set! init-frame-time
187 (set! time (SDL_GetTicks))))
195 (let ((frame-time (- (SDL_GetTicks) time)))
196 (cond ((< frame-time time-per-frame)
197 (SDL_Delay (- time-per-frame frame-time))))))))
200 (define set-game-properties! #f)
201 (define get-game-properties #f)
203 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
204 (set! set-game-properties!
205 (lambda* (#:key title width height bpp fps mode)
210 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
211 (if (or width height bpp)
213 (if width (set! pwidth width))
214 (if height (set! pheight height))
215 (if bpp (set! pbpp bpp))
216 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
220 (set-frames-per-second fps)))
225 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
226 (get-game-properties)))
228 (set! get-game-properties
230 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
233 (define-macro (run-game . code)
234 `(let ((game-function ,(if (null? code)
236 `(lambda () ,@code))))
238 (set-game-code game-function)
239 (cond ((not (game-running?))
242 (define game-loop #f)
243 (define game-running? #f)
244 (define set-game-code #f)
246 (let ((running #f) (game-code #f))
249 (refresh-active-mobs)
258 (cond ((video-mode-on?)
259 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
261 (refresh-active-mobs)
262 (if (procedure? game-code)
264 (lambda () (game-code))
265 (lambda (key . args) #f)))
266 (cond ((video-mode-on?)
268 (SDL_GL_SwapBuffers)))
277 (lambda (game-function)
278 (set! game-code game-function))))