我在玩游戏。我想在发生某些事情时在屏幕上突出显示一个点。
我创建了一个为我做这个的类,并找到了一些绘制矩形的代码:
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次),并且编写时没有考虑到优化。它必须非常快(上面的代码冻结了游戏,目前只有骨架代码)。
有什么建议?
注意:任何新代码都很好(WireFrame/Fill都很好)。我希望能够指定颜色。
最佳答案
XNA Creators Club网站上的SafeArea demo具有专门用于执行此操作的代码。
您不必只在LoadContent
中创建每一帧的Texture。该演示中的代码精简版:
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();
}
}
关于c# - 使用XNA绘制矩形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2792694/