-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.c
285 lines (235 loc) · 7.16 KB
/
Console.c
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/****** Console.c *************************************************************
*
* CADEC -- Copyright (C) 1997 Bill Farmer
*
* Emulation of PDP11 based telecontrol system
* on standard PC hardware.
*
*****************************************************************************/
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include "Console.h"
// Borland console functions, API is in conio.h in Embarcadero C++ 10.2
HANDLE hConsoleOutput, hConsoleInput;
// textmode
void textmode(int screenmode)
{
CONSOLE_SCREEN_BUFFER_INFO info;
COORD size;
DWORD mode;
// Do it once
if (hConsoleOutput == NULL)
{
// Disable Ctrl-C
SetConsoleCtrlHandler(NULL, TRUE);
// Get handles
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
hConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
// Update console mode
GetConsoleMode(hConsoleOutput, &mode);
SetConsoleMode(hConsoleOutput, mode | ENABLE_PROCESSED_OUTPUT);
GetConsoleMode(hConsoleInput, &mode);
SetConsoleMode(hConsoleInput, mode & !ENABLE_ECHO_INPUT);
// Set code page
SetConsoleOutputCP(437);
}
switch (screenmode)
{
case C80:
// Resize window
size.X = C80_BUFFER_WIDTH;
size.Y = C80_BUFFER_HEIGHT;
SetConsoleScreenBufferSize(hConsoleOutput, size);
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
info.srWindow.Right = info.srWindow.Left + C80_SCREEN_WIDTH;
info.srWindow.Bottom = info.srWindow.Top + C80_SCREEN_HEIGHT;
SetConsoleWindowInfo(hConsoleOutput, TRUE, &info.srWindow);
break;
case C50:
// Resize window
size.X = C50_BUFFER_WIDTH;
size.Y = C50_BUFFER_HEIGHT;
SetConsoleScreenBufferSize(hConsoleOutput, size);
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
info.srWindow.Right = info.srWindow.Left + C50_SCREEN_WIDTH;
info.srWindow.Bottom = info.srWindow.Top + C50_SCREEN_HEIGHT;
SetConsoleWindowInfo(hConsoleOutput, TRUE, &info.srWindow);
break;
}
}
void textcolor(int f, int b)
{
SetConsoleTextAttribute(hConsoleOutput, f + b * BACKGROUND_BLUE);
}
void gotoxy(int x, int y)
{
COORD posn = {x + 1, y};
SetConsoleCursorPosition(hConsoleOutput, posn);
}
void clreol()
{
CONSOLE_SCREEN_BUFFER_INFO info;
DWORD n;
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
FillConsoleOutputAttribute(hConsoleOutput, info.wAttributes,
info.dwSize.X - info.dwCursorPosition.X - 1,
info.dwCursorPosition, &n);
FillConsoleOutputCharacter(hConsoleOutput, ' ',
info.dwSize.X - info.dwCursorPosition.X - 1,
info.dwCursorPosition, &n);
}
void clrscr()
{
CONSOLE_SCREEN_BUFFER_INFO info;
COORD posn = {0, 0};
DWORD n;
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
FillConsoleOutputAttribute(hConsoleOutput, info.wAttributes,
info.dwSize.X * info.dwSize.Y,
posn, &n);
FillConsoleOutputCharacter(hConsoleOutput, ' ',
info.dwSize.X * info.dwSize.Y,
posn, &n);
}
int cputs(char *s)
{
DWORD n;
return WriteConsole(hConsoleOutput, s, strlen(s), &n, NULL);
}
int putch(int c)
{
char s[2];
s[0] = c;
s[1] = '\0';
return cputs(s);
}
int cprintf(char *f, ...)
{
char s[96];
va_list args;
va_start(args, f);
vsprintf(s, f, args);
return cputs(s);
}
int kbhit()
{
INPUT_RECORD record;
DWORD n;
// Wait for something to happen
WaitForSingleObject(hConsoleInput, TIMER);
GetNumberOfConsoleInputEvents(hConsoleInput, &n);
if (n > 0)
{
// Ignore unwanted events
PeekConsoleInput(hConsoleInput, &record, 1, &n);
if ((record.EventType != KEY_EVENT) ||
(record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT) ||
(record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL) ||
(record.Event.KeyEvent.wVirtualKeyCode == VK_MENU) ||
(record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL) ||
// And key up events
(!record.Event.KeyEvent.bKeyDown))
ReadConsoleInput(hConsoleInput, &record, 1, &n);
// Update the event count
GetNumberOfConsoleInputEvents(hConsoleInput, &n);
}
return n;
}
void _setcursortype(int type)
{
CONSOLE_CURSOR_INFO info;
switch (type)
{
case _NOCURSOR:
info.bVisible = FALSE;
break;
case _SOLIDCURSOR:
info.dwSize = 100;
info.bVisible = TRUE;
break;
case _NORMALCURSOR:
info.dwSize = 20;
info.bVisible = TRUE;
break;
}
SetConsoleCursorInfo(hConsoleOutput, &info);
}
int getch()
{
INPUT_RECORD record;
static BOOL special;
DWORD n;
// If the char is zero, return zero, but don't read event
PeekConsoleInput(hConsoleInput, &record, 1, &n);
if (!special && record.Event.KeyEvent.uChar.AsciiChar == 0)
{
special = TRUE;
return 0;
}
// Read the event
ReadConsoleInput(hConsoleInput, &record, 1, &n);
// Not special, return the char
if (!special)
return record.Event.KeyEvent.uChar.AsciiChar;
special = FALSE;
switch (record.Event.KeyEvent.dwControlKeyState &
(SHIFT_PRESSED | LEFT_ALT_PRESSED))
{
// Special, return key code
case 0:
return record.Event.KeyEvent.wVirtualKeyCode;
// Special, shift pressed, return key code plus shift
case SHIFT_PRESSED:
return record.Event.KeyEvent.wVirtualKeyCode + VK_SHIFT;
// Special, alt pressed, return key code plus shift
case LEFT_ALT_PRESSED:
return record.Event.KeyEvent.wVirtualKeyCode + VK_SHIFT * 2;
// Special, shift and alt pressed, return key code plus shift
case SHIFT_PRESSED | LEFT_ALT_PRESSED:
return record.Event.KeyEvent.wVirtualKeyCode + VK_SHIFT * 3;
}
}
int wherex()
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
return info.dwCursorPosition.X - 1;
}
int wherey()
{
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
return info.dwCursorPosition.Y;
}
int screenchar()
{
u_char c;
DWORD n;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hConsoleOutput, &info);
ReadConsoleOutputCharacter(hConsoleOutput, &c, 1,
info.dwCursorPosition, &n);
return c;
}
int movetext(int left, int top, int right, int bottom,
int destleft, int desttop)
{
DWORD n;
WORD attr;
COORD source = {left + 1, top};
COORD dest = {destleft + 1, desttop };
SMALL_RECT scroll = {left + 1, top, right + 1, bottom};
CHAR_INFO info = {' ', attr};
ReadConsoleOutputAttribute(hConsoleOutput, &attr, 1, source, &n);
return ScrollConsoleScreenBuffer(hConsoleOutput, &scroll, NULL,
dest, &info);
}
void randomize()
{
srand(time(NULL));
}
int getrandom(int n)
{
return (n == 0)? 0: rand() % n;
}