]> git.jsancho.org Git - gacela.git/blob - gacela.lisp
Gacela con autotools (primer intento)
[gacela.git] / gacela.lisp
1 ;;; Gacela, a GNU Common Lisp 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 (eval-when (compile load) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
19
20 (eval-when (compile load eval)
21            (when (not (find-package 'gacela)) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
22            (in-package 'gacela :nicknames '(gg) :use '(lisp)))
23
24
25 ;;; Default values for Gacela
26 (defvar *width-screen* 640)
27 (defvar *height-screen* 480)
28 (defvar *bpp-screen* 32)
29 (defvar *frames-per-second* 20)
30
31 ;;; SDL Initialization Subsystem
32 (let (initialized)
33
34   (defun init-sdl ()
35     (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
36           (t initialized)))
37
38   (defun quit-sdl ()
39     (setq initialized (SDL_Quit))))
40
41
42 ;;; Video Subsystem
43 (let (screen flags (current-width *width-screen*) (current-height *height-screen*) current-bpp)
44
45   (defun init-video-mode (&key (width current-width) (height current-height) (bpp *bpp-screen*))
46     (cond ((null screen)
47            (init-sdl)
48            (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
49            (setq flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
50                           (if (= (getf (SDL_GetVideoInfo) :hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
51                           (if (= (getf (SDL_GetVideoInfo) :blit_hw) 0) 0 SDL_HWACCEL)))
52            (setq screen (SDL_SetVideoMode width height bpp flags))
53            (init-GL)
54            (resize-screen-GL width height)
55            (setq current-width width current-height height current-bpp bpp))
56           (t t)))
57
58   (defun resize-screen (width height &optional (bpp current-bpp))
59     (cond (screen (setq screen (SDL_SetVideoMode width height bpp flags))
60                   (resize-screen-GL width height)))
61     (setq current-width width current-height height))
62
63   (defun apply-mode-change ()
64     (resize-screen-GL current-width current-height))
65
66   (defun quit-video-mode ()
67     (setq screen nil)))
68
69 (defun set-2d-mode ()
70   (cond ((not (3d-mode?))
71          (init-video-mode)
72          (glDisable GL_DEPTH_TEST)
73          (apply-mode-change))))
74
75 (defun set-3d-mode ()
76   (cond ((3d-mode?)
77          (init-video-mode)
78          (glClearDepth 1)
79          (glEnable GL_DEPTH_TEST)
80          (glDepthFunc GL_LEQUAL)
81          (apply-mode-change))))
82
83 (defun 3d-mode? ()
84   (eq (getf (get-game-properties) :mode) '3d))
85
86 (defun init-GL ()
87   (glShadeModel GL_SMOOTH)
88   (glClearColor 0 0 0 0)
89 ;  (glClearDepth 1)
90 ;  (glDepthFunc GL_LEQUAL)
91 ;  (glEnable GL_BLEND)
92 ;  (glBlendFunc GL_SRC_ALPHA GL_ONE)
93   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
94   t)
95
96 (defun init-lighting ()
97   (init-video-mode)
98   (glEnable GL_LIGHTING))
99
100 (defun resize-screen-GL (width height)
101   (glViewPort 0 0 width height)
102   (glMatrixMode GL_PROJECTION)
103   (glLoadIdentity)
104   (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
105                       (gluPerspective 45 ratio 0.1 100))) ;0.1
106         (t (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
107              (glOrtho -w w -h h 0 1))))
108   (glMatrixMode GL_MODELVIEW)
109   (glLoadIdentity)
110   t))
111
112 (let ((current-color '(1 1 1 1)))
113   (defun get-current-color ()
114     current-color)
115
116   (defun set-current-color (red green blue &optional (alpha 1))
117     (setq current-color (list red green blue alpha))
118     (glColor4f red green blue alpha)))
119
120 (defun load-image (image-file &key (transparent-color nil))
121   (init-video-mode)
122   (let ((loaded-image (IMG_Load image-file)))
123     (cond ((= loaded-image 0) nil)
124           (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
125                (SDL_FreeSurface loaded-image)
126                (cond ((= optimized-image 0) nil)
127                      ((null transparent-color) optimized-image)
128                      (t (SDL_SetColorKey optimized-image
129                                          SDL_SRCCOLORKEY
130                                          (SDL_MapRGB (surface-format optimized-image)
131                                                      (car transparent-color)
132                                                      (cadr transparent-color)
133                                                      (caddr transparent-color)))
134                         optimized-image)))))))
135
136
137 ;;; Audio Subsystem
138 (let ((audio nil))
139
140   (defun init-audio ()
141     (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
142           (t audio)))
143
144   (defun quit-audio ()
145     (setq audio (Mix_CloseAudio))))
146
147
148 ;;; Resources Manager
149 (defstruct resource plist constructor destructor time)
150
151 (defun make-resource-texture (&key filename min-filter mag-filter)
152   `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
153
154 (defun make-resource-font (&key filename encoding)
155   `(:type font :filename ,filename :enconding ,encoding))
156
157 (defun make-resource-sound (&key filename)
158   `(:type sound :filename ,filename))
159
160 (defun make-resource-music (&key filename)
161   `(:type music :filename ,filename))
162
163 (defmacro get-rtime (key)
164   `(resource-time (gethash ,key resources-table)))
165
166 (defmacro get-rplist (key)
167   `(resource-plist (gethash ,key resources-table)))
168
169 (defmacro get-rconstructor (key)
170   `(resource-constructor (gethash ,key resources-table)))
171
172 (defmacro get-rdestructor (key)
173   `(resource-destructor (gethash ,key resources-table)))
174
175 (let ((resources-table (make-hash-table :test 'equal))
176       (expiration-time 50000))
177
178   (defun set-expiration-time (new-time)
179     (setq expiration-time new-time))
180
181   (defun set-resource (key plist constructor destructor &key static)
182     (expire-resources)
183     (setf (gethash key resources-table)
184           (make-resource :plist plist
185                          :constructor constructor
186                          :destructor destructor
187                          :time (if static t (SDL_GetTicks)))))
188
189   (defun get-resource (key)
190     (cond ((null (gethash key resources-table)) nil)
191           (t (let ((time (get-rtime key)))
192                (cond ((null time) (funcall (get-rconstructor key)))
193                      ((numberp time) (setf (get-rtime key) (SDL_GetTicks))))
194                (get-rplist key)))))
195
196   (defun free-resource (key)
197     (funcall (get-rdestructor key))
198     (setf (get-rtime key) nil))
199
200   (defun expire-resource (key &optional (now (SDL_GetTicks)))
201     (let ((time (get-rtime key)))
202       (cond ((and (numberp time) (> (- now time) expiration-time)) (free-resource key)))))
203
204   (defun expire-resources ()
205     (maphash (lambda (key res) (expire-resource key)) resources-table))
206
207   (defun free-all-resources ()
208     (maphash (lambda (key res) (free-resource key)) resources-table)))
209
210
211 ;;; Connection with Gacela Clients
212 (let (server-socket clients)
213   (defun start-server (port)
214     (cond ((null server-socket) (setq server-socket (si::socket port :server #'check-connections)))))
215
216   (defun check-connections ()
217     (cond ((and server-socket (listen server-socket)) (setq clients (cons (si::accept server-socket) clients)))))
218
219   (defun eval-from-clients ()
220     (labels ((eval-clients (cli-socks)
221                            (cond (cli-socks
222                                   (let ((cli (car cli-socks)))
223                                     (cond ((si::listen cli)
224                                            (secure-block cli (eval (read cli)))
225                                            (si::close cli)
226                                            (eval-clients (cdr cli-socks)))
227                                           (t
228                                            (cons cli (eval-clients (cdr cli-socks))))))))))
229             (setq clients (eval-clients clients))))
230
231   (defun stop-server ()
232     (cond (server-socket (si::close server-socket) (setq server-socket nil)))
233     (cond (clients
234            (labels ((close-clients (cli-socks)
235                                    (si::close (car cli-socks))
236                                    (close-clients (cdr cli-socks))))
237                    (close-clients clients))
238            (setq clients nil)))))
239
240
241 ;;; GaCeLa Functions
242 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
243   (defun set-frames-per-second (fps)
244     (setq time-per-frame (/ 1000.0 fps)))
245
246   (defun init-frame-time ()
247     (setq time (SDL_GetTicks)))
248
249   (defun delay-frame ()
250     (let ((frame-time (- (SDL_GetTicks) time)))
251       (cond ((< frame-time time-per-frame)
252              (SDL_Delay (- time-per-frame frame-time)))))))
253
254
255 (let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
256   (defun set-game-properties (&key title width height bpp fps mode)
257     (init-video-mode)
258     (when title (progn (setq ptitle title) (SDL_WM_SetCaption title "")))
259     (when (or width height bpp)
260       (progn
261         (when width (setq pwidth width))
262         (when height (setq pheight height))
263         (when bpp (setq pbpp bpp))
264         (resize-screen pwidth pheight pbpp)))
265     (when fps (progn (setq pfps fps) (set-frames-per-second fps)))
266     (when mode (progn (setq pmode mode) (if (eq mode '3d) (set-3d-mode) (set-2d-mode))))
267     (get-game-properties))
268
269   (defun get-game-properties ()
270     (list :title ptitle :width pwidth :height pheight :bpp pbpp :fps pfps :mode pmode)))
271
272
273 (defmacro run-game (&body code)
274   `(let ((game-function (lambda () ,@code)))
275      (init-video-mode)
276      (set-game-code game-function)
277      (cond ((not (game-running?))
278             (game-loop)))))
279
280 (let (running game-code)
281   (defun game-loop ()
282     (setq running t)
283     (do () ((quit?))
284         (init-frame-time)
285         (check-connections)
286         (eval-from-clients)
287         (process-events)
288         (cond ((not (quit?))
289                (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
290                (to-origin)
291                (refresh-active-objects)
292                (when (functionp game-code) (funcall game-code))
293                (render-objects)
294                (SDL_GL_SwapBuffers)
295                (delay-frame))))
296     (setq running nil))
297
298   (defun game-running? ()
299     running)
300
301   (defun set-game-code (game-function)
302     (setq game-code game-function)))
303
304 (defun quit-game ()
305   (free-all-resources)
306   (quit-audio)
307   (quit-video-mode)
308 ;  (quit-all-mobs)
309   (kill-all-objects)
310 ;  (clear-events)
311 ;  (quit-events)
312   (quit-sdl))