Beispiel #1
0
        /// <summary>
        /// Luo kuvion annetusta kuvasta. Kuvassa tulee olla vain yksi
        /// yhtenäinen muoto (toisin sanoen kuvio ei voi koostua monesta osasta).
        /// </summary>
        /// <remarks>
        /// Kuvion luominen voi olla melko hidasta. Kannattaa luoda kuvio heti pelin alussa
        /// ja käyttää kerran luotua kuviota kaikille olioille.
        /// </remarks>
        /// <param name="image">Kuva, josta muoto luetaan.</param>
        public static Shape FromImage(Image image)
        {
            Texture2D texture = image.XNATexture;
            IBitmap   bm      = new TextureBitmap(texture);

            Vector2D[] vertices = VertexHelper.CreateFromBitmap(bm);

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].X /= texture.Width;
                vertices[i].Y /= texture.Height;

                // The center of the texture coordinates is at top left corner, but the center of
                // game objects is by default at the center of the object.
                vertices[i] -= new Vector2D((float)0.5, (float)0.5);
            }

            Vector[] polygonVertices = new Vector[vertices.Length];
            for (int i = 0; i < vertices.Length; i++)
            {
                polygonVertices[i] = new Vector(vertices[i].X, vertices[i].Y);
            }

            ShapeCache cache = new ShapeCache(polygonVertices);

            return(new Polygon(cache));
        }