XNA里的Draw方法(上)

分类:XNA | 作者:icyfire | 发表于2007/12/11 没有评论  

XNASpriteBatch类提供了7个重载的Draw方法。

SpriteBatch.Draw (Texture2D, Vector2, Color)

这个是最基本的方法。Vector2指定了Texture在屏幕中的位置。

先加载一张图片:
1164812913671.jpg

 


protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
background = content.Load("Content\\texture");
}
}

然后把图片显示出来:

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw(background, Vector2.Zero, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

运行效果如下:
draw01.PNG
Vector2.Zero相当于一个值为(0,0)的Vector2。


SpriteBatch.Draw (Texture2D, Vector2, Nullable<rectangle>, Color)

这个方法里,Vector2指定了Texture在屏幕的位置,而rectangle则指定了要绘制的Texture的区域。也就是只绘制Texture上面的一部分。下面我们尝试在屏幕中心绘制图像中间的1/4。

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

Rectangle sourceRect;
sourceRect.X = background.Width / 4;
sourceRect.Y = background.Height / 4;
sourceRect.Width = background.Width / 2;
sourceRect.Height = background.Height / 2;

Vector2 position = new Vector2();
position.X = (graphics.GraphicsDevice.Viewport.Width - background.Width / 2) / 2;
position.Y = (graphics.GraphicsDevice.Viewport.Height - background.Height / 2) / 2;

spriteBatch.Begin();
spriteBatch.Draw(background, position,sourceRect, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

效果如下:
draw03.png

需要注意的是,如果绘制的区域比图像本身要大,则多余的区域会进行填充而不是拉伸。还有一点,rectangle的值是可以为null的。当rectangle为null时,此时方法相当于SpriteBatch.Draw (Texture2D, Vector2, Color)

SpriteBatch.Draw (Texture2D, Rectangle, Color)

这个方法里。Rectangle指定了Texture在屏幕中某块区域进行绘制。Rectangle包含了位置(X,Y)以及宽高(Width,Height)这4个属性。图像会被拉伸或者缩小以填充那块区域。依旧采用上面的图做例子。

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

Rectangle rect;
rect.X = 0;
rect.Y = 0;
rect.Width = graphics.GraphicsDevice.Viewport.Width;
rect.Height = graphics.GraphicsDevice.Viewport.Height;

spriteBatch.Begin();
spriteBatch.Draw(background, rect, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

我们定义了一个Rectangle。它的位置为(0,0),宽为屏幕的宽度,高为屏幕的高度。效果如下:
draw02.png
可以看到,图像被拉伸到了屏幕的大小。

SpriteBatch.Draw (Texture2D, Rectangle, Nullable<rectangle>, Color)

这个方法里,Rectangle指定了在屏幕中绘制的区域,而Nullable<rectangle&gt则指定了需要绘制的Texture的区域。下面我们在屏幕中心绘制这张图像。

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

Rectangle sourceRect;
sourceRect.X = 0;
sourceRect.Y = 0;
sourceRect.Width = background.Width;
sourceRect.Height = background.Height;

Rectangle destinationRect;
destinationRect.X = (graphics.GraphicsDevice.Viewport.Width - background.Width) / 2;
destinationRect.Y = (graphics.GraphicsDevice.Viewport.Height - background.Height) / 2;
destinationRect.Width = background.Width;
destinationRect.Height = background.Height;

spriteBatch.Begin();
spriteBatch.Draw(background, destinationRect,sourceRect, Color.White);
spriteBatch.End();

base.Draw(gameTime);
}

效果如下:
draw04.png

如果Nullable<rectangle>的值为null的话,则效果相当于SpriteBatch.Draw (Texture2D, Rectangle, Color)

 
标签:
Trackback url : u can trackback from your own site

留下评论

You must be logged in to post a comment.

HTML [翻译]Data URI浅析

jQuery [翻译]jQuery的show/hide性能测试

CSS [翻译]纯CSS实现对话气泡