Browse Source
* Replaced homegrown Vector2 class with the accelerated one from System.Numerics. * Added new Sprite initializers to take advantage of the new ZOrder field. Changed Point data types to Vector2 where appropriate. * Began working on a Sprites sample project.improved_timing
Ian Burgmyer
6 years ago
8 changed files with 117 additions and 89 deletions
@ -1,38 +1,72 @@
|
||||
namespace DotSDL.Graphics { |
||||
using System.Numerics; |
||||
|
||||
namespace DotSDL.Graphics { |
||||
/// <summary> |
||||
/// Represents a graphical, two-dimensional object in a program. |
||||
/// </summary> |
||||
public class Sprite : Canvas { |
||||
public Point Position { get; } |
||||
public Point Scale { get; } |
||||
public Vector2 Position { get; set; } |
||||
public Vector2 Scale { get; set; } |
||||
public int ZOrder { get; set; } |
||||
|
||||
/// <summary> |
||||
/// Initializes a new <see cref="Sprite"/>. |
||||
/// </summary> |
||||
/// <param name="width">The width of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="height">The height of the new <see cref="Sprite"/>.</param> |
||||
public Sprite(int width, int height) : this(width, height, 0, 0) { } |
||||
public Sprite(int width, int height) : this(width, height, Vector2.Zero, Vector2.One, 0) { } |
||||
|
||||
/// <summary> |
||||
/// Initializes a new <see cref="Sprite"/>. |
||||
/// </summary> |
||||
/// <param name="width">The width of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="height">The height of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="x">The initial X position of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="y">The initial Y position of the new <see cref="Sprite"/>.</param> |
||||
public Sprite(int width, int height, int x, int y) : this(width, height, new Point { X = x, Y = y }) { } |
||||
/// <param name="position">A <see cref="Vector2"/> representing the initial position of the new <see cref="Sprite"/>.</param> |
||||
public Sprite(int width, int height, Vector2 position) : this(width, height, position, Vector2.One, 0) { } |
||||
|
||||
/// <summary> |
||||
/// Initializes a new <see cref="Sprite"/>. |
||||
/// </summary> |
||||
/// <param name="width">The width of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="height">The height of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="position">A <see cref="Point"/> representing the initial position of the new <see cref="Sprite"/>.</param> |
||||
public Sprite(int width, int height, Point position) : base(width, height) { |
||||
Position = position; |
||||
/// <param name="zorder">A value indicating the order in which this <see cref="Sprite"/> is drawn. Higher numbered |
||||
/// sprites are drawn on top of other sprites and, thus, will appear above them.</param> |
||||
public Sprite(int width, int height, int zorder) : this(width, height, Vector2.Zero, Vector2.One, zorder) { } |
||||
|
||||
/// <summary> |
||||
/// Initializes a new <see cref="Sprite"/>. |
||||
/// </summary> |
||||
/// <param name="width">The width of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="height">The height of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="position">A <see cref="Vector2"/> representing the initial position of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="scale">A <see cref="Vector2"/> representing the initial scaling of the new <see cref="Sprite"/>.</param> |
||||
public Sprite(int width, int height, Vector2 position, Vector2 scale) : this(width, height, position, scale, 0) { } |
||||
|
||||
/// <summary> |
||||
/// Initializes a new <see cref="Sprite"/>. |
||||
/// </summary> |
||||
/// <param name="width">The width of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="height">The height of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="position">A <see cref="Vector2"/> representing the initial position of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="zorder">A value indicating the order in which this <see cref="Sprite"/> is drawn. Higher numbered |
||||
/// sprites are drawn on top of other sprites and, thus, will appear above them.</param> |
||||
public Sprite(int width, int height, Vector2 position, int zorder) : this(width, height, position, Vector2.One, 0) { } |
||||
|
||||
/// <summary> |
||||
/// Initializes a new <see cref="Sprite"/>. |
||||
/// </summary> |
||||
/// <param name="width">The width of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="height">The height of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="position">A <see cref="Vector2"/> representing the initial position of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="scale">A <see cref="Vector2"/> representing the initial scaling of the new <see cref="Sprite"/>.</param> |
||||
/// <param name="zorder">A value indicating the order in which this <see cref="Sprite"/> is drawn. Higher numbered |
||||
/// sprites are drawn on top of other sprites and, thus, will appear above them.</param> |
||||
public Sprite(int width, int height, Vector2 position, Vector2 scale, int zorder) : base(width, height) { |
||||
SetSize(width, height); |
||||
|
||||
Scale = new Point { X = 1, Y = 1 }; |
||||
Position = position; |
||||
Scale = scale; |
||||
ZOrder = zorder; |
||||
} |
||||
} |
||||
} |
||||
|
@ -1,78 +0,0 @@
|
||||
using System; |
||||
using System.Xml; |
||||
|
||||
namespace DotSDL.Math { |
||||
/// <summary> |
||||
/// Represents a two dimensional vector using single-precision floats. |
||||
/// </summary> |
||||
public class Vector2 { |
||||
public float X { get; set; } |
||||
public float Y { get; set; } |
||||
|
||||
/// <summary> |
||||
/// Creates a new <see cref="Vector2"/>. |
||||
/// </summary> |
||||
public Vector2() : this(0, 0) { } |
||||
|
||||
/// <summary> |
||||
/// Creates a new <see cref="Vector2"/>. |
||||
/// </summary> |
||||
/// <param name="x">The X value of the new <see cref="Vector2"/>.</param> |
||||
/// <param name="y">The Y value of the new <see cref="Vector2"/>.</param> |
||||
public Vector2(float x, float y) { |
||||
X = x; |
||||
Y = y; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Creates a new <see cref="Vector2"/>. |
||||
/// </summary> |
||||
/// <param name="vec">An existing <see cref="Vector2"/> to copy into the new object.</param> |
||||
public Vector2(Vector2 vec) : this(vec.X, vec.Y) { } |
||||
|
||||
/// <summary> |
||||
/// Adds two <see cref="Vector2"/> objects together. |
||||
/// </summary> |
||||
/// <param name="vec1">The left <see cref="Vector2"/> operand.</param> |
||||
/// <param name="vec2">The right <see cref="Vector2"/> operand.</param> |
||||
/// <returns>The result of <paramref name="vec1"/> + <paramref name="vec2"/>.</returns> |
||||
public static Vector2 operator +(Vector2 vec1, Vector2 vec2) { |
||||
return new Vector2(vec1.X + vec2.X, |
||||
vec1.Y + vec2.Y); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Subtracts two <see cref="Vector2"/> objects. |
||||
/// </summary> |
||||
/// <param name="vec1">The left <see cref="Vector2"/> operand.</param> |
||||
/// <param name="vec2">The right <see cref="Vector2"/> operand.</param> |
||||
/// <returns>The result of <paramref name="vec1"/> - <paramref name="vec2"/>.</returns> |
||||
public static Vector2 operator -(Vector2 vec1, Vector2 vec2) { |
||||
return new Vector2(vec1.X - vec2.X, |
||||
vec1.Y - vec2.Y); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Multiplies a <see cref="Vector2"/> by a scalar value. |
||||
/// </summary> |
||||
/// <param name="vec">The left <see cref="Vector2"/> operand.</param> |
||||
/// <param name="scalar">The right <see cref="float"/> operand.</param> |
||||
/// <returns>The result of <paramref name="vec"/> * <paramref name="scalar"/>.</returns> |
||||
public static Vector2 operator *(Vector2 vec, float scalar) { |
||||
return new Vector2(vec.X * scalar, |
||||
vec.Y * scalar); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Divides a <see cref="Vector2"/> by a scalar value. |
||||
/// </summary> |
||||
/// <param name="vec">The left <see cref="Vector2"/> operand.</param> |
||||
/// <param name="scalar">The right <see cref="float"/> operand.</param> |
||||
/// <returns>The result of <paramref name="vec"/> / <paramref name="scalar"/>.</returns> |
||||
/// <exception cref="DivideByZeroException">The scalar value is zero.</exception> |
||||
public static Vector2 operator /(Vector2 vec, float scalar) { |
||||
return new Vector2(vec.X / scalar, |
||||
vec.Y / scalar); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,13 @@
|
||||
using DotSDL.Graphics; |
||||
|
||||
namespace Sample.Sprites { |
||||
public class Player : Sprite { |
||||
private Color _color; |
||||
private int _speed; |
||||
|
||||
public Player(Color color, int speed) : base(64, 64) { |
||||
_color = color; |
||||
_speed = speed; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,8 @@
|
||||
namespace Sample.Sprites { |
||||
internal static class Program { |
||||
private static void Main(string[] args) { |
||||
var window = new Window(2); |
||||
window.Start(16); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk"> |
||||
<PropertyGroup> |
||||
<OutputType>Exe</OutputType> |
||||
<TargetFramework>netcoreapp2.0</TargetFramework> |
||||
</PropertyGroup> |
||||
<ItemGroup> |
||||
<ProjectReference Include="..\..\DotSDL\DotSDL.csproj" /> |
||||
</ItemGroup> |
||||
</Project> |
@ -0,0 +1,28 @@
|
||||
using DotSDL.Events; |
||||
using DotSDL.Graphics; |
||||
|
||||
namespace Sample.Sprites { |
||||
public class Window : SdlWindow { |
||||
public Window(int scale) : base("Sprites Test", |
||||
new Point { X = WindowPosUndefined, Y = WindowPosUndefined }, |
||||
256 * scale, 196 * scale, |
||||
256, 196) { |
||||
KeyPressed += OnKeyPressed; |
||||
KeyReleased += OnKeyReleased; |
||||
} |
||||
|
||||
protected override void OnDraw(ref Canvas canvas) { |
||||
} |
||||
|
||||
private void OnKeyPressed(object sender, KeyboardEvent e) { |
||||
throw new System.NotImplementedException(); |
||||
} |
||||
|
||||
private void OnKeyReleased(object sender, KeyboardEvent e) { |
||||
throw new System.NotImplementedException(); |
||||
} |
||||
|
||||
protected override void OnUpdate() { |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue