1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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))
29 ;;; SDL Initialization Subsystem
33 (cond ((null initialized) (setq initialized (SDL_Init SDL_INIT_EVERYTHING)))
37 (setq initialized (SDL_Quit))))
41 (defstruct surface address clip-w clip-h shape)
45 (defun init-video-mode (&key (width *width-screen*) (height *height-screen*) (bpp *bpp-screen*))
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))
54 (resize-screen-GL width height))
57 (defun resize-screen (width height bpp)
58 (setq screen (SDL_SetVideoMode width height bpp flags))
59 (resize-screen-GL width height))
61 (defun fill-screen (color)
63 (fill-surface screen (getf color :red) (getf color :green) (getf color :blue)))
66 (cond ((null screen) nil)
67 (t (SDL_Flip screen))))
69 (defun quit-video-mode ()
75 (glShadeModel GL_SMOOTH)
76 (glClearColor 0 0 0 0)
78 (glDepthFunc GL_LEQUAL)
80 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
81 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
84 (defmacro progn-textures (&body code)
87 (glEnable GL_TEXTURE_2D)
88 (setq values (multiple-value-list (progn ,@code)))
89 (glDisable GL_TEXTURE_2D)
90 (apply #'values values)))
92 (defun init-lighting ()
94 (glEnable GL_LIGHTING))
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)
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)
108 (let ((current-color '(1 1 1 1)))
109 (defun get-current-color ()
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)))
116 (defun load-image (image-file &key (transparent-color nil))
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
126 (SDL_MapRGB (surface-format optimized-image)
127 (car transparent-color)
128 (cadr transparent-color)
129 (caddr transparent-color)))
130 optimized-image)))))))
132 (defun load-image2 (image-file &key (transparent-color nil))
133 (let ((address-image (load-image image-file :transparent-color transparent-color)))
135 (lambda (x y) (print-surface x y address-image))
136 (lambda () (SDL_FreeSurface address-image)))))
138 (defun clean-screen ()
139 (fill-screen *background-color*))
141 (defun refresh-screen ()
143 (funcall-procs #'print-mob)
151 (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
155 (setq audio (Mix_CloseAudio))))
158 ;;; Resources Manager
159 (defstruct resource plist free-function time)
161 (defun make-resource-texture (&key filename min-filter mag-filter)
162 `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
164 (defun make-resource-font (&key filename size encoding)
165 `(:type font :filename ,filename :size ,size :enconding ,encoding))
167 (let ((resources-table (make-hash-table :test 'equal)))
169 (defun set-resource (key plist free-function &key static)
170 (setf (gethash key resources-table)
171 (make-resource :plist plist
172 :free-function free-function
173 :time (if static -1 (SDL_GetTicks)))))
175 (defun get-resource (key)
176 (let ((resource (gethash key resources-table)))
177 (cond ((null resource) nil)
178 (t (cond ((/= (resource-time resource) -1)
179 (setf (resource-time resource) (SDL_GetTicks))
180 (setf (gethash key resources-table) resource)))
181 (resource-plist resource)))))
183 (defun free-all-resources ()
184 (maphash (lambda (key res) (funcall (resource-free-function res) (resource-address res)))
186 (clrhash resources-table)))
189 ;;; Connection with the GUI
191 (defun connect-to-gui ()
192 (setq socket (si::socket 1984 :host "localhost")))
194 (defun eval-from-gui ()
195 (cond ((and socket (listen socket)) (eval (read socket))))))
200 (defun prog-command (command)
201 (setq commands (cons command commands)))
203 (defun run-commands ()
206 (setq running commands)
208 (labels ((run-com (comlst)
209 (cond (comlst (run-com (cdr comlst))
210 (eval (read-from-string (concatenate 'string "(progn " (car comlst) ")")))))))
211 (run-com running)))))))
213 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
214 (defun set-frames-per-second (fps)
215 (setq time-per-frame (/ 1000.0 fps)))
217 (defun init-frame-time ()
218 (setq time (SDL_GetTicks)))
220 (defun delay-frame ()
221 (let ((frame-time (- (SDL_GetTicks) time)))
222 (cond ((< frame-time time-per-frame)
223 (SDL_Delay (- time-per-frame frame-time)))))))
226 (defmacro run-game (title &body code)
229 (SDL_WM_SetCaption ,title "")
233 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
240 (setq running nil))))
243 ; (free-all-resources)