using DotSDL.Interop.Core; using DotSDL.Math.Vector; using SdlPixels = DotSDL.Interop.Core.Pixels; namespace DotSDL.Graphics { /// /// Represents a graphical, two-dimensional object in a program. /// public class Sprite : Canvas { private readonly Point _effectiveSize = new Point(); private Vector2 _scale; /// /// The position on the screen where the should be drawn. /// public Point Position { get; set; } /// /// The angle that the sprite is drawn, in degrees. Incrementing this will rotate the /// sprite clockwise. /// public float Rotation { get; set; } /// /// The point around which the sprite will be rotated. By default, this will be set to /// the center of the sprite. /// public Point RotationCenter { get; set; } /// /// Indicates which axes this sprice should be flipped across, if any. /// public FlipDirection Flip { get; set; } /// /// The scale of the . 1.0f is 100%. /// public Vector2 Scale { get => _scale; set { _scale = value; _effectiveSize.X = (int)(Width * _scale.X); _effectiveSize.Y = (int)(Height * _scale.Y); } } /// public override ScalingQuality ScalingQuality { get => ScalingQualityValue; set { ScalingQualityValue = value; if(HasTexture) { CreateTexture(); UpdateTexture(); } } } /// /// Gets the size of the sprite as it would be drawn to the screen, in pixels, taking /// both scaling and clipping into account. /// /// /// This value is calculated every time it's called. If you plan to the results of this /// in a loop, be sure to save the results to a variable and perform operations on that /// instead of calling this multiple times. /// public Point DrawSize => new Point( (int)(Clipping.Size.X * Scale.X), (int)(Clipping.Size.Y * Scale.Y) ); /// /// Gets the effective size of the sprite. This is the size of the sprite, in pixels, /// after taking scaling into account. /// public Point EffectiveSize => _effectiveSize; /// /// Defines the coordinate system that this sprite should use. This defaults to /// . /// public CoordinateSystem CoordinateSystem { get; set; } = CoordinateSystem.WorldSpace; /// /// true if the sprite should be drawn to the screen, otherwise false. /// public bool Shown { get; set; } /// /// The order in which the sprite is drawn. Lower numbered instances are drawn first /// and will appear on the bottom. /// public int ZOrder { get; set; } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . public Sprite(int width, int height) : this(width, height, Point.Zero, Vector2.One, 0) { } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . /// A representing the initial position of the new . public Sprite(int width, int height, Point position) : this(width, height, position, Vector2.One, 0) { } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . /// A value indicating the order in which this is drawn. Higher numbered /// sprites are drawn on top of other sprites and, thus, will appear above them. public Sprite(int width, int height, int zOrder) : this(width, height, Point.Zero, Vector2.One, zOrder) { } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . /// A representing the initial position of the new . /// A representing the initial scaling of the new . public Sprite(int width, int height, Point position, Vector2 scale) : this(width, height, position, scale, 0) { } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . /// A representing the initial position of the new . /// A value indicating the order in which this is drawn. Higher numbered /// sprites are drawn on top of other sprites and, thus, will appear above them. public Sprite(int width, int height, Point position, int zOrder) : this(width, height, position, Vector2.One, 0) { } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . /// A representing the initial position of the new . /// A representing the initial scaling of the new . /// A value indicating the order in which this is drawn. Higher numbered /// sprites are drawn on top of other sprites and, thus, will appear above them. public Sprite(int width, int height, Point position, Vector2 scale, int zOrder) : this(width, height, position, new Rectangle(0, 0, width, height), scale, zOrder) { } /// /// Initializes a new . /// /// The width of the new . /// The height of the new . /// A representing the initial position of the new . /// A representing the initial scaling of the new . /// A rectangle specifying which part of this should be drawn. /// A value indicating the order in which this is drawn. Higher numbered /// sprites are drawn on top of other sprites and, thus, will appear above them. public Sprite(int width, int height, Point position, Rectangle clipping, Vector2 scale, int zOrder) : base(width, height, clipping) { Position = position; Scale = scale; ZOrder = zOrder; Shown = false; RotationCenter = new Point(clipping.Size.X / 2, clipping.Size.Y / 2); } /// internal override void CreateTexture() { CreateTexture(Render.TextureAccess.Static); } /// /// Updates the texture associated with this . This function must be called when the /// array is changed after adding this sprite to the sprite list associated /// with the application's . /// /// true if the texture was successfully updated, otherwise false. This will return false if this hasn't been added to the sprite list. public new bool UpdateTexture() { return base.UpdateTexture(); } } }