Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.