-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastConsole.cs
More file actions
153 lines (117 loc) · 5.09 KB
/
FastConsole.cs
File metadata and controls
153 lines (117 loc) · 5.09 KB
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
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace BreadEngine {
public struct ConsoleChar {
public ConsoleColor ForeGroundColor;
public ConsoleColor BackGroundColor;
public char character;
// public ConsoleChar(){
// ForeGroundColor = ConsoleColor.White;
// BackGroundColor = ConsoleColor.Black;
// character = ' ';
// }
public ConsoleChar(char character, ConsoleColor ForeGroundColor,ConsoleColor BackGroundColor){
this.ForeGroundColor = ForeGroundColor;
this.BackGroundColor = BackGroundColor;
this.character = character;
}
public override bool Equals(object obj) {
if (obj.GetType() == typeof(ConsoleChar)) {
ConsoleChar c = (ConsoleChar)obj;
return (c.ForeGroundColor == ForeGroundColor) && (c.BackGroundColor == BackGroundColor) && (c.character == character);
} else {
return false;
}
}
public override int GetHashCode() {
return base.GetHashCode();
}
}
public class FastConsole {
public static ConsoleChar backgroundChar = new ConsoleChar(' ', ConsoleColor.White, ConsoleColor.Black);
static ConsoleChar[] currentBuffer;
static ConsoleChar[] buffer;
private static int offset = 0;
//private static readonly BufferedStream outputStream;
//private static StreamWriter streamWriter;
public static int Width{
get{ return Console.WindowWidth; }
}
public static int Height{
get{ return Console.WindowHeight; }
}
static FastConsole(){
Console.OutputEncoding = System.Text.Encoding.Unicode;
Console.CursorVisible = false;
//outputStream = new BufferedStream(Console.OpenStandardOutput(), Width * Height);
// streamWriter = new StreamWriter(Console.OpenStandardOutput(), Encoding.Unicode, Width * Height * 4);
// streamWriter.AutoFlush = false;
currentBuffer = Enumerable.Repeat(new ConsoleChar(), Width * Height).ToArray();
Clear();
}
public static void SetCursor(int x, int y){
offset = y * Width + x;
//Console.SetCursorPosition(x,y);
}
public static void Write(char c, ConsoleColor ForeGroundColor = ConsoleColor.White, ConsoleColor BackGroundColor = ConsoleColor.Black){
// Write(c.ToString());
// Console.ForegroundColor = ConsoleColor.Blue;
// Console.Write(c);
// Console.BackgroundColor = ConsoleColor.Red;
// Console.SetCursorPosition(offset % Width, offset / Width);
// x: offset % Width; y: offset / Width
// Console.ResetColor();
//0,0 0,1 0,2 0,3 0 1 2 3
//1,0 1,1 1,2 1,3 4 5 6 7
//2,0 2,1 2,2 2,3 8 9 11 12
buffer[offset++] = new ConsoleChar(c, ForeGroundColor, BackGroundColor);
//Write(""+c);
//Console.Write(c);
}
public static void Write(string s, ConsoleColor ForeGroundColor = ConsoleColor.White, ConsoleColor BackGroundColor = ConsoleColor.Black){
if(s == null){
return;
}
for (int i = 0; i < s.Length; i++) {
buffer[offset++] = new ConsoleChar(s[i], ForeGroundColor, BackGroundColor);
}
// offset += s.Length;
// var rgb = new byte[s.Length << 1];
// Encoding.Unicode.GetBytes(s, 0, s.Length, rgb, 0);
// lock (outputStream) { // (optional, can omit if appropriate)
// Console.WriteLine($"offset: {offset}; length: ");
// outputStream.Write(rgb, offset, rgb.Length);
// offset += rgb.Length;
// }
//Console.Write(s);
}
public static void Clear(){
//Console.Clear();
buffer = Enumerable.Repeat(backgroundChar, Width * Height).ToArray();
offset = 0;
}
public static ConsoleKeyInfo ReadKey(){
return Console.ReadKey(true);
}
public static void Flush() {
Console.SetCursorPosition(0,0);
for (int i = 0; i < buffer.Length; i++) {
if(!currentBuffer[i].Equals(buffer[i])){
Console.SetCursorPosition(i % Width, i / Width);
Console.ForegroundColor = buffer[i].ForeGroundColor;
Console.BackgroundColor = buffer[i].BackGroundColor;
Console.Write(buffer[i].character);
}
}
currentBuffer = buffer;
//Console.Clear();
//Console.Write('x');
// byte[] bytes = Encoding.Unicode.GetBytes(buffer, 0, buffer.Length);
}
}
}