This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.Drawing.cs
226 lines (203 loc) · 7.88 KB
/
Screen.Drawing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
namespace Engine;
public partial class Screen {
/// <summary>
/// Drawing with text line
/// </summary>
/// <param name="y">Vertical position</param>
/// <param name="x">Horizontal position</param>
/// <param name="message">Text line</param>
/// <param name="color">Text color</param>
/// <param name="backgroundColor">Background box color</param>
public static void SetText(int y, int x, string message,
ConsoleColor color = ConsoleColor.White,
ConsoleColor backgroundColor = ConsoleColor.Black) {
for(int ctn = 0; ctn < message.Length; ctn++) {
if (x+ctn < 0 || x+ctn >= WindowSize.X)
break;
SetPixel(y, x+ctn, message[ctn], color, backgroundColor);
}
}
/// <summary>
/// Drawing char symbol like screen pixel
/// </summary>
/// <param name="y">Vertical position</param>
/// <param name="x">Horizontal position</param>
/// <param name="symbol">Char symbol</param>
/// <param name="color">Symbol color</param>
/// <param name="backgroundColor">Background box color</param>
public static void SetPixel(int y, int x, char symbol,
ConsoleColor color = ConsoleColor.White,
ConsoleColor backgroundColor = ConsoleColor.Black) {
if (y >= WindowSize.Y || x >= WindowSize.X || y < 0 || x < 0)
return;
Buffer[GetPosition(y, x)] = new Pixel(symbol, color, backgroundColor);
}
/// <summary>
/// Drawing char symbol like screen pixel
/// </summary>
/// <param name="y">Vertical position</param>
/// <param name="x">Horizontal position</param>
/// <param name="pixel">Pixel what be drawen</param>
public static void SetPixel(int y, int x, Pixel pixel) {
if (y >= WindowSize.Y || x >= WindowSize.X || y < 0 || x < 0)
return;
Buffer[GetPosition(y, x)] = pixel;
}
public static void SetTexture(int y, int x, Texture tex) {
for (int xi = 0; xi < tex.Width; xi ++) {
for (int yi = 0; yi < tex.Height; yi ++) {
Pixel texPixel = tex.Buffer[Utility.GetPosition(tex, yi, xi)];
try {
SetPixel(y+yi, x+xi, texPixel);
} catch {
//nothing...
}
}
}
}
public static void Triangle(Point a, Point b, Point c, int color, char character = 'X') {
Triangle(a,b,c,color,0,character);
}
/// <summary> Draws a Triangle. </summary>
/// <param name="a">Point A.</param>
/// <param name="b">Point B.</param>
/// <param name="c">Point C.</param>
/// <param name="fgColor">Color to draw with.</param>
/// <param name="bgColor">Color to draw to the background with.</param>
/// <param name="character">Character to use.</param>
public static void Triangle(Point a, Point b, Point c, int fgColor,int bgColor, char character = 'X')
{
Line(a, b, fgColor,bgColor, character);
Line(b, c, fgColor, bgColor, character);
Line(c, a, fgColor, bgColor, character);
}
// Bresenhams Triangle Algorithm
/// <summary> Draws a Triangle and fills it, calls overloaded method with background as bgColor </summary>
/// <param name="a">Point A.</param>
/// <param name="b">Point B.</param>
/// <param name="c">Point C.</param>
/// <param name="color">Color to draw with.</param>
/// <param name="character">Character to use.</param>
public static void FillTriangle(Point a, Point b, Point c, int color, char character = 'X') {
FillTriangle(a, b, c, color, 0, character);
}
/// <summary> Draws a Triangle and fills it. </summary>
/// <param name="a">Point A.</param>
/// <param name="b">Point B.</param>
/// <param name="c">Point C.</param>
/// <param name="fgColor">Color to draw with.</param>
/// <param name="bgColor">Color to draw to the background with.</param>
/// <param name="character">Character to use.</param>
public static void FillTriangle(Point a, Point b, Point c, int fgColor, int bgColor, char character = 'X')
{
Point min = new Point(Math.Min(Math.Min(a.X, b.X), c.X), Math.Min(Math.Min(a.Y, b.Y), c.Y));
Point max = new Point(Math.Max(Math.Max(a.X, b.X), c.X), Math.Max(Math.Max(a.Y, b.Y), c.Y));
Point p = new Point();
for(p.Y = min.Y; p.Y < max.Y; p.Y++) {
for(p.X = min.X; p.X < max.X; p.X++) {
int w0 = Orient(b, c, p);
int w1 = Orient(c, a, p);
int w2 = Orient(a, b, p);
if(w0 >= 0 && w1 >= 0 && w2 >= 0) SetPixel(p.Y, p.X, character, (ConsoleColor)fgColor, (ConsoleColor)bgColor);
}
}
}
private static int Orient(Point a, Point b, Point c) {
return ((b.X - a.X) * (c.Y - a.Y)) - ((b.Y - a.Y) * (c.X - a.X));
}
// Bresenhams Line Algorithm
// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
/// <summary> Draws a line from start to end. (Bresenhams Line), calls overloaded method with background as bgColor </summary>
/// <param name="start">Point to draw line from.</param>
/// <param name="end">Point to end line at.</param>
/// <param name="color">Color to draw with.</param>
/// <param name="c">Character to use.</param>
public static void Line(Point start, Point end, int color, char c = 'X') {
Line(start, end, color, 0, c);
}
// Bresenhams Line Algorithm
// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
/// <summary> Draws a line from start to end. (Bresenhams Line) </summary>
/// <param name="start">Point to draw line from.</param>
/// <param name="end">Point to end line at.</param>
/// <param name="fgColor">Color to draw with.</param>
/// <param name="bgColor">Color to draw the background with.</param>
/// <param name="c">Character to use.</param>
public static void Line(Point start, Point end, int fgColor,int bgColor, char c = 'X')
{
Point delta = end - start;
Point da = Point.Zero, db = Point.Zero;
if(delta.X < 0) da.X = -1; else if(delta.X > 0) da.X = 1;
if(delta.Y < 0) da.Y = -1; else if(delta.Y > 0) da.Y = 1;
if(delta.X < 0) db.X = -1; else if(delta.X > 0) db.X = 1;
int longest = Math.Abs(delta.X);
int shortest = Math.Abs(delta.Y);
if(!(longest > shortest)) {
longest = Math.Abs(delta.Y);
shortest = Math.Abs(delta.X);
if(delta.Y < 0) db.Y = -1; else if(delta.Y > 0) db.Y = 1;
db.X = 0;
}
int numerator = longest >> 1;
Point p = new Point(start.X, start.Y);
for(int i = 0; i <= longest; i++) {
SetPixel(p.Y, p.X, c, (ConsoleColor)fgColor, (ConsoleColor)bgColor);
numerator += shortest;
if(!(numerator < longest)) {
numerator -= longest;
p += da;
}
else {
p += db;
}
}
}
}
public struct Texture {
public int Width {get;}
public int Height {get;}
public Pixel[] Buffer {get;}
public Texture(int _height, int _width) {
Buffer = new Pixel[_height * _width];
Width = _width;
Height = _height;
}
public Texture(int _height, int _width, string texinfo) {
Buffer = new Pixel[_height * _width];
Width = _width;
Height = _height;
SetTexture(texinfo);
}
public void SetTexture(string texinfo) {
for (int xi = 0; xi < Width; xi ++) {
for (int yi = 0; yi < Height; yi ++) {
Pixel texPixel = new Pixel(texinfo[Utility.GetPosition(this, yi, xi)]);
Buffer[Utility.GetPosition(this, yi, xi)] = texPixel;
}
}
}
// TODO: Refactor and make better way to make color
public void SetColor(ConsoleColor color = ConsoleColor.White, ConsoleColor background = ConsoleColor.Black) {
for (int xi = 0; xi < Width; xi ++) {
for (int yi = 0; yi < Height; yi ++) {
Buffer[Utility.GetPosition(this, yi, xi)].Color = color;
Buffer[Utility.GetPosition(this, yi, xi)].BackgroundColor = background;
}
}
}
}
public class Color {
/// <summary> Red component. </summary>
public uint R { get; set; }
/// <summary> Green component. </summary>
public uint G { get; set; }
/// <summary> Bkue component. </summary>
public uint B { get; set; }
/// <summary> Creates a new Color from rgb. </summary>
public Color(int r, int g, int b) {
this.R = (uint)r;
this.G = (uint)g;
this.B = (uint)b;
}
}