]> git.jsancho.org Git - gacela.git/blob - gacela.lisp
(no commit message)
[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 (in-package :gacela)
19
20 ;;; Default values for Gacela
21 (defvar *width-screen* 640)
22 (defvar *height-screen* 480)
23 (defvar *bpp-screen* 32)
24 (defvar *title-screen* "Happy Hacking!!")
25 (defvar *gacela-freq* 30)
26 (defvar *transparent-color* '(:red 0 :green 0 :blue 0))
27 (defvar *background-color* '(:red 0 :green 0 :blue 0))
28 (defvar *zoom* -10)
29
30 ;;; SDL Initialization Subsystem
31 (let (initialized)
32
33   (defun init-sdl ()
34     (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
35           (t initialized)))
36
37   (defun quit-sdl ()
38     (setq initialized (SDL_Quit))))
39
40
41 ;;; Video Subsystem
42 (defstruct surface address clip-w clip-h shape)
43
44 (let (screen flags)
45
46   (defun init-video-mode (&key (width *width-screen*) (height *height-screen*) (bpp *bpp-screen*))
47     (cond ((null screen)
48            (init-sdl)
49            (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
50            (setq flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
51                           (if (= (getf (SDL_GetVideoInfo) :hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
52                           (if (= (getf (SDL_GetVideoInfo) :blit_hw) 0) 0 SDL_HWACCEL)))
53            (setq screen (SDL_SetVideoMode width height bpp flags))
54            (init-GL)
55            (resize-screen-GL width height))
56           (t t)))
57
58   (defun resize-screen (width height bpp)
59     (setq screen (SDL_SetVideoMode width height bpp flags))
60     (resize-screen-GL width height))
61
62   (defun fill-screen (color)
63     (init-video-mode)
64     (fill-surface screen (getf color :red) (getf color :green) (getf color :blue)))
65
66   (defun flip ()
67     (cond ((null screen) nil)
68           (t (SDL_Flip screen))))
69
70   (defun create-surface (width height &key (trans-color *transparent-color*))
71     (init-video-mode)
72     (let ((new-surface (make-surface
73                         :address (create-SDL_Surface
74                                   (surface-address screen)
75                                   width
76                                   height
77                                   (getf trans-color :red)
78                                   (getf trans-color :green)
79                                   (getf trans-color :blue)))))
80       (set-resource 'image new-surface (gentemp))
81       new-surface))
82
83   (defun print-surface (x y surface)
84     (apply-surface x y surface screen)
85     surface)
86
87   (defun quit-video-mode ()
88     (setq screen nil)))
89
90
91 (defun init-GL ()
92   (glShadeModel GL_SMOOTH)
93   (glClearColor 0 0 0 0)
94   (glClearDepth 1)
95   (glEnable GL_DEPTH_TEST)
96   (glDepthFunc GL_LEQUAL)
97 ;  (glEnable GL_BLEND)
98 ;  (glBlendFunc GL_SRC_ALPHA GL_ONE)
99   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
100   t)
101
102 (defun init-textures ()
103   (init-video-mode)
104   (glEnable GL_TEXTURE_2D))
105
106 (defun init-lighting ()
107   (init-video-mode)
108   (glEnable GL_LIGHTING))
109
110 (defun resize-screen-GL (width height)
111   (let ((ratio (if (= height 0) width (/ width height))))
112     (glViewPort 0 0 width height)
113     (glMatrixMode GL_PROJECTION)
114     (glLoadIdentity)
115     (gluPerspective 45 ratio 0.1 100)
116     (glMatrixMode GL_MODELVIEW)
117     (glLoadIdentity)
118     t))
119
120 (defun copy-surface (source)
121   (cond ((surface-p source)
122          (let ((new-surface
123                 (make-surface :address (copy-SDL_Surface (surface-address source))
124                               :clip-w (surface-clip-w source)
125                               :clip-h (surface-clip-h source)
126                               :shape (surface-shape source))))
127            (set-resource 'image new-surface (gentemp))
128            new-surface))))
129
130 (defun load-image (image-file &key (transparent-color nil))
131   (init-video-mode)
132   (let ((loaded-image (IMG_Load image-file)))
133     (cond ((= loaded-image 0) nil)
134           (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
135                (SDL_FreeSurface loaded-image)
136                (cond ((= optimized-image 0) nil)
137                      ((null transparent-color) optimized-image)
138                      (t (SDL_SetColorKey optimized-image
139                                          SDL_SRCCOLORKEY
140                                          (SDL_MapRGB (surface-format optimized-image)
141                                                      (car transparent-color)
142                                                      (cadr transparent-color)
143                                                      (caddr transparent-color)))
144                         optimized-image)))))))
145
146 (defun load-image2 (image-file &key (transparent-color nil))
147   (let ((address-image (load-image image-file :transparent-color transparent-color)))
148     (list
149      (lambda (x y) (print-surface x y address-image))
150      (lambda () (SDL_FreeSurface address-image)))))
151
152 (defun apply-surface (x y source destination)
153   (let ((offset (SDL_Rect x y 0 0)))
154     (SDL_BlitSurface source 0 destination offset)
155     (free offset)
156     destination))
157
158 (defun apply-surface-old (x y source destination &optional (clip nil))
159   (cond ((null clip)
160          (apply-surface2 x y (surface-address source) (surface-address destination) 0 0 0 0 0))
161         ((integerp clip)
162          (apply-surface2 x y (surface-address source) (surface-address destination) 0 0
163                          (surface-clip-w source) (surface-clip-h source) clip))
164         (t
165          (apply-surface2 x y (surface-address source) (surface-address destination)
166                          (first clip) (second clip) (third clip) (fourth clip) 0)))
167   destination)
168
169
170 (defun print-image (x y image-file &optional (clip nil))
171   (init-video-mode)
172   (let ((image (load-image image-file)))
173     (print-surface x y image clip)
174     image))
175
176
177 (defun clean-screen ()
178   (fill-screen *background-color*))
179
180 (defun refresh-screen ()
181   (clean-screen)
182   (funcall-procs #'print-mob)
183   (flip))
184
185
186 (defun filled-circle (radius &optional (color '(:red 255 :green 255 :blue 255)))
187   (init-video-mode)
188   (let ((new-surface (create-surface (1+ (* radius 2)) (1+ (* radius 2)))))
189     (sge_FilledCircle (surface-address new-surface)
190                       radius radius radius
191                       (getf color :red)
192                       (getf color :green)
193                       (getf color :blue))
194     (setf (surface-shape new-surface)
195           `((,radius ,radius) ,radius))
196     new-surface))
197
198
199 (defun filled-rect (width height &optional (color '(:red 255 :green 255 :blue 255)))
200   (init-video-mode)
201   (let ((new-surface (create-surface width height)))
202     (sge_FilledRect (surface-address new-surface)
203                     0 0 width height
204                     (getf color :red)
205                     (getf color :green)
206                     (getf color :blue))
207     (setf (surface-shape new-surface)
208           (make-rectangle 0 0 width height))
209     new-surface))
210
211
212 ;;; TTF Subsystem
213 (defstruct font address)
214
215 (let ((ttf nil))
216
217   (defun init-ttf ()
218     (cond ((null ttf) (progn (init-sdl) (setq ttf (TTF_Init))))
219           (t ttf)))
220
221   (defun quit-ttf ()
222     (setq ttf (TTF_Quit))))
223
224
225 (defun open-font (font-name tam)
226   (init-ttf)
227   (let ((font (get-resource 'font font-name tam)))
228     (if (null font)
229         (progn (setq font (make-font :address (TTF_OpenFont font-name tam)))
230                (set-resource 'font font font-name tam)))
231     font))
232
233
234 (defun render-text (text-message
235                     &key (color '(:red 255 :green 255 :blue 255))
236                     (font-name "lazy.ttf") (tam 28))
237   (init-ttf)
238   (let ((message (get-resource 'text text-message color font-name tam)))
239     (if (null message)
240         (progn
241           (setq message
242                 (make-surface
243                  :address (render-text2 (open-font font-name tam)
244                                         text-message
245                                         (getf color :red)
246                                         (getf color :green)
247                                         (getf color :blue))))
248           (set-resource 'text message text-message color font-name tam)))
249     message))
250
251
252 (defun print-text (x y text-message
253                      &key (color '(:red 255 :green 255 :blue 255))
254                      (font-name "lazy.ttf") (tam 28))
255   (init-video-mode)
256   (init-ttf)
257   (let ((message (render-text text-message :color color :font-name font-name :tam tam)))
258     (print-surface x y message)
259     message))
260
261
262 ;;; Audio Subsystem
263 (let ((audio nil))
264
265   (defun init-audio ()
266     (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
267           (t audio)))
268
269   (defun quit-audio ()
270     (setq audio (Mix_CloseAudio))))
271
272
273 ;;; Resources Manager
274 (defstruct resource address free-function object)
275
276 (let ((resources-table (make-hash-table :test 'equal)))
277
278   (defun set-resource (type object &rest key)
279     (let ((res
280            (cond ((surface-p object)
281                   (make-resource :address (surface-address object)
282                                  :free-function #'SDL_FreeSurface
283                                  :object object))
284                  ((font-p object)
285                   (make-resource :address (font-address object)
286                                  :free-function #'TTF_CloseFont
287                                  :object object))
288                  ((cp-space-p object)
289                   (make-resource :address (cp-space-address object)
290                                  :free-function #'cpSpaceFree
291                                  :object object))
292                  ((cp-body-p object)
293                   (make-resource :address (cp-body-address object)
294                                  :free-function #'cpBodyFree
295                                  :object object))
296                  ((cp-shape-p object)
297                   (make-resource :address (cp-shape-address object)
298                                  :free-function #'cpShapeFree
299                                  :object object))
300                  (t nil))))
301       (cond (res (setf (gethash `(,type ,@key) resources-table) res)))))
302
303   (defun get-resource (type &rest key)
304     (let ((resource (gethash `(,type ,@key) resources-table)))
305       (cond ((null resource) nil)
306             (t (resource-object resource)))))
307
308   (defun free-all-resources ()
309     (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
310              resources-table)
311     (clrhash resources-table)))
312
313
314 ;;; Connection with the GUI
315 (let (socket)
316   (defun connect-to-gui ()
317     (setq socket (si::socket 1984 :host "localhost")))
318
319   (defun eval-from-gui ()
320     (cond ((and socket (listen socket)) (eval (read socket))))))
321
322
323 ;;; GaCeLa Functions
324 ;(defun game-loop (code)
325 ;  (process-events)
326 ;  (cond ((quit?) nil)
327 ;       (t
328 ;        (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
329 ;        (glLoadIdentity)
330 ;        (translate 0 0 *zoom*)
331 ;        (funcall code)
332 ;        (SDL_GL_SwapBuffers)
333 ;        (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
334 ;        (game-loop code))))
335
336 (let (commands)
337   (defun prog-command (command)
338     (setq commands (cons command commands)))
339
340   (defun run-commands ()
341     (cond (commands
342            (let (running)
343              (setq running commands)
344              (setq commands nil)
345              (labels ((run-com (comlst)
346                                (cond (comlst (run-com (cdr comlst))
347                                              (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
348                      (run-com running)))))))
349
350 (defmacro run-game (title &body code)
351   `(progn
352      (init-video-mode)
353      (SDL_WM_SetCaption ,title "")
354      (process-events)
355      (do () ((quit?))
356          (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
357          (glLoadIdentity)
358          (translate 0 0 *zoom*)
359          ,@code
360          (SDL_GL_SwapBuffers)
361          (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
362          (process-events)
363          (setq running nil))))
364
365 ;(defun run-game ()
366 ;  (init-video-mode)
367 ;  (SDL_WM_SetCaption *title-screen* "")
368 ;  (refresh-active-procs)
369 ;  (enjoy!)
370 ;  (do () ((quit?))
371 ;      (process-events)
372 ;      (logic-procs)
373 ;      (motion-procs)
374 ;      (refresh-active-procs)
375 ;      (refresh-screen)
376 ;      (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))))
377
378 (defun quit-game ()
379 ;  (free-all-resources)
380 ;  (quit-audio)
381 ;  (quit-ttf)
382   (quit-video-mode)
383 ;  (quit-all-procs)
384 ;  (clear-events)
385 ;  (quit-events)
386   (quit-sdl))