]> 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 (in-package :gacela)
19
20 ;;; Default values for Gacela
21 (defvar *width-screen* 640)
22 (defvar *height-screen* 480)
23 (defvar *bpp-screen* 32)
24 (defvar *title-screen* "Happy Hacking!!")
25 (defvar *frames-per-second* 20)
26 (defvar *transparent-color* '(:red 0 :green 0 :blue 0))
27 (defvar *background-color* '(:red 0 :green 0 :blue 0))
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 (defstruct surface address clip-w clip-h shape)
42
43 (let (screen flags)
44
45   (defun init-video-mode (&key (width *width-screen*) (height *height-screen*) (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           (t t)))
56
57   (defun resize-screen (width height bpp)
58     (setq screen (SDL_SetVideoMode width height bpp flags))
59     (resize-screen-GL width height))
60
61   (defun fill-screen (color)
62     (init-video-mode)
63     (fill-surface screen (getf color :red) (getf color :green) (getf color :blue)))
64
65   (defun flip ()
66     (cond ((null screen) nil)
67           (t (SDL_Flip screen))))
68
69   (defun quit-video-mode ()
70     (setq screen nil)))
71
72
73 (defun init-GL ()
74   (2d-mode)
75   (glShadeModel GL_SMOOTH)
76   (glClearColor 0 0 0 0)
77   (glClearDepth 1)
78   (glDepthFunc GL_LEQUAL)
79 ;  (glEnable GL_BLEND)
80 ;  (glBlendFunc GL_SRC_ALPHA GL_ONE)
81   (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
82   t)
83
84 (defmacro progn-textures (&body code)
85   `(let (values)
86      (init-video-mode)
87      (glEnable GL_TEXTURE_2D)
88      (setq values (multiple-value-list (progn ,@code)))
89      (glDisable GL_TEXTURE_2D)
90      (apply #'values values)))
91
92 (defun init-lighting ()
93   (init-video-mode)
94   (glEnable GL_LIGHTING))
95
96 (defun resize-screen-GL (width height)
97   (let ((ratio (if (= height 0) width (/ width height))))
98 ;    (glViewPort 0 0 width height)
99     (glMatrixMode GL_PROJECTION)
100     (glLoadIdentity)
101     (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
102       (glOrtho -w w -h h 0 1))
103 ;    (gluPerspective 45 ratio 0.1 100)
104     (glMatrixMode GL_MODELVIEW)
105     (glLoadIdentity)
106     t))
107
108 (let ((current-color '(1 1 1 1)))
109   (defun get-current-color ()
110     current-color)
111
112   (defun set-current-color (red green blue &optional (alpha 1))
113     (setq current-color (list red green blue alpha))
114     (glColor4f red green blue alpha)))
115
116 (defun load-image (image-file &key (transparent-color nil))
117   (init-video-mode)
118   (let ((loaded-image (IMG_Load image-file)))
119     (cond ((= loaded-image 0) nil)
120           (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
121                (SDL_FreeSurface loaded-image)
122                (cond ((= optimized-image 0) nil)
123                      ((null transparent-color) optimized-image)
124                      (t (SDL_SetColorKey optimized-image
125                                          SDL_SRCCOLORKEY
126                                          (SDL_MapRGB (surface-format optimized-image)
127                                                      (car transparent-color)
128                                                      (cadr transparent-color)
129                                                      (caddr transparent-color)))
130                         optimized-image)))))))
131
132 (defun load-image2 (image-file &key (transparent-color nil))
133   (let ((address-image (load-image image-file :transparent-color transparent-color)))
134     (list
135      (lambda (x y) (print-surface x y address-image))
136      (lambda () (SDL_FreeSurface address-image)))))
137
138 (defun clean-screen ()
139   (fill-screen *background-color*))
140
141 (defun refresh-screen ()
142   (clean-screen)
143   (funcall-procs #'print-mob)
144   (flip))
145
146
147 ;;; Audio Subsystem
148 (let ((audio nil))
149
150   (defun init-audio ()
151     (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
152           (t audio)))
153
154   (defun quit-audio ()
155     (setq audio (Mix_CloseAudio))))
156
157
158 ;;; Resources Manager
159 (defstruct resource plist constructor destructor time)
160
161 (defun make-resource-texture (&key filename min-filter mag-filter)
162   `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
163
164 (defun make-resource-font (&key filename size encoding)
165   `(:type font :filename ,filename :size ,size :enconding ,encoding))
166
167 (let ((resources-table (make-hash-table :test 'equal)))
168
169   (defun set-resource (key plist constructor destructor &key static)
170     (setf (gethash key resources-table)
171           (make-resource :plist plist
172                          :constructor constructor
173                          :destructor destructor
174                          :free-function free-function
175                          :time (if static t (SDL_GetTicks)))))
176
177   (defun get-resource (key)
178     (let ((resource (gethash key resources-table)))
179       (cond ((null resource) nil)
180             (t (cond ((/= (resource-time resource) -1)
181                       (setf (resource-time resource) (SDL_GetTicks))
182                       (setf (gethash key resources-table) resource)))
183                (resource-plist resource)))))
184
185   (defun free-all-resources ()
186     (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
187              resources-table)
188     (clrhash resources-table)))
189
190
191 ;;; Connection with the GUI
192 (let (socket)
193   (defun connect-to-gui ()
194     (setq socket (si::socket 1984 :host "localhost")))
195
196   (defun eval-from-gui ()
197     (cond ((and socket (listen socket)) (eval (read socket))))))
198
199
200 ;;; GaCeLa Functions
201 (let (commands)
202   (defun prog-command (command)
203     (setq commands (cons command commands)))
204
205   (defun run-commands ()
206     (cond (commands
207            (let (running)
208              (setq running commands)
209              (setq commands nil)
210              (labels ((run-com (comlst)
211                                (cond (comlst (run-com (cdr comlst))
212                                              (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
213                      (run-com running)))))))
214
215 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
216   (defun set-frames-per-second (fps)
217     (setq time-per-frame (/ 1000.0 fps)))
218
219   (defun init-frame-time ()
220     (setq time (SDL_GetTicks)))
221
222   (defun delay-frame ()
223     (let ((frame-time (- (SDL_GetTicks) time)))
224       (cond ((< frame-time time-per-frame)
225              (SDL_Delay (- time-per-frame frame-time)))))))
226       
227
228 (defmacro run-game (title &body code)
229   `(progn
230      (init-video-mode)
231      (SDL_WM_SetCaption ,title "")
232      (init-frame-time)
233      (process-events)
234      (do () ((quit?))
235          (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
236          (glLoadIdentity)
237          ,@code
238          (SDL_GL_SwapBuffers)
239          (delay-frame)
240          (init-frame-time)
241          (process-events)
242          (setq running nil))))
243
244 (defun quit-game ()
245 ;  (free-all-resources)
246 ;  (quit-audio)
247 ;  (quit-ttf)
248   (quit-video-mode)
249 ;  (quit-all-procs)
250 ;  (clear-events)
251 ;  (quit-events)
252   (quit-sdl))