Changeset 3496 in orxonox.OLD for orxonox/branches/levelloader
- Timestamp:
- Mar 11, 2005, 10:25:27 AM (20 years ago)
- Location:
- orxonox/branches/levelloader/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/levelloader/src/levelfactory.cc
r3482 r3496 19 19 #include "levelfactory.h" 20 20 21 22 21 using namespace std; 23 22 … … 47 46 if( !XMLDoc.LoadFile()) 48 47 { 49 // report an error somehow50 printf("Error loading XML File: %s @ %d:%d\n", XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol());51 delete ( XMLDoc);48 // report an error 49 PRINTF(ERR)("Error loading XML File: %s @ %d:%d\n", XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol()); 50 delete XMLDoc; 52 51 return -1; 53 52 } 54 53 55 54 // get orxonox simpleton 55 Orxonox* orx = Orxonox::getInstance(); 56 56 57 // find the world 58 World world = orx->getWorld; 59 60 // check basic validity 61 TiXMLElement* element = XMLDoc.RootElement(); 62 TiXMLNode* comment = element->FirstChild( COMMENT); 63 64 if( comment == NULL || ( !strcmp( comment->Value(), "OrxoLev"))) 65 { 66 // report an error 67 PRINTF(ERR)("Specified XML File is not an orxonox data file (OrxoLev comment missing)\n"); 68 delete XMLDoc; 69 return -1; 70 } 71 57 72 // start loading objects into world 73 loadFromXMLElement( element), world, NULL); 74 58 75 // free the XML data 76 delete XMLDoc; 77 } 78 79 /** 80 \brief recursive XML document parsing and loading function 81 \param root the XML Element to be parsed 82 \param world the world where the loaded objects shall be spawned 83 \param parent the object that is parent to all objects in current element 84 */ 85 86 void loadFromXMLElement( TiXMLElement* root, World* world, BaseObject* parent) 87 { 88 TiXMLElement *child, *sibling; 89 BaseObject* object; 90 LoadParameters* data; 91 void* data; 92 93 if( root == NULL) 94 { 95 PRINTF(ERR)("LevelFactory tried to load from a NULL element\n"); 96 return; 97 } 98 99 // load root first 100 101 // check for LevelFactory integrity 102 if( first == NULL) 103 { 104 PRINTF(ERR)("LevelFactory does not know any ObjectFactories, loading not possible\n"); 105 return; 106 } 107 108 // gather & compile attributes 109 data = new LoadParameters( root); 110 111 // load the object 112 object = first->load( root->Value(), data); 113 114 // get rid of parameters 115 delete data; 116 117 if( object != NULL) 118 { 119 // set parent 120 121 // spawn object 122 world->spawn( object); 123 PRINTF(INFO)("Successfully loaded a '%s'\n", root->value()); 124 } 125 else 126 { 127 PRINTF(ERR)("No ObjectFactory found to load '%s'\n", root->value()) 128 } 129 130 // walk children 131 child = root->FirstChildElement(); 132 if( child != NULL) loadFromXMLElement( child, world, object); 133 134 // siblings third 135 sibling = root->NextSiblingElement(); 136 if( sibling != NULL) loadFromXMLElement( sibling, world, parent); 59 137 } 60 138 … … 87 165 void LevelFactory::registerFactory( ObjectFactory* factory) 88 166 { 167 // just to see whether it works 168 PRINTF(INFO)("'%s' registered to LevelFactory\n", factory->getClassname()); 89 169 if( first == NULL) first = factory; 90 170 else first->registerFactory( factory); … … 153 233 fct.registerFactory( this); 154 234 } 235 236 /* -------------------------------------------------- 237 * LoadParameters 238 * -------------------------------------------------- 239 */ 240 241 const char* LoadParameters::grabParameter( const char* name) 242 { 243 return root->Attribute( name); 244 } 245 246 const char* LoadParameters::grabParameter( const char* name, int* i) 247 { 248 return root->Attribute( name, i); 249 } 250 251 const char* LoadParameters::grabParameter( const char* name, double* d) 252 { 253 return root->Attribute( name, d); 254 } -
orxonox/branches/levelloader/src/levelfactory.h
r3482 r3496 11 11 #include "xmlparser/tinystr.h" 12 12 #include "base_object.h" 13 #include "orxonox.h" 14 #include "world.h" 15 16 class LoadParameters; 13 17 14 18 #define FACTORY_ADD(CLASS) class CLASSFactory : public ObjectFactory { \ 15 19 public: \ 16 CLASSFactory (){setNext( NULL); setClassname = "CLASS"; initialize() };\20 CLASSFactory (){setNext( NULL); setClassname = "CLASS"; initialize();} \ 17 21 ~CLASSFactory (); \ 18 22 private: \ 19 BaseObject* fabricateObject( void* data) { return new CLASS( data)};\23 BaseObject* fabricateObject( LoadParameters* data) { return new CLASS( data);} \ 20 24 }; \ 21 25 CLASSFactory global_CLASSFactory(); … … 31 35 ~ObjectFactory (); 32 36 33 BaseObject* load( char* name, void* data);37 BaseObject* load( char* name, LoadParameters* data); 34 38 void initialize(); 35 39 void registerFactory( ObjectFactory* factory); 36 40 void setClassname(char* name) {classname = name}; 41 char* getClassname() {return classname}; 37 42 void setNext( ObjectFactory* factory); 38 43 39 44 private: 40 virtual BaseObject* fabricateObject( void* data);45 virtual BaseObject* fabricateObject( LoadParameters* data); 41 46 char* classname; 42 47 … … 68 73 }; 69 74 75 //! The LoadParameters class is designed for encapsulated interfacing between TiXML and a loading costructor 76 /** 77 This is the liaison between the ObjectFactories and their Objects. A LoadParameters object is passed to 78 the constructors of the objects when the ObjectFactory creates it. It can be easily parsed and features 79 special variable handling methods to extract numeric data from strings. 80 */ 81 class LoadParameters { 82 83 public: 84 LoadParameters() {root = NULL;} 85 LoadParameters(TiXMLElement* element) {root = element;} 86 ~LoadParameters() {} 87 88 const char* grabParameter( const char* name); 89 const char* grabParameter( const char* name, int* i); 90 const char* grabParameter( const char* name, double* d); 91 92 void setElement(TiXMLElement* element) {root = element;} 93 TiXMLElement* getElement() {return root;} 94 95 private: 96 TiXMLElement* root; 97 98 }; 99 70 100 #endif /* _LEVELFACTORY_H */
Note: See TracChangeset
for help on using the changeset viewer.