PrimitiveBatch Documentation

Basic Setup

Think of PrimitiveBatch as the primitive based brother to SpriteBatch. First you create a PrimitiveBatch instance -

public class Game1: Microsoft.Xna.Framework.Game
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    PrimitiveBatch primitiveBatch;

    protected override void LoadContent()
         // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // Create a new PrimitiveBatch, which can be used to draw primitives.
        primitiveBatch = new PrimitiveBatch(GraphicsDevice);

Once created we can either create primitives during initialization or dynamically throughout the programs flow. In the example below we create a Rectangle primitive just before we render it.

protected override void Draw(GameTime gameTime)
    // Clear the back buffer to LighGray.

    // Create an instance of a PBRectangle using the Width, Height, Brush overload.
    PBRectangle rect = new PERectangle(100, 50, new Brush(Color.White, Color.Black, 1.0f));
    // Set the Position and Rotation angle.
    rect.Position = new Vector2(100, 100);
    rect.Rotation = 0.0f;

    // Call Begin on the PrimitiveBatch.

    // Submit the rectangle to the PrimitiveBatch for rendering.

    // Call End on the PrimitiveBatch.

Optionally, we could have passed a View and Projection matrix to the Begin method. Simply calling Begin with no parameters allows the PrimitiveBatch to assume an offset orthographic projection representing the visible area of the window in pixels and a View matrix set to Identity.

Last edited Feb 15, 2010 at 12:34 PM by mattbettcher, version 5


No comments yet.