Browse Source

WIP: More sprite stuff!

* Added a public Rectangle struct.
    * Added a constructor for the Point struct to slightly reduce typing fatigue. :)
    * Modified the way that Canvas classes are initialized so that
      sprite sheets and scrolling backgrounds will be possible to
      implement.
improved_timing
Ian Burgmyer 6 years ago
parent
commit
01bdaaab1f
  1. 66
      DotSDL/Graphics/Canvas.cs
  2. 12
      DotSDL/Graphics/Point.cs
  3. 35
      DotSDL/Graphics/Rectangle.cs
  4. 8
      DotSDL/Graphics/SdlWindow.cs
  5. 18
      DotSDL/Graphics/Sprite.cs

66
DotSDL/Graphics/Canvas.cs

@ -1,31 +1,70 @@
namespace DotSDL.Graphics {
using System;
namespace DotSDL.Graphics {
/// <summary>
/// A representation of the contents of the SDL window, with a number of
/// helper routines.
/// </summary>
public class Canvas {
private int _width, _height;
/// <summary>
/// The raw pixels in the <see cref="Canvas"/>.
/// </summary>
public Color[] Pixels;
/// <summary>
/// Gets the width of the <see cref="Canvas"/>.
/// Gets or sets the width of the <see cref="Canvas"/> texture.
/// </summary>
public int Width { get; private set; }
public int Width {
get => _width;
set {
if(value <= 0) throw new ArgumentException("Width must be greater than 0.");
_width = value;
Resize();
}
}
/// <summary>
/// Gets the height of the <see cref="Canvas"/>.
/// Gets or sets the height of the <see cref="Canvas"/> texture.
/// </summary>
public int Height { get; private set; }
public int Height {
get => _height;
set {
if(value <= 0) throw new ArgumentException("Height must be greater than 0.");
_height = value;
Resize();
}
}
/// <summary>
/// Initialization a new <see cref="Canvas"/>.
/// Sets the section of the <see cref="Canvas"/> that should be drawn. If the size values are set to 0, the
/// <see cref="Canvas"/> will fill as much of its containing object as possible.
/// </summary>
/// <param name="width">The width of the <see cref="Canvas"/>.</param>
/// <param name="height">The height of the <see cref="Canvas"/>.</param>
internal Canvas(int width, int height) {
SetSize(width, height);
public Rectangle Clipping { get; set; }
/// <summary>
/// Initializes a new <see cref="Canvas"/>.
/// </summary>
/// <param name="textureWidth">The width of the <see cref="Canvas"/>.</param>
/// <param name="textureHeight">The height of the <see cref="Canvas"/>.</param>
internal Canvas(int textureWidth, int textureHeight) : this(textureWidth, textureHeight, new Rectangle(0, 0, textureWidth, textureHeight)) { }
/// <summary>
/// Initializes a new <see cref="Canvas"/>.
/// </summary>
/// <param name="textureWidth">The width of the <see cref="Canvas"/>.</param>
/// <param name="textureHeight">The height of the <see cref="Canvas"/>.</param>
/// <param name="clipping">The clipping <see cref="Rectangle"/> for the <see cref="Canvas"/>.</param>
internal Canvas(int textureWidth, int textureHeight, Rectangle clipping) {
_width = textureWidth;
_height = textureHeight;
Clipping = clipping;
Resize();
}
/// <summary>
@ -51,12 +90,7 @@
/// Resizes the <see cref="Canvas"/>. Please note that this will also clear the canvas of
/// its existing contents.
/// </summary>
/// <param name="width">The new width of the <see cref="Canvas"/>.</param>
/// <param name="height">The new height of the <see cref="Canvas"/>.</param>
internal void SetSize(int width, int height) {
Width = width;
Height = height;
protected void Resize() {
Pixels = new Color[Width * Height];
}
}

12
DotSDL/Graphics/Point.cs

@ -2,7 +2,7 @@
/// <summary>
/// Represents a point in 2D space.
/// </summary>
public class Point {
public struct Point {
/// <summary>
/// The X coordinate of the point.
/// </summary>
@ -12,5 +12,15 @@
/// The Y coordinate of the point.
/// </summary>
public int Y { get; set; }
/// <summary>
/// Creates a new <see cref="Point"/>.
/// </summary>
/// <param name="x">The X value of the new <see cref="Point"/>.</param>
/// <param name="y">The Y value of the new <see cref="Point"/>.</param>
public Point(int x, int y) {
X = x;
Y = y;
}
}
}

35
DotSDL/Graphics/Rectangle.cs

@ -0,0 +1,35 @@
namespace DotSDL.Graphics {
/// <summary>
/// Represents a rectangle in 2D space.
/// </summary>
public struct Rectangle {
/// <summary>
/// A <see cref="Point"/> representing the position of the <see cref="Rectangle"/>.
/// </summary>
public Point Position { get; set; }
/// <summary>
/// A <see cref="Point"/> representing the size of the <see cref="Rectangle"/>.
/// </summary>
public Point Size { get; set; }
/// <summary>
/// Creates a new <see cref="Rectangle"/>.
/// </summary>
/// <param name="position">A <see cref="Point"/> representing the position of the new <see cref="Rectangle"/>.</param>
/// <param name="size">A <see cref="Point"/> representing the size of the new <see cref="Rectangle"/>.</param>
public Rectangle(Point position, Point size) {
Position = position;
Size = size;
}
/// <summary>
/// Creates a new <see cref="Rectangle"/>.
/// </summary>
/// <param name="x">The X position of the new <see cref="Rectangle"/>.</param>
/// <param name="y">The Y position of the new <see cref="Rectangle"/>.</param>
/// <param name="width">The width of the new <see cref="Rectangle"/>.</param>
/// <param name="height">The height of the new <see cref="Rectangle"/>.</param>
public Rectangle(int x, int y, int width, int height) : this(new Point(x, y), new Point(width, height)) { }
}
}

8
DotSDL/Graphics/SdlWindow.cs

@ -175,6 +175,14 @@ namespace DotSDL.Graphics {
IsDestroyed = true;
}
/// <summary>
/// Plots the sprites stored in <see cref="Sprites"/> to the screen. Please note that this method is called by
/// DotSDL's drawing routines and does not need to be called manually. You usually do not need to override this
/// method.
/// </summary>
public virtual void DrawSprites() {
}
/// <summary>
/// Gets an <see cref="IntPtr"/> that points to what should be displayed on the window's background. You usually
/// do not need to override this method.

18
DotSDL/Graphics/Sprite.cs

@ -5,9 +5,25 @@ namespace DotSDL.Graphics {
/// Represents a graphical, two-dimensional object in a program.
/// </summary>
public class Sprite : Canvas {
/// <summary>
/// The position on the screen where the <see cref="Sprite"/> should be drawn.
/// </summary>
public Vector2 Position { get; set; }
/// <summary>
/// The scale of the <see cref="Sprite"/>. 1.0f is 100%.
/// </summary>
public Vector2 Scale { get; set; }
/// <summary>
/// The order in which the sprite is drawn. Lower numbered <see cref="Sprite"/> instances are drawn first
/// and will appear on the bottom.
/// </summary>
public int ZOrder { get; set; }
/// <summary>
/// <c>true</c> if the sprite should be drawn to the screen, otherwise <c>false</c>.
/// </summary>
public bool Shown { get; set; }
/// <summary>
@ -63,8 +79,6 @@ namespace DotSDL.Graphics {
/// <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);
Position = position;
Scale = scale;
ZOrder = zorder;

Loading…
Cancel
Save