示例#1
0
	//------------------------------------------------
	void Sound::loadSoundsFromXMLFile( const std::string& filePath, const std::string& file )
	{
		std::string fullFilePath = filePath + file;
		XMLParser parser( fullFilePath.c_str() );
		XMLDocument& doc = parser.getDocument();

		if( !doc.Error() )
		{
			XMLNode* root = doc.FirstChildElement( "Sounds" );
			XMLNode* soundElement;
			for( soundElement = root->FirstChildElement( "Sound" ); soundElement != 0; soundElement = soundElement->NextSiblingElement( "Sound" ) )
			{
				parser.validateXMLAttributes( soundElement, "name,mode", "" );
				std::string soundName = parser.getXMLAttributeAsString( soundElement, "name", "" );
				std::string soundMode = parser.getXMLAttributeAsString( soundElement, "mode", "" );
				const char* soundFile = soundElement->GetText();
				Sound::createOrGetSound( soundName, filePath + std::string( soundFile ), getModeFromString( soundMode ) );
			}
		}
	}
//----------------------------------------------------------------------------
bool FontBitmapImpl::Initlize (int fontWidth, int fontHeight, 
	const char *fontFilename, CharCodingType codingType, unsigned int fontExtStyle)
{
	Font::Initlize(fontWidth, fontHeight, fontFilename, codingType, fontExtStyle);

	mFontFilename = fontFilename;

	if (CCT_UTF8 == codingType)
	{
		mCharCodingObj = new0 CharCodingUTF8();
	}
	else if (CCT_GBK == codingType)
	{
		mCharCodingObj = new0 CharCodingGBK();
	}

	mGlyphMap = this;

	std::string outPath;
	std::string outBaseFilename;
	StringHelp::SplitFilename(fontFilename, outPath, outBaseFilename);

	int bufferSize = 0;
	char *buffer = 0;
	if (PX2_RM.LoadBuffer(fontFilename, bufferSize, buffer))
	{
		XMLData data;
		if (data.LoadBuffer(buffer, bufferSize))
		{
			float imageWidth = 0.0f;
			float imageHeight = 0.0f;

			XMLNode rootNode = data.GetRootNode();
			XMLNode child = rootNode.IterateChild();
			while (!child.IsNull())
			{
				const std::string &nodeName = child.GetName();

				if ("image" == nodeName)
				{
					const char *text = child.GetText();
					std::string imagePath = outPath + text;

					mFontTex = DynamicCast<Texture2D>(PX2_RM.BlockLoad(imagePath));
					if (mFontTex)
					{
						imageWidth = (float)mFontTex->GetWidth();
						imageHeight = (float)mFontTex->GetHeight();
					}
				}
				else if ("symbol" == nodeName)
				{
					std::string ch = child.GetChild("char").GetText();
					int x = StringHelp::StringToInt(child.GetChild("x").GetText());
					int y = StringHelp::StringToInt(child.GetChild("y").GetText());
					int width = StringHelp::StringToInt(child.GetChild("width").GetText());
					int height = StringHelp::StringToInt(child.GetChild("height").GetText());

					unsigned int unicode = mCharCodingObj->ToUnicode((const unsigned char*)ch.c_str());

					BitmapFontGlyph glyph;
					glyph.X = x;
					glyph.Y = y;
					glyph.numWidth = width;
					glyph.numHeight = height;

					float u0 = (float)x/imageWidth;
					float v0 = 1.0f-(float)(y+height)/imageHeight;
					float u1 = (x+width)/imageWidth;
					float v1 = v0 + (float)height/imageHeight;
					glyph.RectUV = Rectf(u0, v0, u1, v1);

					mMapGlyph[unicode] = glyph;
				}

				child = rootNode.IterateChild(child);
			}
		}
	}

	return true;
}