]> 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 (defun init-lighting ()
85   (init-video-mode)
86   (glEnable GL_LIGHTING))
87
88 (defun resize-screen-GL (width height)
89   (let ((ratio (if (= height 0) width (/ width height))))
90 ;    (glViewPort 0 0 width height)
91     (glMatrixMode GL_PROJECTION)
92     (glLoadIdentity)
93     (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
94       (glOrtho -w w -h h 0 1))
95 ;    (gluPerspective 45 ratio 0.1 100)
96     (glMatrixMode GL_MODELVIEW)
97     (glLoadIdentity)
98     t))
99
100 (let ((current-color '(1 1 1 1)))
101   (defun get-current-color ()
102     current-color)
103
104   (defun set-current-color (red green blue &optional (alpha 1))
105     (setq current-color (list red green blue alpha))
106     (glColor4f red green blue alpha)))
107
108 (defun load-image (image-file &key (transparent-color nil))
109   (init-video-mode)
110   (let ((loaded-image (IMG_Load image-file)))
111     (cond ((= loaded-image 0) nil)
112           (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
113                (SDL_FreeSurface loaded-image)
114                (cond ((= optimized-image 0) nil)
115                      ((null transparent-color) optimized-image)
116                      (t (SDL_SetColorKey optimized-image
117                                          SDL_SRCCOLORKEY
118                                          (SDL_MapRGB (surface-format optimized-image)
119                                                      (car transparent-color)
120                                                      (cadr transparent-color)
121                                                      (caddr transparent-color)))
122                         optimized-image)))))))
123
124 (defun load-image2 (image-file &key (transparent-color nil))
125   (let ((address-image (load-image image-file :transparent-color transparent-color)))
126     (list
127      (lambda (x y) (print-surface x y address-image))
128      (lambda () (SDL_FreeSurface address-image)))))
129
130 (defun clean-screen ()
131   (fill-screen *background-color*))
132
133 (defun refresh-screen ()
134   (clean-screen)
135   (funcall-procs #'print-mob)
136   (flip))
137
138
139 ;;; Audio Subsystem
140 (let ((audio nil))
141
142   (defun init-audio ()
143     (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
144           (t audio)))
145
146   (defun quit-audio ()
147     (setq audio (Mix_CloseAudio))))
148
149
150 ;;; Resources Manager
151 (defstruct resource plist constructor destructor time)
152
153 (defun make-resource-texture (&key filename min-filter mag-filter)
154   `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
155
156 (defun make-resource-font (&key filename encoding)
157   `(:type font :filename ,filename :enconding ,encoding))
158
159 (let ((resources-table (make-hash-table :test 'equal)))
160
161   (defun set-resource (key plist constructor destructor &key static)
162     (setf (gethash key resources-table)
163           (make-resource :plist plist
164                          :constructor constructor
165                          :destructor destructor
166                          :time (if static t (SDL_GetTicks)))))
167
168   (defun get-resource (key)
169     (let ((resource (gethash key resources-table)))
170       (cond ((null resource) nil)
171             (t (cond ((/= (resource-time resource) -1)
172                       (setf (resource-time resource) (SDL_GetTicks))
173                       (setf (gethash key resources-table) resource)))
174                (resource-plist resource)))))
175
176   (defun free-resource (key)
177     (funcall (resource-destructor (gethash key resources-table)))
178     (setf (resource-time (gethash key resources-table)) nil))
179
180   (defun free-all-resources ()
181     (maphash (lambda (key res) (free-resource key)) resources-table)))
182
183
184 ;;; Connection with the GUI
185 (let (socket)
186   (defun connect-to-gui ()
187     (setq socket (si::socket 1984 :host "localhost")))
188
189   (defun eval-from-gui ()
190     (cond ((and socket (listen socket)) (eval (read socket))))))
191
192
193 ;;; GaCeLa Functions
194 (let (commands)
195   (defun prog-command (command)
196     (setq commands (cons command commands)))
197
198   (defun run-commands ()
199     (cond (commands
200            (let (running)
201              (setq running commands)
202              (setq commands nil)
203              (labels ((run-com (comlst)
204                                (cond (comlst (run-com (cdr comlst))
205                                              (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
206                      (run-com running)))))))
207
208 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
209   (defun set-frames-per-second (fps)
210     (setq time-per-frame (/ 1000.0 fps)))
211
212   (defun init-frame-time ()
213     (setq time (SDL_GetTicks)))
214
215   (defun delay-frame ()
216     (let ((frame-time (- (SDL_GetTicks) time)))
217       (cond ((< frame-time time-per-frame)
218              (SDL_Delay (- time-per-frame frame-time)))))))
219       
220
221 (defmacro run-game (title &body code)
222   `(progn
223      (init-video-mode)
224      (SDL_WM_SetCaption ,title "")
225      (init-frame-time)
226      (process-events)
227      (do () ((quit?))
228          (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
229          (glLoadIdentity)
230          ,@code
231          (SDL_GL_SwapBuffers)
232          (delay-frame)
233          (init-frame-time)
234          (process-events)
235          (setq running nil))))
236
237 (defun quit-game ()
238 ;  (free-all-resources)
239 ;  (quit-audio)
240 ;  (quit-ttf)
241   (quit-video-mode)
242 ;  (quit-all-procs)
243 ;  (clear-events)
244 ;  (quit-events)
245   (quit-sdl))