From f2272b05558f2ad1902b05979272c77e5d56ed8e Mon Sep 17 00:00:00 2001 From: Bret Deasy Date: Thu, 17 May 2012 00:46:46 -0500 Subject: [PATCH] Adding Cut extension method to Texture2D.cs Will create a subTexture of Width & Height from top-left --- axios.sln | 8 +++++++- axios/Engine/Extensions/Texture2D.cs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/axios.sln b/axios.sln index f0d0e45..e80e6f5 100644 --- a/axios.sln +++ b/axios.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 +# Visual Studio 2010 Express for Windows Phone Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Axios_Windows", "axios\Axios_Windows.csproj", "{742C938F-997D-4EFD-95D2-BB09CDADCD2E}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Axios_Xbox_360", "axios\Axios_Xbox_360.csproj", "{B5664516-72B7-4BA3-9F72-25CAA90867D8}" @@ -45,6 +45,7 @@ Global {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Debug|Windows Phone.ActiveCfg = Debug|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Debug|Windows Phone.Build.0 = Debug|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Debug|x86.ActiveCfg = Debug|Xbox 360 + {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Debug|x86.Build.0 = Debug|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Debug|Xbox 360.ActiveCfg = Debug|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Debug|Xbox 360.Build.0 = Debug|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|Any CPU.ActiveCfg = Release|Xbox 360 @@ -53,6 +54,7 @@ Global {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|Windows Phone.ActiveCfg = Release|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|Windows Phone.Build.0 = Release|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|x86.ActiveCfg = Release|Xbox 360 + {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|x86.Build.0 = Release|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|Xbox 360.ActiveCfg = Release|Xbox 360 {B5664516-72B7-4BA3-9F72-25CAA90867D8}.Release|Xbox 360.Build.0 = Release|Xbox 360 {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|Any CPU.ActiveCfg = Debug|Windows Phone @@ -61,14 +63,18 @@ Global {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|Windows Phone.ActiveCfg = Debug|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|Windows Phone.Build.0 = Debug|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|x86.ActiveCfg = Debug|Windows Phone + {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|x86.Build.0 = Debug|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|Xbox 360.ActiveCfg = Debug|Windows Phone + {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Debug|Xbox 360.Build.0 = Debug|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Any CPU.ActiveCfg = Release|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Mixed Platforms.ActiveCfg = Release|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Mixed Platforms.Build.0 = Release|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Windows Phone.ActiveCfg = Release|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Windows Phone.Build.0 = Release|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|x86.ActiveCfg = Release|Windows Phone + {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|x86.Build.0 = Release|Windows Phone {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Xbox 360.ActiveCfg = Release|Windows Phone + {C09D9005-76AC-4F1A-9479-2787BB3DB158}.Release|Xbox 360.Build.0 = Release|Windows Phone EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/axios/Engine/Extensions/Texture2D.cs b/axios/Engine/Extensions/Texture2D.cs index 3a6b84d..6b0cc91 100644 --- a/axios/Engine/Extensions/Texture2D.cs +++ b/axios/Engine/Extensions/Texture2D.cs @@ -286,6 +286,27 @@ namespace Axios.Engine.Extensions return r; } + //Create new Texture which is a subsection of original with dimensions of partWidth and partHeight + public static Texture2D Cut(this Texture2D original, int partWidth, int partHeight) + { + Texture2D newTexture = new Texture2D(original.GraphicsDevice, partWidth, partHeight); //Create texture of new area = to subsection area + int pixelCount = partWidth * partHeight; //Number of pixels in the new texture + Color[] originalData = new Color[original.Width * original.Height]; + original.GetData(originalData); //Get the pixel data from the original texture + Color[] newData = new Color[pixelCount]; //Create pixel sheet for new texture + + for (int y = 0; y < partHeight; y++) //increment from the top-most pixel + { + for (int x = 0; x < partWidth; x++) //increment from the top-most, left-most pixel + { + int colorIndex = x + y * partWidth; //get pixel at {x, y} coordinates + newData[colorIndex] = originalData[(x + y * original.Width)]; //set the color to pixel at original {x, y} coordinates + } + } + + newTexture.SetData(newData); //set the color data for the new texture + return newTexture; //return Texture2D object cut from original + } } } \ No newline at end of file