]> git.jsancho.org Git - gacela.git/blob - gacela.lisp
4dc430aef63b9f913855670747173f63f45bb6b1
[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 *frames-per-second* 30)
26 ;(defvar *gacela-freq* 100)
27 (defvar *transparent-color* '(:red 0 :green 0 :blue 0))
28 (defvar *background-color* '(:red 0 :green 0 :blue 0))
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   (2d-mode)
93   (glShadeModel GL_SMOOTH)
94   (glClearColor 0 0 0 0)
95   (glClearDepth 1)
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     (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
116       (glOrtho -w w -h h 0 1))
117 ;    (gluPerspective 45 ratio 0.1 100)
118     (glMatrixMode GL_MODELVIEW)
119     (glLoadIdentity)
120     t))
121
122 (defun copy-surface (source)
123   (cond ((surface-p source)
124          (let ((new-surface
125                 (make-surface :address (copy-SDL_Surface (surface-address source))
126                               :clip-w (surface-clip-w source)
127                               :clip-h (surface-clip-h source)
128                               :shape (surface-shape source))))
129            (set-resource 'image new-surface (gentemp))
130            new-surface))))
131
132 (defun load-image (image-file &key (transparent-color nil))
133   (init-video-mode)
134   (let ((loaded-image (IMG_Load image-file)))
135     (cond ((= loaded-image 0) nil)
136           (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
137                (SDL_FreeSurface loaded-image)
138                (cond ((= optimized-image 0) nil)
139                      ((null transparent-color) optimized-image)
140                      (t (SDL_SetColorKey optimized-image
141                                          SDL_SRCCOLORKEY
142                                          (SDL_MapRGB (surface-format optimized-image)
143                                                      (car transparent-color)
144                                                      (cadr transparent-color)
145                                                      (caddr transparent-color)))
146                         optimized-image)))))))
147
148 (defun load-image2 (image-file &key (transparent-color nil))
149   (let ((address-image (load-image image-file :transparent-color transparent-color)))
150     (list
151      (lambda (x y) (print-surface x y address-image))
152      (lambda () (SDL_FreeSurface address-image)))))
153
154 (defun apply-surface (x y source destination)
155   (let ((offset (SDL_Rect x y 0 0)))
156     (SDL_BlitSurface source 0 destination offset)
157     (free offset)
158     destination))
159
160 (defun apply-surface-old (x y source destination &optional (clip nil))
161   (cond ((null clip)
162          (apply-surface2 x y (surface-address source) (surface-address destination) 0 0 0 0 0))
163         ((integerp clip)
164          (apply-surface2 x y (surface-address source) (surface-address destination) 0 0
165                          (surface-clip-w source) (surface-clip-h source) clip))
166         (t
167          (apply-surface2 x y (surface-address source) (surface-address destination)
168                          (first clip) (second clip) (third clip) (fourth clip) 0)))
169   destination)
170
171
172 (defun print-image (x y image-file &optional (clip nil))
173   (init-video-mode)
174   (let ((image (load-image image-file)))
175     (print-surface x y image clip)
176     image))
177
178
179 (defun clean-screen ()
180   (fill-screen *background-color*))
181
182 (defun refresh-screen ()
183   (clean-screen)
184   (funcall-procs #'print-mob)
185   (flip))
186
187
188 (defun filled-circle (radius &optional (color '(:red 255 :green 255 :blue 255)))
189   (init-video-mode)
190   (let ((new-surface (create-surface (1+ (* radius 2)) (1+ (* radius 2)))))
191     (sge_FilledCircle (surface-address new-surface)
192                       radius radius radius
193                       (getf color :red)
194                       (getf color :green)
195                       (getf color :blue))
196     (setf (surface-shape new-surface)
197           `((,radius ,radius) ,radius))
198     new-surface))
199
200
201 (defun filled-rect (width height &optional (color '(:red 255 :green 255 :blue 255)))
202   (init-video-mode)
203   (let ((new-surface (create-surface width height)))
204     (sge_FilledRect (surface-address new-surface)
205                     0 0 width height
206                     (getf color :red)
207                     (getf color :green)
208                     (getf color :blue))
209     (setf (surface-shape new-surface)
210           (make-rectangle 0 0 width height))
211     new-surface))
212
213
214 ;;; TTF Subsystem
215 (defstruct font address)
216
217 (let ((ttf nil))
218
219   (defun init-ttf ()
220     (cond ((null ttf) (progn (init-sdl) (setq ttf (TTF_Init))))
221           (t ttf)))
222
223   (defun quit-ttf ()
224     (setq ttf (TTF_Quit))))
225
226
227 (defun open-font (font-name tam)
228   (init-ttf)
229   (let ((font (get-resource 'font font-name tam)))
230     (if (null font)
231         (progn (setq font (make-font :address (TTF_OpenFont font-name tam)))
232                (set-resource 'font font font-name tam)))
233     font))
234
235
236 (defun render-text (text-message
237                     &key (color '(:red 255 :green 255 :blue 255))
238                     (font-name "lazy.ttf") (tam 28))
239   (init-ttf)
240   (let ((message (get-resource 'text text-message color font-name tam)))
241     (if (null message)
242         (progn
243           (setq message
244                 (make-surface
245                  :address (render-text2 (open-font font-name tam)
246                                         text-message
247                                         (getf color :red)
248                                         (getf color :green)
249                                         (getf color :blue))))
250           (set-resource 'text message text-message color font-name tam)))
251     message))
252
253
254 (defun print-text (x y text-message
255                      &key (color '(:red 255 :green 255 :blue 255))
256                      (font-name "lazy.ttf") (tam 28))
257   (init-video-mode)
258   (init-ttf)
259   (let ((message (render-text text-message :color color :font-name font-name :tam tam)))
260     (print-surface x y message)
261     message))
262
263
264 ;;; Audio Subsystem
265 (let ((audio nil))
266
267   (defun init-audio ()
268     (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
269           (t audio)))
270
271   (defun quit-audio ()
272     (setq audio (Mix_CloseAudio))))
273
274
275 ;;; Resources Manager
276 (defstruct resource address free-function object)
277
278 (let ((resources-table (make-hash-table :test 'equal)))
279
280   (defun set-resource (type object &rest key)
281     (let ((res
282            (cond ((surface-p object)
283                   (make-resource :address (surface-address object)
284                                  :free-function #'SDL_FreeSurface
285                                  :object object))
286                  ((font-p object)
287                   (make-resource :address (font-address object)
288                                  :free-function #'TTF_CloseFont
289                                  :object object))
290                  ((cp-space-p object)
291                   (make-resource :address (cp-space-address object)
292                                  :free-function #'cpSpaceFree
293                                  :object object))
294                  ((cp-body-p object)
295                   (make-resource :address (cp-body-address object)
296                                  :free-function #'cpBodyFree
297                                  :object object))
298                  ((cp-shape-p object)
299                   (make-resource :address (cp-shape-address object)
300                                  :free-function #'cpShapeFree
301                                  :object object))
302                  (t nil))))
303       (cond (res (setf (gethash `(,type ,@key) resources-table) res)))))
304
305   (defun get-resource (type &rest key)
306     (let ((resource (gethash `(,type ,@key) resources-table)))
307       (cond ((null resource) nil)
308             (t (resource-object resource)))))
309
310   (defun free-all-resources ()
311     (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
312              resources-table)
313     (clrhash resources-table)))
314
315
316 ;;; Connection with the GUI
317 (let (socket)
318   (defun connect-to-gui ()
319     (setq socket (si::socket 1984 :host "localhost")))
320
321   (defun eval-from-gui ()
322     (cond ((and socket (listen socket)) (eval (read socket))))))
323
324
325 ;;; GaCeLa Functions
326 ;(defun game-loop (code)
327 ;  (process-events)
328 ;  (cond ((quit?) nil)
329 ;       (t
330 ;        (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
331 ;        (glLoadIdentity)
332 ;        (translate 0 0 *zoom*)
333 ;        (funcall code)
334 ;        (SDL_GL_SwapBuffers)
335 ;        (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
336 ;        (game-loop code))))
337
338 (let (commands)
339   (defun prog-command (command)
340     (setq commands (cons command commands)))
341
342   (defun run-commands ()
343     (cond (commands
344            (let (running)
345              (setq running commands)
346              (setq commands nil)
347              (labels ((run-com (comlst)
348                                (cond (comlst (run-com (cdr comlst))
349                                              (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
350                      (run-com running)))))))
351
352 (defmacro run-game (title &body code)
353   `(let ((fpstemp (make-timer)))
354      (init-video-mode)
355      (SDL_WM_SetCaption ,title "")
356      (process-events)
357      (do () ((quit?))
358          (start-timer fpstemp)
359          (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
360          (glLoadIdentity)
361          ,@code
362          (SDL_GL_SwapBuffers)
363          (let ((frame-time (get-time fpstemp)) (time-per-frame (/ 1000.0 *frames-per-second*)))
364            (cond ((< frame-time time-per-frame)
365                   (SDL_Delay (- time-per-frame frame-time)))))
366 ;        (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
367          (process-events)
368          (setq running nil))))
369
370 ;(defun run-game ()
371 ;  (init-video-mode)
372 ;  (SDL_WM_SetCaption *title-screen* "")
373 ;  (refresh-active-procs)
374 ;  (enjoy!)
375 ;  (do () ((quit?))
376 ;      (process-events)
377 ;      (logic-procs)
378 ;      (motion-procs)
379 ;      (refresh-active-procs)
380 ;      (refresh-screen)
381 ;      (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))))
382
383 (defun quit-game ()
384 ;  (free-all-resources)
385 ;  (quit-audio)
386 ;  (quit-ttf)
387   (quit-video-mode)
388 ;  (quit-all-procs)
389 ;  (clear-events)
390 ;  (quit-events)
391   (quit-sdl))