Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3496 in orxonox.OLD for orxonox/branches


Ignore:
Timestamp:
Mar 11, 2005, 10:25:27 AM (19 years ago)
Author:
chris
Message:

orxonox/branches/levelloader: Finished implementation of LevelFactory and associated, only thing left to do is testing

Location:
orxonox/branches/levelloader/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/levelloader/src/levelfactory.cc

    r3482 r3496  
    1919#include "levelfactory.h"
    2020
    21 
    2221using namespace std;
    2322
     
    4746        if( !XMLDoc.LoadFile())
    4847        {
    49                 // report an error somehow
    50                 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;
    5251                return -1;
    5352        }
    5453       
    5554        // get orxonox simpleton
     55        Orxonox* orx = Orxonox::getInstance();
     56
    5657        // 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       
    5772        // start loading objects into world
     73        loadFromXMLElement( element), world, NULL);
     74       
    5875        // 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
     86void 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);
    59137}
    60138
     
    87165void LevelFactory::registerFactory( ObjectFactory* factory)
    88166{
     167                // just to see whether it works
     168        PRINTF(INFO)("'%s' registered to LevelFactory\n", factory->getClassname());
    89169        if( first == NULL) first = factory;
    90170        else first->registerFactory( factory);
     
    153233        fct.registerFactory( this);
    154234}
     235
     236/*  --------------------------------------------------
     237*                       LoadParameters
     238*   --------------------------------------------------
     239*/
     240       
     241const char* LoadParameters::grabParameter( const char* name)
     242{
     243        return root->Attribute( name);
     244}
     245
     246const char* LoadParameters::grabParameter( const char* name, int* i)
     247{
     248        return root->Attribute( name, i);
     249}
     250
     251const char* LoadParameters::grabParameter( const char* name, double* d)
     252{
     253        return root->Attribute( name, d);
     254}
  • orxonox/branches/levelloader/src/levelfactory.h

    r3482 r3496  
    1111#include "xmlparser/tinystr.h"
    1212#include "base_object.h"
     13#include "orxonox.h"
     14#include "world.h"
     15
     16class LoadParameters;
    1317
    1418#define FACTORY_ADD(CLASS) class CLASSFactory : public ObjectFactory { \
    1519 public:        \
    16   CLASSFactory (){setNext( NULL); setClassname = "CLASS"; initialize()}; \
     20  CLASSFactory (){setNext( NULL); setClassname = "CLASS"; initialize();} \
    1721  ~CLASSFactory (); \
    1822 private: \
    19         BaseObject* fabricateObject( void* data) { return new CLASS( data)}; \
     23        BaseObject* fabricateObject( LoadParameters* data) { return new CLASS( data);} \
    2024}; \
    2125CLASSFactory global_CLASSFactory();
     
    3135  ~ObjectFactory ();
    3236
    33         BaseObject* load( char* name, void* data);
     37        BaseObject* load( char* name, LoadParameters* data);
    3438        void initialize();
    3539  void registerFactory( ObjectFactory* factory);
    3640        void setClassname(char* name) {classname = name};
     41        char* getClassname() {return classname};
    3742        void setNext( ObjectFactory* factory);
    3843       
    3944 private:
    40         virtual BaseObject* fabricateObject( void* data);
     45        virtual BaseObject* fabricateObject( LoadParameters* data);
    4146        char* classname;
    4247       
     
    6873};
    6974
     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*/
     81class 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
    70100#endif /* _LEVELFACTORY_H */
Note: See TracChangeset for help on using the changeset viewer.