This project is read-only.

Drawing Primitives

Ever started working with XNA and then thought to yourself, "man, it'd be nice to be able to draw some simple lines and shapes..."? Well now you can with Xen! For line drawing, Xen generates a single pixel texture and stretches it to the desired rectangular extends to simulate a line. All shapes (circles and polygons) are then broken up into a series of lines. Click the sections below to see how to draw each type of shape:

Drawing Lines

After you have added the Xen2D namespace to your code, with your SpriteBatch object in XNA, call the DrawLine() method in between a Begin() and End() like this:
        protected override void Draw( GameTime gameTime )
        {
            GraphicsDevice.Clear( Color.CornflowerBlue );

            spriteBatch.Begin();
            spriteBatch.DrawLine( Color.Blue, Vector2.Zero, Vector2.One * 200.0f, 3 );
            spriteBatch.End();
            base.Draw( gameTime );
        }
There are several variations of the DrawLine() method, which allow you to set the line color, start/end position, and the line thickness. Since spriteBatch.Begin() and spriteBatch.End() are so common, XenGameBase.GameBase does this for you and then calls DrawInternal, so the code can be further simplified to:
using Microsoft.Xna.Framework;
using Xen2D;
using XenGameBase;
//...
    public class Game : GameBase
    {
        protected override void DrawInternal( GameTime gameTime )
        {
            SpriteRenderer.DrawLine( Color.Orange, Vector2.Zero, new Vector2( 400, 400 ), 3 );
        }
    }

Drawing Circles

With your SpriteBatch object in XNA, call the DrawCircle() method in between a Begin() and End() like this:
        protected override void Draw( GameTime gameTime )
        {
            GraphicsDevice.Clear( Color.CornflowerBlue );

            spriteBatch.Begin();
            // Draw a blue circle with its center at (200,200) with 50 pixel radius using a 3 pixels wide stroke
            spriteBatch.DrawCircle( Vector2.One * 200.0f, 50.0f, Color.Blue, 3 );
            spriteBatch.End();
            base.Draw( gameTime );
        }
Clearing background color and calling SpriteBatch.Begin() and End() are common tasks that have been added to GameBase. If you are deriving from XenGameBase.GameBase, the code would look like the following:
        protected override void DrawInternal( GameTime gameTime )
        {
            // Draw a blue circle with its center at (200,200) with 50 pixel radius using a 3 pixels wide stroke
            spriteBatch.DrawCircle( Vector2.One * 200.0f, 50.0f, Color.Blue, 3 );
        }

Drawing Polygons

With your SpriteBatch object in XNA, call the DrawPolygon() method in between a Begin() and End() like this:
        private Vector2[] polygonPoints;
        // ... other code ...
        protected override void Draw( GameTime gameTime )
        {
            GraphicsDevice.Clear( Color.CornflowerBlue );

            spriteBatch.Begin();
            spriteBatch.DrawPolygon( myPolygonsPoints, myPolygonsPoints.Length, Color.Blue, 3 );
            spriteBatch.End();
            base.Draw( gameTime );
        }
This method takes an array of polygon points, the number of points, color, and thickness. Again, the code is much cleaner if you derive from GameBase:
        private Vector2[] polygonPoints;
        // ... other code ...
        protected override void DrawInternal( GameTime gameTime )
        {
            spriteBatch.DrawPolygon( myPolygonsPoints, myPolygonsPoints.Length, Color.Blue, 3 );
        }

Last edited Feb 14, 2011 at 10:22 AM by robzhu, version 12

Comments

backwardsByte Jan 28, 2013 at 1:28 AM 
as stated by vmakar, spriteBatch results in errors. Ive managed to compile and run the line code with a spritebatch however with no line drawn to the screen. Its not till I use the SpriteRenderer that it works.

Also in the previous tutorial you were instructed to create a new Game.cs, so somebody following along might get confused as to where this spritebatch object comes from.

vmakar Feb 21, 2011 at 2:16 PM 
spriteBatch.DrawCircle( Vector2.One * 200.0f, 50.0f, Color.Blue, 3 ); - error

SpriteRenderer.DrawCircle( Vector2.One * 200.0f, 50.0f, Color.Blue, 3 ); - ok



spriteBatch.DrawPolygon( myPolygonsPoints, myPolygonsPoints.Length, Color.Blue, 3 ); - error

SpriteRenderer..DrawPolygon( myPolygonsPoints, myPolygonsPoints.Length, Color.Blue, 3 ); - ok