-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.windows.cpp
executable file
·263 lines (222 loc) · 7.76 KB
/
console.windows.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//Win32
#include <iostream>
#include <Windows.h>
#include "console.h"
#include <conio.h>
#include <stdio.h>
#include <bitset>
//DLL Creation...
//#if (defined DLLEXPORT && defined __WIN32)
//extern "C" {
//#define CONSOLE __declspec(dllimport)
//#define CONSOLE __declspec(dllexport)
//}
//#endif
//Define constructor
console::constructor console::cons;
HANDLE console::conHandle;
HANDLE console::inHandle;
HANDLE console::ogConHandle;
color_t console::_activeColor;
bool console::ready;
console::constructor::constructor() {
setlocale(LC_ALL, "");
console::ogConHandle = GetStdHandle(STD_OUTPUT_HANDLE);
console::conHandle = CreateConsoleScreenBuffer(0x80000000U | 0x40000000U, 0, 0, 0x00000001, 0);
console::_activeColor = BBLACK | FWHITE;
#ifndef DEBUG
{
SetConsoleActiveScreenBuffer(conHandle);
SetConsoleActiveScreenBuffer(GetStdHandle(STD_INPUT_HANDLE));
}
#endif
inHandle = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(inHandle, ENABLE_WINDOW_INPUT);
ready = true;
}
console::constructor::~constructor() {
SetConsoleActiveScreenBuffer(ogConHandle);
ready = false;
}
void console::sleep(int millis) {
Sleep(millis);
}
int console::getImage() {
return IMAGE_WINDOWS;
}
void console::write(int x, int y, std::string& str) {
int length = str.length();
//CHAR_INFO framebuffer[length]; //WTF CL.EXE
CHAR_INFO* framebuffer = (CHAR_INFO*)alloca(sizeof(CHAR_INFO) * length);
for (int i = 0; i < length; i++) {
framebuffer[i].Char.UnicodeChar = str[i];
framebuffer[i].Attributes = console::_activeColor;
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
SMALL_RECT srwin = { short(x), short(y), short(x + length - 1), short(y) };
WriteConsoleOutput(console::conHandle, framebuffer, csbi.dwSize, {0,0}, &srwin);
}
void console::write(int x, int y, const char* str) {
int length = strlen(str);
CHAR_INFO* framebuffer = (CHAR_INFO*)alloca(sizeof(CHAR_INFO) * length);
for (int i = 0; i < length; i++) {
framebuffer[i].Char.UnicodeChar = str[i];
framebuffer[i].Attributes = console::_activeColor;
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
SMALL_RECT srwin = { short(x), short(y), short(x + length - 1), short(y) };
WriteConsoleOutput(console::conHandle, framebuffer, csbi.dwSize, {0,0}, &srwin);
}
void console::write(int x, int y, const char* str, color_t color) {
console::setConsoleColor(color);
console::write(x,y,str);
}
void console::write(int x, int y, std::string& str, color_t color) {
console::setConsoleColor(color);
console::write(x,y,str);
}
int console::getConsoleWidth() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
return csbi.dwSize.X;
}
int console::getConsoleHeight() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
return csbi.dwSize.Y;
}
void console::clear() {
COORD topLeft = { 0, 0 };
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD written;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
FillConsoleOutputCharacterA(console::conHandle, ' ', csbi.dwSize.X * csbi.dwSize.Y, topLeft, &written);
FillConsoleOutputAttribute(console::conHandle, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, csbi.dwSize.X * csbi.dwSize.Y, topLeft, &written);
SetConsoleCursorPosition(console::conHandle, topLeft);
}
void console::setConsoleColor(color_t color) {
if (console::_activeColor == _getCharInfoColor(color)) {
return;
}
console::_activeColor = _getCharInfoColor(color);
SetConsoleTextAttribute(console::conHandle, console::_getCharInfoColor(color));
}
//Cyan and yellow are flipped with windows
color_t console::_getCharInfoColor(color_t color) {
if ((color & FWHITE) == FCYAN)
color = (color & ~FWHITE) | FYELLOW;
else
if ((color & FWHITE) == FYELLOW)
color = (color & ~FWHITE) | FCYAN;
if ((color & BWHITE) == BCYAN)
color = (color & ~BWHITE) | BYELLOW;
else
if ((color & BWHITE) == BYELLOW)
color = (color & ~BWHITE) | BCYAN;
return color;
}
int console::readKeyAsync() {
INPUT_RECORD irec;
KEY_EVENT_RECORD krec;
DWORD n;
DWORD c;
GetNumberOfConsoleInputEvents(console::inHandle, &c);
if (c > 0) {
ReadConsoleInput(console::inHandle, &irec, 1, &n);
if (irec.EventType == KEY_EVENT && ((KEY_EVENT_RECORD&)irec.Event).bKeyDown) {
char asciiChar;
char modifiers = irec.Event.KeyEvent.dwControlKeyState;
krec = (KEY_EVENT_RECORD&)irec.Event;
asciiChar = krec.uChar.AsciiChar;
if (asciiChar < 128 && asciiChar > 31)
return asciiChar | (modifiers << 24);
return irec.Event.KeyEvent.wVirtualKeyCode | (modifiers << 24);
}
}
return 0;
}
int console::readKey() {
INPUT_RECORD irec;
KEY_EVENT_RECORD krec;
DWORD n;
while (true) {
ReadConsoleInput(console::inHandle, &irec, 1, &n);
if (irec.EventType == KEY_EVENT && ((KEY_EVENT_RECORD&)irec.Event).bKeyDown) {
int asciiChar;
char modifiers = irec.Event.KeyEvent.dwControlKeyState;
krec = (KEY_EVENT_RECORD&)irec.Event;
asciiChar = krec.uChar.AsciiChar;
std::string p0 = std::bitset<8>(modifiers).to_string();
std::string p1 = std::bitset<8>(asciiChar).to_string();
std::string p2 = std::bitset<32>(irec.Event.KeyEvent.wVirtualKeyCode).to_string();
fprintf(stderr, "mod:%i asc:%c bs:%s as:%s %s\r\n", modifiers, asciiChar, p0.c_str(), p1.c_str(), p2.c_str());
if (asciiChar < 128 && asciiChar > 31)
return asciiChar | (modifiers << 24);
return irec.Event.KeyEvent.wVirtualKeyCode | (modifiers << 24);
}
}
return 0;
}
void console::write(char* fb, color_t* cb, int length) {
CHAR_INFO* framebuffer = (CHAR_INFO*)alloca(sizeof(CHAR_INFO) * length);
wchar_t b;
for (int i = 0; i < length; i++) {
b = fb[i];
b &= 0x00FF;
framebuffer[i].Char.UnicodeChar = b;
framebuffer[i].Attributes = _getCharInfoColor(cb[i]);
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
SMALL_RECT srwin = { 0, 0, short(csbi.dwSize.X - 1), csbi.dwSize.Y };
WriteConsoleOutput(console::conHandle, framebuffer, csbi.dwSize, {0,0}, &srwin);
}
void console::write(wchar_t* fb, color_t* cb, int length) {
{
CHAR_INFO *framebuffer = (CHAR_INFO*)alloca(length * sizeof(CHAR_INFO));
for (int i = 0; i < length; i++) {
framebuffer[i].Char.UnicodeChar = fb[i];
framebuffer[i].Attributes = _getCharInfoColor(cb[i]);
}
console::write(&framebuffer[0], length);
}
}
void console::write(CHAR_INFO* fb, int length) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
SMALL_RECT srwin = { 0, 0, short(csbi.dwSize.X - 1), csbi.dwSize.Y };
WriteConsoleOutput(console::conHandle, fb, csbi.dwSize, {0,0}, &srwin);
}
void console::write(int x, int y, char character, color_t color) {
console::setConsoleColor(color);
console::write(x, y, character);
}
void console::write(int x, int y, char character) {
console::setCursorPosition(x, y);
DWORD written;
WriteConsole(console::conHandle, (const char*)&character, 1, &written, NULL);
}
void console::write(int x, int y, wchar_t character, color_t color) {
console::setConsoleColor(color);
console::write(x, y, character);
}
void console::write(int x, int y, wchar_t character) {
console::setCursorPosition(x, y);
DWORD written;
WriteConsole(console::conHandle, (const wchar_t*)&character, 1, &written, NULL);
}
void console::setCursorPosition(int x, int y) {
SetConsoleCursorPosition(console::conHandle, { short(x), short(y) });
}
void console::setCursorLeft(int x) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
SetConsoleCursorPosition(console::conHandle, { short(x), csbi.dwCursorPosition.Y });
}
void console::setCursorTop(int y) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(console::conHandle, &csbi);
SetConsoleCursorPosition(console::conHandle, { csbi.dwCursorPosition.X, short(y) });
}