XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Slicing a Texture2D

Last post 11/19/2009 11:51 PM by Nick Gravelyn. 5 replies.
  • 11/19/2009 9:34 PM

    Slicing a Texture2D

    Hi,
    this is my problem: I have a Texture2D object with multiples sprites ( a spritesheet ), i would like to pick out a region of the such Texture2D object and generate another Texture2D object with only the region that i need. How i do it?
  • 11/19/2009 9:45 PM In reply to

    Re: Slicing a Texture2D

    This is actually fairly easy. All you have to do is create a rectangle specifying the region you want to draw, and in SpriteBatch.Draw, enter it as the SourceRectangle.
    ____________________________________________________________________________________ The question is simple, but the answer is not.
  • 11/19/2009 10:08 PM In reply to

    Re: Slicing a Texture2D

    wowy7:
    This is actually fairly easy. All you have to do is create a rectangle specifying the region you want to draw, and in SpriteBatch.Draw, enter it as the SourceRectangle.


    Ok, i know. But i don't want to call SpriteBatch.Draw so early. I will perform some manipulations in the texture2D object before show it
  • 11/19/2009 11:14 PM In reply to

    Re: Slicing a Texture2D

    Well I am not sure how one would do this. May I ask why you can't just use a SourceRectangle? I see no disadvantages to it.
    ____________________________________________________________________________________ The question is simple, but the answer is not.
  • 11/19/2009 11:46 PM In reply to

    Re: Slicing a Texture2D

    Hi LucasSimao,

    I think the sourceRectangle the Draw method can solve your problem.
    You do not need to cut the texture to display there.


    spriteBatchInstance.Draw(Texture2D, RectangleDestination, RectangleSource, Color);

    Where Texture2D is your texture.
    RectangleDestination is where the texture will be drawn;
    RectangleSource is the rectangle part of the texture2D that will be drawn.
  • 11/19/2009 11:51 PM In reply to

    Re: Slicing a Texture2D

    As said above the source rectangle solves your problem.

    Basically when it comes down to it, you don't want to make a new Texture2D for each sprite. That's, in fact, the entire reason for using sprite sheets: to keep them on one texture. When you go to use that texture, you can specify an area inside of it to use when drawing as opposed to drawing the whole thing. By doing this you keep one texture in memory and can draw any sprite from within it.
Page 1 of 1 (6 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG