/// <summary> /// Sets the foreground color for a specific unit in the buffer. /// </summary> /// <param name="x">The X coordinate of the unit.</param> /// <param name="y">The Y coordinate of the unit.</param> /// <param name="color">The foreground color to set the unit to.</param> public void SetUnitForeColor(int x, int y, BufferColor color) { if (InBounds(ref x, ref y)) { _buffer[y, x].ForeColor = color; } }
/// <summary> /// Draws a solid box with the specified dimensions to the buffer. /// </summary> /// <param name="x">The X coordinate of the box.</param> /// <param name="y">The Y coordinate of the box.</param> /// <param name="w">The width of the box.</param> /// <param name="h">The height of the box.</param> /// <param name="color">The color to draw the box with.</param> public void DrawBox(int x, int y, int w, int h, BufferColor color) { if (w < 0) { w *= -1; x -= w; } if (h < 0) { h *= -1; y -= h; } var b = _buffer; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { if (InBounds(x + i, y + j)) { b[y + j, x + i].BackColor = color; } } } }
/// <summary> /// Prints a string to the buffer with the specified attributes and alignment. /// </summary> /// <param name="x">The X coordinate to start printing at.</param> /// <param name="y">The Y coordinate to start printing at.</param> /// <param name="text">The string to print.</param> /// <param name="color">The color to assign to the text.</param> /// <param name="alignment">The alignment of the string.</param> public void DrawString(int x, int y, string text, BufferColor color, Alignment alignment = Alignment.Left) { var b = _buffer; string[] lines = text.Split(new[] { '\n' }); for (int i = 0; i < lines.Length; i++) { int len = lines[i].Length; for (int j = 0; j < len; j++) { switch (alignment) { case Alignment.Left: if (InBounds(x + j, y + i)) { b[y + i, x + j].CharData = lines[i][j]; b[y + i, x + j].ForeColor = color; } break; case Alignment.Right: if (InBounds(x - len + j, y + i)) { b[y + i, x - len + j].CharData = lines[i][j]; b[y + i, x - len + j].ForeColor = color; } break; } } } }
/// <summary> /// Sets the foreground color for a specific unit in the buffer. /// </summary> /// <param name="point">The location of the unit.</param> /// <param name="color">The foreground color to set the unit to.</param> public void SetUnitForeColor(Point point, BufferColor color) { if (!InBounds(ref point)) { return; } _buffer[point.Y, point.X].ForeColor = color; }
/// <summary> /// Sets the background color for a specific unit in the buffer. /// </summary> /// <param name="x">The X coordinate of the unit.</param> /// <param name="y">The Y coordinate of the unit.</param> /// <param name="color">The background color to set the unit to.</param> public void SetUnitBackColor(int x, int y, BufferColor color) { if (!InBounds(ref x, ref y)) { return; } _buffer[y, x].BackColor = color; }
/// <summary> /// Create a new Plane object. /// </summary> public Plane(BufferColor color) : base() { Faces = new Face[1] { new Face( color, new Triangle3f(new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, 1, 1)), new Triangle3f(new Vector3(-1, -1, 1), new Vector3(1, 1, 1), new Vector3(1, -1, 1)) ) }; }
/// <inheritdoc /> public void Dispose() { BufferColor.ForEach(o => o.Dispose()); BufferColor = default; BufferDepth.Dispose(); BufferDepth = default; Size = default; }
/// <summary> /// Clears all units in the buffer, optionally specifying a color to fill the buffer with. /// </summary> /// <param name="color">The color to fill the buffer with.</param> public unsafe void Clear(BufferColor color = BufferColor.Black) { fixed(BufferUnitInfo *b = _buffer) { for (int i = UnitCount; i >= 0; i--) { b[i].BackColor = color; b[i].ForeColor = color; b[i].CharData = '\0'; } } }
/// <summary> /// Draws a solid circle to the buffer with the specified attributes. /// </summary> /// <param name="x">The X position of the circle, relative to its center.</param> /// <param name="y">The Y position of the circle, relative to its center.</param> /// <param name="radius">The radius of the circle.</param> /// <param name="color">The color to draw the circle with.</param> public void DrawCircle(int x, int y, int radius, BufferColor color) { if (radius < 0) { radius *= -1; } int rr = radius * radius; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { if (i * i + j * j <= rr && InBounds(x + i, y + j)) { _buffer[y + j, x + i].BackColor = color; } } } }
/// <summary> /// Draws a circle with the specified radius, border thickness, and attributes for both border and fill. /// </summary> /// <param name="x">The X position of the circle, relative to its center.</param> /// <param name="y">The Y position of the circle, relative to its center.</param> /// <param name="radius">The radius of the circle.</param> /// <param name="thickness">The border thickness of the circle.</param> /// <param name="border">The border color for the circle.</param> /// <param name="fill">The fill color for the circle.</param> public void DrawCircle(int x, int y, int radius, int thickness, BufferColor border, BufferColor fill) { if (radius < 0) { radius *= -1; } if (thickness < 0) { thickness *= -1; } if (thickness > radius) { thickness = radius; } int rra = radius * radius; int rrb = (radius - thickness) * (radius - thickness); int d = 0; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { d = i * i + j * j; if (InBounds(x + i, y + j)) { if (d < rrb) { _buffer[y + j, x + i].BackColor = fill; } else if (d <= rra) { _buffer[y + j, x + i].BackColor = border; } } } } }
/// <summary> /// Draws a solid box with the specified dimensions to the buffer. /// </summary> /// <param name="x">The X coordinate of the box.</param> /// <param name="y">The Y coordinate of the box.</param> /// <param name="w">The width of the box.</param> /// <param name="h">The height of the box.</param> /// <param name="color">The color to draw the box with.</param> public void DrawBox(int x, int y, int w, int h, BufferColor color) { if (w < 0) { w *= -1; x -= w; } if (h < 0) { h *= -1; y -= h; } var b = _buffer; for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) { if (InBounds(x + i, y + j)) { b[y + j, x + i].BackColor = color; } } }
/// <summary> /// Clears all units in the buffer, optionally specifying a color to fill the buffer with. /// </summary> /// <param name="color">The color to fill the buffer with.</param> public unsafe void Clear(BufferColor color = BufferColor.Black) { fixed (BufferUnitInfo* b = _buffer) { for (int i = UnitCount; i >= 0; i--) { b[i].BackColor = color; b[i].ForeColor = color; b[i].CharData = '\0'; } } }
/// <summary> /// Draws a triangle to the buffer. /// </summary> /// <param name="a">The first point of the triangle.</param> /// <param name="b">The second point of the triangle.</param> /// <param name="c">The third point of the triangle.</param> /// <param name="color">The color of the line.</param> public void DrawTriangle(ref Point a, ref Point b, ref Point c, BufferColor color) { DrawLine(ref a, ref b, color); DrawLine(ref b, ref c, color); DrawLine(ref c, ref a, color); }
/// <summary> /// Draws a line to the buffer. /// </summary> /// <param name="x">The starting X coordinate of the line.</param> /// <param name="y">The starting Y coordinate of the line.</param> /// <param name="x2">The ending X coordinate of the line.</param> /// <param name="y2">The ending Y coordinate of the line.</param> /// <param name="color">The color of the line.</param> public void DrawLine(int x, int y, int x2, int y2, BufferColor color) { var b = _buffer; int w = x2 - x; int h = y2 - y; int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0; if (w < 0) dx1 = -1; else if (w > 0) dx1 = 1; if (h < 0) dy1 = -1; else if (h > 0) dy1 = 1; if (w < 0) dx2 = -1; else if (w > 0) dx2 = 1; int longest = Math.Abs(w); int shortest = Math.Abs(h); if (!(longest > shortest)) { longest = Math.Abs(h); shortest = Math.Abs(w); if (h < 0) dy2 = -1; else if (h > 0) dy2 = 1; dx2 = 0; } int numerator = longest >> 1; for (int i = 0; i <= longest; i++) { if (InBounds(ref x, ref y)) { b[y, x].BackColor = color; } numerator += shortest; if (!(numerator < longest)) { numerator -= longest; x += dx1; y += dy1; } else { x += dx2; y += dy2; } } }
/// <summary> /// Clears the active buffer to the specified attributes. /// </summary> /// <param name="clearColor">The attributes to fill the buffer with.</param> public static void Clear(BufferColor clearColor = BufferColor.Black) { ActiveBuffer.Clear(clearColor); }
/// <summary> /// Draws a colored line to the buffer. /// </summary> /// <param name="a">The starting point of the line.</param> /// <param name="b">The ending point of the line.</param> /// <param name="color">The color of the line.</param> public void DrawLine(ref Point a, ref Point b, BufferColor color) { DrawLine(a.X, a.Y, b.X, b.Y, color); }
/// <summary> /// Draws a triangle to the buffer. /// </summary> /// <param name="triangle">The triangle to draw.</param> /// <param name="color">The color to draw the triangle.</param> public void DrawTriangle(ref Triangle triangle, BufferColor color) { DrawLine(ref triangle.A, ref triangle.B, color); DrawLine(ref triangle.B, ref triangle.C, color); DrawLine(ref triangle.C, ref triangle.A, color); }
/// <summary> /// Draws a solid circle to the buffer with the specified attributes. /// </summary> /// <param name="x">The X position of the circle, relative to its center.</param> /// <param name="y">The Y position of the circle, relative to its center.</param> /// <param name="radius">The radius of the circle.</param> /// <param name="color">The color to draw the circle with.</param> public void DrawCircle(int x, int y, int radius, BufferColor color) { if (radius < 0) radius *= -1; int rr = radius * radius; for(int i = -radius; i <= radius; i++) for (int j = -radius; j <= radius; j++) { if (i * i + j * j <= rr && InBounds(x + i, y + j)) { _buffer[y + j, x + i].BackColor = color; } } }
/// <summary> /// Draws a line to the buffer. /// </summary> /// <param name="x">The starting X coordinate of the line.</param> /// <param name="y">The starting Y coordinate of the line.</param> /// <param name="x2">The ending X coordinate of the line.</param> /// <param name="y2">The ending Y coordinate of the line.</param> /// <param name="color">The color of the line.</param> public void DrawLine(int x, int y, int x2, int y2, BufferColor color) { var b = _buffer; int w = x2 - x; int h = y2 - y; int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0; if (w < 0) { dx1 = -1; } else if (w > 0) { dx1 = 1; } if (h < 0) { dy1 = -1; } else if (h > 0) { dy1 = 1; } if (w < 0) { dx2 = -1; } else if (w > 0) { dx2 = 1; } int longest = Math.Abs(w); int shortest = Math.Abs(h); if (!(longest > shortest)) { longest = Math.Abs(h); shortest = Math.Abs(w); if (h < 0) { dy2 = -1; } else if (h > 0) { dy2 = 1; } dx2 = 0; } int numerator = longest >> 1; for (int i = 0; i <= longest; i++) { if (InBounds(ref x, ref y)) { b[y, x].BackColor = color; } numerator += shortest; if (!(numerator < longest)) { numerator -= longest; x += dx1; y += dy1; } else { x += dx2; y += dy2; } } }
/// <summary> /// Draws a circle with the specified radius, border thickness, and attributes for both border and fill. /// </summary> /// <param name="x">The X position of the circle, relative to its center.</param> /// <param name="y">The Y position of the circle, relative to its center.</param> /// <param name="radius">The radius of the circle.</param> /// <param name="thickness">The border thickness of the circle.</param> /// <param name="border">The border color for the circle.</param> /// <param name="fill">The fill color for the circle.</param> public void DrawCircle(int x, int y, int radius, int thickness, BufferColor border, BufferColor fill) { if (radius < 0) radius *= -1; if (thickness < 0) thickness *= -1; if (thickness > radius) thickness = radius; int rra = radius * radius; int rrb = (radius - thickness) * (radius - thickness); int d = 0; for(int i = -radius; i <= radius; i++) for (int j = -radius; j <= radius; j++) { d = i * i + j * j; if (InBounds(x + i, y + j)) { if(d < rrb) { _buffer[y + j, x + i].BackColor = fill; } else if (d <= rra) { _buffer[y + j, x + i].BackColor = border; } } } }
/// <summary> /// Creates a new SolidBufferBrush of a specified color. /// </summary> /// <param name="color">The color of the brush.</param> public SolidBufferBrush(BufferColor color) { _color = color; }
/// <summary> /// Creates a new CheckeredBufferBrush with the specified colors. /// </summary> /// <param name="colorA">The first color in the pattern.</param> /// <param name="colorB">The second color in the pattern.</param> public CheckeredBufferBrush(BufferColor colorA, BufferColor colorB) { _ca = colorA; _cb = colorB; }
/// <summary> /// Sets the foreground color for a specific unit in the buffer. /// </summary> /// <param name="point">The location of the unit.</param> /// <param name="color">The foreground color to set the unit to.</param> public void SetUnitForeColor(Point point, BufferColor color) { if (!InBounds(ref point)) return; _buffer[point.Y, point.X].ForeColor = color; }
/// <summary> /// Flood fills a closed region containing the specified coordinates with a color. /// </summary> /// <param name="x">The X coordinate to begin filling at.</param> /// <param name="y">The Y coordinate to begin filling at.</param> /// <param name="color">The color to fill the region with.</param> public void FloodFill(int x, int y, BufferColor color) { if (!InBounds(x, y)) return; var initColor = _buffer[y, x].BackColor; if (color == initColor) return; List<Point> queue = new List<Point>(32); queue.Add(new Point(x, y)); Point p; int w, e, j; for (int i = 0; i < queue.Count; i++) { p = queue[i]; w = e = p.X; while(w - 1 >= 0) { if (_buffer[p.Y, w - 1].BackColor == initColor) { w--; } else { break; } } while (e + 1 < _width) { if (_buffer[p.Y, e + 1].BackColor == initColor) { e++; } else { break; } } for (j = w; j <= e; j++) { _buffer[p.Y, j].BackColor = color; if (p.Y + 1 < _height) { if (_buffer[p.Y + 1, j].BackColor == initColor) { queue.Add(new Point(j, p.Y + 1)); } } if (p.Y - 1 >= 0) { if (_buffer[p.Y - 1, j].BackColor == initColor) { queue.Add(new Point(j, p.Y - 1)); } } } } }
/// <summary> /// Flood fills a closed region containing the specified coordinates with a color. /// </summary> /// <param name="x">The X coordinate to begin filling at.</param> /// <param name="y">The Y coordinate to begin filling at.</param> /// <param name="color">The color to fill the region with.</param> public void FloodFill(int x, int y, BufferColor color) { if (!InBounds(x, y)) { return; } var initColor = _buffer[y, x].BackColor; if (color == initColor) { return; } List <Point> queue = new List <Point>(32); queue.Add(new Point(x, y)); Point p; int w, e, j; for (int i = 0; i < queue.Count; i++) { p = queue[i]; w = e = p.X; while (w - 1 >= 0) { if (_buffer[p.Y, w - 1].BackColor == initColor) { w--; } else { break; } } while (e + 1 < _width) { if (_buffer[p.Y, e + 1].BackColor == initColor) { e++; } else { break; } } for (j = w; j <= e; j++) { _buffer[p.Y, j].BackColor = color; if (p.Y + 1 < _height) { if (_buffer[p.Y + 1, j].BackColor == initColor) { queue.Add(new Point(j, p.Y + 1)); } } if (p.Y - 1 >= 0) { if (_buffer[p.Y - 1, j].BackColor == initColor) { queue.Add(new Point(j, p.Y - 1)); } } } } }
/// <summary> /// Create a new Face from one or more triangles. /// </summary> /// <param name="tris">One or more triangles.</param> public Face(BufferColor color, params Triangle3f[] tris) { this.Triangles = tris; this.Color = color; }
/// <summary> /// Sets the background color for a specific unit in the buffer. /// </summary> /// <param name="x">The X coordinate of the unit.</param> /// <param name="y">The Y coordinate of the unit.</param> /// <param name="color">The background color to set the unit to.</param> public void SetUnitBackColor(int x, int y, BufferColor color) { if (!InBounds(ref x, ref y)) return; _buffer[y, x].BackColor = color; }