<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-2903756005659412185</id><updated>2009-10-13T22:41:11.361-07:00</updated><title type='text'>Christian Game Programming</title><subtitle type='html'>Welcome to my basic tutorial blog. This blog introduces XNA game programming using the C# language. I do hope that you learn from the text on this blog even though I am not such a good programmer myself. If you check this blog out, I thank you. 

Also, I want you to know that this site has been dedicated to my Lord and Savior. All I do is worthless unless it is done for Him. Thank you Lord.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default?start-index=26&amp;max-results=25'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>39</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-4589345107724288299</id><published>2008-01-09T08:52:00.000-08:00</published><updated>2008-01-09T09:08:35.945-08:00</updated><title type='text'>Creating a New Project</title><content type='html'>&lt;div align="left"&gt;Welcome to the first tutorial in the &lt;em&gt;2D Tutorial Series&lt;/em&gt;. In this tutorial series, I hope to introduce anyone who is new to C# and XNA game programming, to the programming world.&lt;br /&gt;&lt;br /&gt;In this first tutorial, we will create a new project, in which we will work in and create our small XNA applications in. It is assumed that you have already downloaded and installed both the &lt;em&gt;C# Express Edition&lt;/em&gt; and &lt;em&gt;XNA Game Studio Express 2.0&lt;/em&gt;. If you haven’t, then go to the &lt;em&gt;Getting Started&lt;/em&gt; tab on the right side of the page.&lt;br /&gt;&lt;br /&gt;Now, since everything is downloaded, we can begin. Open up the C# Express Edition which should be accessible from the Start menu. When you open up your project you should be greeted with the &lt;em&gt;Start Page&lt;/em&gt;. &lt;/div&gt;&lt;p align="left"&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/R4T9vh65A1I/AAAAAAAAALw/qML2nVZBuyg/s1600-h/StartPage.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5153522866619745106" style="CURSOR: hand" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/R4T9vh65A1I/AAAAAAAAALw/qML2nVZBuyg/s400/StartPage.jpg" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;p align="left"&gt;&lt;strong&gt;Figure 1.1: When you open the C# Express Edition, you will be greeted with the Start Page.&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Now, we want to create a new project. This project is where we will create our applications in. When you create a new project, you can choose to either create a new, empty, blank project, or you can choose from a group of project templates. These templates do nothing special. All they do is start you out with the needed code and references for you to easily start creating your application. For us, we want to select a project template called the &lt;em&gt;Windows Game&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;From the &lt;strong&gt;File&lt;/strong&gt; tab, select &lt;strong&gt;New Project&lt;/strong&gt;. After this, a New Project dialog will show up. From this dialog, you can choose what type of project you would like to create. As said previously, we want to select the Windows Game project template. This should be the first template in the list. If you don’t see this option, make sure that on the left side of the dialog, you have &lt;em&gt;XNA Game Studio 2.0&lt;/em&gt; selected.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp3.blogger.com/_kLtxSdyLIgE/R4T9wB65A4I/AAAAAAAAAMI/XBUpzb7v5e0/s1600-h/FileNewProject.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5153522875209679746" style="CURSOR: hand" alt="" src="http://bp3.blogger.com/_kLtxSdyLIgE/R4T9wB65A4I/AAAAAAAAAMI/XBUpzb7v5e0/s400/FileNewProject.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 1.2: Create a new project by clicking File / New Project.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Click on the &lt;strong&gt;Windows Game&lt;/strong&gt; template. At the bottom of the page, you will see a few options for naming your project. I named it, &lt;em&gt;TutorialProject&lt;/em&gt;. However, if you dislike this name, feel free to choose something different. The save location is made by default, and is probably where you want to save it. This path saves the project into the Projects directory, where you can easily access it when you open a file in XNA GSE (Game Studio Express). The final textbox is where you can specify the solution name. It is possible to have multiple projects in a single solution, so you may want to name the solution different from an individual project. For our purposes, leave the name as it is. Also, make sure that the &lt;em&gt;Create directory for solution checkbox&lt;/em&gt; is checked because we are creating a new solution and not adding a project to an existing solution. Finally, press &lt;strong&gt;OK&lt;/strong&gt;, to create the project.&lt;br /&gt;&lt;br /&gt;&lt;div align="left"&gt;&lt;a href="http://bp2.blogger.com/_kLtxSdyLIgE/R4T9vx65A2I/AAAAAAAAAL4/HJTDOgHdVgM/s1600-h/NewTutorialProject.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5153522870914712418" style="CURSOR: hand" alt="" src="http://bp2.blogger.com/_kLtxSdyLIgE/R4T9vx65A2I/AAAAAAAAAL4/HJTDOgHdVgM/s400/NewTutorialProject.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 1.3: When you have completed the steps above, your project profile should look similar to this.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;After a few moments, you will be greeted with the &lt;em&gt;Game1.cs&lt;/em&gt; class file, which has been created for you by default. The default code may look confusing, but after a while you will get the hang of what happens. For now though, let’s see what the default code does. Build the project by clicking the green &lt;em&gt;Start Debugging&lt;/em&gt; arrow at the top, middle of the screen (or by pressing &lt;em&gt;F5&lt;/em&gt;). Your project will build and you will see an already created window for you, with a nice cornflower blue color as the background.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp2.blogger.com/_kLtxSdyLIgE/R4T9vx65A3I/AAAAAAAAAMA/H8xkEFy0uSQ/s1600-h/BlueScreen.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5153522870914712434" style="CURSOR: hand" alt="" src="http://bp2.blogger.com/_kLtxSdyLIgE/R4T9vx65A3I/AAAAAAAAAMA/H8xkEFy0uSQ/s400/BlueScreen.jpg" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;strong&gt;Figure 1.4: When you build the project by clicking the green arrow or pressing F5, you will see this screen created for you by default.&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;It’s nice to know that you don’t have to go through all the work of creating a window by hand and that you can start seeing results very quickly. In this tutorial, you learned how to create a new project. You didn’t code anything, but that is coming in the next tutorial.&lt;br /&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;Be a hero: don’t conform; be selfless; make a difference. &lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-4589345107724288299?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/4589345107724288299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=4589345107724288299' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/4589345107724288299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/4589345107724288299'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2008/01/creating-new-project.html' title='Creating a New Project'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp1.blogger.com/_kLtxSdyLIgE/R4T9vh65A1I/AAAAAAAAALw/qML2nVZBuyg/s72-c/StartPage.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-1917156451690785817</id><published>2008-01-09T08:43:00.000-08:00</published><updated>2008-01-09T09:10:45.727-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='2D Tutorial Series'/><title type='text'>2D Tutorial Series</title><content type='html'>Welcome to the 2D Tutorial Series. In this series, we explore XNA and how we can use it for programing 2D applications. 3D graphics is a whole new topic, so we stick to 2D in this series.&lt;br /&gt;&lt;br /&gt;This series is written using C# and XNA Game Studio 2.0. No knowledge of either is needed to begin, as this tutorial series is written for total beginners.&lt;br /&gt;&lt;br /&gt;If you find any errors in any of the tutorials, or if any of the tutorials were confusing, please let me know with a comment. Thanks all.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2008/01/creating-new-project.html"&gt;Tutorial 1: Creating a New Project&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-1917156451690785817?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/1917156451690785817/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=1917156451690785817' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/1917156451690785817'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/1917156451690785817'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2008/01/2d-tutorial-series.html' title='2D Tutorial Series'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-1044728339965956879</id><published>2007-12-19T15:06:00.000-08:00</published><updated>2007-12-20T14:46:20.241-08:00</updated><title type='text'>Lighting Demo</title><content type='html'>&lt;span style="font-family:lucida bright;"&gt;&lt;h1&gt;Creating a Simple Shader&lt;/h1&gt;&lt;p&gt;First, before anything, we need to create the shader that will be used to render our pixels onto the screen. For starters, all the shader will do is transform the geometry into correct world, view, and projection space, and then output all pixels white. Later, however, we will add cool lighting effects for all kinds of materials.&lt;/p&gt;&lt;p&gt;Open up an empty &lt;b&gt;Notepad&lt;/b&gt; document. Click the &lt;b&gt;File&lt;/b&gt; menu tab and then click &lt;b&gt;Save As&lt;/b&gt;. Name the document &lt;i&gt;Transform.fx&lt;/i&gt; (you will need to switch the &lt;b&gt;Files of Type&lt;/b&gt; box to &lt;b&gt;All Files&lt;/b&gt;). Finally, place the document somewhere easily accessed and click &lt;b&gt;Save&lt;/b&gt;.&lt;/p&gt;&lt;h3&gt;Effect Parameters&lt;/h3&gt;&lt;p&gt;The first things we want to add to our shader are effect parameters. These types of variables are used to allow access for the XNA app so that we can manipulate how the shader renders the geometry. At the moment, we only need one parameter that holds the world, view, and projection matrices. We need this parameter to transform all the geometry (vertices) from their local space coordinates to the correct world, view, and projection space.&lt;/p&gt;&lt;p&gt;At the top of the file, add this code:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;uniform extern float4x xWorldViewProjection;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;uniform extern&lt;/span&gt; just means that this variable is like a parameter that can be accessed by the XNA app. &lt;span style="font-family:courier new;"&gt;float4x4&lt;/span&gt; is a type that defines a 4x4 matrix variable (which means the same as &lt;span style="font-family:courier new;"&gt;Matrix&lt;/span&gt; in XNA). &lt;p&gt;&lt;/p&gt;&lt;h3&gt;The VertexInput Structure&lt;/h3&gt;&lt;p&gt;The next step we need to take is to create a structure that defines what type of data the vertex shader takes in from the XNA app.&lt;/p&gt;&lt;p&gt;All the data that we want to take in for now is vertex position data (we do not need color data because we will just output all pixels white). With this in mind, we will have a very simple &lt;span style="font-family:courier new;"&gt;VertexInput&lt;/span&gt; structure. Underneath the effect parameter, create the &lt;span style="font-family:courier new;"&gt;VertexInput&lt;/span&gt;:&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;&lt;pre&gt;// Vertex input&lt;br /&gt;struct VertexInput&lt;br /&gt;{&lt;br /&gt;    float3 pos : POSITION;&lt;br /&gt;};&lt;/pre&gt;&lt;/span&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;As you may have guessed, &lt;span style="font-family:courier new;"&gt;float3&lt;/span&gt; is a keyword that creates a variable similar to an XNA &lt;span style="font-family:courier new;"&gt;Vector3&lt;/span&gt;. Also, we have the &lt;span style="font-family:courier new;"&gt;: POSITION0&lt;/span&gt;. This is called a variable &lt;i&gt;semantic&lt;/i&gt;. The reason for using these is so that the shader knows how to map data incoming from the XNA app to their corresponding variables in the shader (we don’t want the shader to confuse vertex normal data with vertex position data).&lt;/p&gt;&lt;h3&gt;The VertexOutput Structure&lt;/h3&gt;&lt;p&gt;After the &lt;span style="font-family:courier new;"&gt;VertexInput&lt;/span&gt; structure, we create the &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt;. The &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt; structure defines what type of data will be given to the pixel shader. The pixel shader is called after the vertex shader, but does processing on every pixel instead of every vertex. Some operations only need to be done per-vertex instead of per-pixel which can increase performance (generally there are many more pixels than vertices). These operations are completed in the vertex shader. When the pixel shader comes into play, it takes in data from the vertex shader to use for processing the pixels. We declare this data structure in a &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt; structure.&lt;/p&gt;&lt;p&gt;Below the &lt;span style="font-family:courier new;"&gt;VertexInput&lt;/span&gt; structure, we can now create the &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt;:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;// Vertex output&lt;br /&gt;struct VertexOutput&lt;br /&gt;{&lt;br /&gt;    float4 pos : POSITION;&lt;br /&gt;};&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;All we need to output to the pixel shader is the position of the vertex once it has been transformed into correct space. The position has also now been changed to a &lt;span style="font-family:courier new;"&gt;float4&lt;/span&gt;, which could be looked at like an XNA &lt;span style="font-family:courier new;"&gt;Vector4&lt;/span&gt;. The reason for this is so that we can multiply this position with matrices. Also, you may be wondering why we don’t output any color. Usually, we would output a vertex color, but, because our simple pixel shader will only output white color data for every pixel, it would be a waste to store color output for each vertex.&lt;/p&gt;&lt;h3&gt;The Vertex Shader&lt;/h3&gt;&lt;p&gt;Now we can create the &lt;i&gt;vertex shader&lt;/i&gt;. The vertex shader is called first and does operations on the vertices, also known as the geometry, of the mesh to be drawn.&lt;/p&gt;&lt;p&gt;Below the &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt; structure, add the vertex shader:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;// Vertex shader&lt;br /&gt;VertexOutput VS_Transform(VertexInput input)&lt;br /&gt;{&lt;br /&gt;    // Zero out our output.&lt;br /&gt;    VertexOutput output = (VertexOutput)0;&lt;br /&gt;&lt;br /&gt;    // Transform the position of the vertex into correct world,&lt;br /&gt;    // view, and projection space.&lt;br /&gt;    output.pos = mul(float4(input.Pos, 1.0f), xWorldViewProjection);&lt;br /&gt;&lt;br /&gt;    // Done--return the output.&lt;br /&gt;    return out;&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;This is a very, very simple vertex shader. The first line in the vertex shader zeroes out the output that the vertex shader will return. The next line transforms the vertex into correct world, view, and projection space. We use the HLSL intrinsic &lt;span style="font-family:courier new;"&gt;mul&lt;/span&gt; function to multiply a &lt;span style="font-family:courier new;"&gt;float4&lt;/span&gt; and a &lt;span style="font-family:courier new;"&gt;float4x4&lt;/span&gt;. However, the in-position of the vertex is a &lt;span style="font-family:courier new;"&gt;float3&lt;/span&gt;, which means we need to convert the &lt;span style="font-family:courier new;"&gt;float3&lt;/span&gt; to a &lt;span style="font-family:courier new;"&gt;float4&lt;/span&gt;. We do this by creating a &lt;span style="font-family:courier new;"&gt;float4&lt;/span&gt; and passing the in-position and 1 as parameters to the constructor (we pass in 1 as the w-component because the position is a location vector (point). If the vector were a normal or other vector that did not have a position on the screen, but only a direction, then we would specify 0 for the w-component). Also, you will notice that we multiply the position, now in homogeneous (4-component) coordinates, by the world, view, and projection matrices. These matrices can change for each object in the application, thus explains why we make this variable an effect parameter.&lt;/p&gt;&lt;p&gt;Finally, we are finished manipulating the output and can give it to the pixel shader. Note though, that the pixel shader will not actually use the output’s position. Even in advanced shaders, the pixel shader uses color data and texture data. The output’s position is used by the graphics card to place the vertices in correct world, view, and projection space.&lt;/p&gt;&lt;h3&gt;The Pixel Shader&lt;/h3&gt;&lt;p&gt;The &lt;i&gt;pixel shader&lt;/i&gt; is called after the vertex shader and does processing on all of the pixels in the geometry, finally outputting a color for the pixel. Below the vertex shader, create the pixel shader:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;// Pixel shader&lt;br /&gt;float4 PS_Transform() : COLOR&lt;br /&gt;{&lt;br /&gt;    // Output white for each pixel.&lt;br /&gt;    return float4(1.0f, 1.0f, 1.0f, 1.0f);&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Usually, lighting calculations and other processing for cool effects would be done here, but for now, all we do is output the color white. Notice, however, that we didn’t take in a &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt; parameter to the pixel shader. Usually we would but because we don’t need any of the information in the &lt;span style="font-family:courier new;"&gt;VertexOutput&lt;/span&gt; structure, we don’t need to bother with parameters. Finally, a unique detail of the pixel shader is that it represents a color. The pixel shader has a return value of a &lt;span style="font-family:courier new;"&gt;float4&lt;/span&gt;, which is like a color (r, g, b, a) and also has a &lt;span style="font-family:courier new;"&gt;: COLOR&lt;/span&gt; semantic attached to it. Also, note that &lt;span style="font-family:courier new;"&gt;float4&lt;/span&gt;, which can be interpreted as an XNA &lt;span style="font-family:courier new;"&gt;Vector4&lt;/span&gt;, can also represent an XNA &lt;span style="font-family:courier new;"&gt;Color&lt;/span&gt;, which has four components as well.&lt;/p&gt;&lt;h3&gt;The Technique&lt;/h3&gt;&lt;p&gt;We are now 90 percent done with our simple shader. All we have left to do is add the &lt;i&gt;technique&lt;/i&gt;, which respectively calls the vertex and pixels shaders associated with that technique. It is possible to have many different techniques, inputs, outputs, vertex shaders, pixel shaders, and effect parameters in one shader file. You could use this ability to call different vertex and pixel shaders depending on what kind of shader model the user’s computer could support. In the XNA app you would determine which level the computer can support and then call the corresponding technique (e.g. WaterTech1, WaterTech2) which would call the correct vertex and pixel shaders for your computer. This allows for extra flexibility because the user could use the same application without having to get a better graphics card. For us, however, we will just use shader model 2.0.&lt;/p&gt;&lt;p&gt;Below everything you have so far, create the technique for our transform shader:&lt;/p&gt;&lt;p&gt;&lt;pre&gt;// Transform&lt;br /&gt;technique Transform&lt;br /&gt;{&lt;br /&gt;    pass P0&lt;br /&gt;    {&lt;br /&gt;        // Compile the vertex and pixel shaders for this&lt;br /&gt;        // shader using vertex and pixel versions 2.0.&lt;br /&gt;        vertexShader = compile vs_2_0 VS_Transform();&lt;br /&gt;        pixelShader = compile ps_2_0 PS_Transform();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;First, we declare the technique and the name of the technique. Next, we declare a &lt;i&gt;pass&lt;/i&gt;. A pass is a loop inside the technique that calls the associated vertex and pixel shaders. A technique may want to contain more than one pass for special effects so in the XNA app you need to loop through the passes in the currently set technique. Note however that using two passes doubles the processing time for the geometry, and using three passes makes it even worse. If you don’t need to use multiple passes, it would probably be best not to.&lt;/p&gt;&lt;p&gt;Inside the pass, we set the vertex and pixel shaders, respectively, to the vertex and pixel shaders we made previously. Also, we compile our vertex and pixel shaders using vertex and pixel versions 2.0.&lt;/p&gt;&lt;p&gt;We are now done creating the shader, which can be used in any application. We learned how to create effect parameters, vertex input and output structures, vertex shaders, pixel shaders, and techniques. Also, hopefully you learned a bit about the structure of HLSL and how to do some basic coding with it. In the next parts, we will learn how to apply shaders to geometry in the XNA application.&lt;/p&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-1044728339965956879?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/1044728339965956879/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=1044728339965956879' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/1044728339965956879'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/1044728339965956879'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/12/creating-simple-shader-first-before.html' title='Lighting Demo'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-5006173723686674206</id><published>2007-11-22T19:50:00.000-08:00</published><updated>2007-11-22T19:54:01.796-08:00</updated><title type='text'>What are all the possible texture file types that can be used?</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question:&lt;/strong&gt; What are all the possible texture file types that can be used?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Answer: &lt;/strong&gt;The texture file types that can be loaded using the Content Pipeline are: Bmp, Dds, Dib, Hdr, Jpg, Pfm, Ppm, and Tga.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-5006173723686674206?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/5006173723686674206/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=5006173723686674206' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5006173723686674206'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5006173723686674206'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/what-are-all-possible-texture-file.html' title='What are all the possible texture file types that can be used?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-7188871586907597774</id><published>2007-11-18T18:48:00.001-08:00</published><updated>2007-11-18T18:49:06.131-08:00</updated><title type='text'>How do I build a rectangle around an object?</title><content type='html'>&lt;strong&gt;Question:&lt;/strong&gt; How do I build a rectangle around an object?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Answer:&lt;/strong&gt; By using the Rectangle class, an invisible box can be created and used for collision checking, or for virtually any other purpose.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Rectangle rect = new Rectangle((int)objectPosition.X, (int) objectPosition.Y, objectTexture.Width, objectTexture.Height);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The above code shows a common way a Rectangle is created to wrap an object. The Rectangle constructor takes an X and Y coordinate to be the base corner of the rectangle, and then a width and height for the dimensions of the rectangle. In the code above, we retrieved the X and Y coordinates from a Vector2, and we retrieved the width and height from a Texture2D. This is just one way of creating a Rectangle though, and there are many other possibilities also.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-7188871586907597774?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/7188871586907597774/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=7188871586907597774' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/7188871586907597774'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/7188871586907597774'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/how-do-i-build-rectangle-around-object.html' title='How do I build a rectangle around an object?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-7195197245875509974</id><published>2007-11-18T18:27:00.000-08:00</published><updated>2007-11-18T18:34:22.064-08:00</updated><title type='text'>How do I check if two rectangles are overlapping?</title><content type='html'>&lt;span &gt;&lt;strong&gt;Question:&lt;/strong&gt; How do I check if two rectangles are overlapping?&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;strong&gt;Answer:&lt;/strong&gt; If you have two Rectangles and all you want to do is check if they are overlapping eachother or not, you can easily make use of the Intersects method.&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;/span&gt;&lt;br /&gt;&lt;span &gt;In code, let's say you have two Rectangles called rect1 and rect2. To check if they overlap, you could have an if-statement like this:&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if (rect1.Intersects(rect2))&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Code&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;strong&gt;Note:&lt;/strong&gt; It doesn't matter which Rectangle you call Intersects fom.&lt;/span&gt;&lt;br /&gt;&lt;span &gt;&lt;/span&gt;&lt;br /&gt;&lt;span &gt;In the code above, we check if any part of rect1 intersects any part of rect2. If rect1 and rect do overlap, Intersects will return true and then you can continue from there.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-7195197245875509974?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/7195197245875509974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=7195197245875509974' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/7195197245875509974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/7195197245875509974'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/how-do-i-check-if-two-rectangles-are.html' title='How do I check if two rectangles are overlapping?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-589107699891119468</id><published>2007-11-17T16:26:00.000-08:00</published><updated>2007-11-18T13:18:18.474-08:00</updated><title type='text'>Any Requests?</title><content type='html'>Hello,&lt;br /&gt;&lt;br /&gt;It would be nice to see what the community wants to know at the moment. If you have a specific request, please post a comment.&lt;br /&gt;&lt;br /&gt;Thanks&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-589107699891119468?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/589107699891119468/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=589107699891119468' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/589107699891119468'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/589107699891119468'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/any-requests.html' title='Any Requests?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-3132430620285102304</id><published>2007-11-17T16:15:00.000-08:00</published><updated>2007-11-17T16:22:30.216-08:00</updated><title type='text'>How can I check if a key is down or not?</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question:&lt;/strong&gt; How can I check if a key is down or not? &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Answer: &lt;/strong&gt;To check the state of a key on the keyboard, can call the IsKeyDown or IsKeyUp methods of a KeyboardState variable.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;Inside of the Update method, create a KeyboardState and set it to the current state of the Keyboard.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;KeyboardState keyboard = Keyboard.GetState();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;After that, you can add the if-statement to check the value of a given key:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if (keyboard.IsKeyDown(Keys.Space))&lt;br /&gt;{&lt;br /&gt;//Code&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;By using the Keys enum type, we can check against any key on the keyboard.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-3132430620285102304?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/3132430620285102304/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=3132430620285102304' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/3132430620285102304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/3132430620285102304'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/how-can-i-see-if-key-is-down-or-not.html' title='How can I check if a key is down or not?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-4408913770184355153</id><published>2007-11-17T14:33:00.000-08:00</published><updated>2007-11-17T14:49:41.210-08:00</updated><title type='text'>How can I check to see if a controller button is down?</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question:&lt;/strong&gt; How can I check to see if a controller button is down?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;strong&gt;Answer: &lt;/strong&gt;The key to tracking input from the Xbox 360 controller is the GamePadState class. To track if a controller button is down, we want to check the ButtonState of the button in a GamePadState. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;First, inside of the Update method, create a GamePadState variable:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;GamePadState gamePad = GamePad.GetState(PlayerIndex.One);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;After creating the GamePadState, we then initialize it with the current value of player one's game pad. Since this code is processed every Update method, the value that our GamePadState contains is always valid.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;Now, we can check to the value of the controller. In the Update method, you would add something like this to check if a button is pressed:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;if (gamePad.Buttons.A == ButtonState.Pressed)&lt;br /&gt;{&lt;br /&gt;//Code&lt;br /&gt;}&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;In the above if-statement, we ask if the A button is pressed. If it is, then the code inside the if-statement will be processed. You can also check if the button is released by checking against ButtonState.Released.&lt;/span&gt; &lt;ol&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:Courier New;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-4408913770184355153?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/4408913770184355153/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=4408913770184355153' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/4408913770184355153'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/4408913770184355153'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/how-can-i-check-to-see-if-xbox-360.html' title='How can I check to see if a controller button is down?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-8527251845496452236</id><published>2007-11-17T12:07:00.000-08:00</published><updated>2007-11-17T12:24:50.057-08:00</updated><title type='text'>How do I scale the size of a sprite?</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question: &lt;/strong&gt;How do I scale the size of a sprite?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Scaling the size of a sprite when drawn consists of only one step: modifying the scale parameter of a spriteBatch.Draw call.&lt;br /&gt;&lt;br /&gt;After creating a SpriteBatch, between the Begin and End calls, you will want to call spriteBatch.Draw(...); There are seven different overloads of Draw but only two of them take scale parameters. These are overloads 6 and 7.&lt;br /&gt;&lt;br /&gt;You would call the Draw method with overload 6 as follows:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;spriteBatch.Draw(texture, position, sourceRect, color, rotation,&lt;br /&gt;origin, &lt;strong&gt;scale&lt;/strong&gt;, spriteEffects, depth);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The seventh parameter is the scale. This parameter is a float. The default value is 1. Note that if you specify 0, the sprite will dissapear because all its dimensions will be multiplied by 0 which will result in 0.&lt;br /&gt;&lt;br /&gt;You would call the Draw method with overload 7 as follows:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;spriteBatch.Draw(texture, position, sourceRect, color, rotation, origin, &lt;strong&gt;scaleVector&lt;/strong&gt;, spriteEffects, depth);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Again, the seventh parameter is the scale parameter. This parameter is different from overload 6 in that it is a Vector2 instead of a float. The reason for this is so that the user can scale the width of the texture differently from the height.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; You could also scale a texture by using the destination rectangle, but that will not be covered in this post.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-8527251845496452236?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/8527251845496452236/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=8527251845496452236' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/8527251845496452236'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/8527251845496452236'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/question-how-do-i-scale-size-of-sprite.html' title='How do I scale the size of a sprite?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-67948222637067161</id><published>2007-11-16T17:13:00.001-08:00</published><updated>2007-11-16T17:16:43.191-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Getting Started With XNA'/><title type='text'>Getting Started with XNA</title><content type='html'>Getting started with XNA is very simple and easy. Here are a few posts to help out with this process.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/08/what-is-xna.html"&gt;A Quick Article on What XNA Is&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/08/xna-and-c-download-instructions.html"&gt;The XNA and C# Express Edition Download Instructions&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-67948222637067161?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/67948222637067161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=67948222637067161' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/67948222637067161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/67948222637067161'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/getting-started-with-xna.html' title='Getting Started with XNA'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-576952365910810711</id><published>2007-11-15T17:06:00.000-08:00</published><updated>2007-11-15T17:09:28.518-08:00</updated><title type='text'>How to Print Simple Text onto the Screen</title><content type='html'>&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;Most games need to be able to render text on the screen. Since many people would like to know how to do this, I’ve decided to write a small tutorial on this subject.&lt;br /&gt;&lt;br /&gt;Text in itself is rendered onto the screen in the same way as a texture; through a bitmap image. On this image, every letter and symbol has its own small area. Before XNA 1.0 came out, I saw text being rendered through someone using their own system where a string is dissected and then each letter is drawn separately. Fortunately for us though, XNA 1.0 gives us the SpriteFont. This name refers both to an XML file and a C# class.&lt;br /&gt;&lt;br /&gt;As said before, to render text follows almost the same concepts as rendering textures. The steps in the process are few and simple: &lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Create a SpriteFont XML file and specify the needed properties.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Load the SpriteFont file using the Content Pipeline, into a SpriteFont class.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Render text using the specified SpriteFont and a shader (we use the SpriteBatch).&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;From these steps it is very easy to recognize the similarity between rendering textures and rendering text.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Creating the SpriteFont&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;So the first step to implementing a certain font is to create that font. This can be accomplished using the SpriteFont XML file. Inside of the Solution Explorer, &lt;strong&gt;right click&lt;/strong&gt; on your project and click &lt;strong&gt;Add / New Item&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RzpgEPzVo9I/AAAAAAAAAIU/VYnowdtU88Y/s1600-h/AddNewItemSE.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132520351419704274" style="CURSOR: hand" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RzpgEPzVo9I/AAAAAAAAAIU/VYnowdtU88Y/s400/AddNewItemSE.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Right Click on your project name and click Add / New Item.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; A default Windows Game template is used with the namespace of WindowsGame1. It is not in the scope of this lesson to explain about project templates though.&lt;br /&gt;&lt;br /&gt;You should now be taken to a page where you can choose what you want to create. We want to create a SpriteFont. Click on that template and name the SpriteFont, “Verdana”.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RzpgEPzVo-I/AAAAAAAAAIc/DYQ5pdnpzWA/s1600-h/AddSpriteFontVerdana.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132520351419704290" style="CURSOR: hand" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RzpgEPzVo-I/AAAAAAAAAIc/DYQ5pdnpzWA/s400/AddSpriteFontVerdana.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Create a SpriteFont named “Verdana”.&lt;br /&gt;&lt;br /&gt;After clicking &lt;strong&gt;Add&lt;/strong&gt;, in your Solution Explorer you will see the Verdana.spritefont file in your list. This file is already opened. If you take a quick look around the file you will notice several different XML tags specifying the different properties of the font. These properties are as follows: &lt;ul&gt;&lt;li&gt;&lt;strong&gt;FontName:&lt;/strong&gt; This property changes the font family of the font (i.e. Times New Roman, Verdana, etc.). &lt;/li&gt;&lt;li&gt;&lt;strong&gt;Size:&lt;/strong&gt; This property changes the size of the font.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Spacing:&lt;/strong&gt; This property changes the amount of spacing in between the characters of the text. Usually, you can leave this as the default value of two.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Style:&lt;/strong&gt; This property changes the appearance of your text. You can enter “Regular” for no changes; “Bold” for bolded text; “Italic” for italicized text; or finally, “Bold, Italic” for both bolded and italicized text. Unfortunately, there is not ability for underlined text.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;For our simple purposes, we will leave the font properties as they are.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; If understanding the XML is confusing, do not worry. That is not necessary as long as you know what text you change. For all tags there is a common structure: [name]text[/endname] &lt;name&gt;Where you want to place the code is where the “text” would be; in between the name and endname tags.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Code Note: &lt;/strong&gt;For the rest of the tutorial, open and close brackets will be used to replace open and close tags. I cannot use tags in the text due to the HTML problems.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Using the Content Pipeline to Create a SpriteFont Class&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Now that we have our SpriteFont file created, we can load it into a SpriteFont class. This class will then be used to draw a specified string along with the help of the SpriteBatch class.&lt;br /&gt;The first step is to create a variable for the SpriteFont. At the top of the class where the GraphicsDeviceManager and ContentManager are created, add this code:&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;//The SpriteFont to hold the Verdana XML font.&lt;br /&gt;SpriteFont verdanaFont;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Next, we can initialize this variable. Similar to a Texture2D, instead of calling the constructor of the SpriteFont class (which is virtually useless) we instead directly load it with an outside file using content.Load(). We want to place this code into the LoadGraphicsContent method (so that it can be re-loaded when necessary), specifically inside of the if (loadAllContent) statement:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Init the SpriteFont with a real SpriteFont file&lt;br /&gt;//from the Solution Explorer.&lt;br /&gt;verdanaFont = content.Load[Texture2D]&lt;spritefont&gt;("Verdana");&lt;br /&gt;&lt;/span&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Code Note: &lt;/strong&gt;Switch the two forward and backward slashes for tags. I couldn't use tags in this post because of the HTML. This code practice will be used for the rest of the tutorial.&lt;/p&gt;&lt;p&gt;This code first calls the Load method of the ContentManager variable content and tells it to load a SpriteFont. Then, we pass in the name of the SpriteFont XML file. Notice that we remove the file extension (i.e. .spriteFont).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; The reason why we load the SpriteFont inside of the LoadGraphicsContent method is not to be explained in this lesson. For now you don’t really need to know anyway.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Rendering Text onto the Screen Using the SpriteBatch&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Finally, we have come to the part where we can render text onto the screen. As said earlier, a font is exactly the same in that it is a bitmap image that needs to be rendered. The SpriteBatch class, which is commonly used to render textures, can also be used along with its DrawString method to render text. Note that, the SpriteBatch actually draws the text. All the SpriteFont does is hold the value of what the text will look like.&lt;br /&gt;&lt;br /&gt;Since we first need a SpriteBatch to begin drawing, add this code to the top of your class along with the other variable declarations. This code creates a SpriteBatch variable.&lt;br /&gt;&lt;summary&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//The SpriteBatch used to draw the text.&lt;br /&gt;&lt;/summary&gt;SpriteBatch spriteBatch;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Now we want to initialize this variable. Inside of the Initialize method, place this code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Initialize the SpriteBatch.&lt;br /&gt;spriteBatch = new SpriteBatch(graphics.GraphicsDevice);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The SpriteBatch constructor needs a GraphicsDevice class in order to be initialized. The GraphicsDeviceManager graphics has a GraphicsDevice in it, so we just use that.&lt;br /&gt;&lt;br /&gt;Finally, we can draw the text. Using the SpriteBatch class and the SpriteFont class, we can render any string that we want. Inside of the Draw method, after the graphics.GraphicsDevice.Clear(Color.CornflowerBlue); line, add this code.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;/*Start the SpriteBatch. This call is required to set different &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;options. For us, we don't need any special modes so we call the &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;zero-parameter method.*/&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;spriteBatch.Begin(); &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;/*Draw the font. We pass in the SpriteFont to use, the string to draw, the position, and the color of the text.*/&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;spriteBatch.DrawString(verdanaFont, "Look at me!!", new&lt;br /&gt;Vector2(100, 100), Color.Black); &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;/*Finally, end the SpriteBatch. For better processing results, all things to draw are placed in a list. When we call End, we draw all these things at once. Also, we return the different changed parameters back to their default values.*/&lt;/span&gt;&lt;/p&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;spriteBatch.End();&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This code may seem confusing, but it can be easily divided into three simple parts.&lt;br /&gt;&lt;br /&gt;First, we start the SpriteBatch. This is needed so that we can set different render-states for the device such as alpha-blending (transparency), or the way you want sprites to be sorted.&lt;br /&gt;&lt;br /&gt;Then, you draw the SpriteBatch. This is the most important part of the code. In this call, we pass in several different parameters. First, we pass in the SpriteFont to be used. For us we passed in our Verdana font. Next, there is the actual string to draw. This can be anything you want. After this we pass in the position. The position is stored as a Vector2. We pass in 100, 100, which is just a little off of the top-left corner of the screen. Note that the top, left corner of the first letter of the string will be drawn at 100, 100. To change this you need to change the origin of where the text is drawn. Finally, we have the color of the text which mostly explains its purpose by its name.&lt;br /&gt;&lt;br /&gt;The last part of the code is the call to end the SpriteBatch. As mentioned in the comments, it would be very inefficient to render each sprite whenever a Draw call was made. For this reason, all sprites to be drawn are put into a list. When the End method is called, all the sprites in the list are drawn at once. Also, once all the sprites are drawn, the different render-states for the device are reset back to their original values.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;The Final Results&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Finally, we can see the results of our work. If you run the project (F5), you should see something very similar to this:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp3.blogger.com/_kLtxSdyLIgE/RzpgEvzVo_I/AAAAAAAAAIk/AcVV7ACj_HY/s1600-h/LookAtMeTextResult.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132520360009638898" style="CURSOR: hand" alt="" src="http://bp3.blogger.com/_kLtxSdyLIgE/RzpgEvzVo_I/AAAAAAAAAIk/AcVV7ACj_HY/s400/LookAtMeTextResult.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt; The results of our efforts. Simple text printed onto the screen.&lt;br /&gt;&lt;br /&gt;Hopefully, now you will see, “Look at me!!” written onto the screen. If you do not see these results or if you are confused or found an error, please post a comment so that I can fix it. I do hope this helped you with in your programming quest.&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-576952365910810711?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/576952365910810711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=576952365910810711' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/576952365910810711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/576952365910810711'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/how-to-print-simple-text-onto-screen.html' title='How to Print Simple Text onto the Screen'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp1.blogger.com/_kLtxSdyLIgE/RzpgEPzVo9I/AAAAAAAAAIU/VYnowdtU88Y/s72-c/AddNewItemSE.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-6218175184784256745</id><published>2007-11-15T17:04:00.002-08:00</published><updated>2007-11-15T17:05:32.336-08:00</updated><title type='text'>How to Render a Sprite Using the SpriteBatch</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;One of the most common requests for a beginner programmer would probably be how to render a 2D image onto the screen. This process is not actually very complicated at all. Using the SpriteBatch class given to us you can render 2D texture images in no time.&lt;br /&gt;&lt;br /&gt;To render a sprite (a 2D texture) there are three main steps that need to be followed. These steps are as follows: &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-family:trebuchet ms;"&gt;Add an image to your project so that it can be accessed in your code.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:trebuchet ms;"&gt;Create a Texture2D variable in your project and load the texture image into that variable using the Content Pipeline.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:trebuchet ms;"&gt;Draw the texture onto the screen with certain parameters that you would like to be applied to the texture.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Adding an Image to Your Project&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;First, before you can draw the texture, you need to be able to access the texture you want to draw. Through a system called the Content Pipeline, you can load different content files into your project and then access them using a ContentManager. Though this process is important, we first need to load an image into our project.&lt;br /&gt;&lt;br /&gt;To load an image, you need to first navigate to your Solution Explorer (View / Solution Explorer). After this, &lt;strong&gt;right click&lt;/strong&gt; on the name of your project and select &lt;strong&gt;Add / Existing Item&lt;/strong&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp3.blogger.com/_kLtxSdyLIgE/RzugUfcdJnI/AAAAAAAAAI8/eqveCMh6x4w/s1600-h/AddExistingItemSE.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132872474217490034" style="CURSOR: hand" alt="" src="http://bp3.blogger.com/_kLtxSdyLIgE/RzugUfcdJnI/AAAAAAAAAI8/eqveCMh6x4w/s400/AddExistingItemSE.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Right click on the name of your project and select Add / Existing Item&lt;br /&gt;&lt;br /&gt;You should now be taken to a page where you can browse through your computer files. What you want to do is browse to where a simple texture is located. Once you have located where the file should be, in the Files of Type drop-down box, switch to Content Pipeline Files. This option shows all files that XNA will allow to be added to your project. Most likely, your image is of file type TGA, PNG, BMP, JPG, or DDS, all of which can be added into your project. Once your file is select, click Add. The file now should be added to your Solution Explorer where you can see it:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RzugT_cdJlI/AAAAAAAAAIs/ROWQgBB2Trk/s1600-h/StoneTextureInSolutionExplorer.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132872465627555410" style="CURSOR: hand" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RzugT_cdJlI/AAAAAAAAAIs/ROWQgBB2Trk/s400/StoneTextureInSolutionExplorer.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; You can now see the texture file inside of your Solution Explorer. My texture was a JPG file called StoneTexture.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Creating a Variable to Hold the Texture&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;After loading an image into our project, we can now create a variable to contain that image data. This variable will then be used to draw the image onto the screen.&lt;br /&gt;&lt;br /&gt;First, because our texture is a 2D image, we want to create a variable of type Texture2D. Create this variable at the top of the class near the GraphicsDeviceManager and ContentManager variables.&lt;br /&gt;&lt;summary&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//The texture variable that will hold our image data.&lt;br /&gt;&lt;/summary&gt;Texture2D texture;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Next, we need to initialize this variable. With most variables, you call the constructor of the class to create it. Unlike these classes, all content-related variables are created by directly loading them with a file. For our case, this will be the texture loaded into our project. Because of graphics device related issues which do not need to be explained in this lesson, put this code inside of the if (loadAllContent) statement of the LoadGraphicsContent method. This code initializes our Texture2D:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;/*Use the ContentManager content variable to initialize&lt;br /&gt;the Texture2D with a real texture.*/&lt;br /&gt;texture = content.Load[Texture2D]&lt;texture2d&gt;("StoneTexture");&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Code Note: &lt;/strong&gt;The brackets in the above code should actually be tags. I needed to use brackets because the HTML would not allow tags.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;This code statement may look long and confusing but it is quite simple. We call the Load method of a ContentManager variable named content (created by default). Then we specify for the method that we are loading a Texture2D and that the texture name is “StoneTexture” (for you that will probably be different). That is really all there is to that line.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Drawing the Texture Using the SpriteBatch&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;Finally, we have come to the part where we can display our texture. Using the SpriteBatch class and its Draw method, we can easily draw our texture.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; To &lt;em&gt;draw&lt;/em&gt; a sprite means the same thing as to &lt;em&gt;render&lt;/em&gt; a sprite.&lt;br /&gt;&lt;br /&gt;The first step is to create the SpriteBatch. At the top of the class along with the other variable declarations, create a SpriteBatch:&lt;br /&gt;&lt;summary&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//The SpriteBatch we will use to draw the texture. &lt;/summary&gt;&lt;br /&gt;SpriteBatch spriteBatch;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Now, we can initialize it. Inside of the Initialize method, add this code to initialize our SpriteBatch:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Initialize the SpriteBatch.&lt;br /&gt;spriteBatch = new SpriteBatch(graphics.GraphicsDevice);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The constructor for the SpriteBatch needs a GraphicsDevice. We access one by using the GraphicsDevice property inside of our GraphicsDeviceManager variable.&lt;br /&gt;&lt;br /&gt;Since our SpriteBatch has now been created, we can now use it. The following calls to the SpriteBatch class will be placed in the Draw method right after the graphics.GraphicsDevice.Clear(Color.CornflowerBlue); line.&lt;br /&gt;&lt;br /&gt;First, we need to start the SpriteBatch. The reason for doing this is to set any needed parameters for the SpriteBatch. These parameters are things such as allowing the ability to take transparency into account or setting the way the SpriteBatch will sort the sprites. Place this code into the Draw method:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;/*Start the SpriteBatch. Your sprite may contain transparency so we &lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-family:courier new;"&gt;allow Alpha-Blending.*/&lt;br /&gt;spriteBatch.Begin(SpriteBlendMode.AlphaBlend);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Next, we want to draw the sprite. Place this code right after the previous line:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Draw the sprite.&lt;br /&gt;spriteBatch.Draw(texture, new Vector2(0, 0), Color.White);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;We draw the sprite by calling the Draw method of the SpriteBatch variable. The first parameter is the Texture2D that we want to draw. We pass in texture. The next parameter is the position to draw the sprite at. This value is stored in a Vector2. For us, we specify 0, 0, which means that the origin of the sprite will be drawn at that position. The final parameter is the color to taint the background of the sprite with. We want the sprite to look completely normal so we specify Color.White.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; The default origin of the sprite is at the top-left corner of the sprite. To change this, a different overload of Draw needs to be called.&lt;br /&gt;&lt;br /&gt;Finally, we need to “end” the SpriteBatch. There are two purposes for this. One is that instead of the sprites being drawn at the moment you call the Draw method, they are actually added to a list. Once End is called, all the sprites in the list are drawn at once. This is actually more efficient. The second reason is to reset the different values you changed when you called the Begin method. The different values are changed back to their default value until you call the Begin method again. After the two previous calls to the SpriteBatch, place this final statement:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//End the SpriteBatch.&lt;br /&gt;spriteBatch.End();&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Congratulations! You have now successfully coded a small application that renders a sprite onto the screen. Running the project now should result in your sprite drawn at the top-left corner of the screen (because we specified 0, 0 for the position).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;br /&gt;&lt;a href="http://bp2.blogger.com/_kLtxSdyLIgE/RzugUPcdJmI/AAAAAAAAAI0/JxLGLC4iuI8/s1600-h/StoneTextureRendered.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132872469922522722" style="CURSOR: hand" alt="" src="http://bp2.blogger.com/_kLtxSdyLIgE/RzugUPcdJmI/AAAAAAAAAI0/JxLGLC4iuI8/s400/StoneTextureRendered.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt; After completing this lesson, you should hopefully see a result resembling this screen-shot.&lt;br /&gt;&lt;br /&gt;I do hope that you have been able to solve some confusion by reading this lesson and I fully hope that you can easily continue along with your gaming quest. If you find any errors or mistakes, or if any parts of this lesson were confusing, please post a comment so that I can fix it. Thanks again.&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-6218175184784256745?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/6218175184784256745/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=6218175184784256745' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/6218175184784256745'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/6218175184784256745'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/how-to-render-sprite-using-spritebatch.html' title='How to Render a Sprite Using the SpriteBatch'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp3.blogger.com/_kLtxSdyLIgE/RzugUfcdJnI/AAAAAAAAAI8/eqveCMh6x4w/s72-c/AddExistingItemSE.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-1029216410845492660</id><published>2007-11-15T17:04:00.001-08:00</published><updated>2007-11-18T18:55:30.279-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Tutorials'/><title type='text'>Tutorials</title><content type='html'>These tutorials I hope can get you going onto a new topic or help you with an unknown subject. If you have any requests, please post a comment. Thanks.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The SpriteFont&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/how-to-print-simple-text-onto-screen.html"&gt;How to Print Simple Text onto the Screen&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2D Textures&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/how-to-render-sprite-using-spritebatch.html"&gt;How to Render a Sprite Using the SpriteBatch&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I give all the desires of my heart to you, Lord. May you work in everyone's heart so that they will discover the true joy that comes with following you. Amen.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-1029216410845492660?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/1029216410845492660/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=1029216410845492660' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/1029216410845492660'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/1029216410845492660'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/tutorials.html' title='Tutorials'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-6164763659974037958</id><published>2007-11-15T17:00:00.001-08:00</published><updated>2007-11-15T17:00:39.729-08:00</updated><title type='text'>Methods and Properties</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:130%;"&gt;&lt;strong&gt;Basic Method Creation&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;Methods are very useful in the C# language. Methods can help organize and simplify code, and are used all the time in game programming. This lesson should be able to introduce anyone who knows even nothing about programming, to the world of methods and how to create them, use them, and benefit from them in your programming. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;br /&gt;A method can be looked at like a named container that holds code and does something to a value. This can be useful, for example, if you wanted to explode a ship. Instead of re-writing the code to explode the ship every time there is a collision, you can just create a method that explodes a ship, and then just call that method every time you want a ship to explode.&lt;br /&gt;&lt;br /&gt;First, before we can call a method, we need to learn how to create methods. Here is the method creation structure:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;accessModifier returnType methodName ( parameters )&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;First of all, methods need an access modifier. This modifier serves the same purpose as access modifiers on variables.&lt;br /&gt;&lt;br /&gt;Next, there is the return type. The return type shows what type of variable you want the method to return. For example, let’s say that we have created a method called GiveBackEight. This method would look like the following:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private int GiveBackEight()&lt;br /&gt;{&lt;br /&gt;return 8;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;When we call this method, we want to set any integer to a value of eight. In order to be able to set an integer to the value that the method returns we need to have an int as the parameter type. This way, we can set the value of a variable to the value this method returns by using the command &lt;span style="font-family:courier new;"&gt;int myVariable = GiveBackEight();&lt;/span&gt; This sets the value of myVariable to the value of what GiveBackEight returns.&lt;br /&gt;You can also create a variable that has a return type of, void which means the method does not return anything. These types of methods could be used for example to just draw something. You could declare a method that drew the number eight on the screen like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private void DrawEight()&lt;br /&gt;{&lt;br /&gt;Console.WriteLine("8");&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This just draws the number eight onto the screen. These types of methods don’t give back anything. These types of methods are to do commands that will finish themselves. They are never used to change the value of a variable and give it back because with a void return type, the methods cannot return anything. You can call this method by using a command like &lt;span style="font-family:courier new;"&gt;DrawEight();&lt;/span&gt; This just calls the method to do what it does.&lt;br /&gt;&lt;br /&gt;After the return type, you have the method name, which is just like a variable name, and is used to access the method.&lt;br /&gt;&lt;br /&gt;Finally, you have the parameters, which are placed in parentheses. A parameter is like a value that is given from an outside source so that the method can complete what it needs to do accurately. We have talked about these types of variables in the previous chapter. For example, if we wanted to allow the capability to draw any number, not just eight, we could change the DrawEight method to:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private void DrawNumber(int numberToDraw)&lt;br /&gt;{&lt;br /&gt;Console.WriteLine(numberToDraw.ToString());&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This code takes any number that you choose as a parameter, and then draws that number. This is very helpful because, instead of having to make a billion different methods to draw every number, you can just create one method that has the capability to draw any number.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Method Overloads&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Sometimes you may want to create the same method, but with maybe a few different parameters. An example of this is, maybe you have an explosion method that explodes at a given intensity value. You may also want to make it where the default intensity value is 100. You could do something like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private void ExlodeShip(int intensity)&lt;br /&gt;{&lt;br /&gt;//Explosion code&lt;br /&gt;}&lt;br /&gt;private void ExplodeShip()&lt;br /&gt;{&lt;br /&gt;ExplodeShip(100);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This code first creates the ExplodeShip method. This method takes an int parameter for the intensity of the explosion. Below that method though, we have another method with the same name. This method is an overload of the original ExplodeShip method but takes no parameters. Since 100 is the most common explosion intensity, for most cases the user will just pass in 100 for the intensity value. Instead of making him do that, we create an overload of ExplodeShip that takes no parameters but calls the one-parameter ExplodeShip version and passes in 100 because that is what the user wants. For more complicated methods, this can save a lot of time.&lt;br /&gt;&lt;br /&gt;There are a few things you need to watch out for when using method overloads. You need to be careful not to have a continual loop. This could maybe happen when you on accident call the same method that you are in. For example calling ExplodeShip(); in the zero-parameter method would result in a continual loop because you are inside the method that you are calling.&lt;br /&gt;&lt;br /&gt;Another thing is that you can’t have the method overload have the same parameter types. Even if one overload has int intensity and another method has int timeDelay, you can’t do this because the C# compiler (program that executes the code) will not be able to tell the difference. For it, both overloads have the same parameter types and it won’t know which method to call.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Properties&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;Properties are another type of code-block that you can use in C#. Some people may think properties are really confusing, but they aren’t at all, and are almost exactly the same as methods. You could look at a property like a method that doesn’t have parameters. To create a property, you would do something like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;accessModifier returnType propertyName&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;You can see that this format is almost the same as the method format. We have an access modifier, a return type, and a property name. These all serve the same purpose as for methods.&lt;br /&gt;&lt;br /&gt;There is one exception though. Besides the fact that properties can’t take parameters, they also can’t have the void return type. This is because properties are usually used a little differently than methods. Methods are more used to do things to values and then give the value back. Properties aren’t so much used that way. Properties are more used to give a value or set a value of something. This would explain why you can’t have the void return type because properties can’t just do something; they have to return a value.&lt;br /&gt;&lt;br /&gt;To explain this, you could create this method which would return the value of ten:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private int Ten()&lt;br /&gt;{&lt;br /&gt;return 10;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;You could also create the same thing using a property:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private int Ten&lt;br /&gt;{&lt;br /&gt;get&lt;br /&gt;{&lt;br /&gt;return 10;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The property would be the correct way to go. All this property does, is, when you try to access the value of what Ten has, the get part of the property is called. The only difference is that instead of calling &lt;span style="font-family:courier new;"&gt;myInt = Ten();&lt;/span&gt; which is the method version, you call &lt;span style="font-family:courier new;"&gt;myInt = Ten;&lt;/span&gt; which doesn’t use the parentheses because there can be no parameters in a property.&lt;br /&gt;&lt;br /&gt;Properties are mostly used for when you want to access a value in a class. For example the MathHelper class has a property called Pi which gives the value of PI.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Set Properties&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;Set properties are also types of properties that you can use. The way I will explain this is in a game example. Pretend you had a player class. Inside that player class you have a health property that allows access to the health. You would want someone to be able to access the health variable and change it to his liking. You could do this by using this code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;private int health;&lt;br /&gt;public int Health&lt;br /&gt;{&lt;br /&gt;get&lt;br /&gt;{&lt;br /&gt;return health;&lt;br /&gt;}&lt;br /&gt;set&lt;br /&gt;{&lt;br /&gt;health = value;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;First, we declare a private variable called health. This variable allows us to access health in our class when we want to change it or set it to a value.&lt;br /&gt;&lt;br /&gt;After that we have our property called Health (remember variable names are case-sensitive). First we call the get part which returns the value of what our private health variable is. Then we call the set part. This part is called when we set the value of Health using something like &lt;span style="font-family:courier new;"&gt;Health = aValue&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;;&lt;/span&gt;. In this part of the property we set health, our private variable, to value which is the value that we put on the right side of the equal sign when we set Health.&lt;br /&gt;&lt;br /&gt;Since names in C# are case-sensitive, we can make a variable and a property have the same name. Of course, you may think that we can just make the private health variable into a&lt;br /&gt;public one, we won’t have to worry about the property stuff. We could do this, but it would be incorrect programming practice. Also, using properties allows us to change the value of what we return which you can’t do if you just have a public variable. This could look like the following:&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;private int health;&lt;br /&gt;public int TenMoreThanHealth&lt;br /&gt;{&lt;br /&gt;get&lt;br /&gt;{&lt;br /&gt;return health + 10;&lt;br /&gt;}&lt;br /&gt;set&lt;br /&gt;{&lt;br /&gt;health = value;&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-6164763659974037958?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/6164763659974037958/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=6164763659974037958' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/6164763659974037958'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/6164763659974037958'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/methods-and-properties.html' title='Methods and Properties'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-5448592491759162516</id><published>2007-11-15T16:59:00.001-08:00</published><updated>2007-11-18T18:55:57.836-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Primer'/><title type='text'>C# Primer</title><content type='html'>Since XNA uses the C# language, I've decided to write some simple lessons on the basics of C#. This way, understanding the XNA code won't have to be so confusing.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/variables.html"&gt;Variables&lt;/a&gt;--Explains about variables and how to use them.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/methods-and-properties.html"&gt;Methods and Properties&lt;/a&gt;--Explains about how to create and call methods and properties.&lt;br /&gt;&lt;br /&gt;I give all the desires of my heart to you, Lord. May you work in everyone's heart so that they will discover the true joy that comes with following you. Amen.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-5448592491759162516?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/5448592491759162516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=5448592491759162516' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5448592491759162516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5448592491759162516'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/c-primer.html' title='C# Primer'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-5228995239506854832</id><published>2007-11-15T16:57:00.000-08:00</published><updated>2007-11-15T16:58:18.839-08:00</updated><title type='text'>Variables</title><content type='html'>&lt;p&gt;&lt;strong&gt;&lt;span style="font-family:trebuchet ms;font-size:130%;"&gt;Creating Variables&lt;/span&gt;&lt;/strong&gt; &lt;p&gt;&lt;span style="font-family:Trebuchet MS;"&gt;Variables are very important in the C# language. Using variables gives you control of many different values. There are tons of different variables that can be used in C# and they all follow a similar pattern; one that we will explain.&lt;/span&gt; &lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;First of all, what is a variable? A variable is named object that can be accessed and manipulated. You can access these values to use them in your project. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;There are three different ways you can declare variables. The first way you can declare a variable is at the top of a class. This allows you to access the variable from any method or property inside of the class. These types of variables can be called class-variables. To declare a class variable, you would try and follow this format:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;accessModifier variableType variableName ;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;First of all, there is the access modifier. An access modifier is a keyword placed in front of a variable, method or other member, which restricts or allows access to it from some other class or struct. A member is any variable, property, or method inside of a class. The private modifier allows the least amount of access and allows the item to only be accessed inside the class it was created in. The protected modifier allows access only from inherited classes, and the public modifier allows total access from all classes. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;There is also an access modifier named static. The static keyword allows the item it is being applied to, to not be part of a class instance but, instead, related to the class name. This modifier is not used at the moment but will be explained later.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;After the access modifier we have the variable type. Each variable type contains different information needed for that variable type. For example, the int class contains information that holds integers. The string class contains information that holds text, and the bool class contains information that holds true or false values. Each variable type is different but most can be created the same way. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;Then, after the variable type, you can name the variable. Variable names can be anything they want, but they are case-sensitive. &lt;/span&gt;&lt;/p&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;p&gt;Finally, you have the semi-colon which you place to signify that you are done creating the variable.&lt;/p&gt;&lt;p&gt;As I told you, there are different ways that you can create a variable. We just looked at the class variable type. There is also another way to create variables. In this way, you create a variable inside a method or property. The difference between these types of variables is that they can only be accessed inside of that method or property. You create these types of variables the same way as a class variable, but you do not place an access modifier because you can’t access the variable outside of the function it was created in. This limit of access is called a variables scope. You would want to create this type of variable like so:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;variableType variableName ;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Finally, there is one last way you can create variables. These types of variables are used inside method calls and are called parameters. For example if I called the, Multiply method of a Math class then I could pass in, 5 as an parameter. I wouldn’t name the variable because I can never access it again, and I don’t need any access modifiers, variable type name, or even a semi-colon. All I need is the value of the variable. &lt;/p&gt;&lt;p&gt;There are many different types of variables and their uses can be used in different ways. Here is a list giving a quick description of the variable types and their uses:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Variable Type; Containment Type; Constructor Type&lt;br /&gt;Int32(int); (-)2,147,483,64(7-8); int intVar;&lt;br /&gt;Int64(long); (-)9,223,372,036,854,775,80(7-8);long longVar;&lt;br /&gt;Byte(byte); 255; byte byteVar;&lt;br /&gt;UInt32(uint); 4,294,967,295; uint uintVar;&lt;br /&gt;Uint64(ulong); 18,446,744,073,709,551,615; ulong ulongVar;&lt;br /&gt;Single(float); (-)7 Digits; float floatVar&lt;br /&gt;Double(double); (-)15-16 Digits; double doubleVar;&lt;br /&gt;Decimal(decimal); (-)28-29 Digits; decimal decimalVar;&lt;br /&gt;String(string); Text between quotes(“”); string stringVar;&lt;br /&gt;Char(char); One Character between symbols(‘’);char charVar;&lt;br /&gt;Boolean(bool); True or False; bool boolVar;&lt;br /&gt;Random; Used for Randomizing; Random rnd = new Random()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The Constructor Type shows how you can create a variable of that type. The Random class inherits from the Object class instead of the Type class and needs to be created using the, &lt;span style="font-family:courier new;"&gt;new ClassType(...);&lt;/span&gt; format. This way calls the constructor of that variable. The constructor of a variable is the method that is called to create the variable.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Assigning Values to Variables&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;Assigning values to variables is also very important. In order to actually be able to use the variable in your project, you need a way to assign values to it.&lt;/p&gt;&lt;p&gt;The assignment operator is used for assigning values. An operator is a symbol that C# recognizes as a command to do something to a variable(s). You can assign variables using the following structure:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;leftHandVar = rightHandValue ;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;First, we place the name of the variable we want to assign a value to. If a variable does not yet have a specified value, then this process is called initializing. A variables initial value is the value it is first set to. You can also initialize a variable at the same time it is created like, &lt;span style="font-family:courier new;"&gt;private int intVar = 4;&lt;/span&gt; If you don’t initialize a variable it will either be set to a default value like zero, or will be set to null, which means no value.&lt;/p&gt;&lt;p&gt;Next we have the equal operator. This operator gets the preceding variable ready to be assigned a value. &lt;/p&gt;&lt;p&gt;Finally, we have the new value we want the variable to be set to.&lt;br /&gt;You can also assign a variable to equal the value of another variable. This could look like, &lt;span style="font-family:courier new;"&gt;intVar = intVar2;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-size:130%;"&gt;Modifying Variable Values&lt;/span&gt;&lt;br /&gt;&lt;/strong&gt;After you have assigned a value to a variable, you will probably want to be able to modify that value. For numbers, there are some of the normal math operators that can be used. These operators can be used as such:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;int1 += 10;&lt;br /&gt;int1 -= 10;&lt;br /&gt;int1 *= 10;&lt;br /&gt;int1 /= 10;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;These operators change the values of variables. The &lt;span style="font-family:courier new;"&gt;+=&lt;/span&gt; means the same thing as, &lt;span style="font-family:courier new;"&gt;int1 = int1 + 10;&lt;/span&gt; You can also modify the right hand value by doing something like, &lt;span style="font-family:courier new;"&gt;int1 += 10 * 3&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-5228995239506854832?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/5228995239506854832/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=5228995239506854832' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5228995239506854832'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5228995239506854832'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/variables.html' title='Variables'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-5334250595153076157</id><published>2007-11-15T16:52:00.000-08:00</published><updated>2007-11-17T14:35:00.773-08:00</updated><title type='text'>What are all the possible font names that you can use for a SpriteFont?</title><content type='html'>&lt;p&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question:&lt;/strong&gt; What are all the possible font names that you can use for a SpriteFont?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Microsoft has allowed almost all the font families to be used for a SpriteFont. If you are having problems though, take not that there are some standards that need to be used in order to have XNA recognize the font family name.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-family:trebuchet ms;"&gt;You need to allow spaces which may seem odd but is needed (i.e. the font family Times New Roman would still be entered in as “Times New Roman” and not “TimesNewRoman”).&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-family:trebuchet ms;"&gt;Also, obviously you need to spell the name correctly. No little mistakes as every letter matters. Also, there is no abbreviating.&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-family:trebuchet ms;"&gt;The Algerian font family is not accepted.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-5334250595153076157?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/5334250595153076157/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=5334250595153076157' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5334250595153076157'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5334250595153076157'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/question-what-are-all-possible-font.html' title='What are all the possible font names that you can use for a SpriteFont?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-3472453843606508872</id><published>2007-11-15T16:50:00.000-08:00</published><updated>2007-11-17T14:35:22.772-08:00</updated><title type='text'>How can I vibrate an Xbox 360 controller?</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question:&lt;/strong&gt; How can I vibrate an Xbox 360 controller?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Vibrating an Xbox 360 controller is actually quite simple. All you need to do is call the SetVibration method of the static GamePad class.&lt;br /&gt;&lt;br /&gt;To vibrate a controller, you don’t actually need a GamePadState to work with because all you have to do is call GamePad.SetVibration(…). For our sake though, we will create a GamePadState just so that we can check if certain buttons are down and then vibrate the controller respectively. Create a GamePadState at the top of the Update method.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//The GamePad to check for input from.&lt;br /&gt;GamePadState gamePad = GamePad.GetState(PlayerIndex.One);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Now, for a simple effect, whenever you are pressing the A button, the left motor of the controller vibrates and whenever you are pressing the B button, the right motor of the controller vibrates. To do this, place this code also into the Update method.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Create two variables to keep track of the rotation values of the &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-family:courier new;"&gt;//motors.&lt;br /&gt;float leftMotorVibration = 0;&lt;br /&gt;float rightMotorVibration = 0;&lt;br /&gt;&lt;br /&gt;//If A is pressed, vibrate the left motor.&lt;br /&gt;if (gamePad.Buttons.A == ButtonState.Pressed)&lt;br /&gt;{&lt;br /&gt;leftMotorVibration = 1;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;leftMotorVibration = 0;&lt;br /&gt;}&lt;br /&gt;//If B is pressed, vibrate the right motor.&lt;br /&gt;if (gamePad.Buttons.B == ButtonState.Pressed)&lt;br /&gt;{&lt;br /&gt;rightMotorVibration = 1;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;rightMotorVibration = 0;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;//Finally, vibrate the motors based on their individual values.&lt;br /&gt;GamePad.SetVibration(PlayerIndex.One, leftMotorVibration,&lt;br /&gt;rightMotorVibration);&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;First we create two simple floats to keep track of the vibration value of the motors. After that we check for input. Note that we have to check if the buttons are not pressed along with if they are pressed. This is because, if the buttons were pressed and then were released, nothing ever sets the vibration back to zero. Finally, after checking for input, we call the SetVibration method of the GamePad class. The first parameter specifies the player controller to vibrate which for us was Player 1. The second and third parameters specify the amount to rotate the motors by. For us we passed in leftMotorVibration and rightMotorVibration.&lt;br /&gt;&lt;br /&gt;If you run the project now and press A or B, Player 1’s game controller should vibrate accordingly.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-3472453843606508872?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/3472453843606508872/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=3472453843606508872' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/3472453843606508872'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/3472453843606508872'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/question-how-can-i-vibrate-xbox-360.html' title='How can I vibrate an Xbox 360 controller?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-4750775550028797166</id><published>2007-11-15T16:45:00.000-08:00</published><updated>2007-11-15T16:46:23.556-08:00</updated><title type='text'></title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Question:&lt;/strong&gt; How do I tint the color of a Texture2D using the SpriteBatch?&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Answer:&lt;/strong&gt; To tint the color of a Texture2D when using the Draw call of a SpriteBatch, you need to change the Color parameter. For example, to tint the background of a sprite to the color orange, you would make this call in the Draw method:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;spriteBatch.Draw(myTexture, new Vector2(0, 0), &lt;strong&gt;Color.Orange&lt;/strong&gt;);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The Color.Orange parameter is the important parameter. This tells the SpriteBatch to draw the texture with an overall orange-ish background. An example of the same texture drawn twice is shown below: &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RzpXkPzVo8I/AAAAAAAAAIM/IH3qOd1OTOQ/s1600-h/FacePicture.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132511005570868162" style="CURSOR: hand" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RzpXkPzVo8I/AAAAAAAAAIM/IH3qOd1OTOQ/s400/FacePicture.jpg" border="0" /&gt;&lt;/a&gt;&lt;a href="http://bp0.blogger.com/_kLtxSdyLIgE/RzpXj_zVo7I/AAAAAAAAAIE/8A1lvs4Om0I/s1600-h/OrangeFacePicture.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5132511001275900850" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_kLtxSdyLIgE/RzpXj_zVo7I/AAAAAAAAAIE/8A1lvs4Om0I/s400/OrangeFacePicture.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; On the left there is the texture drawn with a white background. On the left, the same texture is drawn with an orange background.&lt;br /&gt;&lt;br /&gt;The same texture was drawn once with Color.White. This value specifies that you want no changes to the background of the texture (i.e. if the original background was blue and you specified Color.White, the background would stay blue). Then the texture was drawn with Color.Orange. This drew the sprite with an orange-ish color. Note that if the original background was blue, the background would not turn orange, but instead the blue background would be mixed with the orange color to create a new, odd color.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-4750775550028797166?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/4750775550028797166/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=4750775550028797166' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/4750775550028797166'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/4750775550028797166'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/question-how-do-i-tint-color-of.html' title=''/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp1.blogger.com/_kLtxSdyLIgE/RzpXkPzVo8I/AAAAAAAAAIM/IH3qOd1OTOQ/s72-c/FacePicture.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-8080013780416578132</id><published>2007-11-15T16:43:00.000-08:00</published><updated>2007-11-22T19:55:04.091-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Question and Answer'/><title type='text'>Question and Answer</title><content type='html'>Hopefully, these question and answer posts will help you. If you have a question that is not answered, please post a comment and ask for it to be answered.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The SpriteFont&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/question-what-are-all-possible-font.html"&gt;What are all the possible font names that you can use for a SpriteFont?&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Xbox 360 Controller&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/question-how-can-i-vibrate-xbox-360.html"&gt;How can I vibrate an Xbox 360 controller?&lt;/a&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/how-can-i-check-to-see-if-xbox-360.html"&gt;How can I check to see if an controller button is down?&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Keyboard&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/how-can-i-see-if-key-is-down-or-not.html"&gt;How can I check if a key is down or not?&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The Content Pipeline&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/what-are-all-possible-texture-file.html"&gt;What are all the possible texture file types that can be used?&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Drawing 2D Textures With the SpriteBatch&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/question-how-do-i-tint-color-of.html"&gt;How do I tint the color of a Texture2D using the SpriteBatch?&lt;/a&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/question-how-do-i-scale-size-of-sprite.html"&gt;How do I scale the size of a sprite?&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2D Rectangles&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/how-do-i-build-rectangle-around-object.html"&gt;How do I build a rectangle around an object?&lt;/a&gt;&lt;br /&gt;&lt;a href="http://christiangameprogramming.blogspot.com/2007/11/how-do-i-check-if-two-rectangles-are.html"&gt;How do I check if two rectangles are overlapping?&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I give all the desires of my heart to you, Lord. May you work in everyone's heart so that they will discover the true joy that comes with following you. Amen.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-8080013780416578132?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/8080013780416578132/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=8080013780416578132' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/8080013780416578132'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/8080013780416578132'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/11/question-and-answer.html' title='Question and Answer'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-565546489106161008</id><published>2007-09-09T17:34:00.000-07:00</published><updated>2007-09-09T17:39:34.540-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='2D Tutorials'/><title type='text'>Tutorial 8 -- Keyboard Key Presses and Mouse Clicks</title><content type='html'>&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;This tutorial shows you how to track keyboard key presses and mouse button clicks.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Requirements&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Knowledge of &lt;a href="http://christiangameprogramming.blogspot.com/2007/09/tutorial-7-advanced-sprite-drawing.html"&gt;Tutorial 7&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Read&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Read the tutorial here: &lt;a href="http://christiangameprogramming.blogspot.com/2007/09/handling-keyboard-key-presses-and-mouse.html"&gt;Tutorial 8&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Remember&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;You don't get to Heaven by what you do, but by what you believe.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-565546489106161008?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/565546489106161008/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=565546489106161008' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/565546489106161008'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/565546489106161008'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/09/tutorial-8-keyboard-key-presses-and.html' title='Tutorial 8 -- Keyboard Key Presses and Mouse Clicks'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-3527298162207417293</id><published>2007-09-09T11:59:00.001-07:00</published><updated>2007-09-09T17:34:06.269-07:00</updated><title type='text'>Handling Keyboard Key Presses and Mouse Clicks</title><content type='html'>&lt;span&gt;&lt;/span&gt;In this tutorial, I want to talk about how you can handle keyboard key presses and mouse button clicks. You already know how to track if a keyboard key or mouse button is down, but a lot of input checks need to only occur if a key is presses or a button is clicked.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Handling Keyboard Key Presses&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;What we need to do is keep a KeyboardState variable, that actually contains the data for the last Update loop. That way, if we need to have a check for key being down, all we have to do is make sure the current keyboard state is not equal to the last keyboard state. Then, after each Update, we set the value of the old keyboard to the value of the new keyboard. You may see how this works once we get started.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 1: Create an Old Keyboard State Variable&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;First we need to create a variable that holds the old state of the keyboard. Create this variable in the fields are:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#66cccc;"&gt;KeyboardState&lt;/span&gt; myOldKeyboard = &lt;span style="color:#66cccc;"&gt;Keyboard&lt;/span&gt;.GetState();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As you see here, we are creating a keyboard state variable and naming it &lt;em&gt;myOldKeyboard&lt;/em&gt;. Also, we are intializing it right now. This is because we are not going to initialize it at the beginning of the first update loop, so we should initialize it now. This is only going to matter if a key happens to be pressed right when the project starts(which would have to be in like the first 1.6 milliseconds or so).&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 2: Update the Old KeyboardState at the End Of The Update Method&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Now we need to update &lt;em&gt;oldKeyboard&lt;/em&gt; at the end of the &lt;em&gt;Update&lt;/em&gt; method. This is because, if a key happens to be pressed, and we don't keep track of that in the old KeyboardState variable, then &lt;em&gt;oldKeyboard&lt;/em&gt; will not be exactly correct.&lt;br /&gt;&lt;br /&gt;To update the old keyboard to the old state of the real keyboard, put this line at the end of the &lt;em&gt;Update&lt;/em&gt; method:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;myOldKeyboard = myKeyboard;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;All this does is set the value of the &lt;em&gt;oldKeyboard&lt;/em&gt;, to the value of the current keyboard(&lt;em&gt;myKeyboard&lt;/em&gt;), which is actually old because it is used at the end of the &lt;em&gt;Update&lt;/em&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; When I say, "&lt;em&gt;the end of the Update method&lt;/em&gt;", I always mean at the bottom of the &lt;em&gt;Update&lt;/em&gt; method, but above the &lt;em&gt;base.Update&lt;/em&gt; line.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Step 3: Test Key Presses&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Now we get to test if what we have done so far actually works. In order to track a keyboard key press, we will check if the space bar is pressed. If the space bar has been pressed, we will taint our first sprite with a color of blue. If the space bar is pressed again, we will take the blue off of the color.&lt;br /&gt;&lt;br /&gt;Put this code underneath the other keyboard input code for our first sprite:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#3333ff;"&gt;if&lt;/span&gt;(myKeyboard.IsKeyDown(&lt;span style="color:#66cccc;"&gt;Keys&lt;/span&gt;.Space) &amp;&amp;amp;&lt;br /&gt;myOldKeyboard.IsKeyUp(&lt;span style="color:#66cccc;"&gt;Keys&lt;/span&gt;.Space))&lt;br /&gt;{&lt;br /&gt;     spriteColored = !spriteColored;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;a href="http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr8I/AAAAAAAAAHc/AOG3NSMy7us/s1600-h/ScreenShot1.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5108364879338188738" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr8I/AAAAAAAAAHc/AOG3NSMy7us/s400/ScreenShot1.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-1 The Code to Track a Key Press&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;First this code checks if currently, the space bar is pressed. Since &lt;em&gt;myKeyboard&lt;/em&gt; is always set to the current state of the keyboard at the beginning of the&lt;em&gt; Update&lt;/em&gt; method, if the space bar is pressed, this will be true. Then we also check(using the and(&amp;&amp;amp;) operator) if the old state of the keyboard, which was set to a value at the end of the &lt;strong&gt;last&lt;/strong&gt; &lt;em&gt;Update&lt;/em&gt; call, does not have the space bar pressed. If both of these if checks return true, then we set a variable called &lt;em&gt;spriteColored&lt;/em&gt; to its opposite. Now we just need to create this variable in the fields area:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#3333ff;"&gt;bool&lt;/span&gt; spriteColored = &lt;span style="color:#3333ff;"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This creates a bool(a class that can have a value of either true or false) variable and initializes it with a value of false so that the sprite will start out normal.&lt;br /&gt;&lt;br /&gt;Now we just need to make the sprite appear blue when &lt;em&gt;spriteColored&lt;/em&gt; equals true.&lt;br /&gt;&lt;br /&gt;In our &lt;em&gt;Draw&lt;/em&gt; method, locate the call that draws the first sprite. Delete that and replace it with this code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#3333ff;"&gt;if&lt;/span&gt;(spriteColored)&lt;br /&gt;{&lt;br /&gt;mySpriteBatch.Draw(myTexture, spritePosition, &lt;strong&gt;&lt;span style="color:#66cccc;"&gt;Color&lt;/span&gt;.Blue&lt;/strong&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;else&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;mySpriteBatch.Draw(myTexture, spritePosition, &lt;strong&gt;&lt;span style="color:#66cccc;"&gt;Color&lt;/span&gt;.White&lt;/strong&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This draws the sprite with a blue color if &lt;em&gt;spriteColored&lt;/em&gt; equals true, and white if not. We use the else statement to signal that if &lt;em&gt;spriteColored&lt;/em&gt; equals false, then the code inside the else statement will be executed.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr9I/AAAAAAAAAHk/mzYvpXhyBi8/s1600-h/ScreenShot2.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5108364879338188754" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr9I/AAAAAAAAAHk/mzYvpXhyBi8/s400/ScreenShot2.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 1-2 Drawing Our Sprite Blue If SpriteColored Equals True&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; Instead of using &lt;em&gt;if(spriteColored == true)&lt;/em&gt;, I can just put, &lt;em&gt;if(spriteColored)&lt;/em&gt;. Also, &lt;em&gt;if(spriteColored == false)&lt;/em&gt; is the same as &lt;em&gt;if(!spriteColored)&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;Now run the project. You should be able to press the space bar and the sprite will turn blue. You can press it again and the sprite will turn back to white.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr-I/AAAAAAAAAHs/h9HuHWQG9Uc/s1600-h/ScreenShot3.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5108364879338188770" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr-I/AAAAAAAAAHs/h9HuHWQG9Uc/s400/ScreenShot3.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-3 The Sprite When it is Blue&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;That is how you keep track of a key press. Now time to check for mouse clicks.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Handling Mouse Button Clicks&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Handling mouse clicks goes pretty much the same as handling keyboard key presses. To show when a mouse button is clicked, we will flip the second sprite using its SpriteEffects parameter when the right mouse button is clicked.&lt;br /&gt;&lt;br /&gt;First, just like the KeyboardState variable, create a MouseState variable and initialize it:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#66cccc;"&gt;MouseState&lt;/span&gt; myOldMouse = &lt;span style="color:#66cccc;"&gt;Mouse&lt;/span&gt;.GetState();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Then, at the bottom of the Update method, set it so that it always has the current, old value of the keyboard. In other words, so that it always is one &lt;em&gt;Update&lt;/em&gt; method behind.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;myOldMouse = myMouse;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Now we can check if the right button is clicked. Put this code under the mouse input code for the second sprite:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#3333ff;"&gt;if&lt;/span&gt;(myMouse.RightButton == &lt;span style="color:#66cccc;"&gt;ButtonState&lt;/span&gt;.Pressed &amp;&amp;amp; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;myOldMouse.RightButton == &lt;span style="color:#66cccc;"&gt;ButtonState&lt;/span&gt;.Released)&lt;br /&gt;{&lt;br /&gt;sprite2Flipped = !sprite2Flipped;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Create the &lt;em&gt;sprite2Flipped&lt;/em&gt; bool in the fields area:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#3366ff;"&gt;bool&lt;/span&gt; sprite2Flipped = &lt;span style="color:#3366ff;"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Finally, flip the sprite when &lt;em&gt;sprite2Flipped&lt;/em&gt; equals true. Delete the code that draws the second sprite and replace it with this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#3333ff;"&gt;if&lt;/span&gt;(sprite2Flipped)&lt;br /&gt;{&lt;br /&gt;mySpriteBatch.Draw(myTexture2, spritePosition2, &lt;span style="color:#3333ff;"&gt;null&lt;/span&gt;,&lt;span style="color:#66cccc;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#66cccc;"&gt;Color&lt;/span&gt;.White, 0, &lt;span style="color:#3333ff;"&gt;new&lt;/span&gt; &lt;span style="color:#66cccc;"&gt;Vector2&lt;/span&gt;(myTexture2.Width / 2,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;myTexture2.Height / 2), 1, &lt;strong&gt;&lt;span style="color:#66cccc;"&gt;SpriteEffects&lt;/span&gt;.FlipHorizontally&lt;/strong&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;0); &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;else&lt;/span&gt;&lt;br /&gt;{ &lt;span style="color:#66cccc;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;mySpriteBatch.Draw(myTexture2, spritePosition2, &lt;span style="color:#3333ff;"&gt;null&lt;/span&gt;, &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#66cccc;"&gt;Color&lt;/span&gt;.White, 0, &lt;span style="color:#3333ff;"&gt;new&lt;/span&gt; &lt;span style="color:#66cccc;"&gt;Vector2&lt;/span&gt;(myTexture2.Width / 2, &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;myTexture2.Height / 2), 1, &lt;span style="color:#66cccc;"&gt;SpriteEffects&lt;/span&gt;.None, 0);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This code flips the sprite horizontally when &lt;em&gt;sprite2Flipped&lt;/em&gt; equals true, and doesn't when &lt;em&gt;sprite2Flipped&lt;/em&gt; is false. Now you can run the project. You should be able to click the right mouse button and the sprite should flip horizontally.&lt;br /&gt;&lt;br /&gt;Now you know how to track keyboard key presses and mouse button clicks.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RuSOy-AIr_I/AAAAAAAAAH0/9uUHXk-Pu2E/s1600-h/ScreenShot4.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5108364883633156082" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RuSOy-AIr_I/AAAAAAAAAH0/9uUHXk-Pu2E/s400/ScreenShot4.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-4 The Sprite When it is Flipped&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;If you have any requests or questions, please post a comment and I will try to answer. Thanks for reading the tutorial.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Remember:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;You don't get to Heaven by what you do, but by what you believe.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-3527298162207417293?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/3527298162207417293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=3527298162207417293' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/3527298162207417293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/3527298162207417293'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/09/handling-keyboard-key-presses-and-mouse.html' title='Handling Keyboard Key Presses and Mouse Clicks'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_kLtxSdyLIgE/RuSOyuAIr8I/AAAAAAAAAHc/AOG3NSMy7us/s72-c/ScreenShot1.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-6348643764146763941</id><published>2007-09-08T16:56:00.000-07:00</published><updated>2007-09-08T17:44:28.300-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='2D Tutorials'/><title type='text'>Tutorial 7 -- Advanced Sprite Drawing</title><content type='html'>&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;This tutorial talks about the different parameters used in the different method overloads of spriteBatchVariable.Draw. This tutorial also talks about what method overloads are.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Requirements&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;A sprite drawn on the screen.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Read &lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Read the tutorial here: &lt;a href="http://christiangameprogramming.blogspot.com/2007/09/advanced-sprite-drawing.html"&gt;Tutorial 7&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Remember&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Be a hero: Don't conform; Be selfless; Make a difference&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-6348643764146763941?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/6348643764146763941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=6348643764146763941' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/6348643764146763941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/6348643764146763941'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/09/tutorial-7-advanced-sprite-drawing.html' title='Tutorial 7 -- Advanced Sprite Drawing'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2903756005659412185.post-5546095634117481597</id><published>2007-09-07T17:18:00.000-07:00</published><updated>2007-09-08T17:06:17.428-07:00</updated><title type='text'>Advanced Sprite Drawing</title><content type='html'>&lt;div align="left"&gt;In this tutorial, I will talk about how you can draw a sprite with more effects applied to the sprite than just a color to taint the sprite with.&lt;br /&gt;&lt;br /&gt;What we need to do is call the draw method of the our SpriteBatch variable, but just fill in a few more parameters.&lt;br /&gt;&lt;br /&gt;You may ask, "&lt;em&gt;But how can a method have different amounts of parameters and still be the same method?&lt;/em&gt;". This is possible with a thing called overloading. If you overload a method, you create a method with the same name as the method you are overloading, but you create the method so that it takes a different amount of parameters. Method overloading is useful for making it easier for the user to use the method.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Example:&lt;/strong&gt; Pretend you have a method that explodes a ship called &lt;em&gt;Explode&lt;/em&gt;. This method needs a Ship class as a parameter so that it can explode that ship and an intensity for the ship to explode at.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#009900;"&gt;//This is the original method&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;private&lt;/span&gt; &lt;span style="color:#3333ff;"&gt;void&lt;/span&gt; Explode(&lt;span style="color:#66cccc;"&gt;Ship&lt;/span&gt; shipVariable, &lt;span style="color:#3333ff;"&gt;int&lt;/span&gt; explosionIntensity)&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:#009900;"&gt;//Code that explodes the ship.&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;For your game, 99/100 explosions occur with a 100/1000 intensity. Method overloading comes in here. For that 1 ship that needs a 200/1000 intensity explosion, you have the orginal &lt;em&gt;Explode&lt;/em&gt; method which allows you to pass in the ship to explode, and the intensity of the explosion. But now you want an easier way to explode you 99 ships that all have a 100/1000 intensity explosion. You can create an overload of the &lt;em&gt;Explode&lt;/em&gt; method that only takes one parameter, the ship to explode, and then passes in 100 as a default value for the intensity:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#009900;"&gt;//Overload of original Explode method&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;private&lt;/span&gt; &lt;span style="color:#3333ff;"&gt;void&lt;/span&gt; Explode(&lt;span style="color:#66cccc;"&gt;Ship&lt;/span&gt; shipVariable)&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:#009900;"&gt;//Call the other Explode method and pass in the &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#009900;"&gt;//shipVariable it got as a parameter, and then 100 as the&lt;br /&gt;//&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#009900;"&gt;default explosion intensity&lt;br /&gt;&lt;/span&gt;Explode(shipVariable, 100);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;You can see how method overloading makes it easier for the user to use the method because now, since 99/100 of your ships use the default explosion intensity which is 100/1000, instead of always calling &lt;em&gt;Explode(shipVariable, 100);&lt;/em&gt;, you can just call &lt;em&gt;Explode(shipVariable);&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;I hope that made sense to you but if you don't get it you may get it more along the tutorial or sometime later.&lt;br /&gt;&lt;br /&gt;Now, since I explained all that to you, I am going to show you where it is actually used.&lt;br /&gt;&lt;br /&gt;In the &lt;em&gt;mySpriteBatch.Draw&lt;/em&gt; call we passed in three parameters, a Texture2D to draw, a Vector2 position, and a Color to taint the sprite with. This was good for our purpose because we didn't really want to change anything on the sprite. Well, now we do want to change the attributes of the sprite. The &lt;em&gt;Draw&lt;/em&gt; method of the SpriteBatch class has several overloads. In fact, you can see all the overloads by typing or re-typing the first parenthesis of the &lt;em&gt;Draw&lt;/em&gt; method call. A tan box should appear(we used this in the second tutorial), but this time we will use this to find how many overloads the method has. In the top right corner you can see &lt;em&gt;1 of 7&lt;/em&gt;. This shows that you are on method overload one out of seven different method overloads. If you push the up or down arrow keys you can see what parameters are needed for this method overload. If you haven't found for yourself by now, we are currently using method overload two. This takes a Texture2D, a Vector2, and a Color.&lt;br /&gt;&lt;/div&gt;&lt;a href="http://bp0.blogger.com/_kLtxSdyLIgE/RuM0y-AIr3I/AAAAAAAAAG0/6Od_kDjBgpo/s1600-h/ScreenShot1.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5107984452609945458" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp0.blogger.com/_kLtxSdyLIgE/RuM0y-AIr3I/AAAAAAAAAG0/6Od_kDjBgpo/s400/ScreenShot1.jpg" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;strong&gt;Figure 1-1 The Tan Box Showing the Current Method Overload and Parameters Needed&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;For what we want to do we will use overload number six which is one of the overloads that allows the most options to change the way the sprite is drawn. I will explain what all of the overloads we are using mean, and then I will get along with the code.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Texture2D texture&lt;/strong&gt;&lt;br /&gt;This parameter is asking for the texture to draw on the screen.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; There is no default, you have to give the texture in all overloads.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Vector2 position&lt;/strong&gt;&lt;br /&gt;This parameter is asking for the position to draw the sprite on the screen at.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; There is no default, you have to give the position in all overloads. In some cases you may use a rectangle that holds the position instead though.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Rectangle sourceRectangle&lt;/strong&gt;&lt;br /&gt;This parameter is asking for the rectangle out of the texture you want to draw. For example. pretend you have a texture that has all the button images in the game on it. If you wanted to draw only one part of that image, you would specify the size and position for that rectangle to be at on the texture. If you want to use the whole image, you can pass in null instead which means nothing so the whole image whill be drawn.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; &lt;em&gt;null&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;&lt;strong&gt;Color color&lt;br /&gt;&lt;/strong&gt;This parameter specifies what color to taint the sprite with, i.e. orange maybe if the sprite is the player and blue if the sprite is any enemy.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; There is no default, you have to give the color in all overloads. If you don't want any color to be applied to your sprite though, then just use &lt;em&gt;Color.White&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;float rotation&lt;br /&gt;&lt;/strong&gt;This parameter specifies how much you want the sprite to be rotated by.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; 0.0f&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Vector2 origin&lt;/strong&gt;&lt;br /&gt;This parameter specifies the main point for the sprite. If the origin is the center of the sprite, when you draw the sprite, the sprite will be drawn around the center point which means the position you specify for the sprite to be drawn at will refer to the center of the sprite.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; 0, 0 (the top left corner)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;float scale &lt;/strong&gt;&lt;br /&gt;This parameter sets how much you want the sprite to be scaled by.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; 1 (if you put 0 the sprite will end up as small as nothing because you are multiplying your sprites original size by zero which always gives zero.)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;SpriteEffects effects&lt;/strong&gt;&lt;br /&gt;This parameter specifies which type of sprite effects you want applied to your image. You can flip horizontally, vertically, or just do nothing.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; &lt;em&gt;SpriteEffects.None&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;float layerDepth&lt;/strong&gt;&lt;br /&gt;This parameter specifies the depth position of your sprite. This is useful for making certain sprites appear on top of eachother.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Default value:&lt;/strong&gt; It depends on which order your sprite is drawn at out of all the other sprites. Sometimes the last sprite drawn appears on top of the others and sometimes the sprite drawn first appears on top. It depends on which sort mode you pass into the second or third overload of &lt;em&gt;spriteBatchVariable.Begin&lt;/em&gt;. We are not using the second or third overload so the default value is &lt;em&gt;SpriteSortMode.BackToFront&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;That was the last parameter for the overload of &lt;em&gt;Draw&lt;/em&gt; that we are using. Now we can get to some code. You can mostly test the different parameters on your own, I don't have to explain what they do because you can probably figure out, but, I will show you a few things.&lt;br /&gt;&lt;br /&gt;First run your project. Now click the mouse and the sprite should move to that position. If you look closely though, you will see that the top left corner of the sprite is going to the position where you clicked. That is because we are not specifying an origin for the sprite. If we want to change where the sprite goes to, you must change the origin for that sprite.&lt;br /&gt;&lt;br /&gt;Change the call to draw the second texture to look like this:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;mySpriteBatch.Draw(myTexture2, spritePosition2, &lt;span style="color:#3333ff;"&gt;null&lt;/span&gt;, &lt;span style="color:#66cccc;"&gt;Color&lt;/span&gt;.White, 0, &lt;span style="color:#66cccc;"&gt;Vector2&lt;/span&gt;.Zero, 1, &lt;span style="color:#66cccc;"&gt;SpriteEffects&lt;/span&gt;.None, 0); &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This code draws the exact same thing we had before. All the parameter values we have now are the default values they would been. This is not so useful to us now, until we want to change anything.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; In the code above, we used &lt;em&gt;Vector2.Zero&lt;/em&gt; which is accesssing a value that the Vector2 class has in it. This is the same as using &lt;em&gt;new Vector2(0, 0);&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Now we can actually do something. Since our origin is still 0, 0, the sprite will still be drawn with the top-left corner of the sprite drawn at &lt;em&gt;spritePosition2&lt;/em&gt;. Instead of using &lt;em&gt;Vector2.Zero&lt;/em&gt; for our origin parameter, we will use this: &lt;em&gt;new Vector2(myTexture2.Width / 2, myTexture2.Height / 2);&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;This gives our sprite an origin that refers to the center of the sprite.&lt;br /&gt;&lt;br /&gt;Now run the project. If you press the left mouse button, the sprite will be moved to where you clicked but the center of the sprite will be moved to the position of the mouse.&lt;br /&gt;&lt;br /&gt;For the next test, I will show you how to use the source rectangle. For this rectangle, you are specifying what part of the texture you want to be drawn. For an example, just put this line instead of &lt;em&gt;null&lt;/em&gt;: &lt;em&gt;new Rectangle(0, 0, 50, 50);&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;This is creating a rectangle that starts from the top left corner of your texture, and has a width and height of 50 pixels. Run the project now. You should see only a 50 pixel square of the texture.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_kLtxSdyLIgE/RuM0y-AIr4I/AAAAAAAAAG8/GQ1JS7VNX40/s1600-h/ScreenShot2.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5107984452609945474" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp0.blogger.com/_kLtxSdyLIgE/RuM0y-AIr4I/AAAAAAAAAG8/GQ1JS7VNX40/s400/ScreenShot2.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-2 The Source Rectangle Example&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;That was just an example, so change the source rectangle parameter back to &lt;em&gt;null&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;For the last example, I will show you how to rotate the sprite correctly. You may think, "Well all I have to do is type in a number and the sprite will rotate by that many degrees."&lt;br /&gt;&lt;br /&gt;This is incorrect thinking. When you type in a number, the sprite is not rotated in degrees. Type in, 45, for the rotation value. You should see that the sprite is not rotated by 45 degrees, but in radians. Now type in this instead: &lt;em&gt;MathHelper.ToRadians(45);&lt;/em&gt; This draws your sprite at a rotation relative to the number you put in. Now run the project. You should see the sprite is rotated exactly by 45 degrees.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you put in, 45, as a parameter for the &lt;em&gt;ToRadians&lt;/em&gt; method, you are not actually drawing the sprite in degrees. Whats happening is, you are converting a value that is in degrees, into a value in radians. You can also use &lt;em&gt;MathHelper.ToDegrees(MathHelper.ToRadians(45))&lt;/em&gt; which converts a radian value into a degree again.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RuM0zOAIr5I/AAAAAAAAAHE/NUcBzA50WPg/s1600-h/ScreenShot3.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5107984456904912786" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RuM0zOAIr5I/AAAAAAAAAHE/NUcBzA50WPg/s400/ScreenShot3.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-3 The Correctly Rotated Sprite&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Again, erase the value for rotation and put 0&lt;em&gt;.&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Now you know a little more about how sprites are drawn. You also know about method overloading.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RuM0zOAIr6I/AAAAAAAAAHM/vn96t-l0qy4/s1600-h/ScreenShot4.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5107984456904912802" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RuM0zOAIr6I/AAAAAAAAAHM/vn96t-l0qy4/s400/ScreenShot4.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-4 The Code That Draws the Second Texture&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp1.blogger.com/_kLtxSdyLIgE/RuM0zOAIr7I/AAAAAAAAAHU/D0wjuzjCmpM/s1600-h/ScreenShot5.jpg"&gt;&lt;img id="BLOGGER_PHOTO_ID_5107984456904912818" style="DISPLAY: block; MARGIN: 0px auto 10px; CURSOR: hand; TEXT-ALIGN: center" alt="" src="http://bp1.blogger.com/_kLtxSdyLIgE/RuM0zOAIr7I/AAAAAAAAAHU/D0wjuzjCmpM/s400/ScreenShot5.jpg" border="0" /&gt;&lt;/a&gt;&lt;strong&gt;Figure 1-5 The Complete Tutorial. Nothing Much Different That You Can See&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you want you can change the values of other parameters to see what they do. Make sure to change everything back to the way it is now though, when you are done testing.&lt;br /&gt;&lt;br /&gt;If you have any questions or requests, please post a comment and I will try to answer.&lt;br /&gt;&lt;br /&gt;Thanks for reading the tutorial.&lt;br /&gt;&lt;br /&gt;Be a hero: Don't conform; Be selfless; Make a difference&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2903756005659412185-5546095634117481597?l=christiangameprogramming.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://christiangameprogramming.blogspot.com/feeds/5546095634117481597/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=2903756005659412185&amp;postID=5546095634117481597' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5546095634117481597'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2903756005659412185/posts/default/5546095634117481597'/><link rel='alternate' type='text/html' href='http://christiangameprogramming.blogspot.com/2007/09/advanced-sprite-drawing.html' title='Advanced Sprite Drawing'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11293668203909748441</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='06561165522296809319'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_kLtxSdyLIgE/RuM0y-AIr3I/AAAAAAAAAG0/6Od_kDjBgpo/s72-c/ScreenShot1.jpg' height='72' width='72'/><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry></feed>