-
Notifications
You must be signed in to change notification settings - Fork 8
/
console.c
239 lines (193 loc) · 4.27 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
#include "console.h"
#ifdef _WIN32
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <wtypes.h>
#include <wincon.h>
#include <locale.h>
short color_attrs[CON_MAX_COLORS]; // - like ncurses color pairs
void con_init()
{
setlocale( LC_ALL, ".OCP" );
con_showCursor();
}
void con_deinit()
{
;
}
#define CONSOLE_CURSOR_SIZE 25
static void con_setCursor(int show) {
CONSOLE_CURSOR_INFO ci = {CONSOLE_CURSOR_SIZE, show};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(hStdOut, &ci);
}
void con_showCursor()
{
con_setCursor(TRUE);
}
void con_hideCursor()
{
con_setCursor(FALSE);
}
int con_gotoXY(int x, int y)
{
COORD pos = { x, y };
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
return SetConsoleCursorPosition(hStdOut, pos);
}
int con_getXY(int *px, int *py)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
*px = csbi.dwCursorPosition.X;
*py = csbi.dwCursorPosition.Y;
return 1;
}
return 0; // error
}
int con_getMaxXY(int *px, int *py)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
*px = csbi.srWindow.Right - csbi.srWindow.Left;
*py = csbi.srWindow.Bottom - csbi.srWindow.Top;
return 1;
}
return 0; // error
}
int con_clearScr()
{
int mx, my;
if (con_getMaxXY(&mx, &my))
{
COORD szBuf = {mx + 1, my + 1};
COORD BufferCoord= {0, 0};
SMALL_RECT srWriteRegion = {0, 0, mx, my};
int nResult;
CHAR_INFO *lpBuffer = (CHAR_INFO *)calloc(szBuf.X * szBuf.Y, sizeof(CHAR_INFO));
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
nResult = WriteConsoleOutput(hStdOut, lpBuffer, szBuf, BufferCoord, &srWriteRegion);
free(lpBuffer);
return nResult;
}
return 0; // error
}
int con_outTxt(const char * format , ...)
{
va_list arglist;
char buffer[1024];
int len;
va_start(arglist, format);
assert(format);
len = _vsnprintf(buffer, sizeof(buffer)-1, format, arglist);
assert(len >= 0);
return _cputs(buffer) == 0 ? len : -1;
}
int con_keyPressed()
{
return _kbhit();
}
int con_getKey()
{
int ch = _getch();
if (ch == 0x00 || ch == 0xE0) // extended code
return - _getch();
else
return ch;
}
int con_initPair(short n, short fg, short bg)
{
if (n < 0 || n > CON_MAX_COLORS)
return 0;
color_attrs[n] = fg | FOREGROUND_INTENSITY | (bg << 4);
return 1;
}
int con_setColor(short n)
{
HANDLE hStdOut;
if (n < 0 || n > CON_MAX_COLORS)
return 0;
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
return SetConsoleTextAttribute(hStdOut, color_attrs[n]);
}
#else // Unix-based OS
#include <stdarg.h>
void con_init()
{
initscr();
noecho();
cbreak();
nodelay(stdscr, TRUE);
keypad(stdscr, TRUE);
start_color();
attron(A_BOLD);
con_showCursor();
}
void con_deinit()
{
endwin();
}
void con_showCursor()
{
curs_set(1);
}
void con_hideCursor()
{
curs_set(0);
}
int con_gotoXY(int x, int y)
{
return move(y, x);
}
int con_getXY(int *px, int *py)
{
return getyx(stdscr, *py, *px);
}
int con_getMaxXY(int *px, int *py)
{
return getmaxyx(stdscr, *py, *px);
}
int con_clearScr()
{
clear();
return refresh();
}
int con_outTxt(const char *format, ...)
{
va_list arglist;
char buffer[1024];
int len;
va_start(arglist, format);
len = vsnprintf(buffer, sizeof(buffer)-1, format, arglist);
len = (printw(buffer) != ERR) ? len : -1;
refresh();
return len;
}
int con_keyPressed()
{
int ch = getch();
if (ch != ERR)
{
ungetch(ch);
return 1;
}
return 0;
}
int con_getKey()
{
return getch();
}
int con_initPair(short n, short fg, short bg)
{
return init_pair(n, fg, bg);
}
int con_setColor(short n)
{
return attron(COLOR_PAIR(n));
}
#endif