Browse Source

Pulled line drawing routines into the Canvas object; updated example project.

improved_timing
Ian Burgmyer 7 years ago
parent
commit
1149f74096
  1. 69
      DotSDL/Graphics/Canvas.cs
  2. 16
      DotSDL/Graphics/Line.cs
  3. 21
      DotSDL/Graphics/Rect.cs
  4. 58
      Samples/Sample.BasicPixels/Window.cs

69
DotSDL/Graphics/Canvas.cs

@ -1,4 +1,6 @@
namespace DotSDL.Graphics {
using System;
namespace DotSDL.Graphics {
/// <summary>
/// A representation of the contents of the SDL window, with a number of
/// helper routines.
@ -28,6 +30,71 @@
SetSize(width, height);
}
/// <summary>
/// Plots a line on the <see cref="Canvas"/>.
/// </summary>
/// <param name="color">The color of the line.</param>
/// <param name="line">A <see cref="Line"/> object representing the shape that should be drawn.</param>
public void DrawLine(Color color, Line line) {
var dx = line.End.X - line.Start.X;
var dy = line.End.Y - line.Start.Y;
if(Math.Abs(dx) > Math.Abs(dy)) {
if(line.End.X > line.Start.X) {
for(var x = line.Start.X; x < line.End.X; x++) {
var y = line.Start.Y + dy * (x - line.Start.X) / dx;
Pixels[GetIndex(x, y)] = color;
}
} else {
for(var x = line.Start.X; x > line.End.X; x--) {
var y = line.Start.Y + dy * (x - line.Start.X) / dx;
Pixels[GetIndex(x, y)] = color;
}
}
} else {
if(line.End.Y > line.Start.Y) {
for(var y = line.Start.Y; y < line.End.Y; y++) {
var x = line.Start.X + dx * (y - line.Start.Y) / dy;
Pixels[GetIndex(x, y)] = color;
}
} else {
for(var y = line.Start.Y; y > line.End.Y; y--) {
var x = line.Start.X + dx * (y - line.Start.Y) / dy;
Pixels[GetIndex(x, y)] = color;
}
}
}
}
/// <summary>
/// Plots a sequence of lines on the <see cref="Canvas"/>.
/// </summary>
/// <param name="color">The color of the lines.</param>
/// <param name="lines">A set of <see cref="Line"/> objects representing the shapes that should be drawn.</param>
public void DrawLines(Color color, params Line[] lines) {
foreach(var line in lines)
DrawLine(color, line);
}
/// <summary>
/// Retrieves an array index on the <see cref="Canvas"/>.
/// </summary>
/// <param name="x">The Y coordinate of the desired location on the <see cref="Canvas"/>.</param>
/// <param name="y">The Y coordinate of the desired location on the <see cref="Canvas"/>.</param>
/// <returns>The array index for the given point.</returns>
public int GetIndex(int x, int y) {
return (Width * y) + x;
}
/// <summary>
/// Retrieves an array index on the <see cref="Canvas"/>.
/// </summary>
/// <param name="point">A <see cref="Point"/> representing the desired location on the <see cref="Canvas"/>.</param>
/// <returns>The array index for the given point.</returns>
public int GetIndex(Point point) {
return (Width * point.Y) + point.X;
}
/// <summary>
/// Resizes the <see cref="Canvas"/>.
/// </summary>

16
DotSDL/Graphics/Line.cs

@ -0,0 +1,16 @@
namespace DotSDL.Graphics {
/// <summary>
/// Defines a line.
/// </summary>
public struct Line {
/// <summary>
/// The starting point of the <see cref="Line"/>.
/// </summary>
public Point Start;
/// <summary>
/// The ending point of the <see cref="Line"/>.
/// </summary>
public Point End;
}
}

21
DotSDL/Graphics/Rect.cs

@ -0,0 +1,21 @@
namespace DotSDL.Graphics {
/// <summary>
/// Defines a rectangle.
/// </summary>
public struct Rect {
/// <summary>
/// The upper-left corner of the <see cref="Rect"/>.
/// </summary>
public Point Origin;
/// <summary>
/// The width of the <see cref="Rect"/>.
/// </summary>
public int Width;
/// <summary>
/// The height of the <see cref="Rect"/>.
/// </summary>
public int Height;
}
}

58
Samples/Sample.BasicPixels/Window.cs

@ -5,11 +5,6 @@ namespace DotSDL.Sample.BasicPixels {
internal class Window : SdlWindow {
public Window(int width, int height) : base("Basic Pixels", new Point { X = WindowPosUndefined, Y = WindowPosUndefined }, width, height) {}
private struct Line {
public Point Start;
public Point End;
}
private void DrawBackground(ref Color[] pixels) {
byte d = 0;
var dec = false;
@ -46,54 +41,13 @@ namespace DotSDL.Sample.BasicPixels {
}
}
private void DrawLine(ref Color[] pixels, Color color, Line line) {
var dx = line.End.X - line.Start.X;
var dy = line.End.Y - line.Start.Y;
if(Math.Abs(dx) > Math.Abs(dy)) {
if(line.End.X > line.Start.X) {
for(var x = line.Start.X; x < line.End.X; x++) {
var y = line.Start.Y + dy * (x - line.Start.X) / dx;
pixels[GetPosition(x, y)] = color;
}
} else {
for(var x = line.Start.X; x > line.End.X; x--) {
var y = line.Start.Y + dy * (x - line.Start.X) / dx;
pixels[GetPosition(x, y)] = color;
}
}
} else {
if(line.End.Y > line.Start.Y) {
for(var y = line.Start.Y; y < line.End.Y; y++) {
var x = line.Start.X + dx * (y - line.Start.Y) / dy;
pixels[GetPosition(x, y)] = color;
}
} else {
for(var y = line.Start.Y; y > line.End.Y; y--) {
var x = line.Start.X + dx * (y - line.Start.Y) / dy;
pixels[GetPosition(x, y)] = color;
}
}
}
}
private void DrawSequence(ref Color[] pixels, Color color, params Line[] lines) {
foreach(var line in lines) {
DrawLine(ref pixels, color, line);
}
}
private int GetPosition(int x, int y) {
return (Width * y) + x;
}
protected override void OnDraw(ref Canvas canvas) {
const byte min = 128, max = 255;
const int offsetX = 96, offsetY = 80;
DrawBackground(ref canvas.Pixels);
// D
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX, Y = offsetY + 96 }, End = new Point { X = offsetX, Y = offsetY } },
new Line { Start = new Point { X = offsetX, Y = offsetY }, End = new Point { X = offsetX + 32, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 32, Y = offsetY }, End = new Point { X = offsetX + 48, Y = offsetY + 16 } },
@ -102,7 +56,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 32, Y = offsetY + 96 }, End = new Point { X = offsetX, Y = offsetY + 96 } }
);
// o
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 64, Y = offsetY + 88}, End = new Point { X = offsetX + 64, Y = offsetY + 56 } },
new Line { Start = new Point { X = offsetX + 64, Y = offsetY + 56}, End = new Point { X = offsetX + 72, Y = offsetY + 48 } },
new Line { Start = new Point { X = offsetX + 72, Y = offsetY + 48}, End = new Point { X = offsetX + 80, Y = offsetY + 48 } },
@ -113,13 +67,13 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 72, Y = offsetY + 96}, End = new Point { X = offsetX + 64, Y = offsetY + 88 } }
);
// t
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 104 + 12, Y = offsetY + 24}, End = new Point { X = offsetX + 104 + 12, Y = offsetY + 96 } },
new Line { Start = new Point { X = offsetX + 104, Y = offsetY + 48}, End = new Point { X = offsetX + 104 + 24, Y = offsetY + 48 } }
);
// S
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 144 + 48, Y = offsetY }, End = new Point { X = offsetX + 144, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 144, Y = offsetY }, End = new Point { X = offsetX + 144, Y = offsetY + 48 } },
new Line { Start = new Point { X = offsetX + 144, Y = offsetY + 48 }, End = new Point { X = offsetX + 144 + 48 , Y = offsetY + 48 } },
@ -127,7 +81,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 144 + 48, Y = offsetY + 96 }, End = new Point { X = offsetX + 144, Y = offsetY + 96 } }
);
// D
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 208, Y = offsetY + 96 }, End = new Point { X = offsetX + 208, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 208, Y = offsetY }, End = new Point { X = offsetX + 208 + 32, Y = offsetY } },
new Line { Start = new Point { X = offsetX + 208 + 32, Y = offsetY }, End = new Point { X = offsetX + 208 + 48, Y = offsetY + 16 } },
@ -136,7 +90,7 @@ namespace DotSDL.Sample.BasicPixels {
new Line { Start = new Point { X = offsetX + 208 + 32, Y = offsetY + 96 }, End = new Point { X = offsetX + 208, Y = offsetY + 96 } }
);
// L
DrawSequence(ref canvas.Pixels, RandomColor(min, max),
canvas.DrawLines(RandomColor(min, max),
new Line { Start = new Point { X = offsetX + 272, Y = offsetY }, End = new Point { X = offsetX + 272, Y = offsetY + 96} },
new Line { Start = new Point { X = offsetX + 272, Y = offsetY + 96 }, End = new Point { X = offsetX + 272 + 48, Y = offsetY + 96} }
);

Loading…
Cancel
Save