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