6 Replies - 1568 Views - Last Post: 08 June 2012 - 08:28 AM

#1 nickkorta   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-June 12

Draw sprite explaination

Posted 06 June 2012 - 04:11 PM

namespace WindowsGame1
{
 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

     
        [color="#FFFF00"]
Texture2D paddle;//Says that we are using this texture in the game
        Vector2 paddleposition = Vector2.Zero;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

       
        protected override void Initialize()
        {
            

            base.Initialize();
        }

      
        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            paddle = Content.Load < Texture2D >("paddle");

          
        }

     
        protected override void UnloadContent()
        {
            
        }

   
        protected override void Update(GameTime gameTime)
        {
          
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            
          

            base.Update(gameTime);
        }

      
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
            spriteBatch.Draw(paddle,paddleposition,Color.White);
            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}



Sorry It posted while I was asking the question.

Can someone explain this part of the code
spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
 spriteBatch.Draw(paddle,paddleposition,Color.White);
 spriteBatch.End();


This post has been edited by Kilorn: 06 June 2012 - 06:44 PM
Reason for edit:: Fixed your code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Draw sprite explaination

#2 Kilorn   User is offline

  • XNArchitect
  • member icon



Reputation: 1361
  • View blog
  • Posts: 3,534
  • Joined: 03-May 10

Re: Draw sprite explaination

Posted 06 June 2012 - 07:18 PM

Fair warning: Wall of text incoming. (I'm channeling my inner BBeck here.)

The first line of that snippet of code calls the Begin method of the SpriteBatch class, with a few parameters. The parameters in Begin method calls are optional, and in this case the SpriteSortMode and BlendState parameters are used.

The SpriteSortMode enumeration is just a way to control the way the SpriteBatch object arranges sprites. BackToFront is one of the modes that can be set, this particular mode will organize the sprites by depth before drawing. This is great when you've got some sprites with transparency that needs to be on top of other sprites that do not have any transparency. For instance if you're drawing two tiles in the same 32x32 pixel space on the screen, and one of them has some partially transparent pixels such as a shadow that needs to sit on top of the other tile texture.

BlendState is the second parameter being passed here. BlendState is a class in the Graphics Namespace of the XNA Framework which handles blending together sprites and textures that are occupying the same screen space at any given time. The default for this class when passed to a SpriteBatch object is AlphaBlend. In the case of your code, the person who wrote it used the default. You might be asking why it's even there if it is the default, and the only reason it is there is because there isn't a constructor for the Begin method where the only parameter is the SpriteSortMode and the person who wrote this code wanted to be able to change the default setting for SpriteSortMode. The default SpriteSortMode is SpriteSortMode.Deferred, which draws the objects in the order in which the Draw calls were processed by the compiler. There will be no sorting.

That covers the Begin method call. The next line is the actual Draw call. The Draw method chosen in this case requires 3 parameters. The first parameter is a texture object which you will define and load elsewhere in the code.

The second parameter is a Vector2 which is used to dictate the position on the screen in which the texture object was drawn. This Vector2 is the top left corner of the object that is being drawn and the texture will be drawn from that point, and expand to the right and down based on the size, in pixels, of the texture object.

The third parameter that is passed to the Draw call here is a Color object. In this case, it is Color.White. This color can be a little difficult to understand at first. This is basically the color of the object that you are drawing. If your image has multiple colors in it, you're going to want to keep this at Color.White. If you choose to go with a different color, it will not change the default color values of the pixels in the image, but it will tint the colors of the image when drawn to the screen. Let's say you have an image of a solid yellow square. If you use Color.Red, the square will be tinted with a red hue, which will result in the square being orange when drawn to the screen. If you go with the standard Color.White, the image will be yellow when drawn to the screen just as it is supposed to be. This color can be useful if you suddenly have a need to tint an image at draw time, but you will most likely use Color.White the majority of the time.

Each call to SpriteBatch.Draw, no matter which Draw method you use, will add an object to the SpriteBatch object that will be drawn to the screen when the End method is called. The final line in this piece of code is the SpriteBatch.End call. This closes the current SpriteBatch object and draws all of the textures to the screen that were added using the Draw call(s) using the parameters that were passed into the SpriteBatch.Begin call.

So to recap, you Begin the SpriteBatch and pass in the parameters you need, then you use the Draw call to add all of the textures that you want to draw to the SpriteBatch object and apply the settings to those textures based on the Begin parameters. Finally, you call the End method to draw all of these sprites, including the changes made by the Begin parameters, to the screen and close the SpriteBatch object.

Hopefully this long explanation will shed some light on it for you and help you understand what exactly is going on here. Feel free to ask more questions if I wasn't clear enough on anything.

This post has been edited by Kilorn: 06 June 2012 - 07:19 PM

Was This Post Helpful? 2
  • +
  • -

#3 BBeck   User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: Draw sprite explaination

Posted 07 June 2012 - 04:39 AM

View PostKilorn, on 06 June 2012 - 07:18 PM, said:

Fair warning: Wall of text incoming. (I'm channeling my inner BBeck here.)



ROFL. It happens sometimes.
Was This Post Helpful? 0
  • +
  • -

#4 DanielLeone   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: Draw sprite explaination

Posted 07 June 2012 - 06:55 AM

View PostBBeck, on 07 June 2012 - 07:39 PM, said:

View PostKilorn, on 06 June 2012 - 07:18 PM, said:

Fair warning: Wall of text incoming. (I'm channeling my inner BBeck here.)



ROFL. It happens sometimes.


Hahahahaha!
Couldn't agree more :D
Was This Post Helpful? 0
  • +
  • -

#5 nickkorta   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-June 12

Re: Draw sprite explaination

Posted 07 June 2012 - 10:29 AM

Thanks Kilron. I have two more questions.

The first question is, if I read your repy correctly you can have more then one spriteBatch.Draw between the spriteBatch.Start and spriteBatch.End.

My second question is how should I go about learning this XNA. I Just came from Game Maker and I used only code and not any of the Drag and drop. Also before even starting XNA I learned the basics of C#(Very similar to game maker lanquage) :bigsmile:
My big goal is to make a 3d 3rd persion shooter with flying ships as veichles.
I know I wont get to that project for a while.
I plan on doing 2d games first. If you have any website I can go to, were there are tutorials(been loking but can't find none), perferably VIDEO TUTORIALS, that whould be great.

Thank you and your long text was not to bad I understood it all mostly.
Was This Post Helpful? 0
  • +
  • -

#6 DanielLeone   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: Draw sprite explaination

Posted 08 June 2012 - 03:14 AM

Okay, you're right about the first question.

You have to think of the spriteBatch as it says, you begin it, draw as many things as you want, end it.

Think of it as an Xbox :).

You turn on the TV (spriteBatch.Begin)

You play a game (spriteBatch.Draw)
You play a game (spriteBatch.Draw)
You play a game (spriteBatch.Draw)
You play a game (spriteBatch.Draw)

You turn off the TV (spriteBatch.End)

;) What an analogy!


So yeah, you really have to understand how all the Drawing the spriteBatch takes place.
Remember, you can only draw one texture, and one rectangle in a spriteBatch.Draw() and obviously in a game there is going to be more than one sprite!

But as Kilorn said, you call spriteBatch.Begin, all the spriteBatch.Draws and then the SpriteBatch.End. Only once the spriteBatch.End has been called will the sprites actually be drawn.

As for learning XNA, I have about 15GB in video tutorials :D. I downloaded them all of YouTube, and then I can refer to them later.

To Download then, if your using Google Chrome, install this extension, or if you're not, you can paste the URLs here(takes a lot longer)

As for the actual videos, I would recommend these channels.
http://www.youtube.c...ser/Whiplashx86
http://www.youtube.c...ser/SaladRaider
http://www.youtube.com/user/oyyou91

I'm sure you know how to use YouTube, but they all have very good playlists.

Hope that helped,
Daniel,

This post has been edited by DanielLeone: 08 June 2012 - 03:20 AM

Was This Post Helpful? 0
  • +
  • -

#7 nickkorta   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 06-June 12

Re: Draw sprite explaination

Posted 08 June 2012 - 08:28 AM

Thanks, I understand this comepltly now.
Thanks for the links.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1