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 (define-module (gacela gacela)
19 #:use-module (gacela events)
20 #:use-module (gacela video)
21 #:use-module (gacela audio)
22 #:use-module (ice-9 optargs)
26 ;;; Default values for Gacela
28 (define *title* "Gacela")
29 (define *width-screen* 640)
30 (define *height-screen* 480)
31 (define *bpp-screen* 32)
32 (define *frames-per-second* 20)
38 (define resources-cache (make-weak-value-hash-table))
40 (define from-cache #f)
41 (define into-cache #f)
46 (hash-ref resources-cache key)))
50 (hash-set! resources-cache key res))))
52 (define-macro (use-cache-with-procedure proc-name)
54 (define ,(string->symbol (string-concatenate (list (symbol->string proc-name) "-without-cache"))) ,proc-name)))
59 (define set-frames-per-second #f)
60 (define init-frame-time #f)
61 (define get-frame-time #f)
62 (define delay-frame #f)
64 (let ((time 0) (time-per-frame (/ 1000.0 *frames-per-second*)))
65 (set! set-frames-per-second
67 (set! time-per-frame (/ 1000.0 fps))))
71 (set! time (SDL_GetTicks))))
79 (let ((frame-time (- (SDL_GetTicks) time)))
80 (cond ((< frame-time time-per-frame)
81 (SDL_Delay (- time-per-frame frame-time))))))))
84 (define set-game-properties! #f)
85 (define get-game-properties #f)
87 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
88 (set! set-game-properties!
89 (lambda* (#:key title width height bpp fps mode)
94 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
95 (if (or width height bpp)
97 (if width (set! pwidth width))
98 (if height (set! pheight height))
99 (if bpp (set! pbpp bpp))
100 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
104 (set-frames-per-second fps)))
109 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
110 (get-game-properties)))
112 (set! get-game-properties
114 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
117 (define-macro (run-game . code)
118 `(let ((game-function ,(if (null? code)
120 `(lambda () ,@code))))
122 (set-game-code game-function)
123 (cond ((not (game-running?))
126 (define game-loop #f)
127 (define game-running? #f)
128 (define set-game-code #f)
130 (let ((running #f) (game-code #f))
133 (refresh-active-mobs)
142 (cond ((video-mode-on?)
143 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
145 (refresh-active-mobs)
146 (if (procedure? game-code)
148 (lambda () (game-code))
149 (lambda (key . args) #f)))
150 (cond ((video-mode-on?)
152 (SDL_GL_SwapBuffers)))
161 (lambda (game-function)
162 (set! game-code game-function))))