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