- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/Utilities/TextureHelpers.cs

    00001 ﻿#region File Description
    00002 //-----------------------------------------------------------------------------
    00003 // TextureHelpers.cs
    00004 //-----------------------------------------------------------------------------
    00005 #endregion
    00006
    00007 namespace NewGamePhysics.Utilities
    00008 {
    00009     using System;
    00010     using Microsoft.Xna.Framework;
    00011     using Microsoft.Xna.Framework.Graphics;
    00012
    00016     public class TextureHelpers
    00017     {
    00024         public static Texture2D CreateColorTexture(GraphicsDevice graphicsDevice)
    00025         {
    00026             return Create(graphicsDevice, 1, 1, new Color());
    00027         }
    00028
    00036         public static Texture2D Create(GraphicsDevice graphicsDevice, Color color)
    00037         {
    00038             return Create(graphicsDevice, 1, 1, color);
    00039         }
    00040
    00050         public static Texture2D Create(GraphicsDevice graphicsDevice, int width, int height, Color color)
    00051         {
    00052             // create the rectangle texture without colors
    00053             Texture2D texture = new Texture2D(
    00054                 graphicsDevice,
    00055                 width,
    00056                 height,
    00057                 1,
    00058                 TextureUsage.None,
    00059                 SurfaceFormat.Color);
    00060
    00061             // Create a color array for the pixels
    00062             Color[] colors = new Color[width * height];
    00063             for (int i = 0; i < colors.Length; i++)
    00064             {
    00065                 colors[i] = new Color(color.ToVector3());
    00066             }
    00067
    00068             // Set the color data for the texture
    00069             texture.SetData(colors);
    00070
    00071             return texture;
    00072         }
    00073
    00079         public static Color[] TextureToColors(Texture2D texture)
    00080         {
    00081             Color[] color1D = new Color[texture.Width * texture.Height];
    00082             texture.GetData(color1D);
    00083             return color1D;
    00084         }
    00085
    00091         public static Color[,] TextureToColorArray(Texture2D texture)
    00092         {
    00093             // Get colors as list
    00094             Color[] color1D = TextureToColors(texture);
    00095
    00096             // Create new 2D array for colors
    00097             Color[,] color2D = new Color[texture.Width, texture.Height];
    00098
    00099             // Copy color from list to array
    00100             int x=0;
    00101             int y=0;
    00102             foreach (Color color in color1D)
    00103             {
    00104                 color2D[x, y] = color;
    00105                 x++;
    00106                 if (x > texture.Width)
    00107                 {
    00108                     y++;
    00109                     x = 0;
    00110                 }
    00111             }
    00112
    00113             return color2D;
    00114         }
    00115     }
    00116 }