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/>.
18 (eval-when (compile load) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
20 (eval-when (compile load eval)
21 (when (not (find-package 'gacela)) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
22 (in-package 'gacela :nicknames '(gg) :use '(lisp)))
25 ;;; Default values for Gacela
26 (defvar *width-screen* 640)
27 (defvar *height-screen* 480)
28 (defvar *bpp-screen* 32)
29 (defvar *frames-per-second* 20)
31 ;;; SDL Initialization Subsystem
35 (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
39 (setq initialized (SDL_Quit))))
43 (let (screen flags (current-width *width-screen*) (current-height *height-screen*) current-bpp)
45 (defun init-video-mode (&key (width current-width) (height current-height) (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)
55 (setq current-width width current-height height current-bpp bpp))
58 (defun resize-screen (width height &optional (bpp current-bpp))
59 (cond (screen (setq screen (SDL_SetVideoMode width height bpp flags))
60 (resize-screen-GL width height)))
61 (setq current-width width current-height height))
63 (defun apply-mode-change ()
64 (resize-screen-GL current-width current-height))
66 (defun quit-video-mode ()
70 (cond ((not (3d-mode?))
72 (glDisable GL_DEPTH_TEST)
73 (apply-mode-change))))
79 (glEnable GL_DEPTH_TEST)
80 (glDepthFunc GL_LEQUAL)
81 (apply-mode-change))))
84 (eq (getf (get-game-properties) :mode) '3d))
87 (glShadeModel GL_SMOOTH)
88 (glClearColor 0 0 0 0)
90 ; (glDepthFunc GL_LEQUAL)
92 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
93 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
96 (defun init-lighting ()
98 (glEnable GL_LIGHTING))
100 (defun resize-screen-GL (width height)
101 (glViewPort 0 0 width height)
102 (glMatrixMode GL_PROJECTION)
104 (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
105 (gluPerspective 45 ratio 0.1 100))) ;0.1
106 (t (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
107 (glOrtho -w w -h h 0 1))))
108 (glMatrixMode GL_MODELVIEW)
112 (let ((current-color '(1 1 1 1)))
113 (defun get-current-color ()
116 (defun set-current-color (red green blue &optional (alpha 1))
117 (setq current-color (list red green blue alpha))
118 (glColor4f red green blue alpha)))
120 (defun load-image (image-file &key (transparent-color nil))
122 (let ((loaded-image (IMG_Load image-file)))
123 (cond ((= loaded-image 0) nil)
124 (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
125 (SDL_FreeSurface loaded-image)
126 (cond ((= optimized-image 0) nil)
127 ((null transparent-color) optimized-image)
128 (t (SDL_SetColorKey optimized-image
130 (SDL_MapRGB (surface-format optimized-image)
131 (car transparent-color)
132 (cadr transparent-color)
133 (caddr transparent-color)))
134 optimized-image)))))))
141 (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
145 (setq audio (Mix_CloseAudio))))
148 ;;; Resources Manager
149 (defstruct resource plist constructor destructor time)
151 (defun make-resource-texture (&key filename min-filter mag-filter)
152 `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
154 (defun make-resource-font (&key filename encoding)
155 `(:type font :filename ,filename :enconding ,encoding))
157 (defun make-resource-sound (&key filename)
158 `(:type sound :filename ,filename))
160 (defun make-resource-music (&key filename)
161 `(:type music :filename ,filename))
163 (defmacro get-rtime (key)
164 `(resource-time (gethash ,key resources-table)))
166 (defmacro get-rplist (key)
167 `(resource-plist (gethash ,key resources-table)))
169 (defmacro get-rconstructor (key)
170 `(resource-constructor (gethash ,key resources-table)))
172 (defmacro get-rdestructor (key)
173 `(resource-destructor (gethash ,key resources-table)))
175 (let ((resources-table (make-hash-table :test 'equal))
176 (expiration-time 50000))
178 (defun set-expiration-time (new-time)
179 (setq expiration-time new-time))
181 (defun set-resource (key plist constructor destructor &key static)
183 (setf (gethash key resources-table)
184 (make-resource :plist plist
185 :constructor constructor
186 :destructor destructor
187 :time (if static t (SDL_GetTicks)))))
189 (defun get-resource (key)
190 (cond ((null (gethash key resources-table)) nil)
191 (t (let ((time (get-rtime key)))
192 (cond ((null time) (funcall (get-rconstructor key)))
193 ((numberp time) (setf (get-rtime key) (SDL_GetTicks))))
196 (defun free-resource (key)
197 (funcall (get-rdestructor key))
198 (setf (get-rtime key) nil))
200 (defun expire-resource (key &optional (now (SDL_GetTicks)))
201 (let ((time (get-rtime key)))
202 (cond ((and (numberp time) (> (- now time) expiration-time)) (free-resource key)))))
204 (defun expire-resources ()
205 (maphash (lambda (key res) (expire-resource key)) resources-table))
207 (defun free-all-resources ()
208 (maphash (lambda (key res) (free-resource key)) resources-table)))
211 ;;; Connection with Gacela Clients
212 (let (server-socket clients)
213 (defun start-server (port)
214 (cond ((null server-socket) (setq server-socket (si::socket port :server #'check-connections)))))
216 (defun check-connections ()
217 (cond ((and server-socket (listen server-socket)) (setq clients (cons (si::accept server-socket) clients)))))
219 (defun eval-from-clients ()
220 (labels ((eval-clients (cli-socks)
222 (let ((cli (car cli-socks)))
223 (cond ((si::listen cli)
224 (secure-block cli (eval (read cli)))
226 (eval-clients (cdr cli-socks)))
228 (cons cli (eval-clients (cdr cli-socks))))))))))
229 (setq clients (eval-clients clients))))
231 (defun stop-server ()
232 (cond (server-socket (si::close server-socket) (setq server-socket nil)))
234 (labels ((close-clients (cli-socks)
235 (si::close (car cli-socks))
236 (close-clients (cdr cli-socks))))
237 (close-clients clients))
238 (setq clients nil)))))
242 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
243 (defun set-frames-per-second (fps)
244 (setq time-per-frame (/ 1000.0 fps)))
246 (defun init-frame-time ()
247 (setq time (SDL_GetTicks)))
249 (defun delay-frame ()
250 (let ((frame-time (- (SDL_GetTicks) time)))
251 (cond ((< frame-time time-per-frame)
252 (SDL_Delay (- time-per-frame frame-time)))))))
255 (let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
256 (defun set-game-properties (&key title width height bpp fps mode)
258 (when title (progn (setq ptitle title) (SDL_WM_SetCaption title "")))
259 (when (or width height bpp)
261 (when width (setq pwidth width))
262 (when height (setq pheight height))
263 (when bpp (setq pbpp bpp))
264 (resize-screen pwidth pheight pbpp)))
265 (when fps (progn (setq pfps fps) (set-frames-per-second fps)))
266 (when mode (progn (setq pmode mode) (if (eq mode '3d) (set-3d-mode) (set-2d-mode))))
267 (get-game-properties))
269 (defun get-game-properties ()
270 (list :title ptitle :width pwidth :height pheight :bpp pbpp :fps pfps :mode pmode)))
273 (defmacro run-game (&body code)
274 `(let ((game-function (lambda () ,@code)))
276 (set-game-code game-function)
277 (cond ((not (game-running?))
282 (let (running game-code)
286 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
288 (when (functionp game-code) (funcall game-code))
297 (defun game-running? ()
300 (defun set-game-code (game-function)
301 (setq game-code game-function)))