FSO.Common.Utils.TextureUtils.Clip C# (CSharp) Method

Clip() public static method

public static Clip ( GraphicsDevice gd, Microsoft.Xna.Framework.Graphics.Texture2D texture, Rectangle source ) : Microsoft.Xna.Framework.Graphics.Texture2D
gd GraphicsDevice
texture Microsoft.Xna.Framework.Graphics.Texture2D
source Microsoft.Xna.Framework.Rectangle
return Microsoft.Xna.Framework.Graphics.Texture2D
        public static Texture2D Clip(GraphicsDevice gd, Texture2D texture, Rectangle source)
        {
            var newTexture = new Texture2D(gd, source.Width, source.Height);
            var size = source.Width * source.Height;
            uint[] buffer = GetBuffer(size);
            if (FSOEnvironment.SoftwareDepth)
            {
                //opengl es does not like this
                var texBuf = GetBuffer(texture.Width * texture.Height);
                texture.GetData(texBuf);
                var destOff = 0;
                for (int y=source.Y; y<source.Bottom; y++)
                {
                    int offset = y * texture.Width + source.X;
                    for (int x = 0; x < source.Width; x++)
                    {
                        buffer[destOff++] = texBuf[offset++];
                    }
                }
            }
            else
            {
                texture.GetData(0, source, buffer, 0, size);
            }

            newTexture.SetData(buffer);
            return newTexture;
        }