]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
Run resize operations in game loop.
[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 ;;; Main Loop
88
89 (define loop-flag #f)
90 (define game-code #f)
91 (define game-loop-thread #f)
92
93 (define-macro (game . code)
94   `(let ((game-function ,(if (null? code)
95                              `(lambda () #f)
96                              `(lambda () ,@code))))
97      (set-game-code game-function)
98      (cond ((not (game-running?))
99             (game-loop)))))
100
101 (define-macro (run-in-game-loop . code)
102   `(if game-loop-thread
103        (system-async-mark (lambda () ,@code) game-loop-thread)
104        (begin ,@code)))
105
106 (define (init-gacela)
107   (set! game-loop-thread (call-with-new-thread (lambda () (game)))))
108
109 (define (quit-gacela)
110   (set! game-loop-thread #f)
111   (set! loop-flag #f))
112
113 (define (game-loop)
114 ;         (refresh-active-mobs)
115   (set! loop-flag #t)
116   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
117   (while loop-flag
118          (init-frame-time)
119 ;           (check-connections)
120          (process-events)
121          (cond ((quit-signal?)
122                 (quit-gacela))
123                (else
124                 (clear-screen)
125                 (to-origin)
126 ;                  (refresh-active-mobs)
127                 (if (procedure? game-code)
128                     (catch #t
129                            (lambda () (game-code))
130                            (lambda (key . args) #f)))
131 ;                         (run-mobs)
132                 (flip-screen)
133                 (delay-frame))))
134   (quit-video))
135
136 (define (game-running?)
137   loop-flag)
138
139 (define (set-game-code game-function)
140   (set! game-code game-function))
141
142
143 ;;; Game Properties
144
145 (define *title* "Gacela")
146 (define *width-screen* 640)
147 (define *height-screen* 480)
148 (define *bpp-screen* 32)
149 (define *frames-per-second* 20)
150 (define *mode* '2d)
151
152 (define* (set-game-properties! #:key title width height bpp fps mode)
153   (if title
154       (set-screen-title! title))
155   (if bpp
156       (run-in-game-loop (set-screen-bpp! bpp)))
157   (if (or width height)
158       (begin
159         (if (not width) (set! width (get-screen-width)))
160         (if (not height) (set! height (get-screen-height)))
161         (run-in-game-loop (resize-screen width height))))
162   (if fps
163       (set-frames-per-second! fps))
164   (if mode
165       (if (eq? mode '3d) (set-3d-mode) (set-2d-mode))))
166
167 (define (get-game-properties)
168   `((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))))