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 (when (not (find-package 'gacela))
19 (make-package 'gacela :nicknames '(gg) :use '(lisp)))
21 (eval-when (eval) (in-package 'gacela :nicknames '(gg) :use '(lisp)))
23 ;;; Default values for Gacela
24 (defvar *width-screen* 640)
25 (defvar *height-screen* 480)
26 (defvar *bpp-screen* 32)
27 (defvar *frames-per-second* 20)
29 ;;; SDL Initialization Subsystem
33 (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
37 (setq initialized (SDL_Quit))))
41 (let (screen flags (current-width *width-screen*) (current-height *height-screen*) current-bpp)
43 (defun init-video-mode (&key (width current-width) (height current-height) (bpp *bpp-screen*))
46 (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1)
47 (setq flags (+ SDL_OPENGL SDL_GL_DOUBLEBUFFER SDL_HWPALETTE SDL_RESIZABLE
48 (if (= (getf (SDL_GetVideoInfo) :hw_available) 0) SDL_SWSURFACE SDL_HWSURFACE)
49 (if (= (getf (SDL_GetVideoInfo) :blit_hw) 0) 0 SDL_HWACCEL)))
50 (setq screen (SDL_SetVideoMode width height bpp flags))
52 (resize-screen-GL width height)
53 (setq current-width width current-height height current-bpp bpp))
56 (defun resize-screen (width height &optional (bpp current-bpp))
57 (cond (screen (setq screen (SDL_SetVideoMode width height bpp flags))
58 (resize-screen-GL width height)))
59 (setq current-width width current-height height))
61 (defun apply-mode-change ()
62 (resize-screen-GL current-width current-height))
64 (defun quit-video-mode ()
70 (glDisable GL_DEPTH_TEST)
71 (apply-mode-change))))
74 (cond ((not (3d-mode?))
77 (glEnable GL_DEPTH_TEST)
78 (glDepthFunc GL_LEQUAL)
79 (apply-mode-change))))
82 (eq (getf (get-game-properties) :mode) '3d))
85 (glShadeModel GL_SMOOTH)
86 (glClearColor 0 0 0 0)
88 ; (glDepthFunc GL_LEQUAL)
90 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
91 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
94 (defun init-lighting ()
96 (glEnable GL_LIGHTING))
98 (defun resize-screen-GL (width height)
99 (glViewPort 0 0 width height)
100 (glMatrixMode GL_PROJECTION)
102 (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
103 (gluPerspective 45 ratio 0.1 100))) ;0.1
104 (t (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
105 (glOrtho -w w -h h 0 1))))
106 (glMatrixMode GL_MODELVIEW)
110 (let ((current-color '(1 1 1 1)))
111 (defun get-current-color ()
114 (defun set-current-color (red green blue &optional (alpha 1))
115 (setq current-color (list red green blue alpha))
116 (glColor4f red green blue alpha)))
118 (defun load-image (image-file &key (transparent-color nil))
120 (let ((loaded-image (IMG_Load image-file)))
121 (cond ((= loaded-image 0) nil)
122 (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
123 (SDL_FreeSurface loaded-image)
124 (cond ((= optimized-image 0) nil)
125 ((null transparent-color) optimized-image)
126 (t (SDL_SetColorKey optimized-image
128 (SDL_MapRGB (surface-format optimized-image)
129 (car transparent-color)
130 (cadr transparent-color)
131 (caddr transparent-color)))
132 optimized-image)))))))
139 (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
143 (setq audio (Mix_CloseAudio))))
146 ;;; Resources Manager
147 (defstruct resource plist constructor destructor time)
149 (defun make-resource-texture (&key filename min-filter mag-filter)
150 `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
152 (defun make-resource-font (&key filename encoding)
153 `(:type font :filename ,filename :enconding ,encoding))
155 (defmacro get-rtime (key)
156 `(resource-time (gethash ,key resources-table)))
158 (defmacro get-rplist (key)
159 `(resource-plist (gethash ,key resources-table)))
161 (defmacro get-rconstructor (key)
162 `(resource-constructor (gethash ,key resources-table)))
164 (defmacro get-rdestructor (key)
165 `(resource-destructor (gethash ,key resources-table)))
167 (let ((resources-table (make-hash-table :test 'equal))
168 (expiration-time 50000))
170 (defun set-expiration-time (new-time)
171 (setq expiration-time new-time))
173 (defun set-resource (key plist constructor destructor &key static)
175 (setf (gethash key resources-table)
176 (make-resource :plist plist
177 :constructor constructor
178 :destructor destructor
179 :time (if static t (SDL_GetTicks)))))
181 (defun get-resource (key)
182 (cond ((null (gethash key resources-table)) nil)
183 (t (let ((time (get-rtime key)))
184 (cond ((null time) (funcall (get-rconstructor key)))
185 ((numberp time) (setf (get-rtime key) (SDL_GetTicks))))
188 (defun free-resource (key)
189 (funcall (get-rdestructor key))
190 (setf (get-rtime key) nil))
192 (defun expire-resource (key &optional (now (SDL_GetTicks)))
193 (let ((time (get-rtime key)))
194 (cond ((and (numberp time) (> (- now time) expiration-time)) (free-resource key)))))
196 (defun expire-resources ()
197 (maphash (lambda (key res) (expire-resource key)) resources-table))
199 (defun free-all-resources ()
200 (maphash (lambda (key res) (free-resource key)) resources-table)))
203 ;;; Connection with Gacela Clients
204 (let (server-socket clients)
205 (defun start-server (port)
206 (cond ((null server-socket) (setq server-socket (si::socket port :server #'check-connections)))))
208 (defun check-connections ()
209 (cond ((and server-socket (listen server-socket)) (setq clients (cons (si::accept server-socket) clients)))))
211 (defun eval-from-clients ()
212 (labels ((eval-clients (cli-socks)
214 (let ((cli (car cli-socks)))
215 (cond ((si::listen cli)
216 (secure-block cli (eval (read cli)))
218 (eval-clients (cdr cli-socks)))
220 (cons cli (eval-clients (cdr cli-socks))))))))))
221 (setq clients (eval-clients clients))))
223 (defun stop-server ()
224 (cond (server-socket (si::close server-socket) (setq server-socket nil)))
226 (labels ((close-clients (cli-socks)
227 (si::close (car cli-socks))
228 (close-clients (cdr cli-socks))))
229 (close-clients clients))
230 (setq clients nil)))))
234 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
235 (defun set-frames-per-second (fps)
236 (setq time-per-frame (/ 1000.0 fps)))
238 (defun init-frame-time ()
239 (setq time (SDL_GetTicks)))
241 (defun delay-frame ()
242 (let ((frame-time (- (SDL_GetTicks) time)))
243 (cond ((< frame-time time-per-frame)
244 (SDL_Delay (- time-per-frame frame-time)))))))
247 (let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
248 (defun set-game-properties (&key title width height bpp fps mode)
250 (when title (progn (setq ptitle title) (SDL_WM_SetCaption title "")))
251 (when (or width height bpp)
253 (when width (setq pwidth width))
254 (when height (setq pheight height))
255 (when bpp (setq pbpp bpp))
256 (resize-screen pwidth pheight pbpp)))
257 (when fps (progn (setq pfps fps) (set-frames-per-second fps)))
258 (when mode (progn (setq pmode mode) (if (eq mode '3d) (set-3d-mode) (set-2d-mode))))
259 (get-game-properties))
261 (defun get-game-properties ()
262 (list :title ptitle :width pwidth :height pheight :bpp pbpp :fps pfps :mode pmode)))
265 (defmacro run-game (&body code)
266 `(let ((game-function (lambda () ,@code)))
268 (set-game-code game-function)
269 (cond ((not (game-running?))
274 (let (running game-code)
278 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
280 (when (functionp game-code) (funcall game-code))
289 (defun game-running? ()
292 (defun set-game-code (game-function)
293 (setq game-code game-function)))