绘制矩形与XNA [英] Draw Rectangle with XNA
问题描述
我工作的游戏。我想强调的事情发生的时候,屏幕上的一个点
I am working on game. I want to highlight a spot on the screen when something happens.
我创建了一个类来为我做这一点,并找到了一些代码来绘制矩形:
I created a class to do this for me, and found a bit of code to draw the rectangle:
static private Texture2D CreateRectangle(int width, int height, Color colori)
{
Texture2D rectangleTexture = new Texture2D(game.GraphicsDevice, width, height, 1, TextureUsage.None,
SurfaceFormat.Color);// create the rectangle texture, ,but it will have no color! lets fix that
Color[] color = new Color[width * height];//set the color to the amount of pixels in the textures
for (int i = 0; i < color.Length; i++)//loop through all the colors setting them to whatever values we want
{
color[i] = colori;
}
rectangleTexture.SetData(color);//set the color data on the texture
return rectangleTexture;//return the texture
}
的问题是,上述代码被称为每次更新,(60次),以及它没有考虑优化写入。它需要非常快的(上面的代码冻结的游戏,它只有骨架代码现在)。
The problem is that the code above is called every update, (60 times a second), and it was not written with optimization in mind. It needs to be extremely fast (the code above freezes the game, which has only skeleton code right now).
任何建议
请注意:任何新代码将是巨大的(线框/填充都好)。我想可以指定颜色。
Note: Any new code would be great (WireFrame/Fill are both fine). I would like to be able to specify color.
推荐答案
的在XNA创作者俱乐部网站SafeArea演示有代码,具体做到这一点。
The SafeArea demo on the XNA Creators Club site has code to do specifically that.
您不必创建纹理每一帧,只需在 LoadContent
。从演示代码的一个非常精简版:
You don't have to create the Texture every frame, just in LoadContent
. A very stripped down version of the code from that demo:
public class RectangleOverlay : DrawableGameComponent
{
SpriteBatch spriteBatch;
Texture2D dummyTexture;
Rectangle dummyRectangle;
Color Colori;
public RectangleOverlay(Rectangle rect, Color colori, Game game)
: base(game)
{
// Choose a high number, so we will draw on top of other components.
DrawOrder = 1000;
dummyRectangle = rect;
Colori = colori;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
dummyTexture = new Texture2D(GraphicsDevice, 1, 1);
dummyTexture.SetData(new Color[] { Color.White });
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(dummyTexture, dummyRectangle, Colori);
spriteBatch.End();
}
}
这篇关于绘制矩形与XNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!