Figure 2-3. Adding an image to the game project in Font

Paint PDF417 in Font Figure 2-3. Adding an image to the game project

Figure 2-3. Adding an image to the game project
PDF 417 Encoder In None
Using Barcode drawer for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
Generating Barcode In None
Using Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
CH APT ER 2 2D GRA PH I CS , AUD I O, AND I NPUT BAS IC S
Encoding QR Code ISO/IEC18004 In None
Using Barcode printer for Font Control to generate, create QR Code image in Font applications.
www.OnBarcode.com
Generating PDF 417 In None
Using Barcode printer for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
After including the image in the game solution, select the image name in the Solution Explorer window and press F4. This brings up (if it s not already visible) the Properties window for the recently included image, as shown in Figure 2-4.
EAN / UCC - 13 Encoder In None
Using Barcode generator for Font Control to generate, create EAN / UCC - 14 image in Font applications.
www.OnBarcode.com
Print ANSI/AIM Code 39 In None
Using Barcode creation for Font Control to generate, create Code 39 image in Font applications.
www.OnBarcode.com
Figure 2-4. The image properties The Properties window presents information such as the content importer and the content processor used for this content (also called asset), which were introduced in the previous chapter The Asset Name property defines how your code will refer to this content.
Code128 Printer In None
Using Barcode generator for Font Control to generate, create Code-128 image in Font applications.
www.OnBarcode.com
Make ISBN In None
Using Barcode encoder for Font Control to generate, create ISBN - 10 image in Font applications.
www.OnBarcode.com
Drawing the Sprite on the Screen
PDF417 Scanner In .NET Framework
Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Creating PDF417 In None
Using Barcode printer for Software Control to generate, create PDF417 image in Software applications.
www.OnBarcode.com
Once you have an image, the next step is including the code for drawing it on the screen. To do this, you ll need a SpriteBatch (an XNA class that draws sprites on the screen) and the texture that will be used as the sprite image (in this case, you ll load this texture into your clsSprite class). Usually, there is more than one way to code a particular task. In this case, you can read the texture from the clsSprite class and draw it in the Draw method of the Game1 class, or you can extend your clsSprite class to create a Draw method that will draw the sprite. Let s go with the former option, by including this new method in the clsSprite class: public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, position, Color.White); } The Draw method has many overloads, which allow you to draw only part of the original texture, to scale or rotate the image, and so on. Here, you are using the simplest one, which receives only three arguments: the texture to draw, the position in screen coordinates (both are already properties of clsSprite class), and a color channel modulation used to tint the image.
EAN / UCC - 13 Generator In None
Using Barcode generation for Microsoft Excel Control to generate, create GTIN - 128 image in Microsoft Excel applications.
www.OnBarcode.com
Encode Barcode In VS .NET
Using Barcode drawer for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
C HA PTER 2 2 D GR APH IC S, A UD IO , A ND IN PUT BA SI CS
Print Code 39 Full ASCII In Java
Using Barcode maker for BIRT reports Control to generate, create Code-39 image in Eclipse BIRT applications.
www.OnBarcode.com
QR Code Creator In .NET
Using Barcode printer for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
www.OnBarcode.com
Using any color other than white in this last parameter draws the image with a composition of its original colors and color tone.
Generate Data Matrix 2d Barcode In None
Using Barcode generator for Software Control to generate, create Data Matrix ECC200 image in Software applications.
www.OnBarcode.com
Recognizing Data Matrix In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Note For information about the other Draw method overloads, see the MSDN documentation for the
UPC-A Supplement 5 Printer In .NET
Using Barcode generator for Reporting Service Control to generate, create UPC-A image in Reporting Service applications.
www.OnBarcode.com
PDF-417 2d Barcode Decoder In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
XNA 3.0 SpriteBatch.Draw method (http://msdn.microsoft.com/en-us/library/microsoft. xna.framework.graphics.spritebatch.draw.aspx). For example, If you want to rotate your image, look for the overloads that expect the rotation parameter, or use the SpriteEffects parameter, if you just want to flip the sprite horizontally or vertically. Overloads with a scale parameter allow you to change the size of the sprite, which can be used in many ways, such as to create a zoom effect.
Draw ECC200 In None
Using Barcode printer for Word Control to generate, create DataMatrix image in Office Word applications.
www.OnBarcode.com
QR Creator In None
Using Barcode encoder for Software Control to generate, create Denso QR Bar Code image in Software applications.
www.OnBarcode.com
Now let s adjust the Game1 class. A new Windows Game project already creates a SpriteBatch object for you, so you ll start by creating a clsSprite object in the Game1 class. Include this definition at the beginning of the class, just after the device and SpriteBatch objects that were automatically created for you. You ll see something like the next code fragment: public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; // The device SpriteBatch spriteBatch; // The sprite renderer clsSprite mySprite1; // My sprite class
Obviously, you need to create these objects with valid values before using them. You do so in the LoadContent method, which is where you include graphics initialization (as discussed in the previous chapter). Because the project already creates the SpriteBatch object, all you need to do is create the clsSprite object: protected override void LoadContent() { // Load a 2D texture sprite mySprite1 = new clsSprite(Content.Load<Texture2D>("ball"), new Vector2(0f, 0f), new Vector2(64f, 64f)); // Create a new SpriteBatch, which can be used to draw textures spriteBatch = new SpriteBatch(GraphicsDevice); }
Note The previous code sample uses Vector2(0f, 0f) to define a zeroed 2D vector, but you could use
the Vector2.Zero static property as well. The XNA Framework offers such properties to improve the code s readability.
CH APT ER 2 2D GRA PH I CS , AUD I O, AND I NPUT BAS IC S
Even though you included a single code line (for creating the mySprite1 object), a lot of things are going on. You created your sprite class by using the content manager to load the Texture2D based on the image asset name, ball. You also defined the sprite position as (0, 0) and decided on the sprite size: 64 pixels wide and 64 pixels tall. For the SpriteBatch s creation, you re passing the graphics device as a parameter. In the previous chapter, we mentioned that the device (represented here by the GraphicsDevice variable) is your entry point to the graphics handling layer, and through it you do any graphical operations. Here, you are informing the SpriteBatch which device it should use when drawing the sprites. In the next section, you ll see how to use the device to change the program s window size. It s always a good programming practice to destroy everything you created when the program ends. To do this, you need to dispose of the texture of clsSprite you created in the LoadContent method. As you probably guessed, you do this in the UnloadContent method. The code for disposing of the object follows: protected override void UnloadContent() { // Free the previously allocated resources mySprite1.texture.Dispose(); }
Note You could also create a Dispose method in the clsSprite class to dispose of the texture, and call
it from the UnloadContent method. This would be a more object-oriented code practice. It s up to you to choose the code practice you think is best.
Finally, you need to include code to draw the sprite using the SpriteBatch object you created. You use the SpriteBatch, as its name suggests, to draw a batch of sprites, grouping one or more calls to its Draw method inside a block started by a call to the Begin method and closed by a call to the End method, as follows: protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); mySprite1.Draw(spriteBatch); spriteBatch.End(); base.Draw(gameTime); } The Begin method can also receive parameters that will be used when rendering every sprite in the block. For instance, if the texture has transparency information, you can tell the SpriteBatch to take this into account when drawing, by changing the Begin code line to the following: spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
Copyright © OnBarcode.com . All rights reserved.