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)
23 #:export (load-texture
25 #:re-export (get-current-color
47 ;;; Default values for Gacela
49 (define *title* "Gacela")
50 (define *width-screen* 640)
51 (define *height-screen* 480)
52 (define *bpp-screen* 32)
53 (define *frames-per-second* 20)
59 (define resources-cache (make-weak-value-hash-table))
61 (define from-cache #f)
62 (define into-cache #f)
67 (hash-ref resources-cache key)))
71 (hash-set! resources-cache key res))))
73 (define-macro (use-cache-with module proc)
74 (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
76 (define ,pwc (@ ,module ,proc))
77 (define (,proc . param)
79 (res (from-cache key)))
83 (set! res (apply ,pwc param))
87 (use-cache-with (gacela video) load-texture)
88 (use-cache-with (gacela video) load-font)
93 (define set-game-properties! #f)
94 (define get-game-properties #f)
96 (let ((ptitle *title*) (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode *mode*))
97 (set! set-game-properties!
98 (lambda* (#:key title width height bpp fps mode)
103 (if (video-mode-on?) (SDL_WM_SetCaption title ""))))
104 (if (or width height bpp)
106 (if width (set! pwidth width))
107 (if height (set! pheight height))
108 (if bpp (set! pbpp bpp))
109 (if (video-mode-on?) (resize-screen pwidth pheight pbpp))))
113 (set-frames-per-second fps)))
118 (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))))
119 (get-game-properties)))
121 (set! get-game-properties
123 `((title . ,ptitle) (width . ,pwidth) (height . ,pheight) (bpp . ,pbpp) (fps . ,pfps) (mode . ,pmode)))))
126 (define-macro (run-game . code)
127 `(let ((game-function ,(if (null? code)
129 `(lambda () ,@code))))
131 (set-game-code game-function)
132 (cond ((not (game-running?))
135 (define game-loop #f)
136 (define game-running? #f)
137 (define set-game-code #f)
139 (let ((running #f) (game-code #f))
142 (refresh-active-mobs)
151 (cond ((video-mode-on?)
152 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
154 (refresh-active-mobs)
155 (if (procedure? game-code)
157 (lambda () (game-code))
158 (lambda (key . args) #f)))
159 (cond ((video-mode-on?)
161 (SDL_GL_SwapBuffers)))
170 (lambda (game-function)
171 (set! game-code game-function))))