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