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);
}

2008年11月1日 星期六

XNA - SpriteBatch 與 RenderState


SpriteBatch.Begin Method

GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
GraphicsDevice.RenderState.DepthBufferEnable = false;

GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;

GraphicsDevice.RenderState.AlphaTestEnable = true;
GraphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater;
GraphicsDevice.RenderState.ReferenceAlpha = 0;

GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Clamp;
GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Clamp;

GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Linear;
GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Linear;

GraphicsDevice.SamplerStates[0].MipMapLevelOfDetailBias = 0.0f;
GraphicsDevice.SamplerStates[0].MaxMipLevel = 0;



RenderState的部分:

GraphicsDevice.RenderState.DepthBufferEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.AlphaTestEnable = false;
// 像是我會開雙面貼圖,就要加上去
GraphicsDevice.RenderState.CullMode = CullMode.None;


SamplerStates的部分:

GraphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
GraphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;


對應的網誌