Ejemplo n.º 1
0
        /// <summary>
        /// Draws the sprite on the screen
        /// </summary>
        /// <param name="position">The position to draw at</param>
        /// <param name="rectangle">The rectangle to draw from</param>
        /// <param name="centerRef">The center reference of the sprite</param>
        /// <param name="rotation">The rotation in radians</param>
        /// <param name="scale">The scale</param>
        public void Draw(Vector2 position, Rectangle?rectangle, Vector3?centerRef, float?rotation = null, Vector2?scale = null)
        {
            if (Handle == null || Handle.IsDisposed)
            {
                return;
            }

            if (!IsDrawing)
            {
                Core.EndAllDrawing(Core.RenderingType.Sprite);
                Handle.Begin();
                IsDrawing = true;
            }

            if (!rotation.HasValue && !scale.HasValue)
            {
                // Draw the sprite
                Handle.Draw(Texture, _colorBrga, rectangle, centerRef, new Vector3(position, 0) + (centerRef ?? Vector3.Zero));
            }
            else
            {
                // Store previous transform
                var transform = Handle.Transform;

                try
                {
                    // Apply transformation
                    Handle.Transform *= (Matrix.Scaling(new Vector3(scale ?? new Vector2(1), 0))) * Matrix.RotationZ(rotation ?? 0) *
                                        Matrix.Translation(new Vector3(position, 0) + (centerRef ?? Vector3.Zero));

                    // Draw the sprite
                    Handle.Draw(Texture, _colorBrga, rectangle, centerRef);
                }
                catch (Exception e)
                {
                    Logger.Debug("Failed to draw sprite with transformation:");
                    Logger.Debug(e.ToString());
                }

                // Restore previous transform
                Handle.Transform = transform;
            }
        }
Ejemplo n.º 2
0
        internal void Draw(Color color, Matrix?transform, Vector2[] vertices, float width = DefaultWidth, bool antialias = true)
        {
            // Validation
            if (Handle == null || Handle.IsDisposed || vertices.Length < 2 || width <= 0)
            {
                return;
            }

            if (!IsDrawing)
            {
                Core.EndAllDrawing(Core.RenderingType.Line);
                Handle.Antialias = antialias;
                Handle.Begin();
                IsDrawing = true;
            }

            // Draw the line(s)
            if (Math.Abs(Handle.Width - width) > float.Epsilon)
            {
                Handle.End();
                Handle.Width = width;
                Handle.Begin();
            }
            if (!transform.HasValue)
            {
                Handle.Draw(vertices, new ColorBGRA(color.R, color.G, color.B, color.A));
            }
            else
            {
                Handle.DrawTransform(vertices, transform.Value, new ColorBGRA(color.R, color.G, color.B, color.A));
            }

            if (!antialias)
            {
                Handle.End();
                Handle.Antialias = false;
                IsDrawing        = false;
            }
        }