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 rect
This is how I did it. It is probably not the fastest or the best solution, but it works.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Engine
{
///
/// An extended version of the SpriteBatch class that supports line and
/// rectangle drawing.
///
public class ExtendedSpriteBatch : SpriteBatch
{
///
/// The texture used when drawing rectangles, lines and other
/// primitives. This is a 1x1 white texture created at runtime.
///
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 });
}
///
/// Draw a line between the two supplied points.
///
/// Starting point.
/// End point.
/// The draw color.
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);
}
///
/// Draw a rectangle.
///
/// The rectangle to draw.
/// The draw color.
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);
}
///
/// Fill a rectangle.
///
/// The rectangle to fill.
/// The fill color.
public void FillRectangle(Rectangle rectangle, Color color)
{
this.Draw(this.WhiteTexture, rectangle, color);
}
}
}