Note: This tutorial builds on the second YAP tutorial, and it assumes you have already worked through it.

In the next Tutorial, we will be adding a physics engine in our pong game. However, with the physics engine in, it appeared that the size of the objects used in the previous tutorials were too big and the inertia was too important causing everything to be slow. So I divided every dimension of the objects by 40.

But doing so, I thought it would be a good idea to finally follow Regress proposal and load everything from an XML file, to demonstrate the use of XML with yake. I also wanted to have the possibility to create the file with default value in case it is not found. Unfortunatly, it is not currently possible with Yake (or I didn't find how to do it) to add nodes in a file, so I'm doing it “the hard way” for the moment.


void YapMainState::SetupVariables(void)
{		
	boost::filesystem::path ConfigFilePath("../../media/YapConfig.xml");

	
	//Check if the config file exist.
	if (!boost::filesystem::exists( ConfigFilePath ))
	{
		//File not found, create a new one with default value.
		std::ofstream f ( ConfigFilePath.native_file_string().c_str() );
		f << "<?xml version=\"1.0\" ?>" << std::endl;
		f << "<YapConfig>" << std::endl;
		f << "	<general BallSpeed=\"20\" PaddleSpeed=\"3000\" PaddleStr=\"2\"/>" << std::endl;
		f << "	<arena>" << std::endl;
		f << "		<bounds lower =\"15\" upper =\"-15\" left=\"-15\" right=\"15\"/>" << std::endl;
		f << "	</arena>" << std::endl;
		f << "	<objects>" << std::endl;
		f << "		<paddle1 x =\"-12\" y=\"0\" Size =\"5\" Height =\"1\">" << std::endl;
		f << "			<bounds lower =\"-12.5\" upper =\"12.5\" left=\"-12\" right=\"-12\"/>" << std::endl;
		f << "		</paddle1>" << std::endl;
		f << "		<paddle2 x =\"12\" y=\"0\" Size =\"5\" Height =\"1\">" << std::endl;
		f << "			<bounds lower =\"-12.5\" upper =\"12.5\" left=\"12\" right=\"12\"/>" << std::endl;
		f << "		</paddle2>" << std::endl;
		f << "		<ball x =\"0\" y=\"0\" Size =\"1\" Height =\"0.5\">" << std::endl;
		f << "			<bounds lower =\"15\" upper =\"-15\" left=\"-15\" right=\"15\"/>" << std::endl;
		f << "		</ball>" << std::endl;
		f << "	</objects>" << std::endl;
		f << "</YapConfig>"<< std::endl;
		f.close();
	}

	// load a XML file into a DOM tree
	SharedPtr<yake::data::dom::ISerializer> ser( new yake::data::dom::xml::XmlSerializer() );
	ser->parse(ConfigFilePath.native_file_string().c_str(), true);

	
	//get general data from the DOM tree
	SharedPtr<yake::data::dom::INode> pDocNode = ser->getDocumentNode();	
	SharedPtr<yake::data::dom::INode> pNode = pDocNode->getNodeByName("general");

	mBallSpeed = StringUtil::parseReal (pNode->getAttributeValueAs<String>("BallSpeed"));
	mPaddleSpeed = StringUtil::parseReal (pNode->getAttributeValueAs<String>("PaddleSpeed"));
	mPaddleStr = StringUtil::parseReal (pNode->getAttributeValueAs<String>("PaddleStr"));

	//get playground info.
	pNode = pDocNode->getNodeByName ("arena");
	pNode = pNode->getNodeByName("bounds");

	mArena.leftBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("left"));
	mArena.rightBound =StringUtil::parseReal (pNode->getAttributeValueAs<String>("right"));
	mArena.upperBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("upper"));
	mArena.lowerBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("lower"));

	//get paddle 1 info.
	pNode = pDocNode->getNodeByName ("objects");
	pNode = pNode->getNodeByName ("paddle1");
	

	mPaddle1.x = StringUtil::parseReal (pNode->getAttributeValueAs<String>("x"));
	mPaddle1.y = StringUtil::parseReal (pNode->getAttributeValueAs<String>("y"));
	mPaddle1.mSize = StringUtil::parseReal (pNode->getAttributeValueAs<String>("Size"));
	mPaddle1.mHeight = StringUtil::parseReal (pNode->getAttributeValueAs<String>("Height"));

	pNode = pNode->getNodeByName("bounds");

	mPaddle1.bounds.leftBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("left"));
	mPaddle1.bounds.rightBound =StringUtil::parseReal (pNode->getAttributeValueAs<String>("right"));
	mPaddle1.bounds.upperBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("upper"));
	mPaddle1.bounds.lowerBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("lower"));


	//get paddle 2 info.
	pNode = pDocNode->getNodeByName ("objects");
	pNode = pNode->getNodeByName ("paddle2");
	

	mPaddle2.x = StringUtil::parseReal (pNode->getAttributeValueAs<String>("x"));
	mPaddle2.y = StringUtil::parseReal (pNode->getAttributeValueAs<String>("y"));
	mPaddle2.mSize = StringUtil::parseReal (pNode->getAttributeValueAs<String>("Size"));
	mPaddle2.mHeight = StringUtil::parseReal (pNode->getAttributeValueAs<String>("Height"));

	pNode = pNode->getNodeByName("bounds");

	mPaddle2.bounds.leftBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("left"));
	mPaddle2.bounds.rightBound =StringUtil::parseReal (pNode->getAttributeValueAs<String>("right"));
	mPaddle2.bounds.upperBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("upper"));
	mPaddle2.bounds.lowerBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("lower"));

	pNode = pDocNode->getNodeByName ("objects");
	pNode = pNode->getNodeByName ("ball");
	
	//get ball info.
	mBall.x = StringUtil::parseReal (pNode->getAttributeValueAs<String>("x"));
	mBall.y = StringUtil::parseReal (pNode->getAttributeValueAs<String>("y"));
	mBall.mSize = StringUtil::parseReal (pNode->getAttributeValueAs<String>("Size"));
	mBall.mHeight = StringUtil::parseReal (pNode->getAttributeValueAs<String>("Height"));

	pNode = pNode->getNodeByName("bounds");

	mBall.bounds.leftBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("left"));
	mBall.bounds.rightBound =StringUtil::parseReal (pNode->getAttributeValueAs<String>("right"));
	mBall.bounds.upperBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("upper"));
	mBall.bounds.lowerBound = StringUtil::parseReal (pNode->getAttributeValueAs<String>("lower"));

	//Reset the serializer.
	ser->reset();

		
	
}

Lythaniel

 
tutorials/beginners/yap_3._loading_xml_files.txt · Last modified: 2008/02/21 21:59 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki