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 *frames-per-second* 20)
26 (defvar *transparent-color* '(:red 0 :green 0 :blue 0))
27 (defvar *background-color* '(:red 0 :green 0 :blue 0))
29 ;;; SDL Initialization Subsystem
33 (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
37 (setq initialized (SDL_Quit))))
41 (defstruct surface address clip-w clip-h shape)
45 (defun init-video-mode (&key (width *width-screen*) (height *height-screen*) (bpp *bpp-screen*))
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))
54 (resize-screen-GL width height))
57 (defun resize-screen (width height bpp)
58 (setq screen (SDL_SetVideoMode width height bpp flags))
59 (resize-screen-GL width height))
61 (defun fill-screen (color)
63 (fill-surface screen (getf color :red) (getf color :green) (getf color :blue)))
66 (cond ((null screen) nil)
67 (t (SDL_Flip screen))))
69 (defun create-surface (width height &key (trans-color *transparent-color*))
71 (let ((new-surface (make-surface
72 :address (create-SDL_Surface
73 (surface-address screen)
76 (getf trans-color :red)
77 (getf trans-color :green)
78 (getf trans-color :blue)))))
79 (set-resource 'image new-surface (gentemp))
82 (defun print-surface (x y surface)
83 (apply-surface x y surface screen)
86 (defun quit-video-mode ()
92 (glShadeModel GL_SMOOTH)
93 (glClearColor 0 0 0 0)
95 (glDepthFunc GL_LEQUAL)
97 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
98 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
101 (defun init-textures ()
103 (glEnable GL_TEXTURE_2D))
105 (defun init-lighting ()
107 (glEnable GL_LIGHTING))
109 (defun resize-screen-GL (width height)
110 (let ((ratio (if (= height 0) width (/ width height))))
111 ; (glViewPort 0 0 width height)
112 (glMatrixMode GL_PROJECTION)
114 (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
115 (glOrtho -w w -h h 0 1))
116 ; (gluPerspective 45 ratio 0.1 100)
117 (glMatrixMode GL_MODELVIEW)
121 (defun copy-surface (source)
122 (cond ((surface-p source)
124 (make-surface :address (copy-SDL_Surface (surface-address source))
125 :clip-w (surface-clip-w source)
126 :clip-h (surface-clip-h source)
127 :shape (surface-shape source))))
128 (set-resource 'image new-surface (gentemp))
131 (defun load-image (image-file &key (transparent-color nil))
133 (let ((loaded-image (IMG_Load image-file)))
134 (cond ((= loaded-image 0) nil)
135 (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
136 (SDL_FreeSurface loaded-image)
137 (cond ((= optimized-image 0) nil)
138 ((null transparent-color) optimized-image)
139 (t (SDL_SetColorKey optimized-image
141 (SDL_MapRGB (surface-format optimized-image)
142 (car transparent-color)
143 (cadr transparent-color)
144 (caddr transparent-color)))
145 optimized-image)))))))
147 (defun load-image2 (image-file &key (transparent-color nil))
148 (let ((address-image (load-image image-file :transparent-color transparent-color)))
150 (lambda (x y) (print-surface x y address-image))
151 (lambda () (SDL_FreeSurface address-image)))))
153 (defun apply-surface (x y source destination)
154 (let ((offset (SDL_Rect x y 0 0)))
155 (SDL_BlitSurface source 0 destination offset)
159 (defun apply-surface-old (x y source destination &optional (clip nil))
161 (apply-surface2 x y (surface-address source) (surface-address destination) 0 0 0 0 0))
163 (apply-surface2 x y (surface-address source) (surface-address destination) 0 0
164 (surface-clip-w source) (surface-clip-h source) clip))
166 (apply-surface2 x y (surface-address source) (surface-address destination)
167 (first clip) (second clip) (third clip) (fourth clip) 0)))
171 (defun print-image (x y image-file &optional (clip nil))
173 (let ((image (load-image image-file)))
174 (print-surface x y image clip)
178 (defun clean-screen ()
179 (fill-screen *background-color*))
181 (defun refresh-screen ()
183 (funcall-procs #'print-mob)
188 (defstruct font address)
193 (cond ((null ttf) (progn (init-sdl) (setq ttf (TTF_Init))))
197 (setq ttf (TTF_Quit))))
200 (defun open-font (font-name tam)
202 (let ((font (get-resource 'font font-name tam)))
204 (progn (setq font (make-font :address (TTF_OpenFont font-name tam)))
205 (set-resource 'font font font-name tam)))
209 (defun render-text (text-message
210 &key (color '(:red 255 :green 255 :blue 255))
211 (font-name "lazy.ttf") (tam 28))
213 (let ((message (get-resource 'text text-message color font-name tam)))
218 :address (render-text2 (open-font font-name tam)
222 (getf color :blue))))
223 (set-resource 'text message text-message color font-name tam)))
227 (defun print-text (x y text-message
228 &key (color '(:red 255 :green 255 :blue 255))
229 (font-name "lazy.ttf") (tam 28))
232 (let ((message (render-text text-message :color color :font-name font-name :tam tam)))
233 (print-surface x y message)
241 (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
245 (setq audio (Mix_CloseAudio))))
248 ;;; Resources Manager
249 (defstruct resource address free-function object)
251 (let ((resources-table (make-hash-table :test 'equal)))
253 (defun set-resource (type object &rest key)
255 (cond ((surface-p object)
256 (make-resource :address (surface-address object)
257 :free-function #'SDL_FreeSurface
260 (make-resource :address (font-address object)
261 :free-function #'TTF_CloseFont
264 (make-resource :address (cp-space-address object)
265 :free-function #'cpSpaceFree
268 (make-resource :address (cp-body-address object)
269 :free-function #'cpBodyFree
272 (make-resource :address (cp-shape-address object)
273 :free-function #'cpShapeFree
276 (cond (res (setf (gethash `(,type ,@key) resources-table) res)))))
278 (defun get-resource (type &rest key)
279 (let ((resource (gethash `(,type ,@key) resources-table)))
280 (cond ((null resource) nil)
281 (t (resource-object resource)))))
283 (defun free-all-resources ()
284 (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
286 (clrhash resources-table)))
289 ;;; Connection with the GUI
291 (defun connect-to-gui ()
292 (setq socket (si::socket 1984 :host "localhost")))
294 (defun eval-from-gui ()
295 (cond ((and socket (listen socket)) (eval (read socket))))))
299 ;(defun game-loop (code)
301 ; (cond ((quit?) nil)
303 ; (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
305 ; (translate 0 0 *zoom*)
307 ; (SDL_GL_SwapBuffers)
308 ; (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))
309 ; (game-loop code))))
312 (defun prog-command (command)
313 (setq commands (cons command commands)))
315 (defun run-commands ()
318 (setq running commands)
320 (labels ((run-com (comlst)
321 (cond (comlst (run-com (cdr comlst))
322 (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
323 (run-com running)))))))
325 (let ((gacela-timer (make-timer)))
326 (defun start-gacela-timer () (start-timer gacela-timer))
327 (defun get-gacela-time () (get-time gacela-timer)))
329 (defmacro run-game (title &body code)
332 (SDL_WM_SetCaption ,title "")
336 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
340 (let ((frame-time (get-gacela-time)) (time-per-frame (/ 1000.0 *frames-per-second*)))
341 (cond ((< frame-time time-per-frame)
342 (SDL_Delay (- time-per-frame frame-time)))))
344 (setq running nil))))
348 ; (SDL_WM_SetCaption *title-screen* "")
349 ; (refresh-active-procs)
355 ; (refresh-active-procs)
357 ; (SDL_Delay (- *gacela-freq* (rem (SDL_GetTicks) *gacela-freq*)))))
360 ; (free-all-resources)