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 apply-mode-change #f)
49 (define quit-video-mode #f)
51 (let ((screen #f) (flags 0) (current-width *width-screen*) (current-height *height-screen*) (current-bpp *bpp-screen*))
53 (lambda* (#:optional (width current-width) (height current-height) (bpp current-bpp))
56 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
57 (set! flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
58 (if (= (assoc-ref (SDL_GetVideoInfo) 'hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
59 (if (= (assoc-ref (SDL_GetVideoInfo) 'blit_hw) 0) 0 SDL_HWACCEL)))
60 (set! screen (SDL_SetVideoMode width height bpp flags))
62 (resize-screen-GL width height)
63 (set! current-width width)
64 (set! current-height height)
65 (set! current-bpp bpp))
69 (lambda () (if screen #t #f)))
72 (lambda* (width height #:optional (bpp current-bpp))
73 (cond (screen (set! screen (SDL_SetVideoMode width height bpp flags))
74 (resize-screen-GL width height)))
75 (set! current-width width)
76 (set! current-height height)))
78 (set! apply-mode-change
79 (lambda () (resize-screen-GL current-width current-height)))
82 (lambda () (set! screen #f))))
85 (cond ((not (3d-mode?))
87 (glDisable GL_DEPTH_TEST)
88 (apply-mode-change))))
94 (glEnable GL_DEPTH_TEST)
95 (glDepthFunc GL_LEQUAL)
96 (apply-mode-change))))
99 (eq? (assoc-ref (get-game-properties) 'mode) '3d))
102 (glShadeModel GL_SMOOTH)
103 (glClearColor 0 0 0 0)
105 ; (glDepthFunc GL_LEQUAL)
106 ; (glEnable GL_BLEND)
107 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
108 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
111 (define (init-lighting)
113 (glEnable GL_LIGHTING))
115 (define (resize-screen-GL width height)
116 (glViewport 0 0 width height)
117 (glMatrixMode GL_PROJECTION)
119 (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
120 (gluPerspective 45 ratio 0.1 100))) ;0.1
121 (else (let* ((w (/ width 2)) (h (/ height 2)))
122 (glOrtho (- w) w (- h) h 0 1))))
123 (glMatrixMode GL_MODELVIEW)
127 (define get-current-color #f)
128 (define set-current-color #f)
130 (let ((current-color '(1 1 1 1)))
131 (set! get-current-color
135 (set! set-current-color
136 (lambda* (red green blue #:optional (alpha 1))
137 (set! current-color (list red green blue alpha))
138 (glColor4f red green blue alpha))))
140 (define* (load-image image-file #:key transparent-color)
142 (let ((loaded-image (IMG_Load image-file)))
143 (cond ((= loaded-image 0) #f)
144 (else (let ((optimized-image (SDL_DisplayFormat loaded-image)))
145 (SDL_FreeSurface loaded-image)
146 (cond ((= optimized-image 0) #f)
147 ((not transparent-color) optimized-image)
148 (else (SDL_SetColorKey optimized-image
150 (SDL_MapRGB (surface-format optimized-image)
151 (car transparent-color)
152 (cadr transparent-color)
153 (caddr transparent-color)))
154 optimized-image)))))))
159 (define init-audio #f)
160 (define quit-audio #f)
165 (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
176 (define (init-gacela)
181 (define set-frames-per-second #f)
182 (define init-frame-time #f)
183 (define delay-frame #f)
185 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
186 (set! set-frames-per-second
188 (set! time-per-frame (/ 1000.0 fps))))
190 (set! init-frame-time
192 (set! time (SDL_GetTicks))))
196 (let ((frame-time (- (SDL_GetTicks) time)))
197 (cond ((< frame-time time-per-frame)
198 (SDL_Delay (- time-per-frame frame-time))))))))
201 (define set-game-properties #f)
202 (define get-game-properties #f)
204 (let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
205 (set! set-game-properties
206 (lambda* (#:key title width height bpp fps mode)
211 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
212 (if (or width height bpp)
214 (if width (set! pwidth width))
215 (if height (set! pheight height))
216 (if bpp (set! pbpp bpp))
217 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
221 (set-frames-per-second fps)))
226 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
227 (get-game-properties)))
229 (set! get-game-properties
231 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
234 (define-macro (run-game . code)
235 `(let ((game-function ,(if (null? code)
237 `(lambda () ,@code))))
239 (set-game-code game-function)
240 (cond ((not (game-running?))
243 (define game-loop #f)
244 (define game-running? #f)
245 (define set-game-code #f)
247 (let ((running #f) (game-code #f) (mobs '()))
250 (set! mobs (get-active-mobs))
255 ; (check-connections)
256 ; (eval-from-clients)
259 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
261 (cond ((mobs-changed?) (set! mobs (get-active-mobs))))
262 (if (procedure? game-code) (game-code))
273 (lambda (game-function)
274 (set! game-code game-function))))