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 resources-cache (make-weak-value-hash-table))
32 (define from-cache #f)
33 (define into-cache #f)
38 (hash-ref resources-cache key)))
42 (hash-set! resources-cache key res))))
47 (define set-frames-per-second #f)
48 (define init-frame-time #f)
49 (define get-frame-time #f)
50 (define delay-frame #f)
52 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
53 (set! set-frames-per-second
55 (set! time-per-frame (/ 1000.0 fps))))
59 (set! time (SDL_GetTicks))))
67 (let ((frame-time (- (SDL_GetTicks) time)))
68 (cond ((< frame-time time-per-frame)
69 (SDL_Delay (- time-per-frame frame-time))))))))
72 (define set-game-properties! #f)
73 (define get-game-properties #f)
75 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
76 (set! set-game-properties!
77 (lambda* (#:key title width height bpp fps mode)
82 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
83 (if (or width height bpp)
85 (if width (set! pwidth width))
86 (if height (set! pheight height))
87 (if bpp (set! pbpp bpp))
88 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
92 (set-frames-per-second fps)))
97 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
98 (get-game-properties)))
100 (set! get-game-properties
102 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
105 (define-macro (run-game . code)
106 `(let ((game-function ,(if (null? code)
108 `(lambda () ,@code))))
110 (set-game-code game-function)
111 (cond ((not (game-running?))
114 (define game-loop #f)
115 (define game-running? #f)
116 (define set-game-code #f)
118 (let ((running #f) (game-code #f))
121 (refresh-active-mobs)
130 (cond ((video-mode-on?)
131 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
133 (refresh-active-mobs)
134 (if (procedure? game-code)
136 (lambda () (game-code))
137 (lambda (key . args) #f)))
138 (cond ((video-mode-on?)
140 (SDL_GL_SwapBuffers)))
149 (lambda (game-function)
150 (set! game-code game-function))))