3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifndef OBJECT_FACTORY_H
20 #define OBJECT_FACTORY_H
25 #include "lisp/lisp.h"
26 #include "special/game_object.h"
27 #include "math/vector.h"
29 using namespace SuperTux;
34 /** Creates a new gameobject from a lisp node.
35 * Remember to delete the objects later
37 virtual GameObject* create_object(const lisp::Lisp& reader) = 0;
39 // hack for now will be removed later
40 virtual GameObject* create_object(const Vector& )
46 typedef std::map<std::string, Factory*> Factories;
47 extern Factories* object_factories;
49 GameObject* create_object(const std::string& name, const lisp::Lisp& reader);
50 GameObject* create_object(const std::string& name, const Vector& pos);
52 /** comment from Matze:
53 * Yes I know macros are evil, but in this specific case they save
54 * A LOT of typing and evil code duplication.
55 * I'll happily acceppt alternatives if someone can present me one that does
56 * not involve typing 4 or more lines for each object class
58 #define IMPLEMENT_FACTORY(CLASS, NAME) \
59 class INTERN_##CLASS##Factory : public Factory \
62 INTERN_##CLASS##Factory() \
64 if(object_factories == 0) \
65 object_factories = new Factories; \
67 object_factories->insert(std::make_pair(NAME, this)); \
70 virtual GameObject* create_object(const lisp::Lisp& reader) \
72 return new CLASS(reader); \
75 static INTERN_##CLASS##Factory factory_##CLASS;