2008年11月23日 星期日

XNA - 中文字(各國文字)

XNA - 中文字(各國文字)

使用方式很簡單…

// 取得 Graphics
Graphics g = Graphics.FromHwnd(this.Handle);
// 先取得一個字的大小
SizeF strSize1 = g.MeasureString("一", font);
// 接著取得兩個字的大小
SizeF strSize2 = g.MeasureString("一二", font);
// 相減得字元寬度
int charWidth = (int)(strSize1.Width - strSize2.Width);



將pupu大大的code整理一下:

public void DrawChar(SpriteBatch spriteBatch, char c, Vector2 position, Color color)
{
// 轉換成int
// 事實上還要扣除掉起始字元,目前為了方便一律從'\u0000'開始
int offset = (int)c;
// 計算出字元位在第幾張圖
int picIndex = offset / this.mGridPreTexture;
// 計算出在圖中的位移量
offset -= picIndex * this.mGridPreTexture;

// 取得字元的 rectangle
Rectangle rect = new Rectangle((offset % this.mTextureGrid.Width) * this.mFontSize.Width,
(offset / this.mTextureGrid.Height) * this.mFontSize.Height,
this.mFontSize.Width,
this.mFontSize.Height
);

// 繪製
spriteBatch.Draw(this.mFontTexture[picIndex], position, rect, color);
}



希望你也知道如何繪製紋理圖上的某個區塊。

public void DrawChar(SpriteBatch spriteBatch, char c, Vector2 position, Color color)
{
// 轉換成int
// 事實上還要扣除掉起始字元,目前為了方便一律從'\u0000'開始
int offset = (int)c;
// 計算出字元位在第幾張圖
int picIndex = offset / this.mGridPreTexture;
// 計算出在圖中的位移量
offset -= picIndex * this.mGridPreTexture;

// 取得字元的 rectangle
Rectangle rect = new Rectangle((offset % this.mTextureGrid.Width) * this.mFontSize.Width,
(offset / this.mTextureGrid.Height) * this.mFontSize.Height,
this.mFontSize.Width,
this.mFontSize.Height
);

// 繪製
spriteBatch.Draw(this.mFontTexture[picIndex], position, rect, color);
}

沒有留言: