Browse Source

WIP: Sprite support.

* 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
parent
commit
b06fc6e3a1
  1. 11
      DotSDL.sln
  2. 3
      DotSDL/DotSDL.csproj
  3. 56
      DotSDL/Graphics/Sprite.cs
  4. 78
      DotSDL/Math/Vector2.cs
  5. 13
      Samples/Sample.Sprites/Player.cs
  6. 8
      Samples/Sample.Sprites/Program.cs
  7. 9
      Samples/Sample.Sprites/Sample.Sprites.csproj
  8. 28
      Samples/Sample.Sprites/Window.cs

11
DotSDL.sln

@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Audio", "Samples\Sam
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Power", "Samples\Sample.Power\Sample.Power.csproj", "{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Sprites", "Samples\Sample.Sprites\Sample.Sprites.csproj", "{305585D5-BD22-400D-80AA-459E05A9E243}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -59,6 +61,14 @@ Global
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x64.Build.0 = Release|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x86.ActiveCfg = Release|Any CPU
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40}.Release|x86.Build.0 = Release|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Debug|x64.ActiveCfg = Debug|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Debug|x64.Build.0 = Debug|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Debug|x86.ActiveCfg = Debug|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Debug|x86.Build.0 = Debug|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Release|x64.ActiveCfg = Release|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Release|x64.Build.0 = Release|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Release|x86.ActiveCfg = Release|Any CPU
{305585D5-BD22-400D-80AA-459E05A9E243}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -69,5 +79,6 @@ Global
{05C4735D-89CD-4501-88EC-B332A15D425E} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
{B71D69F1-3BB1-4CC8-AB6A-2A0F2DAE9FE5} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
{45E8AA1C-97DC-4889-B64E-2874BCAC6D40} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
{305585D5-BD22-400D-80AA-459E05A9E243} = {70DA3135-B76E-421D-B9CF-E49CD6440B0A}
EndGlobalSection
EndGlobal

3
DotSDL/DotSDL.csproj

@ -15,4 +15,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup>
</Project>

56
DotSDL/Graphics/Sprite.cs

@ -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;
}
}
}

78
DotSDL/Math/Vector2.cs

@ -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);
}
}
}

13
Samples/Sample.Sprites/Player.cs

@ -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;
}
}
}

8
Samples/Sample.Sprites/Program.cs

@ -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);
}
}
}

9
Samples/Sample.Sprites/Sample.Sprites.csproj

@ -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>

28
Samples/Sample.Sprites/Window.cs

@ -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…
Cancel
Save