]> 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 (eval-when (compile load) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
19
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)))
23
24
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)
30
31 ;;; SDL Initialization Subsystem
32 (let (initialized)
33
34   (defun init-sdl ()
35     (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
36           (t initialized)))
37
38   (defun quit-sdl ()
39     (setq initialized (SDL_Quit))))
40
41
42 ;;; Video Subsystem
43 (let (screen flags (current-width *width-screen*) (current-height *height-screen*) current-bpp)
44
45   (defun init-video-mode (&key (width current-width) (height current-height) (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            (setq current-width width current-height height current-bpp bpp))
56           (t t)))
57
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))
62
63   (defun apply-mode-change ()
64     (resize-screen-GL current-width current-height))
65
66   (defun quit-video-mode ()
67     (setq screen nil)))
68
69 (defun set-2d-mode ()
70   (cond ((not (3d-mode?))
71          (init-video-mode)
72          (glDisable GL_DEPTH_TEST)
73          (apply-mode-change))))
74
75 (defun set-3d-mode ()
76   (cond ((3d-mode?)
77          (init-video-mode)
78          (glClearDepth 1)
79          (glEnable GL_DEPTH_TEST)
80          (glDepthFunc GL_LEQUAL)
81          (apply-mode-change))))
82
83 (defun 3d-mode? ()
84   (eq (getf (get-game-properties) :mode) '3d))
85
86 (defun init-GL ()
87   (glShadeModel GL_SMOOTH)
88   (glClearColor 0 0 0 0)
89 ;  (glClearDepth 1)
90 ;  (glDepthFunc GL_LEQUAL)
91 ;  (glEnable GL_BLEND)
92 ;  (glBlendFunc GL_SRC_ALPHA GL_ONE)
93   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
94   t)
95
96 (defun init-lighting ()
97   (init-video-mode)
98   (glEnable GL_LIGHTING))
99
100 (defun resize-screen-GL (width height)
101   (glViewPort 0 0 width height)
102   (glMatrixMode GL_PROJECTION)
103   (glLoadIdentity)
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)
109   (glLoadIdentity)
110   t))
111
112 (let ((current-color '(1 1 1 1)))
113   (defun get-current-color ()
114     current-color)
115
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)))
119
120 (defun load-image (image-file &key (transparent-color nil))
121   (init-video-mode)
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
129                                          SDL_SRCCOLORKEY
130                                          (SDL_MapRGB (surface-format optimized-image)
131                                                      (car transparent-color)
132                                                      (cadr transparent-color)
133                                                      (caddr transparent-color)))
134                         optimized-image)))))))
135
136
137 ;;; Audio Subsystem
138 (let ((audio nil))
139
140   (defun init-audio ()
141     (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 MIX_DEFAULT_FORMAT 2 4096))))
142           (t audio)))
143
144   (defun quit-audio ()
145     (setq audio (Mix_CloseAudio))))
146
147
148 ;;; Resources Manager
149 (defstruct resource plist constructor destructor time)
150
151 (defun make-resource-texture (&key filename min-filter mag-filter)
152   `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
153
154 (defun make-resource-font (&key filename encoding)
155   `(:type font :filename ,filename :enconding ,encoding))
156
157 (defmacro get-rtime (key)
158   `(resource-time (gethash ,key resources-table)))
159
160 (defmacro get-rplist (key)
161   `(resource-plist (gethash ,key resources-table)))
162
163 (defmacro get-rconstructor (key)
164   `(resource-constructor (gethash ,key resources-table)))
165
166 (defmacro get-rdestructor (key)
167   `(resource-destructor (gethash ,key resources-table)))
168
169 (let ((resources-table (make-hash-table :test 'equal))
170       (expiration-time 50000))
171
172   (defun set-expiration-time (new-time)
173     (setq expiration-time new-time))
174
175   (defun set-resource (key plist constructor destructor &key static)
176     (expire-resources)
177     (setf (gethash key resources-table)
178           (make-resource :plist plist
179                          :constructor constructor
180                          :destructor destructor
181                          :time (if static t (SDL_GetTicks)))))
182
183   (defun get-resource (key)
184     (cond ((null (gethash key resources-table)) nil)
185           (t (let ((time (get-rtime key)))
186                (cond ((null time) (funcall (get-rconstructor key)))
187                      ((numberp time) (setf (get-rtime key) (SDL_GetTicks))))
188                (get-rplist key)))))
189
190   (defun free-resource (key)
191     (funcall (get-rdestructor key))
192     (setf (get-rtime key) nil))
193
194   (defun expire-resource (key &optional (now (SDL_GetTicks)))
195     (let ((time (get-rtime key)))
196       (cond ((and (numberp time) (> (- now time) expiration-time)) (free-resource key)))))
197
198   (defun expire-resources ()
199     (maphash (lambda (key res) (expire-resource key)) resources-table))
200
201   (defun free-all-resources ()
202     (maphash (lambda (key res) (free-resource key)) resources-table)))
203
204
205 ;;; Connection with Gacela Clients
206 (let (server-socket clients)
207   (defun start-server (port)
208     (cond ((null server-socket) (setq server-socket (si::socket port :server #'check-connections)))))
209
210   (defun check-connections ()
211     (cond ((and server-socket (listen server-socket)) (setq clients (cons (si::accept server-socket) clients)))))
212
213   (defun eval-from-clients ()
214     (labels ((eval-clients (cli-socks)
215                            (cond (cli-socks
216                                   (let ((cli (car cli-socks)))
217                                     (cond ((si::listen cli)
218                                            (secure-block cli (eval (read cli)))
219                                            (si::close cli)
220                                            (eval-clients (cdr cli-socks)))
221                                           (t
222                                            (cons cli (eval-clients (cdr cli-socks))))))))))
223             (setq clients (eval-clients clients))))
224
225   (defun stop-server ()
226     (cond (server-socket (si::close server-socket) (setq server-socket nil)))
227     (cond (clients
228            (labels ((close-clients (cli-socks)
229                                    (si::close (car cli-socks))
230                                    (close-clients (cdr cli-socks))))
231                    (close-clients clients))
232            (setq clients nil)))))
233
234
235 ;;; GaCeLa Functions
236 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
237   (defun set-frames-per-second (fps)
238     (setq time-per-frame (/ 1000.0 fps)))
239
240   (defun init-frame-time ()
241     (setq time (SDL_GetTicks)))
242
243   (defun delay-frame ()
244     (let ((frame-time (- (SDL_GetTicks) time)))
245       (cond ((< frame-time time-per-frame)
246              (SDL_Delay (- time-per-frame frame-time)))))))
247
248
249 (let ((ptitle "") (pwidth *width-screen*) (pheight *height-screen*) (pbpp *bpp-screen*) (pfps *frames-per-second*) (pmode '2d))
250   (defun set-game-properties (&key title width height bpp fps mode)
251     (init-video-mode)
252     (when title (progn (setq ptitle title) (SDL_WM_SetCaption title "")))
253     (when (or width height bpp)
254       (progn
255         (when width (setq pwidth width))
256         (when height (setq pheight height))
257         (when bpp (setq pbpp bpp))
258         (resize-screen pwidth pheight pbpp)))
259     (when fps (progn (setq pfps fps) (set-frames-per-second fps)))
260     (when mode (progn (setq pmode mode) (if (eq mode '3d) (set-3d-mode) (set-2d-mode))))
261     (get-game-properties))
262
263   (defun get-game-properties ()
264     (list :title ptitle :width pwidth :height pheight :bpp pbpp :fps pfps :mode pmode)))
265
266
267 (defmacro run-game (&body code)
268   `(let ((game-function (lambda () ,@code)))
269      (init-video-mode)
270      (set-game-code game-function)
271      (cond ((not (game-running?))
272             (init-frame-time)
273             (process-events)
274             (game-loop)))))
275
276 (let (running game-code)
277   (defun game-loop ()
278     (setq running t)
279     (do () ((quit?))
280         (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
281         (to-origin)
282         (when (functionp game-code) (funcall game-code))
283         (SDL_GL_SwapBuffers)
284         (delay-frame)
285         (init-frame-time)
286         (check-connections)
287         (eval-from-clients)
288         (process-events))
289     (setq running nil))
290
291   (defun game-running? ()
292     running)
293
294   (defun set-game-code (game-function)
295     (setq game-code game-function)))
296
297 (defun quit-game ()
298   (free-all-resources)
299   (quit-audio)
300   (quit-video-mode)
301   (quit-all-mobs)
302 ;  (clear-events)
303 ;  (quit-events)
304   (quit-sdl))