Difference between revisions of "Drawing Textures"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Declare a Texture= =Load a Texture= =Draw a Texture=")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
=Preparation=
 +
You need either create or find your image, `.PNG` are best because they have alpha transparency. Once you have your image you will need to build it using the content pipeline into an `.xnb` file.
 +
 +
In order to load a texture it must be in the correct place in your `bin` folder. Find the `Content` inside the `bin` folder and put your `.xnb` files there.
 +
 
=Declare a Texture=
 
=Declare a Texture=
 +
One of the built in MonoGame data types is Texture2D, this can be used to store a graphic or image. You will also need to declare a Vector2 to store the on screen position. Declare these within your Game1.cs:
 +
 +
<syntaxhighlight lang=csharp>
 +
// Animation representing the player
 +
Texture2D PlayerTexture;
 +
 +
// Position of the Player relative to the upper left side of the screen
 +
Vector2 Position;
 +
</syntaxhighlight>
  
 
=Load a Texture=
 
=Load a Texture=
 +
Within the LoadContent method we need to create the object for texture and load in actual texture:
 +
 +
<syntaxhighlight lang=c#>
 +
PlayerTexture = Content.Load<Texture2D>("hero");
 +
</syntaxhighlight>
 +
 +
In order to load the texture (ie hero) you will need to place your image in the content pipeline, and build the pipeline. Adding an image called "hero.png" will allow you to load "hero" in the Content.Load command. Check [[Generating and using XNB files]].
 +
 +
<syntaxhighlight lang=c#>
 +
Position = Vector2.Zero;
 +
</syntaxhighlight>
 +
 +
You also need to set Position, this will set it to a vector of 0,0.
  
 
=Draw a Texture=
 
=Draw a Texture=
 +
Now the Initialize has setup the basic setting, we can use these to draw to the screen. Make the following alterations:
 +
 +
<syntaxhighlight lang=csharp>
 +
public void Draw(SpriteBatch spriteBatch)
 +
{
 +
    spriteBatch.Begin()
 +
    spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
 +
      SpriteEffects.None, 0f);
 +
    spriteBatch.End();
 +
}
 +
</syntaxhighlight>

Latest revision as of 14:33, 22 September 2024

Preparation

You need either create or find your image, `.PNG` are best because they have alpha transparency. Once you have your image you will need to build it using the content pipeline into an `.xnb` file.

In order to load a texture it must be in the correct place in your `bin` folder. Find the `Content` inside the `bin` folder and put your `.xnb` files there.

Declare a Texture

One of the built in MonoGame data types is Texture2D, this can be used to store a graphic or image. You will also need to declare a Vector2 to store the on screen position. Declare these within your Game1.cs:

// Animation representing the player
Texture2D PlayerTexture;

// Position of the Player relative to the upper left side of the screen
Vector2 Position;

Load a Texture

Within the LoadContent method we need to create the object for texture and load in actual texture:

PlayerTexture = Content.Load<Texture2D>("hero");

In order to load the texture (ie hero) you will need to place your image in the content pipeline, and build the pipeline. Adding an image called "hero.png" will allow you to load "hero" in the Content.Load command. Check Generating and using XNB files.

Position = Vector2.Zero;

You also need to set Position, this will set it to a vector of 0,0.

Draw a Texture

Now the Initialize has setup the basic setting, we can use these to draw to the screen. Make the following alterations:

public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin() 
    spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
       SpriteEffects.None, 0f);
    spriteBatch.End();
}