1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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))
30 ;;; SDL Initialization Subsystem
34 (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
38 (setq initialized (SDL_Quit))))
42 (defstruct surface address clip-w clip-h shape)
46 (defun init-video-mode (&key (width *width-screen*) (height *height-screen*) (bpp *bpp-screen*))
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))
55 (resize-screen-GL width height))
58 (defun resize-screen (width height bpp)
59 (setq screen (SDL_SetVideoMode width height bpp flags))
60 (resize-screen-GL width height))
62 (defun fill-screen (color)
64 (fill-surface screen (getf color :red) (getf color :green) (getf color :blue)))
67 (cond ((null screen) nil)
68 (t (SDL_Flip screen))))
70 (defun create-surface (width height &key (trans-color *transparent-color*))
72 (let ((new-surface (make-surface
73 :address (create-SDL_Surface
74 (surface-address screen)
77 (getf trans-color :red)
78 (getf trans-color :green)
79 (getf trans-color :blue)))))
80 (set-resource 'image new-surface (gentemp))
83 (defun print-surface (x y surface)
84 (apply-surface x y surface screen)
87 (defun quit-video-mode ()
92 (glShadeModel GL_SMOOTH)
93 (glClearColor 0 0 0 0)
95 (glEnable GL_DEPTH_TEST)
96 (glDepthFunc GL_LEQUAL)
98 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
99 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
102 (defun init-textures ()
104 (glEnable GL_TEXTURE_2D))
106 (defun init-lighting ()
108 (glEnable GL_LIGHTING))
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)
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)
122 (defun copy-surface (source)
123 (cond ((surface-p source)
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))
132 (defun load-image (image-file &key (transparent-color nil))
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
142 (SDL_MapRGB (surface-format optimized-image)
143 (car transparent-color)
144 (cadr transparent-color)
145 (caddr transparent-color)))
146 optimized-image)))))))
148 (defun load-image2 (image-file &key (transparent-color nil))
149 (let ((address-image (load-image image-file :transparent-color transparent-color)))
151 (lambda (x y) (print-surface x y address-image))
152 (lambda () (SDL_FreeSurface address-image)))))
154 (defun apply-surface (x y source destination)
155 (let ((offset (SDL_Rect x y 0 0)))
156 (SDL_BlitSurface source 0 destination offset)
160 (defun apply-surface-old (x y source destination &optional (clip nil))
162 (apply-surface2 x y (surface-address source) (surface-address destination) 0 0 0 0 0))
164 (apply-surface2 x y (surface-address source) (surface-address destination) 0 0
165 (surface-clip-w source) (surface-clip-h source) clip))
167 (apply-surface2 x y (surface-address source) (surface-address destination)
168 (first clip) (second clip) (third clip) (fourth clip) 0)))
172 (defun print-image (x y image-file &optional (clip nil))
174 (let ((image (load-image image-file)))
175 (print-surface x y image clip)
179 (defun clean-screen ()
180 (fill-screen *background-color*))
182 (defun refresh-screen ()
184 (funcall-procs #'print-mob)
188 (defun filled-circle (radius &optional (color '(:red 255 :green 255 :blue 255)))
190 (let ((new-surface (create-surface (1+ (* radius 2)) (1+ (* radius 2)))))
191 (sge_FilledCircle (surface-address new-surface)
196 (setf (surface-shape new-surface)
197 `((,radius ,radius) ,radius))
201 (defun filled-rect (width height &optional (color '(:red 255 :green 255 :blue 255)))
203 (let ((new-surface (create-surface width height)))
204 (sge_FilledRect (surface-address new-surface)
209 (setf (surface-shape new-surface)
210 (make-rectangle 0 0 width height))
215 (defstruct font address)
220 (cond ((null ttf) (progn (init-sdl) (setq ttf (TTF_Init))))
224 (setq ttf (TTF_Quit))))
227 (defun open-font (font-name tam)
229 (let ((font (get-resource 'font font-name tam)))
231 (progn (setq font (make-font :address (TTF_OpenFont font-name tam)))
232 (set-resource 'font font font-name tam)))
236 (defun render-text (text-message
237 &key (color '(:red 255 :green 255 :blue 255))
238 (font-name "lazy.ttf") (tam 28))
240 (let ((message (get-resource 'text text-message color font-name tam)))
245 :address (render-text2 (open-font font-name tam)
249 (getf color :blue))))
250 (set-resource 'text message text-message color font-name tam)))
254 (defun print-text (x y text-message
255 &key (color '(:red 255 :green 255 :blue 255))
256 (font-name "lazy.ttf") (tam 28))
259 (let ((message (render-text text-message :color color :font-name font-name :tam tam)))
260 (print-surface x y message)
268 (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
272 (setq audio (Mix_CloseAudio))))
275 ;;; Resources Manager
276 (defstruct resource address free-function object)
278 (let ((resources-table (make-hash-table :test 'equal)))
280 (defun set-resource (type object &rest key)
282 (cond ((surface-p object)
283 (make-resource :address (surface-address object)
284 :free-function #'SDL_FreeSurface
287 (make-resource :address (font-address object)
288 :free-function #'TTF_CloseFont
291 (make-resource :address (cp-space-address object)
292 :free-function #'cpSpaceFree
295 (make-resource :address (cp-body-address object)
296 :free-function #'cpBodyFree
299 (make-resource :address (cp-shape-address object)
300 :free-function #'cpShapeFree
303 (cond (res (setf (gethash `(,type ,@key) resources-table) res)))))
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)))))
310 (defun free-all-resources ()
311 (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
313 (clrhash resources-table)))
316 ;;; Connection with the GUI
318 (defun connect-to-gui ()
319 (setq socket (si::socket 1984 :host "localhost")))
321 (defun eval-from-gui ()
322 (cond ((and socket (listen socket)) (eval (read socket))))))
326 ;(defun game-loop (code)
328 ; (cond ((quit?) nil)
330 ; (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
332 ; (translate 0 0 *zoom*)
334 ; (SDL_GL_SwapBuffers)
335 ; (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
336 ; (game-loop code))))
339 (defun prog-command (command)
340 (setq commands (cons command commands)))
342 (defun run-commands ()
345 (setq running commands)
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)))))))
352 (defmacro run-game (title &body code)
355 (SDL_WM_SetCaption ,title "")
358 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
360 (translate 0 0 *zoom*)
363 (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
365 (setq running nil))))
369 ; (SDL_WM_SetCaption *title-screen* "")
370 ; (refresh-active-procs)
376 ; (refresh-active-procs)
378 ; (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))))
381 ; (free-all-resources)