示例#1
0
void ParseXML( std::vector< GameObject* > &gos, const char *filename, const Vectormath::Aos::Transform3 &transform, WorldDB *worlddb )
{
	char *xml;
	if ( !PHYSFS_readFile( filename, (void**)&xml ) )
	{
		return;
	}
	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.parse( xml );//load_file(filename);
	if ( result )
	{
		pugi::xml_node scene = doc.child( "Scene" );
		if ( scene )
		{
			for (pugi::xml_node go = scene.child("GameObject"); go; go = go.next_sibling("GameObject"))
			{
				GameObject *gameObject = new GameObject(worlddb);
				gameObject->Bind( NULL, NULL );
				for (pugi::xml_node tag = go.first_child(); tag; tag = tag.next_sibling())
				{
					if ( stricmp( tag.name(), "Component" ) == 0 )
					{
						Component *c = gameObject->AddComponent( tag.attribute("name").value(), tag.attribute("type").value() );
						c->Bind(NULL,NULL);

						for (pugi::xml_node ctag = tag.first_child(); ctag; ctag = ctag.next_sibling())
						{
							c->Bind( ctag.name(), ctag.child_value() );
						}
						CTransform *t = dynamic_cast<CTransform*>( c );
						if ( t )
						{
							t->setLocal( transform * t->local() );
						}
					} else
					{
						gameObject->Bind( tag.name(), tag.child_value() );
					}
				}
				gameObject->Enter();
				gos.push_back( gameObject );
			}
		}
	}
	free( xml );
}