]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
Games properties.
[gacela.git] / src / gacela.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
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
24             load-font
25             *title*
26             *width-screen*
27             *height-screen*
28             *bpp-screen*
29             *frames-per-second*
30             *mode*
31             set-game-properties!
32             get-game-properties
33             init-gacela
34             quit-gacela
35             game-loop
36             game-running?
37             set-game-code)
38   #:export-syntax (game)
39   #:re-export (get-current-color
40                set-current-color
41                with-color
42                progn-textures
43                draw
44                draw-texture
45                draw-line
46                draw-quad
47                draw-rectangle
48                draw-square
49                draw-cube
50                translate
51                rotate
52                to-origin
53                add-light
54                set-camera
55                camera-look
56                render-text))
57
58
59 ;;; Resources Cache
60
61 (define resources-cache (make-weak-value-hash-table))
62
63 (define (from-cache key)
64   (hash-ref resources-cache key))
65
66 (define (into-cache key res)
67   (hash-set! resources-cache key res))
68
69 (define-macro (use-cache-with module proc)
70   (let* ((pwc (string->symbol (string-concatenate (list (symbol->string proc) "-without-cache")))))
71     `(begin
72        (define ,pwc (@ ,module ,proc))
73        (define (,proc . param)
74          (let* ((key param)
75                 (res (from-cache key)))
76            (cond (res
77                   res)
78                  (else
79                   (set! res (apply ,pwc param))
80                   (into-cache key res)
81                   res)))))))
82
83 (use-cache-with (gacela video) load-texture)
84 (use-cache-with (gacela video) load-font)
85
86
87 ;;; Game Properties
88
89 (define *title* "Gacela")
90 (define *width-screen* 640)
91 (define *height-screen* 480)
92 (define *bpp-screen* 32)
93 (define *frames-per-second* 20)
94 (define *mode* '2d)
95
96 (define* (set-game-properties! #:key title width height bpp fps mode)
97   (if title
98       (set-screen-title! title))
99   (if bpp
100       (set-screen-bpp! bpp))
101   (if (or width height)
102       (begin
103         (if (not width) (set! width (get-screen-width)))
104         (if (not height) (set! height (get-screen-height)))
105         (resize-screen width height)))
106   (if fps
107       (set-frames-per-second! fps))
108   (if mode
109       (if (eq? mode '3d) (set-3d-mode) (set-2d-mode)))
110   (get-game-properties))
111
112 (define (get-game-properties)
113   `((title . ,(get-screen-title)) (width . ,(get-screen-width)) (height . ,(get-screen-height)) (bpp . ,(get-screen-bpp)) (fps . ,(get-frames-per-second)) (mode . ,(if (3d-mode?) '3d '2d))))
114
115
116 ;;; Main Loop
117
118 (define loop-flag #f)
119 (define game-code #f)
120
121 (define-macro (game . code)
122   `(let ((game-function ,(if (null? code)
123                              `(lambda () #f)
124                              `(lambda () ,@code))))
125      (set-game-code game-function)
126      (cond ((not (game-running?))
127             (game-loop)))))
128
129 (define (init-gacela)
130   (call-with-new-thread (lambda () (game))))
131
132 (define (quit-gacela)
133   (set! loop-flag #f))
134
135 (define (game-loop)
136 ;         (refresh-active-mobs)
137   (set! loop-flag #t)
138   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
139   (while loop-flag
140          (init-frame-time)
141 ;           (check-connections)
142          (process-events)
143          (cond ((quit-signal?)
144                 (quit-gacela))
145                (else
146                 (clear-screen)
147                 (to-origin)
148 ;                  (refresh-active-mobs)
149                 (if (procedure? game-code)
150                     (catch #t
151                            (lambda () (game-code))
152                            (lambda (key . args) #f)))
153 ;                         (run-mobs)
154                 (flip-screen)
155                 (delay-frame))))
156   (quit-video))
157
158 (define (game-running?)
159   loop-flag)
160
161 (define (set-game-code game-function)
162   (set! game-code game-function))