Primitives Sample

This sample shows you how to extend the Spacewar VectorShape code to create PrimitiveBatch. This enables you to easily draw points, lines, and triangles on the screen.

Sample Overview

The Spacewar Windows Starter Kit (2.0) includes a retro mode. The retro mode does all of its drawing with points and lines. This sample shows how you can extend the Spacewar VectorShape code to create PrimitiveBatch, which enables you to easily draw points, lines, and triangles on the screen. Such functionality can be useful, not only for drawing your game, but also for debugging.

Minimum Shader Profile

Vertex Shader Model 1.1
Pixel Shader Model 1.1

Sample Controls

This sample uses the following keyboard and gamepad controls.

Action Keyboard Control Gamepad Control
Exit. ESC or ALT+F4 BACK

How the Sample Works

The Spacewar VectorShape code is designed to draw shapes that are set up once, and then not modified throughout the game. However, it is not the easiest component with which to work. For debugging purposes, you may sometimes want to draw arbitrary primitives, without the overhead of setting up a VertexBuffer object.

PrimitiveBatch solves this problem by creating an abstraction layer that sits on top of GraphicsDevice.DrawUserPrimitives. To minimize shader changes and the number of draw calls issued, PrimitiveBatch, like SpriteBatch, follows a Begin/End pattern. When a user wants to draw using PrimitiveBatch, the user should first call Begin, call AddVertex the appropriate number of times, and then End.

Internally, PrimitiveBatch maintains an array of vertices, and an index into that array. AddVertex uses this information to fill the array. If there is not enough room in the array, Flush is called. This submits the draw call to the graphics card and resets the array index to zero. Once this is done, copying can resume, starting over at zero.

The main Game class for the sample shows how to use PrimitiveBatch by drawing a scene from Spacewar.