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/>.
18 (in-package :gacela :nicknames '(gg))
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 (let (screen flags current-width current-height)
43 (defun init-video-mode (&key (width *width-screen*) (height *height-screen*) (bpp *bpp-screen*))
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))
52 (resize-screen-GL width height)
53 (setq current-width width current-height height))
56 (defun resize-screen (width height bpp)
57 (setq screen (SDL_SetVideoMode width height bpp flags))
58 (resize-screen-GL width height)
59 (setq current-width width current-height height))
61 (defun apply-mode-change ()
62 (resize-screen-GL current-width current-height))
64 (defun fill-screen (color)
66 (fill-surface screen (getf color :red) (getf color :green) (getf color :blue)))
69 (cond ((null screen) nil)
70 (t (SDL_Flip screen))))
72 (defun quit-video-mode ()
80 (glDisable GL_DEPTH_TEST)
81 (apply-mode-change))))
84 (cond ((not (3d-mode?))
87 (glEnable GL_DEPTH_TEST)
88 (apply-mode-change))))
94 (glShadeModel GL_SMOOTH)
95 (glClearColor 0 0 0 0)
97 (glDepthFunc GL_LEQUAL)
99 ; (glBlendFunc GL_SRC_ALPHA GL_ONE)
100 (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
103 (defun init-lighting ()
105 (glEnable GL_LIGHTING))
107 (defun resize-screen-GL (width height)
108 (glViewPort 0 0 width height)
109 (glMatrixMode GL_PROJECTION)
111 (cond ((3d-mode?) (let ((ratio (if (= height 0) width (/ width height))))
112 (gluPerspective 45 ratio 0.1 100))) ;0.1
113 (t (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
114 (glOrtho -w w -h h 0 1))))
115 (glMatrixMode GL_MODELVIEW)
119 (let ((current-color '(1 1 1 1)))
120 (defun get-current-color ()
123 (defun set-current-color (red green blue &optional (alpha 1))
124 (setq current-color (list red green blue alpha))
125 (glColor4f red green blue alpha)))
127 (defun load-image (image-file &key (transparent-color nil))
129 (let ((loaded-image (IMG_Load image-file)))
130 (cond ((= loaded-image 0) nil)
131 (t (let ((optimized-image (SDL_DisplayFormat loaded-image)))
132 (SDL_FreeSurface loaded-image)
133 (cond ((= optimized-image 0) nil)
134 ((null transparent-color) optimized-image)
135 (t (SDL_SetColorKey optimized-image
137 (SDL_MapRGB (surface-format optimized-image)
138 (car transparent-color)
139 (cadr transparent-color)
140 (caddr transparent-color)))
141 optimized-image)))))))
148 (cond ((null audio) (progn (init-sdl) (setq audio (Mix_OpenAudio 22050 2 4096))))
152 (setq audio (Mix_CloseAudio))))
155 ;;; Resources Manager
156 (defstruct resource plist constructor destructor time)
158 (defun make-resource-texture (&key filename min-filter mag-filter)
159 `(:type texture :filename ,filename :min-filter ,min-filter :mag-filter ,mag-filter))
161 (defun make-resource-font (&key filename encoding)
162 `(:type font :filename ,filename :enconding ,encoding))
164 (defmacro get-rtime (key)
165 `(resource-time (gethash ,key resources-table)))
167 (defmacro get-rplist (key)
168 `(resource-plist (gethash ,key resources-table)))
170 (defmacro get-rconstructor (key)
171 `(resource-constructor (gethash ,key resources-table)))
173 (defmacro get-rdestructor (key)
174 `(resource-destructor (gethash ,key resources-table)))
176 (let ((resources-table (make-hash-table :test 'equal))
177 (expiration-time 50000))
179 (defun set-expiration-time (new-time)
180 (setq expiration-time new-time))
182 (defun set-resource (key plist constructor destructor &key static)
184 (setf (gethash key resources-table)
185 (make-resource :plist plist
186 :constructor constructor
187 :destructor destructor
188 :time (if static t (SDL_GetTicks)))))
190 (defun get-resource (key)
191 (cond ((null (gethash key resources-table)) nil)
192 (t (let ((time (get-rtime key)))
193 (cond ((null time) (funcall (get-rconstructor key)))
194 ((numberp time) (setf (get-rtime key) (SDL_GetTicks))))
197 (defun free-resource (key)
198 (funcall (get-rdestructor key))
199 (setf (get-rtime key) nil))
201 (defun expire-resource (key &optional (now (SDL_GetTicks)))
202 (let ((time (get-rtime key)))
203 (cond ((and (numberp time) (> (- now time) expiration-time)) (free-resource key)))))
205 (defun expire-resources ()
206 (maphash (lambda (key res) (expire-resource key)) resources-table))
208 (defun free-all-resources ()
209 (maphash (lambda (key res) (free-resource key)) resources-table)))
212 ;;; Connection with Gacela Skin
214 (defun start-skin-client (port)
215 (when (null socket) (setq socket (si::socket port :host "localhost"))))
217 (defun eval-from-skin ()
218 (when (si::listen socket)
219 (secure-block socket (eval (read socket)))))
221 (defun stop-skin-client ()
228 (let (time (time-per-frame (/ 1000.0 *frames-per-second*)))
229 (defun set-frames-per-second (fps)
230 (setq time-per-frame (/ 1000.0 fps)))
232 (defun init-frame-time ()
233 (setq time (SDL_GetTicks)))
235 (defun delay-frame ()
236 (let ((frame-time (- (SDL_GetTicks) time)))
237 (cond ((< frame-time time-per-frame)
238 (SDL_Delay (- time-per-frame frame-time)))))))
241 (defmacro run-game (title &body code)
242 `(let ((game-function (lambda () ,@code)))
244 (SDL_WM_SetCaption ,title "")
245 (set-game-code game-function)
246 (cond ((not (game-running?))
251 (let (running game-code)
255 (glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
257 (when (functionp game-code) (funcall game-code))
265 (defun game-running? ()
268 (defun set-game-code (game-function)
269 (setq game-code game-function)))