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 (eval-when (compile load eval)
19 (when (not (find-package 'gacela)) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
20 (in-package 'gacela :nicknames '(gg) :use '(lisp)))
23 ;;; Behaviours of objects
25 (defmacro make-behaviour (name attr &rest code)
26 `(defun ,(get-behaviour-fun-name name) (object-attr)
27 (let ,(mapcar #'attribute-definition attr)
29 ,(cons 'progn (mapcar #'attribute-save (reverse attr)))
32 (defun get-behaviour-fun-name (name)
33 (intern (concatenate 'string "BEHAVIOUR-" (string-upcase (string name))) 'gacela))
35 (defun attribute-name (attribute)
36 (intern (string attribute) 'keyword))
38 (defun attribute-definition (attribute)
39 (let* ((name (cond ((listp attribute) (car attribute))
41 (pname (attribute-name name))
42 (value (cond ((listp attribute) (cadr attribute)))))
43 `(,name (getf object-attr ,pname ,value))))
45 (defun attribute-save (attribute)
46 (let* ((name (cond ((listp attribute) (car attribute))
48 (pname (attribute-name name)))
49 `(setf (getf object-attr ,pname) ,name)))
55 (let (active-objects objects-to-add objects-to-kill)
56 (defun add-object (object)
57 (pushnew object objects-to-add))
59 (defun kill-object (object)
60 (pushnew object objects-to-kill))
62 (defun kill-all-objects ()
63 (setq active-objects nil objects-to-add nil objects-to-kill nil))
65 (defun refresh-active-objects ()
67 (setq active-objects (union active-objects objects-to-add))
68 (setq objects-to-add nil)))
69 (cond (objects-to-kill
70 (setq active-objects (reverse (set-difference active-objects objects-to-kill)))
71 (setq objects-to-kill nil))))
74 (dolist (o active-objects) (funcall o :action)))
76 (defun render-objects ()
77 (dolist (o active-objects) (funcall o :render))))
80 (defmacro make-object (name attr bhv &body look)
82 (let ((attr ,(cond (attr (cons 'list (make-object-attributes attr)))))
83 (bhv ,(cond (bhv (cons 'list (make-object-behaviour bhv))))))
84 (defun ,name (option &rest param)
86 (:action (dolist (b bhv t) (setq attr (funcall (get-behaviour-fun-name b) attr))))
89 (:set-bhv (setq bhv (car param)))
90 (:render (glPushMatrix)
91 ,@(mapcar (lambda (x) (if (stringp x) `(draw-image ,x) x)) look)
96 (defun make-object-attributes (attr)
97 (cond ((or (null attr) (atom attr)) nil)
98 (t (let ((rest (make-object-attributes (cdr attr)))
99 (this (object-attribute-definition (car attr))))
100 (setf (getf rest (car this)) (cadr this))
103 (defun object-attribute-definition (attribute)
104 (let* ((name (cond ((listp attribute) (car attribute))
106 (pname (attribute-name name))
107 (value (cond ((listp attribute) (cadr attribute)))))
110 (defun make-object-behaviour (bhv)
111 (cond ((null bhv) nil)
112 ((atom bhv) (list bhv))