Пример #1
0
        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;
        }
Пример #2
0
        public Level(Texture2D _brick, Texture2D _badGuys, Texture2D _projectile, Texture2D _blank, GraphicsDevice graphicsDevice, SpriteBatch _spriteBatch)
        {
            spriteBatch = _spriteBatch;

            int brick2 = brickSize * brickSize;

            singleBrick = new Color[brick2];
            beginBrick = new Color[brick2];
            contBrick = new Color[brick2];
            endBrick = new Color[brick2];
            topDoor = new Color[brick2];
            botDoor = new Color[brick2];
            destructBrick = new Color[brick2];
            collectable = new Color[brick2];

            openDoor = new AnimatedSprite();

            projectile = _projectile;
            badGuys = _badGuys;
            brick = _brick;
            _brick.GetData<Color>(0, new Rectangle(0, 0, brickSize, brickSize), topDoor, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, brickSize, brickSize, brickSize), botDoor, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, 2 * brickSize, brickSize, brickSize), singleBrick, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, 3 * brickSize, brickSize, brickSize), beginBrick, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, 4 * brickSize, brickSize, brickSize), contBrick, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, 5 * brickSize, brickSize, brickSize), endBrick, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, 6 * brickSize, brickSize, brickSize), destructBrick, 0, brick2);
            _brick.GetData<Color>(0, new Rectangle(0, 7 * brickSize, brickSize, brickSize), collectable, 0, brick2);

            currLvlText = new Texture2D(graphicsDevice, _blank.Width, _blank.Height);
            blankLevel = new Color[_blank.Height*_blank.Width];
            _blank.GetData<Color>(blankLevel);
            currLvlText.SetData<Color>(blankLevel);
        }
 public override void GetData <T>(T[] data, int mipLevel, Rectangle?subimage, int startIndex, int elementCount)
 {
     if (subimage.HasValue)
     {
         XNA.Rectangle rect;
         Rectangle     v = subimage.Value;
         XNAHelper.ConvertRectangle(ref v, out rect);
         _texture2D.GetData <T>(mipLevel, rect, data, startIndex, elementCount);
     }
     else
     {
         _texture2D.GetData <T>(mipLevel, null, data, startIndex, elementCount);
     }
 }
Пример #4
0
        public static Vertices FromTextrure(Texture2D polygonTexture)
        {
            uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];
            polygonTexture.GetData(data);

            return PolygonTools.CreatePolygon(data, polygonTexture.Width);
        }
Пример #5
0
        public Bonus(ContentManager content, Viewport view, int type, int positionX, int positionY, int velocityX, int velocityY, float rotation)
        {
            this.type = type;
            switch (type)
            {
                case 0:
                    bonusTexture = content.Load<Texture2D>("bonus/damageup");
                    break;
                case 1:
                     bonusTexture = content.Load<Texture2D>("bonus/levelup");
                    break;
                case 2:
                     bonusTexture = content.Load<Texture2D>("bonus/lifeup");
                    break;
                case 3:
                     bonusTexture = content.Load<Texture2D>("bonus/speedup");
                    break;

            }

            position = new Vector2(positionX, positionY);
            velocity = new Vector2(velocityX, velocityY);
            rotationSpeed = rotation;
            origin = new Vector2(bonusTexture.Width / 2, bonusTexture.Height / 2);
            textureData = new Color[bonusTexture.Width * bonusTexture.Height];
            bonusTexture.GetData(textureData);
        }
        private Texture2D processTexture(Texture2D texture)
        {
            //Some programs such as Photoshop save PNG and other RGBA images without premultiplied alpha
            if (!Constants.Instance.PremultipliedAlpha)
            {

                //XNA 4.0+ assumes that all RGBA images have premultiplied alpha channels.
                //Code snippet borrowed from http://xboxforums.create.msdn.com/forums/p/62320/383015.aspx#485897

                Byte4[] data = new Byte4[texture.Width * texture.Height];
                texture.GetData<Byte4>(data);
                for (int i = 0; i < data.Length; i++)
                {
                    Vector4 vec = data[i].ToVector4();
                    float alpha = vec.W / 255.0f;
                    int a = (int)(vec.W);
                    int r = (int)(alpha * vec.X);
                    int g = (int)(alpha * vec.Y);
                    int b = (int)(alpha * vec.Z);
                    uint packed = (uint)(
                        (a << 24) +
                        (b << 16) +
                        (g << 8) +
                        r
                        );

                    data[i].PackedValue = packed;
                }

                texture.SetData<Byte4>(data);
            }

            return texture;
        }
Пример #7
0
        public override void LoadContent()
        {
            //load texture that will represent the physics body
            _polygonTexture = ScreenManager.ContentManager.Load<Texture2D>("Content/Texture");

            //Create an array to hold the data from the texture
            uint[] data = new uint[_polygonTexture.Width * _polygonTexture.Height];

            //Transfer the texture data to the array
            _polygonTexture.GetData(data);

            //Calculate the vertices from the array
            Vertices verts = Vertices.CreatePolygon(data, _polygonTexture.Width, _polygonTexture.Height);

            //Make sure that the origin of the texture is the centroid (real center of geometry)
            _polygonOrigin = verts.GetCentroid();

            //Use the body factory to create the physics body
            _polygonBody = BodyFactory.Instance.CreatePolygonBody(PhysicsSimulator, verts, 5);
            _polygonBody.Position = new Vector2(500, 400);

            GeomFactory.Instance.CreatePolygonGeom(PhysicsSimulator, _polygonBody, verts, 0);

            _circleTexture = DrawingHelper.CreateCircleTexture(ScreenManager.GraphicsDevice, 35, Color.Gold, Color.Black);
            _circleOrigin = new Vector2(_circleTexture.Width / 2f, _circleTexture.Height / 2f);
            _circleBody = BodyFactory.Instance.CreateCircleBody(PhysicsSimulator, 35, 1);
            _circleBody.Position = new Vector2(300, 400);

            GeomFactory.Instance.CreateCircleGeom(PhysicsSimulator, _circleBody, 35, 20);

            base.LoadContent();
        }
Пример #8
0
            public static XGraphics.Texture2D FromFile(GraphicsDevice device, string file, bool premultiplyAlpha)
            {
                XGraphics.Texture2D tex = null;

                using (FileStream fs = File.OpenRead(file)) {
                    tex = XGraphics.Texture2D.FromStream(device, fs);
                }

                if (premultiplyAlpha)
                {
                    byte[] data = new byte[tex.Width * tex.Height * 4];
                    tex.GetData(data);

                    for (int i = 0; i < data.Length; i += 4)
                    {
                        int a = data[i + 3];
                        data[i + 0] = (byte)(data[i + 0] * a / 255);
                        data[i + 1] = (byte)(data[i + 1] * a / 255);
                        data[i + 2] = (byte)(data[i + 2] * a / 255);
                    }

                    tex.SetData(data);
                }

                return(tex);
            }
        private void BuildSelectorTexture()
        {
            Vector2 textureSize = new Vector2(this.Size.X * 2, this.Size.Y * 2);
            selectorTexture = new Texture2D(Globals.GraphicsDevice, (int)textureSize.X, (int)textureSize.Y);
            Texture hold = Globals.GraphicsDevice.Textures[0];
            Globals.GraphicsDevice.Textures[0] = null;

            Color[] colors = new Color[(int)textureSize.X * (int)textureSize.Y];
            selectorTexture.GetData(colors);

            Vector2 center = new Vector2(textureSize.X / 2, textureSize.Y / 2);
            int ndx = 0;
            for (int y = 0; y < textureSize.Y; y++)
            {
                for (int x = 0; x < textureSize.X; x++)
                {
                    Vector2 location = new Vector2(x, y);
                    float radius = (center - location).Length();
                    if (radius + 3 >= textureSize.X / 2 && radius <= textureSize.X / 2)
                        colors[ndx] = Color.Black;
                    ndx++;
                }
            }

                selectorTexture.SetData(colors);
            Globals.GraphicsDevice.Textures[0] = hold;
        }
Пример #10
0
        public Map(Game game, string name)
            : base(game)
        {
            Name = name;
            DrawOrder = 1;

            MapImage = _sandGame.Content.Load<Texture2D>(string.Format("Textures/Maps/{0}-image", Name));

            _map = _sandGame.Content.Load<Texture2D>(string.Format("Textures/Maps/{0}", Name));

            Width = _map.Width;
            Height = _map.Height;

            _mapTexture = new Color[(int)(Width * Height)];
            _map.GetData(_mapTexture);

            for(int x = 0; x < Width; x++)
            {
                for(int y = 0; y < Height; y++)
                {
                    var color = _mapTexture[(int)(x + (y * Width))];

                    if(color == Color.Red)
                    {
                        RedSpawn = new Vector2(x, y);
                    }
                    else if(color == Color.Lime)
                    {
                        BlueSpawn = new Vector2(x, y);
                    }
                }
            }
        }
Пример #11
0
 public static void generateShadedTexture(Color col, Texture2D bsprite, ref Texture2D target)
 {
     //System.Console.WriteLine("Input colour: " + col.ToString());
     try
     {
         Color[] shades = generateShades(col);
         /*System.Console.WriteLine("Output colours: " + shades[0]);
         System.Console.WriteLine("\t" + shades[1]);
         System.Console.WriteLine("\t" + shades[2]);
         System.Console.WriteLine("\t" + shades[3]);
         System.Console.WriteLine("\t" + shades[4]);*/
         Color[] colArray = new Color[bsprite.Width * bsprite.Height];
         bsprite.GetData<Color>(colArray);
         for (int c = 0; c < colArray.Length; c++)
         {
             for (int i = 0; i < 5; i++)
             {
                 if (colArray[c] == BLUE_SHADES[i])
                 {
                     colArray[c] = shades[i];
                     break;
                 }
             }
         }
         target.SetData<Color>(colArray);
     }
     catch (System.Exception e)
     {
         System.Console.OpenStandardError();
         System.Console.WriteLine(e.Message);
         System.Console.WriteLine(e.StackTrace);
         System.Console.Error.Close();
     }
     //System.Console.WriteLine("Finished texture shading!");
 }
Пример #12
0
		protected void LoadHeightData( Texture2D heightMap, float magnify ) {
			float minimumHeight = float.MaxValue;
			float maximumHeight = float.MinValue;

			mWidth = heightMap.Width;
			mLength = heightMap.Height;

			Color[] heightMapColors = new Color[ mWidth * mLength ];
			heightMap.GetData( heightMapColors );

			mHeightMap = new float[ mWidth * mLength ];
			for( int x = 0; x < mWidth; x++ )
				for( int y = 0; y < mLength; y++ ) {
					mHeightMap[ x + y * mWidth ] = heightMapColors[ x + y * mWidth ].R;
					if( mHeightMap[ x + y * mWidth ] < minimumHeight )
						minimumHeight = mHeightMap[ x + y * mWidth ];
					if( mHeightMap[ x + y * mWidth ] > maximumHeight )
						maximumHeight = mHeightMap[ x + y * mWidth ];
				}

			// normalise height data to be between 0 and 30.0f
			for( int x = 0; x < mWidth; x++ )
				for( int y = 0; y < mLength; y++ )
					mHeightMap[ x + y * mWidth ] = ( mHeightMap[ x + y * mWidth ] - minimumHeight ) / ( maximumHeight - minimumHeight ) * magnify;
		}
Пример #13
0
        public static List<Vertices> GetVertsFromMapImage( Texture2D image, float scale, out Vector2 origin )
        {
            uint[] data = new uint[ image.Width * image.Height ];
            image.GetData( data );

            Vertices textureVertices = PolygonTools.CreatePolygon( data, image.Width, false );
            Vector2 centroid = -textureVertices.GetCentroid();
            textureVertices.Translate( ref centroid );

            origin = -centroid;

            //We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.ReduceByDistance( textureVertices, 4f );

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons

            //scale the vertices from graphics space to sim space
            //Settings.MaxPolygonVertices = 1000;
            var list = BayazitDecomposer.ConvexPartition( textureVertices );

            Vector2 vertScale = new Vector2( ConvertUnits.ToSimUnits( 1 ) ) * scale;
            foreach ( Vertices vertices in list ) {
                vertices.Scale( ref vertScale );
            }
            return list;
        }
        private void BuildColorSelector()
        {
            Texture hold = Globals.GraphicsDevice.Textures[0];
            Globals.GraphicsDevice.Textures[0] = null;

            Vector2 selectorSize = new Vector2(5, 10);
            colorSelector = new Texture2D(Globals.GraphicsDevice, (int)selectorSize.X, (int)selectorSize.Y);

            Color[] colors = new Color[(int)selectorSize.X * (int)selectorSize.Y];
            colorSelector.GetData(colors);

            int ndx = 0;
            for (int y = 0; y < selectorSize.Y; y++)
            {
                for (int x = 0; x < selectorSize.X; x++)
                {
                    if (y < selectorSize.Y / 2)
                    {
                        if (x <= y)
                            colors[ndx] = Color.Black;
                    }
                    else
                    {
                        colors[ndx] = Color.Black;
                        if (x >= selectorSize.Y - y)
                            colors[ndx] = Color.Transparent;
                    }

                    ndx++;
                }
            }

            colorSelector.SetData(colors);
            Globals.GraphicsDevice.Textures[0] = hold;
        }
        private static Boolean PixelPerfectCollision(Rectangle Bounds1, Texture2D Texture1, Rectangle Bounds2, Texture2D Texture2)
        {
            Color[] bitsA = new Color[Bounds1.Width * Bounds1.Height];
            Color[] bitsB = new Color[Bounds2.Width * Bounds1.Height];
            Texture1.GetData(bitsA);
            Texture2.GetData(bitsB);

            int x1 = Math.Max(Bounds1.X, Bounds2.X);
            int x2 = Math.Min(Bounds1.X + Bounds1.Width, Bounds2.X + Bounds2.Width);

            int y1 = Math.Max(Bounds1.Y, Bounds2.Y);
            int y2 = Math.Min(Bounds1.Y + Bounds1.Height, Bounds2.Y + Bounds2.Height);

            // For each single pixel in the intersecting rectangle
            for (int y = y1; y < y2; ++y)
            {
                for (int x = x1; x < x2; ++x)
                {
                    // Get the color from each texture
                    Color a = bitsA[(x - Bounds1.X) + (y - Bounds1.Y) * Texture1.Width];
                    Color b = bitsB[(x - Bounds2.X) + (y - Bounds2.Y) * Texture2.Width];

                    if (a.A != 0 && b.A != 0) // If both colors are not transparent (the alpha channel is not 0), then there is a collision
                    {
                        return true;
                    }
                }
            }
            return false;
        }
Пример #16
0
        public static float[,] LoadHeightData(Texture2D height_map, ref int terrain_width, ref int terrain_height, ref float min_height, ref float max_height, float height_limit)
        {
            terrain_width = height_map.Width;
            terrain_height = height_map.Height;

            Color[] map_colors = new Color[terrain_height * terrain_width];
            height_map.GetData<Color>(map_colors);

            float[,] height_data = new float[terrain_width, terrain_height];
            for(int x=0; x<terrain_width; x++)
            {
                for(int y=0; y<terrain_height; y++)
                {
                    height_data[x, y] = map_colors[x+y*terrain_width].R;
                    if (height_data[x, y] > max_height)
                        max_height = height_data[x, y];
                    if(height_data[x, y] < min_height)
                        min_height = height_data[x, y];
                }
            }

            for (int x = 0; x < terrain_width; x++)
            {
                for(int y=0; y< terrain_height; y++)
                {
                    if (max_height == min_height)
                        height_data[x, y] = Math.Min(min_height, height_limit);
                    else
                        height_data[x, y] = (height_data[x, y] - min_height) / (max_height - min_height) * height_limit;
                }
            }

            return height_data;
        }
Пример #17
0
        private void BuildVertices(Texture2D heightMap)
        {
            var heightMapColors = new Color[_vertexCount];
            heightMap.GetData(heightMapColors);

            float x = _position.X;
            float z = _position.Z;
            float y = _position.Y;
            float maxX = x + _topSize;

            for (int i = 0; i < _vertexCount; i++)
            {
                if (x > maxX)
                {
                    x = _position.X;
                    z++;
                }

                y = _position.Y + (heightMapColors[i].R / 2.0f);
                var vert = new VertexPositionNormalTexture(new Vector3(x * _scale, y * _scale, z * _scale), Vector3.Zero, Vector2.Zero);
                vert.TextureCoordinate = new Vector2(((vert.Position.X - _position.X) / _topSize), ((vert.Position.Z - _position.Z) / _topSize)) / _scale;
                Vertices[i] = vert;
                x++;
            }
        }
Пример #18
0
 public Trap(Texture2D texture, Vector2 pos)
     : base(texture, pos)
 {
     hitbox = new Rectangle((int)pos.X, (int)pos.Y, texture.Width, texture.Height);
     texData = new Color[texture.Width * texture.Height];
     texture.GetData(texData);
 }
Пример #19
0
 public AnimatedSprite(Texture2D texture)
 {
     this.texture = texture;
     int area = texture.Width * texture.Height;
     data = new Color[area];
     texture.GetData(0, null, data, 0, area);
 }
Пример #20
0
        public static bool AddNewLevelBlock(Level level, Vector3 coordinate, Texture2D heightMap, Texture2D alphaMap, string[] detailTextureNames, Vector2[] uvOffsets, Vector2[] uvScales)
        {
            if (level.Contains(coordinate))
            {
                return false;
            }

            Color[] heightColors = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES];
            if (heightMap.Width != HeightMapMesh.NUM_SIDE_VERTICES || heightMap.Height != HeightMapMesh.NUM_SIDE_VERTICES)
            {
                RenderTarget2D resizedHeightMap = new RenderTarget2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES);
                GraphicsManager.Device.SetRenderTarget(resizedHeightMap);

                GraphicsManager.SpriteBatch.Begin();
                GraphicsManager.SpriteBatch.Draw(heightMap, new Rectangle(0, 0, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES), Color.White);
                GraphicsManager.SpriteBatch.End();

                GraphicsManager.Device.SetRenderTarget(null);
                resizedHeightMap.GetData(heightColors);
            }
            else
            {
                heightMap.GetData(heightColors);
            }

            float[,] heights = new float[HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES];
            for (int i = 0; i < heightColors.Length; i++)
            {
                heights[i % HeightMapMesh.NUM_SIDE_VERTICES, i / HeightMapMesh.NUM_SIDE_VERTICES] = ConvertColorToFloat(heightColors[i]);
            }

            if (alphaMap.Height != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD ||
                alphaMap.Width != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD)
            {
                RenderTarget2D resizedAlphaMap = new RenderTarget2D(GraphicsManager.Device,
                    HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD,
                    HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD);

                GraphicsManager.Device.SetRenderTarget(resizedAlphaMap);

                GraphicsManager.SpriteBatch.Begin();
                GraphicsManager.SpriteBatch.Draw(alphaMap,
                    new Rectangle(0,
                        0,
                        HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD,
                        HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD),
                    Color.White);
                GraphicsManager.SpriteBatch.End();

                GraphicsManager.Device.SetRenderTarget(null);

                alphaMap = resizedAlphaMap;
            }

            HeightMapMesh heightMapMesh = new HeightMapMesh(heights, alphaMap, detailTextureNames, uvOffsets, uvScales);

            AssetLibrary.AddHeightMap(level.Name + coordinate.ToString(), heightMapMesh);
            level.AddNewBlock(coordinate);
            return true;
        }
Пример #21
0
        /// <summary>
        /// It gets picture and splits it into 9 parts (3x3).
        /// </summary>
        /// <param name="image">the picture that sholud be splitted</param>
        /// <param name="width">into parts with width = <paramref name="width"/></param>
        /// <param name="height">into parts with height = height</param>
        /// <param name="xCount">rows</param>
        /// <param name="yCount">columns</param>
        /// <returns></returns>
        public List<Shape> Split(Texture2D image, int width, int height, int xCount = 3, int yCount = 3)
        {
            List<Shape> result = new List<Shape>();
            int dataPerPart = width * height;
            int uniqueIndexer = 1;
            Color[] originalData = new Color[image.Width * image.Height];
            image.GetData<Color>(originalData);

            for (int y = 0; y < yCount * height; y += height)
                for (int x = 0; x < xCount * width; x += width)
                {
                    Texture2D part = new Texture2D(image.GraphicsDevice, width, height);
                    Color[] partData = new Color[dataPerPart];

                    for (int py = 0; py < height; py++)
                        for (int px = 0; px < width; px++)
                        {
                            int partIndex = px + py * width;
                            if (y + py >= image.Height || x + px >= image.Width)
                                partData[partIndex] = Color.Transparent;
                            else
                                partData[partIndex] = originalData[(x + px) + (y + py) * image.Width];
                        }
                    part.SetData<Color>(partData);
                    Shape shape = new Shape(part);
                    shape.UniqueNumber = uniqueIndexer;
                    uniqueIndexer++;
                    result.Add(shape);
                }

            return result;
        }
Пример #22
0
        private void LoadTexture(Texture2D texture)
        {
            var data = new Int32[texture.Width*texture.Height];
            texture.GetData(data);

            // Make levels of darkness
            m_TextureData = new Int32[Darknesslevels][,];

            float darkness = 1.0f;
            for (int d = 0; d < Darknesslevels; d++)
            {
                m_TextureData[d] = new Int32[texture.Width,texture.Height];
                for (int x = 0; x < texture.Width; x++)
                {
                    for (int y = 0; y < texture.Height; y++)
                    {
                        var index = x + (texture.Width*y);
                        var pixel = data[index];
                        var bytes = BitConverter.GetBytes(pixel);

                        pixel = 255 << 24 | (Byte) (bytes[2]/darkness) << 16 | (Byte) (bytes[1]/darkness) << 8 |
                                (Byte) (bytes[0]/darkness);

                        m_TextureData[d][x, y] = pixel;
                    }
                }

                // TODO find the perfect co-efficents for the equation of darkness
                darkness += 0.00f + (d*0.0125f) + (d*d*0.00025f);
            }
        }
Пример #23
0
 public static uint[] GetTextureData(Texture2D texture)
 {
     int dataLength = texture.Width * texture.Height;
     uint[] pixelData = new uint[dataLength];
     texture.GetData<uint>(pixelData);
     return pixelData;
 }
Пример #24
0
        public NinePatchImage(Texture2D texture)
        {
            _texture = texture;
            XnaColor[] data = new XnaColor[texture.Width * texture.Height];
            texture.GetData(data);
            Stretch = new Area(
                vertical: GetLine(texture, data, 0, 1, 0, 0),
                horizontal: GetLine(texture, data, 1, 0, 0, 0));
            Content = new Area(
                vertical: GetLine(texture, data, 0, 1, _texture.Width - 1, 0),
                horizontal: GetLine(texture, data, 1, 0, 0, _texture.Height - 1));

            Width = _texture.Width - 2;
            Height = _texture.Height - 2;

            _leftTop = new XnaRectangle(1, 1, Stretch.Horizontal.Start, Stretch.Vertical.Start);
            _leftCenter = new XnaRectangle(1, Stretch.Vertical.Start, Stretch.Horizontal.Start, Stretch.Vertical.Size);
            _leftBottom = new XnaRectangle(1, Stretch.Vertical.End, Stretch.Horizontal.Start, texture.Height - 1 - Stretch.Vertical.End);
            _centerTop = new XnaRectangle(Stretch.Horizontal.Start, 1, Stretch.Horizontal.Size, Stretch.Vertical.Start);
            _center = new XnaRectangle(Stretch.Horizontal.Start, Stretch.Vertical.Start, Stretch.Horizontal.Size, Stretch.Vertical.Size);
            _centerBottom = new XnaRectangle(Stretch.Horizontal.Start, Stretch.Vertical.End, Stretch.Horizontal.Size, texture.Height - 1 - Stretch.Vertical.End);
            _rightTop = new XnaRectangle(Stretch.Horizontal.End, 1, texture.Width - 1 - Stretch.Horizontal.End, Stretch.Vertical.Start);
            _rightCenter = new XnaRectangle(Stretch.Horizontal.End, Stretch.Vertical.Start, texture.Width - 1 - Stretch.Horizontal.End, Stretch.Vertical.Size);
            _rightBottom = new XnaRectangle(Stretch.Horizontal.End, Stretch.Vertical.End, texture.Width - 1 - Stretch.Horizontal.End, texture.Height - 1 - Stretch.Vertical.End);
        }
Пример #25
0
        public Level Parse(Texture2D mapImage)
        {
            Level level = new Level();

            _levelScaleFactor = 4;
            _imageHeight = mapImage.Height;
            _imageWidth = mapImage.Width;

            Color[] colorData;
            colorData = new Color[_imageWidth * _imageHeight];
            mapImage.GetData<Color>(colorData);

            for (int y = 0; y < _imageHeight; y++)
            {
                for (int x = 0; x < _imageWidth; x++)
                {
                    Color currentPixel = colorData[_imageWidth * y + x];
                    if (currentPixel == Color.White)
                        continue;

                    Point position = new Point(x, y);

                    //Wall if all are the same
                    if (currentPixel.R == currentPixel.G && currentPixel.G == currentPixel.B)
                        HandleWall(position, currentPixel, colorData, level);
                    else if (currentPixel == PLAYER_COLOR)
                        HandlePlayer(position, currentPixel, colorData, level);
                    else if (currentPixel == FRIENDLY_COLOR)
                        HandleFriendly(position, currentPixel, colorData, level);
                    else if (currentPixel == ENEMY_COLOR)
                        HandleEnemy(position, currentPixel, colorData, level);
                }
            }
            return level;
        }
        public static Texture2D Crop(Texture2D source, Rectangle area)
        {
            if (source == null)
            return null;

            Texture2D cropped = new Texture2D(source.GraphicsDevice, area.Width, area.Height);
            Color[] data = new Color[source.Width * source.Height];
            Color[] cropData = new Color[cropped.Width * cropped.Height];

            source.GetData<Color>(data);

            int index = 0;
            for (int y = area.Y; y < area.Y + area.Height; y++)
            {
                for (int x = area.X; x < area.X + area.Width; x++)
                {
                    cropData[index] = data[x + (y * source.Width)];
                    index++;
                }
            }

            cropped.SetData<Color>(cropData);

            return cropped;
        }
Пример #27
0
        public IMTexture(Texture2D texture)
        {
            Texture = texture;
            LODColor = Color.Black;

            // If this is a null texture, use a black LOD color.
            if (Texture == null)
                return;

            // Calculate the load color dynamically.
            float r = 0, g = 0, b = 0;
            Color[] pixelData = new Color[texture.Width * texture.Height];
            texture.GetData<Color>(pixelData);
            for (int i = 0; i < texture.Width; i++)
                for (int j = 0; j < texture.Height; j++)
                {
                    r += pixelData[i + j * texture.Width].R;
                    g += pixelData[i + j * texture.Width].G;
                    b += pixelData[i + j * texture.Width].B;
                }
            r /= texture.Width * texture.Height;
            g /= texture.Width * texture.Height;
            b /= texture.Width * texture.Height;
            LODColor = new Color(r / 256, g / 256, b / 256);
        }
Пример #28
0
        public static void CopyAlpha(ref Texture2D TextureTo, Texture2D TextureFrom)
        {
            if (TextureTo.Width != TextureFrom.Width || TextureTo.Height != TextureFrom.Height)
            {
                return;
            }

            var size = TextureTo.Width * TextureTo.Height;
            uint[] buffer = GetBuffer();
            TextureTo.GetData(buffer, 0, size);

            var sizeFrom = TextureFrom.Width * TextureFrom.Height;
            var bufferFrom = GetBuffer();
            TextureFrom.GetData(bufferFrom, 0, sizeFrom);

            for (int i = 0; i < size; i++)
            {
                //ARGB
                buffer[i] = (buffer[i] & 0x00FFFFFF) | (bufferFrom[i] & 0xFF000000);
            }

            TextureTo.SetData(buffer, 0, size, SetDataOptions.None);

            FreeBuffer(buffer);
            FreeBuffer(bufferFrom);
        }
Пример #29
0
        /// <summary>
        /// Checks for collision in the bitmap
        /// </summary>
        /// <param name="collisionTexture">texture you want collision against</param>
        /// <param name="checkPoint">point you want to check for collision</param>
        /// <returns>collision type</returns>
        public CollisionType CheckForCollisions(Texture2D collisionTexture, Point checkPoint)
        {
            var collisionColors = new Color[collisionTexture.Height * collisionTexture.Width];
            collisionTexture.GetData(collisionColors);

            var checkIndex = checkPoint.X + checkPoint.Y * collisionTexture.Width;

            // Index out of bounds
            if (checkIndex > collisionColors.Length - 1)
                return CollisionType.Unwalkable;

            var detectedColor = collisionColors[checkIndex];

            if (detectedColor == BushTile)
                return CollisionType.Bush;

            if ( detectedColor == HealingHerb )
                return CollisionType.HealingHerb;

            if (detectedColor == WalkableTile)
                return CollisionType.Walkable;

            if (detectedColor == TrainerTriggerBushTile)
                return CollisionType.TrainerTriggerBush;

            if (detectedColor == TrainerTriggerTile)
                return CollisionType.TrainerTrigger;

            return CollisionType.Unwalkable;
        }
Пример #30
0
 public void LoadContent(ContentManager contentManager, string theAsset)
 {
     Texture = contentManager.Load<Texture2D>(theAsset);
     Size = new Rectangle(0, 0, (int) (Texture.Width * Scale), (int) (Texture.Height * Scale));
     TextureData = new Color[Texture.Width * Texture.Height];
     Texture.GetData(TextureData);
 }
Пример #31
0
        public bool PixelCollision(Texture2D player, Rectangle playerR, Rectangle cannonR)
        {
            Texture2D cannon = this.texCannon;
            Color[] colorDataPlayer = new Color[player.Width * player.Height];
            Color[] colorDataCannon = new Color[cannon.Width * cannon.Height];
            player.GetData<Color>(colorDataPlayer);
            cannon.GetData<Color>(colorDataCannon);

            int top, bottom, left, right;

            top = Math.Max(playerR.Top, cannonR.Top); //el mayor top (el q esta mas abajo) osea el top del rectangulo q se forma al colisionar
            bottom = Math.Min(playerR.Bottom, cannonR.Bottom);
            left = Math.Max(playerR.Left, cannonR.Left);
            right = Math.Min(playerR.Right, cannonR.Right);

            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    Color A = colorDataPlayer[(y - playerR.Top) * (playerR.Width) + (x - playerR.Left)];
                    Color B = colorDataCannon[(y - cannonR.Top) * (cannonR.Width) + (x - cannonR.Left)];

                    if (A.A != 0 && B.A != 0)//si el alpha no es 0 (no es transparente) colisiona
                        return true;
                }
            }
            return false;
        }
Пример #32
0
 public static void blit(Texture2D src, Rectangle srcRect, Texture2D dest, Rectangle destRect)
 {
     int count = srcRect.Width * srcRect.Height;
     uint[] data = new uint[count];
     src.GetData<uint>(0, srcRect, data, 0, count);
     dest.SetData<uint>(0, destRect, data, 0, count, SetDataOptions.None);
 }
Пример #33
0
        public static WriteableBitmap ToBitmapSource(this Texture2D texture)
        {
            var bmp       = new WriteableBitmap(texture.Width, texture.Height, 96, 96, PixelFormats.Bgra32, null);
            var pixelData = new int[texture.Width * texture.Height];

            texture.GetData(pixelData);

            bmp.Lock();

            unsafe
            {
                var pixels = (int *)bmp.BackBuffer;

                for (var i = 0; i < pixelData.Length; i++)
                {
                    pixels[i] = ColorToWindows(pixelData[i]);
                }
            }

            bmp.AddDirtyRect(new Int32Rect(0, 0, texture.Width, texture.Height));
            bmp.Unlock();

            return(bmp);
        }
Пример #34
0
        public static CellSurface ToSurface(this Texture2D image, SadConsole.Font font, bool blockMode = false)
        {
            int imageWidth  = image.Width;
            int imageHeight = image.Height;

            Color[] pixels = new Color[imageWidth * imageHeight];
            image.GetData <Color>(pixels);

            CellSurface surface = new CellSurface(imageWidth / font.Size.X, imageHeight / font.Size.Y);

            global::System.Threading.Tasks.Parallel.For((int)0, (int)(imageHeight / font.Size.Y), (h) =>
                                                        //for (int h = 0; h < imageHeight / font.Size.Y; h++)
            {
                int startY = (h * font.Size.Y);
                //System.Threading.Tasks.Parallel.For(0, imageWidth / font.Size.X, (w) =>
                for (int w = 0; w < imageWidth / font.Size.X; w++)
                {
                    int startX = (w * font.Size.X);

                    float allR = 0;
                    float allG = 0;
                    float allB = 0;

                    for (int y = 0; y < font.Size.Y; y++)
                    {
                        for (int x = 0; x < font.Size.X; x++)
                        {
                            int cY = y + startY;
                            int cX = x + startX;

                            Color color = pixels[cY * imageWidth + cX];

                            allR += color.R;
                            allG += color.G;
                            allB += color.B;
                        }
                    }

                    byte sr = (byte)(allR / (font.Size.X * font.Size.Y));
                    byte sg = (byte)(allG / (font.Size.X * font.Size.Y));
                    byte sb = (byte)(allB / (font.Size.X * font.Size.Y));

                    var newColor = new Color(sr, sg, sb);

                    float sbri = newColor.GetBrightness() * 255;

                    if (blockMode)
                    {
                        if (sbri > 204)
                        {
                            surface.SetGlyph(w, h, 219, newColor); //█
                        }
                        else if (sbri > 152)
                        {
                            surface.SetGlyph(w, h, 178, newColor); //▓
                        }
                        else if (sbri > 100)
                        {
                            surface.SetGlyph(w, h, 177, newColor); //▒
                        }
                        else if (sbri > 48)
                        {
                            surface.SetGlyph(w, h, 176, newColor); //░
                        }
                    }
                    else
                    {
                        if (sbri > 230)
                        {
                            surface.SetGlyph(w, h, (int)'#', newColor);
                        }
                        else if (sbri > 207)
                        {
                            surface.SetGlyph(w, h, (int)'&', newColor);
                        }
                        else if (sbri > 184)
                        {
                            surface.SetGlyph(w, h, (int)'$', newColor);
                        }
                        else if (sbri > 161)
                        {
                            surface.SetGlyph(w, h, (int)'X', newColor);
                        }
                        else if (sbri > 138)
                        {
                            surface.SetGlyph(w, h, (int)'x', newColor);
                        }
                        else if (sbri > 115)
                        {
                            surface.SetGlyph(w, h, (int)'=', newColor);
                        }
                        else if (sbri > 92)
                        {
                            surface.SetGlyph(w, h, (int)'+', newColor);
                        }
                        else if (sbri > 69)
                        {
                            surface.SetGlyph(w, h, (int)';', newColor);
                        }
                        else if (sbri > 46)
                        {
                            surface.SetGlyph(w, h, (int)':', newColor);
                        }
                        else if (sbri > 23)
                        {
                            surface.SetGlyph(w, h, (int)'.', newColor);
                        }
                    }
                }
            }
                                                        );

            return(surface);
        }
Пример #35
0
        public static void ToSurface(this Texture2D image, BasicSurface surface, Color[] cachedColorArray, bool blockMode = false)
        {
            int imageWidth  = image.Width;
            int imageHeight = image.Height;

            image.GetData <Color>(cachedColorArray);

            SurfaceEditor editor = new SurfaceEditor(surface);

            editor.Clear();
            global::System.Threading.Tasks.Parallel.For(0, imageHeight / surface.Font.Size.Y, (h) =>
                                                        //for (int h = 0; h < imageHeight / surface.Font.Size.Y; h++)
            {
                int startY = (h * surface.Font.Size.Y);
                //System.Threading.Tasks.Parallel.For(0, imageWidth / surface.Font.Size.X, (w) =>
                for (int w = 0; w < imageWidth / surface.Font.Size.X; w++)
                {
                    int startX = (w * surface.Font.Size.X);

                    float allR = 0;
                    float allG = 0;
                    float allB = 0;

                    for (int y = 0; y < surface.Font.Size.Y; y++)
                    {
                        for (int x = 0; x < surface.Font.Size.X; x++)
                        {
                            int cY = y + startY;
                            int cX = x + startX;

                            Color color = cachedColorArray[cY * imageWidth + cX];

                            allR += color.R;
                            allG += color.G;
                            allB += color.B;
                        }
                    }

                    byte sr = (byte)(allR / (surface.Font.Size.X * surface.Font.Size.Y));
                    byte sg = (byte)(allG / (surface.Font.Size.X * surface.Font.Size.Y));
                    byte sb = (byte)(allB / (surface.Font.Size.X * surface.Font.Size.Y));

                    var newColor = new Color(sr, sg, sb);

                    float sbri = newColor.GetBrightness() * 255;

                    if (blockMode)
                    {
                        if (sbri > 204)
                        {
                            editor.SetGlyph(w, h, 219, newColor); //█
                        }
                        else if (sbri > 152)
                        {
                            editor.SetGlyph(w, h, 178, newColor); //▓
                        }
                        else if (sbri > 100)
                        {
                            editor.SetGlyph(w, h, 177, newColor); //▒
                        }
                        else if (sbri > 48)
                        {
                            editor.SetGlyph(w, h, 176, newColor); //░
                        }
                    }
                    else
                    {
                        if (sbri > 230)
                        {
                            editor.SetGlyph(w, h, (int)'#', newColor);
                        }
                        else if (sbri > 207)
                        {
                            editor.SetGlyph(w, h, (int)'&', newColor);
                        }
                        else if (sbri > 184)
                        {
                            editor.SetGlyph(w, h, (int)'$', newColor);
                        }
                        else if (sbri > 161)
                        {
                            editor.SetGlyph(w, h, (int)'X', newColor);
                        }
                        else if (sbri > 138)
                        {
                            editor.SetGlyph(w, h, (int)'x', newColor);
                        }
                        else if (sbri > 115)
                        {
                            editor.SetGlyph(w, h, (int)'=', newColor);
                        }
                        else if (sbri > 92)
                        {
                            editor.SetGlyph(w, h, (int)'+', newColor);
                        }
                        else if (sbri > 69)
                        {
                            editor.SetGlyph(w, h, (int)';', newColor);
                        }
                        else if (sbri > 46)
                        {
                            editor.SetGlyph(w, h, (int)':', newColor);
                        }
                        else if (sbri > 23)
                        {
                            editor.SetGlyph(w, h, (int)'.', newColor);
                        }
                    }
                }
            }
                                                        );
        }
Пример #36
0
 public override void GetData <T>(T[] data, int mipLevel, int left, int right, int startIndex, int elementCount)
 {
     _texture2D.GetData <T>(mipLevel, new Microsoft.Xna.Framework.Rectangle(0, 0, left, right), data, startIndex, elementCount);
 }