私はゲームに取り組んでいます。何かが起こったときに画面上のスポットを強調表示したいです。
私はこれを実行するためのクラスを作成し、四角形を描画するためのコードをいくつか見つけました。
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
}
問題は、上記のコードが更新のたびに呼び出されること (1 秒あたり 60 回) であり、最適化を考慮して書かれていないことです。非常に高速である必要があります (上記のコードは、現時点ではスケルトン コードしかないゲームをフリーズさせます)。
助言がありますか?
注: 新しいコードがあれば嬉しいです (WireFrame/Fill はどちらも問題ありません)。色を指定できるようにしたいと思います。
ベストアンサー1
のSafeArea デモXNA Creators Club サイトには、具体的にそれを実行するコードがあります。
フレームごとにテクスチャを作成する必要はなく、 で作成するだけですLoadContent
。そのデモのコードの非常に簡略化されたバージョンは次のとおりです。
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();
}
}