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)
30 (define init-audio #f)
31 (define quit-audio #f)
36 (cond ((not audio) (begin (init-sdl) (set! audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
47 (define resources-cache (make-weak-value-hash-table))
49 (define get-resource-from-cache #f)
50 (define insert-resource-into-cache #f)
53 (set! get-resource-from-cache
55 (hash-ref resources-cache key)))
57 (set! insert-resource-into-cache
59 (hash-set! resources-cache key res))))
63 (define set-frames-per-second #f)
64 (define init-frame-time #f)
65 (define get-frame-time #f)
66 (define delay-frame #f)
68 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
69 (set! set-frames-per-second
71 (set! time-per-frame (/ 1000.0 fps))))
75 (set! time (SDL_GetTicks))))
83 (let ((frame-time (- (SDL_GetTicks) time)))
84 (cond ((< frame-time time-per-frame)
85 (SDL_Delay (- time-per-frame frame-time))))))))
88 (define set-game-properties! #f)
89 (define get-game-properties #f)
91 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
92 (set! set-game-properties!
93 (lambda* (#:key title width height bpp fps mode)
98 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
99 (if (or width height bpp)
101 (if width (set! pwidth width))
102 (if height (set! pheight height))
103 (if bpp (set! pbpp bpp))
104 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
108 (set-frames-per-second fps)))
113 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
114 (get-game-properties)))
116 (set! get-game-properties
118 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
121 (define-macro (run-game . code)
122 `(let ((game-function ,(if (null? code)
124 `(lambda () ,@code))))
126 (set-game-code game-function)
127 (cond ((not (game-running?))
130 (define game-loop #f)
131 (define game-running? #f)
132 (define set-game-code #f)
134 (let ((running #f) (game-code #f))
137 (refresh-active-mobs)
146 (cond ((video-mode-on?)
147 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
149 (refresh-active-mobs)
150 (if (procedure? game-code)
152 (lambda () (game-code))
153 (lambda (key . args) #f)))
154 (cond ((video-mode-on?)
156 (SDL_GL_SwapBuffers)))
165 (lambda (game-function)
166 (set! game-code game-function))))