[Super-tux-commit] supertux/src object_factory.cpp,NONE,1.1 object_factory.h,NONE,1.1 leveleditor.cp
Brought to you by:
wkendrick
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28891/src Modified Files: leveleditor.cpp misc.h resources.cpp sector.cpp Added Files: object_factory.cpp object_factory.h Removed Files: gameobjs_bridge.cpp gameobjs_bridge.h Log Message: implemented a new object factory mechanism which is now really independent of the objects --- NEW FILE: object_factory.h --- // $Id: object_factory.h,v 1.1 2004/12/20 21:24:25 matzebraun Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Ricardo Cruz <ri...@ae...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef OBJECT_FACTORY_H #define OBJECT_FACTORY_H #include <string> #include <map> #include "lisp/lisp.h" #include "special/game_object.h" #include "math/vector.h" using namespace SuperTux; class Factory { public: /** Creates a new gameobject from a lisp node. * Remember to delete the objects later */ virtual GameObject* create_object(const lisp::Lisp& reader) = 0; // hack for now will be removed later virtual GameObject* create_object(const Vector& ) { return 0; } }; typedef std::map<std::string, Factory*> Factories; extern Factories* object_factories; GameObject* create_object(const std::string& name, const lisp::Lisp& reader); GameObject* create_object(const std::string& name, const Vector& pos); /** comment from Matze: * Yes I know macros are evil, but in this specific case they save * A LOT of typing and evil code duplication. * I'll happily acceppt alternatives if someone can present me one that does * not involve typing 4 or more lines for each object class */ #define IMPLEMENT_FACTORY(CLASS, NAME) \ class INTERN_##CLASS##Factory : public Factory \ { \ public: \ INTERN_##CLASS##Factory() \ { \ if(object_factories == 0) \ object_factories = new Factories; \ \ object_factories->insert(std::make_pair(NAME, this)); \ } \ \ virtual GameObject* create_object(const lisp::Lisp& reader) \ { \ return new CLASS(reader); \ } \ }; \ static INTERN_##CLASS##Factory factory_##CLASS; #endif --- NEW FILE: object_factory.cpp --- // $Id: object_factory.cpp,v 1.1 2004/12/20 21:24:25 matzebraun Exp $ // // SuperTux - A Jump'n Run // Copyright (C) 2004 Ricardo Cruz <ri...@ae...> // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <config.h> #include <sstream> #include <stdexcept> #include "object_factory.h" Factories* object_factories = 0; GameObject* create_object(const std::string& name, const lisp::Lisp& reader) { Factories::iterator i = object_factories->find(name); if(i == object_factories->end()) { std::stringstream msg; msg << "No factory for object '" << name << "' found."; throw std::runtime_error(msg.str()); } return i->second->create_object(reader); } Index: sector.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/sector.cpp,v retrieving revision 1.61 retrieving revision 1.62 diff -u -d -r1.61 -r1.62 --- sector.cpp 19 Dec 2004 16:57:27 -0000 1.61 +++ sector.cpp 20 Dec 2004 21:24:25 -0000 1.62 @@ -44,30 +44,17 @@ #include "statistics.h" #include "collision_grid.h" #include "collision_grid_iterator.h" +#include "object_factory.h" #include "special/collision.h" #include "math/rectangle.h" #include "math/aatriangle.h" #include "object/coin.h" #include "object/block.h" #include "object/invisible_block.h" -#include "object/platform.h" #include "object/bullet.h" -#include "object/rock.h" #include "badguy/jumpy.h" -#include "badguy/snowball.h" -#include "badguy/bouncing_snowball.h" -#include "badguy/flame.h" -#include "badguy/flyingsnowball.h" -#include "badguy/mriceblock.h" -#include "badguy/mrbomb.h" -#include "badguy/dispenser.h" #include "badguy/spike.h" -#include "badguy/spiky.h" -#include "badguy/nolok_01.h" -#include "trigger/door.h" #include "trigger/sequence_trigger.h" -#include "trigger/secretarea_trigger.h" -#include "gameobjs_bridge.h" Sector* Sector::_current = 0; @@ -105,14 +92,10 @@ GameObject* Sector::parse_object(const std::string& name, const lisp::Lisp& reader) { - if(name == "background") { - return new Background(reader); - } else if(name == "camera") { + if(name == "camera") { Camera* camera = new Camera(this); camera->parse(reader); return camera; - } else if(name == "tilemap") { - return new TileMap(reader); } else if(name == "particles-snow") { SnowParticleSystem* partsys = new SnowParticleSystem(); partsys->parse(reader); @@ -121,14 +104,16 @@ CloudParticleSystem* partsys = new CloudParticleSystem(); partsys->parse(reader); return partsys; - } else if(name == "secretarea") { - return new SecretAreaTrigger(reader); - } else if(name == "sequencetrigger") { - return new SequenceTrigger(reader); - } else if(is_object(name)) { - return create_object(object_name_to_type(name), reader); - } else - std::cerr << "Unknown object type '" << name << "'.\n"; + } else if(name == "money") { // for compatibility with old maps + return new Jumpy(reader); + } + + try { + return create_object(name, reader); + } catch(std::exception& e) { + std::cerr << e.what() << "\n"; + } + return 0; } --- gameobjs_bridge.cpp DELETED --- Index: leveleditor.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/leveleditor.cpp,v retrieving revision 1.174 retrieving revision 1.175 diff -u -d -r1.174 -r1.175 --- leveleditor.cpp 19 Dec 2004 16:57:27 -0000 1.174 +++ leveleditor.cpp 20 Dec 2004 21:24:22 -0000 1.175 @@ -34,11 +34,11 @@ #include "tile_manager.h" #include "sector.h" #include "gameloop.h" +#include "object_factory.h" #include "object/gameobjs.h" #include "object/camera.h" #include "object/tilemap.h" #include "object/background.h" -#include "gameobjs_bridge.h" LevelEditor::LevelEditor() { @@ -119,13 +119,14 @@ } gameobjs_first_id = id; - for(id = 0; id < TOTAL_GAMEOBJECTS; id++) - { + for(Factories::iterator i = object_factories->begin(); + i != object_factories->end(); ++i) { + // Surface *img = badguy.get_image(); // FIXME: get image from object. Using the rubber in the meanwhile. - tiles_board->add_button(Button(img_rubber_bt, object_type_to_string(id), - SDLKey(SDLK_1+id)), id+gameobjs_first_id); - } + tiles_board->add_button(Button(img_rubber_bt, i->first, + SDLKey(SDLK_1+id)), id++); + } tiles_layer = new ButtonGroup(Vector(12, screen->h-64), Vector(80,20), Vector(1,3)); tiles_layer->add_button(Button(img_foreground_bt, _("Edtit foreground tiles"), @@ -844,7 +845,8 @@ level_changed = true; if(zoom != 1) - { // no need to do this for normal view (no zoom) + { + // no need to do this for normal view (no zoom) x = (int)(x * (zoom*32) / 32); y = (int)(y * (zoom*32) / 32); } @@ -854,9 +856,20 @@ // remove an active tile or object that might be there change(x, y, 0, LAYER_TILES); - create_object((GameObjectsType)(newtile-gameobjs_first_id),Vector(x,y)); - - sector->update_game_objects(); + int id = 0; + GameObject* object = 0; + for(Factories::iterator i = object_factories->begin(); i != + object_factories->end(); ++i) { + if(id == newtile - gameobjs_first_id) { + object = i->second->create_object(Vector(x, y)); + break; + } + id++; + } + if(object) { + sector->add_object(object); + sector->update_game_objects(); + } } else if(cur_layer == LAYER_FOREGROUNDTILES) { foregrounds->change(x/32, y/32, newtile); } else if(cur_layer == LAYER_TILES) { --- gameobjs_bridge.h DELETED --- Index: misc.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/misc.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- misc.h 28 Nov 2004 14:56:49 -0000 1.5 +++ misc.h 20 Dec 2004 21:24:25 -0000 1.6 @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - #ifndef SUPERTUX_MISC_H #define SUPERTUX_MISC_H @@ -51,4 +50,3 @@ void st_menu_free(); #endif //SUPERTUX_MISC_H - Index: resources.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/resources.cpp,v retrieving revision 1.62 retrieving revision 1.63 diff -u -d -r1.62 -r1.63 --- resources.cpp 19 Dec 2004 13:34:11 -0000 1.62 +++ resources.cpp 20 Dec 2004 21:24:25 -0000 1.63 @@ -154,7 +154,6 @@ true); /* Sound effects: */ - for (i = 0; i < NUM_SOUNDS; i++) SoundManager::get()->add_sound(SoundManager::get ()->load_sound(datadir + soundfilenames[i]),i); |