예제 #1
0
            /// <summary>
            /// Draws an ellipse with a specified color.
            /// </summary>
            internal void DrawEllipse(SpriteBatch batch, Rectangle rectangle, Color color, double scale = 1D)
            {
                EllipseConfiguration ellipse;
                var checksum = EllipseConfiguration.GenerateChecksum(rectangle.Width, rectangle.Height);

                if (_ellipseConfigs.ContainsKey(checksum))
                {
                    ellipse = _ellipseConfigs[checksum];
                }
                else
                {
                    ellipse = new EllipseConfiguration((int)Math.Ceiling(rectangle.Width / scale), (int)Math.Ceiling(rectangle.Height / scale));
                    _ellipseConfigs.Add(checksum, ellipse);
                }

                // Finally, draw the configuration:
                ellipse.Draw(batch, rectangle, color);
            }
        /// <summary>
        /// Creates a joined texture from multiple ellipses.
        /// </summary>
        /// <param name="outerWidth">The full width of the joined texture.</param>
        /// <param name="outerHeight">The full height of the joined texture.</param>
        /// <param name="ellipses">Ellipse defintion (size and fill color).</param>
        public static Texture2D CreateJoined(int outerWidth, int outerHeight, SimpleEllipse[] ellipses)
        {
            // The objects at the same index in the ellipses and colors arrays are corresponding.

            var colorArr      = new Color[outerWidth * outerHeight];
            var returnTexture = new Texture2D(GameInstanceProvider.Instance.GraphicsDevice, outerWidth, outerHeight);

            // By default, the return texture is entirely transparent:
            for (var i = 0; i < colorArr.Length; i++)
            {
                colorArr[i] = Color.Transparent;
            }

            // Loop through the ellipses and fill the color array with the ellipse colors:
            for (var i = 0; i < ellipses.Length; i++)
            {
                var ellipse = ellipses[i];
                var color   = ellipses[i].FillColor;

                var ellipseTextureData = EllipseConfiguration.GenerateTextureData(ellipse.Bounds.Width, ellipse.Bounds.Height);

                for (var x = 0; x < ellipse.Bounds.Width; x++)
                {
                    for (var y = 0; y < ellipse.Bounds.Height; y++)
                    {
                        var index    = y * ellipse.Bounds.Width + x;
                        var colIndex = (y + ellipse.Bounds.Y) * outerWidth + (x + ellipse.Bounds.X);

                        // Only fill in when the ellipse's color is not transparent:
                        if (ellipseTextureData[index] != Color.Transparent)
                        {
                            colorArr[colIndex] = color;
                        }
                    }
                }
            }

            returnTexture.SetData(colorArr);

            return(returnTexture);
        }