2. The Simple XNA Example

back to Tutorial main page

Building and Running: Here is the source code to a bare-bone XNA project. Download and unzip this file and you should see a SourceCode folder. With XNA Game Studio Express installed on your PC, you should be able to double click on the SimpleXNA.sln file to invoke the IDE.

You can now Build and run this program. When the application first starts, you will see:
The right thumbstick on the gamepad controller controls the relative position of the triangle. Notice that, for this application, you cannot control the triangle on the PC without a XBOX 360 wired controller connected to your machine.

 



Project Creation: This project is a simple modification to the project created by the XNA project wizard, as shown:


Modifications: We have made added code to the Game1.cs file of simple default project at 4 separate places: (each of the following changes are enclosed by regions with label: __CODE_ADDED__)
  1. Declare variables (in the beginning of the file):
    #region __CODE_ADDED__
             EffectPool effectPool;    // for transformation and shading
             BasicEffect basicEffect; // traditional pipeline and fixed 3-stage transform
             float dy, dx;                    // movements of the thumbstick
    #endregion

  2. Initialize graphics and application state (in Initialize() function):
    #region __CODE_ADDED__ // Initialize the graphics rendering state
             graphics.GraphicsDevice.RenderState.DepthBufferEnable = false;
             graphics.GraphicsDevice.RenderState.MultiSampleAntiAlias = true;
             graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;
             graphics.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
             graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
             ...
             // Create default shading procedure effectPool = new EffectPool();
             basicEffect = new BasicEffect(graphics.GraphicsDevice, effectPool);
             ...
             // Initialize thumbstick position
             dy = 0.0f; dx = 0.0f;
    #endregion

  3. Poll the game pad thumbstick position (in Update() function):
    #region __CODE_ADDED__
             Vector2 p = GamePad.GetState(PlayerIndex.One).ThumbSticks.Right;
             dx = p.X;                // Thumbstick x-movement
             dy = p.Y;                // Thumbstick y-movement
    #endregion

  4. Draw the triangle based on the thumbstick position (in Draw() function):
    #region __CODE_ADDED__
             // Begin drawing ...
             ...
             // Triangle vertex position based on thumbstick position (dx, dy)
             Vector3 p = new Vector3(dx, dy, 0);
             VertexPositionColor[] vertices = new VertexPositionColor[3];
             for (int i=0; i<3; i++)
                      vertices[i].Position = p;       // Center of triangle
             ...    // color of the triangle vertices
             vertices[0].Position.X -= 0.1f;      // left vertex of triangle 
             vertices[1].Position.X += 0.1f;     // right vertex of triangle
             vertices[2].Position.Y += 0.2f;     // top vertex of triangle
             // draw the triangle
             graphics...DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1);
             ...
    #endregion


Lesson Learned: Here, we are not interested in the details of the graphics initialization operations. Instead, we observe that the program code structure generated by the wizard is indeed quite simple. Besides the handful of initialization and setup routines, an application programmer will update and draw the application state in the update() and draw() functions correspondingly. However, it is also true that for those of us who have no experience in games/graphics programming , the tedious initialization and setup can be overwhelming. For this reason, we have designed the XnaAssignmentBase library.
This document and the related materials are developed with support from Microsoft Research Computer Gaming Initiative under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871.


Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu