Difference between revisions of "Drawing Shapes"
(Created page with " <syntaxhighlight lang=c#> // Make a 1x1 texture named pixel. Texture2D pixel = new Texture2D(graphicsDeviceReferenceHere, 1, 1); // Create a 1D array of color data to...") |
|||
Line 1: | Line 1: | ||
+ | =Primitives2d= | ||
+ | |||
+ | https://bitbucket.org/C3/2d-xna-primitives/wiki/Home | ||
+ | =Using Textures= | ||
+ | ==Filled Rectangle== | ||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
Line 17: | Line 22: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | ==Outline Only== | ||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
// In LoadContent() | // In LoadContent() | ||
Line 54: | Line 60: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− |
Revision as of 11:08, 22 June 2018
Primitives2d
https://bitbucket.org/C3/2d-xna-primitives/wiki/Home
Using Textures
Filled Rectangle
// Make a 1x1 texture named pixel.
Texture2D pixel = new Texture2D(graphicsDeviceReferenceHere, 1, 1);
// Create a 1D array of color data to fill the pixel texture with.
Color[] colorData = {
Color.White,
};
// Set the texture data with our color information.
pixel.SetData<Color>(colorData);
// Draw a fancy purple rectangle.
spriteBatch.Draw(pixel, new Rectangle(0, 0, 300, 300), Color.Purple);
Outline Only
// In LoadContent()
Texture2D pixel = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
pixel.SetData(new[] { Color.White }); // so that we can draw whatever color we want on top of it
// In Draw()
spriteBatch.Begin();
Rectangle titleSafeRectangle = GraphicsDevice.Viewport.TitleSafeArea;
DrawBorder(titleSafeRectangle, 5, Color.Red); // can draw any rectangle here
spriteBatch.End();
/// <summary>
/// Will draw a border (hollow rectangle) of the given 'thicknessOfBorder' (in pixels)
/// of the specified color.
/// </summary>
/// <param name="rectangleToDraw"></param>
/// <param name="thicknessOfBorder"></param>
private void DrawBorder(Rectangle rectangleToDraw, int thicknessOfBorder, Color borderColor)
{
// Draw top line
spriteBatch.Draw(_pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, rectangleToDraw.Width, thicknessOfBorder), borderColor);
// Draw left line
spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X, rectangleToDraw.Y, thicknessOfBorder, rectangleToDraw.Height), borderColor);
// Draw right line
spriteBatch.Draw(pixel, new Rectangle((rectangleToDraw.X + rectangleToDraw.Width - thicknessOfBorder),
rectangleToDraw.Y,
thicknessOfBorder,
rectangleToDraw.Height), borderColor);
// Draw bottom line
spriteBatch.Draw(pixel, new Rectangle(rectangleToDraw.X,
rectangleToDraw.Y + rectangleToDraw.Height - thicknessOfBorder,
rectangleToDraw.Width,
thicknessOfBorder), borderColor);
}