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

Texture 2D Block Tranfer. Exists?

Last post 11/21/2009 1:50 AM by Craig Martin. 6 replies.
  • 10/16/2009 10:36 PM

    Texture 2D Block Tranfer. Exists?

    Well, let's supose i have a spritesheet like this:



    thanks for that example bonzai. Well, i won't use that one, is just a example. Well, i have this spritesheet imported in XNA and loaded as a Texture 2D. But i want to get only a part of that image, so that i can do a animation. I have the x and y coordinates and the width and height of the part that i want. The thing is, can i create another texture 2D with that part that i want? Like a block transfer. I give the Texture, a rectangle of the part that i want, and it returs me a new Texture containing the part that i trasfered. Thanks for the help!
  • 10/16/2009 10:43 PM In reply to

    Re: Texture 2D Block Tranfer. Exists?

    If you want the block for drawing, you can use the SourceRect param on the SpriteBatch.Draw overloads and don't need to copy each part out into a separate texture.

    Game hobbyist hell-bent on coding a diabolical Matrix
  • 10/16/2009 10:45 PM In reply to

    Re: Texture 2D Block Tranfer. Exists?

    Intresting! That can be usefull! Thanks for the help!

    Still opened for new and easier ideas (If there is a easier way)
  • 10/16/2009 10:57 PM In reply to

    Re: Texture 2D Block Tranfer. Exists?

    You could make an extension method like so:

            public static Texture2D Transfer<T>(this Texture2D srcTexture, Rectangle srcRect) where T : struct 
            { 
                T[] buffer = new T[srcRect.Width * srcRect.Height]; 
                srcTexture.GetData<T>(0, srcRect, buffer, 0, buffer.Length); 
                Texture2D result = new Texture2D(srcTexture.GraphicsDevice, srcRect.Width, srcRect.Height); 
                result.SetData<T>(buffer); 
                return result;           
            } 
     


    Note: I just typed this, it's not tested.

    Edit: Added the where constraint.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 10/17/2009 12:36 AM In reply to

    Re: Texture 2D Block Tranfer. Exists?

    I already solved with the first method. Thanks!
  • 11/20/2009 10:03 AM In reply to

    Re: Texture 2D Block Tranfer. Exists?

    Craig Martin:
    You could make an extension method like so:

            public static Texture2D Transfer<T>(this Texture2D srcTexture, Rectangle srcRect) where T : struct 
            { 
                T[] buffer = new T[srcRect.Width * srcRect.Height]; 
                srcTexture.GetData<T>(0, srcRect, buffer, 0, buffer.Length); 
                Texture2D result = new Texture2D(srcTexture.GraphicsDevice, srcRect.Width, srcRect.Height); 
                result.SetData<T>(buffer); 
                return result;           
            } 
     


    Note: I just typed this, it's not tested.

    Edit: Added the where constraint.


    I don't mean to be rude but I don't think this is a good idea to use at all. You'll end up with a bunch of textures that need to be swamped if you are using it on a animation sprite.
    The whole point of using sprite sheets and rendering just portions it to avoid the GPU to be switching textures.
    Imagine that you are always rendering you main character, it has an animation, every few milliseconds you switch it's texture, and if the GPU has is memory full it was to erase the previous texture and load the new one. If you have everything on the same texture swamping is not necessary. It's already there. Usually I use a Texture2d for each animation.
  • 11/21/2009 1:50 AM In reply to

    Re: Texture 2D Block Tranfer. Exists?

    I agree with you David, it is not the right way. I already showed the right way, but they asked for an alternative, so I gave one :p
    Game hobbyist hell-bent on coding a diabolical Matrix
Page 1 of 1 (7 items) Previous Next