2010-05-08 4 views
25

나는 게임 중입니다. 뭔가 일이 생길 때 화면의 한 지점을 강조하고 싶습니다. XNA로 사각형 그리기

나는 사각형을 그리는 코드를 조금 나를 위해이 작업을 수행하는 클래스를 생성하고, 발견

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은 모두 괜찮습니다). 색상을 지정하고 싶습니다.

답변

49

XNA 제작자 클럽 사이트의 SafeArea demo에는 특별히이를위한 코드가 있습니다.

프레임마다 질감을 만들 필요는 없습니다 (단지 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(); 
    } 
} 
+0

감사! 매력처럼 작동합니다. – Ben

4

아마도 이것이 최선의 해결책은 아니지만 사각형에 맞게 펼쳐진 1x1 픽셀 텍스처를 사용할 수 있어야합니다.

+0

이것은? – vnshetty

5

이것은 내가 그것을 한 방법이다. 그것은 아마도 가장 빠르거나 가장 좋은 해결책은 아니지만 작동합니다.

using System; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace Engine 
{ 
    /// <summary> 
    /// An extended version of the SpriteBatch class that supports line and 
    /// rectangle drawing. 
    /// </summary> 
    public class ExtendedSpriteBatch : SpriteBatch 
    { 
     /// <summary> 
     /// The texture used when drawing rectangles, lines and other 
     /// primitives. This is a 1x1 white texture created at runtime. 
     /// </summary> 
     public Texture2D WhiteTexture { get; protected set; } 

     public ExtendedSpriteBatch(GraphicsDevice graphicsDevice) 
      : base(graphicsDevice) 
     { 
      this.WhiteTexture = new Texture2D(this.GraphicsDevice, 1, 1); 
      this.WhiteTexture.SetData(new Color[] { Color.White }); 
     } 

     /// <summary> 
     /// Draw a line between the two supplied points. 
     /// </summary> 
     /// <param name="start">Starting point.</param> 
     /// <param name="end">End point.</param> 
     /// <param name="color">The draw color.</param> 
     public void DrawLine(Vector2 start, Vector2 end, Color color) 
     { 
      float length = (end - start).Length(); 
      float rotation = (float)Math.Atan2(end.Y - start.Y, end.X - start.X); 
      this.Draw(this.WhiteTexture, start, null, color, rotation, Vector2.Zero, new Vector2(length, 1), SpriteEffects.None, 0); 
     } 

     /// <summary> 
     /// Draw a rectangle. 
     /// </summary> 
     /// <param name="rectangle">The rectangle to draw.</param> 
     /// <param name="color">The draw color.</param> 
     public void DrawRectangle(Rectangle rectangle, Color color) 
     { 
      this.Draw(this.WhiteTexture, new Rectangle(rectangle.Left, rectangle.Top, rectangle.Width, 1), color); 
      this.Draw(this.WhiteTexture, new Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, 1), color); 
      this.Draw(this.WhiteTexture, new Rectangle(rectangle.Left, rectangle.Top, 1, rectangle.Height), color); 
      this.Draw(this.WhiteTexture, new Rectangle(rectangle.Right, rectangle.Top, 1, rectangle.Height + 1), color); 
     } 

     /// <summary> 
     /// Fill a rectangle. 
     /// </summary> 
     /// <param name="rectangle">The rectangle to fill.</param> 
     /// <param name="color">The fill color.</param> 
     public void FillRectangle(Rectangle rectangle, Color color) 
     { 
      this.Draw(this.WhiteTexture, rectangle, color); 
     } 
    } 
}