-
Notifications
You must be signed in to change notification settings - Fork 1
/
wincon.cpp
159 lines (146 loc) · 3.28 KB
/
wincon.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
#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <ctype.h>
#include "metrics.h"
void setWindowSize()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
GetConsoleScreenBufferInfo(h, &bufferInfo);
SMALL_RECT& winInfo = bufferInfo.srWindow;
COORD windowSize = { (SHORT)(winInfo.Right - winInfo.Left + 1), (SHORT)(winInfo.Bottom - winInfo.Top + 1) };
if (windowSize.X > COLS || windowSize.Y > ROWS)
{
SMALL_RECT info =
{
0,
0,
(SHORT)(COLS < windowSize.X ? COLS - 1 : windowSize.X - 1),
(SHORT)(ROWS < windowSize.Y ? ROWS - 1 : windowSize.Y - 1)
};
SetConsoleWindowInfo(h, TRUE, &info);
}
COORD size = { COLS, ROWS };
SetConsoleScreenBufferSize(h, size);
SMALL_RECT info = { 0, 0, COLS - 1, ROWS - 1 };
SetConsoleWindowInfo(h, TRUE, &info);
SetConsoleTitleA("Softporn Adventure");
}
void clearScreen()
{
COORD tl = { 0,0 };
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &s);
DWORD written, cells = s.dwSize.X * s.dwSize.Y;
FillConsoleOutputCharacter(console, ' ', cells, tl, &written);
FillConsoleOutputAttribute(console, REGULARCOLOR, cells, tl, &written);
SetConsoleCursorPosition(console, tl);
}
void setPos(int x, int y)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = { (short)x, (short)y };
SetConsoleCursorPosition(console, coord);
}
void getPos(int *x, int *y)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi = { 0 };
GetConsoleScreenBufferInfo(console, &csbi);
if (x != NULL) *x = csbi.dwCursorPosition.X;
if (y != NULL) *y = csbi.dwCursorPosition.Y;
}
void setColor(int color)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, (short)color);
}
void writeHeader(int line, const char* leftText, const char* rightText)
{
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
int x, y;
CONSOLE_SCREEN_BUFFER_INFO csbi = { 0 };
GetConsoleScreenBufferInfo(console, &csbi);
getPos(&x, &y);
SetConsoleTextAttribute(console, HEADERCOLOR);
setPos(0, line);
for (int i = 0; i < COLS; i++)
putchar(' ');
if (leftText != NULL)
{
setPos(1, line);
puts(leftText);
}
if (rightText != NULL)
{
setPos(COLS - 1 - (int)strlen(rightText), 0);
puts(rightText);
}
SetConsoleTextAttribute(console, REGULARCOLOR);
setPos(x, y);
}
void delay(int ms)
{
Sleep(ms);
}
char getKey()
{
char answer = (char)toupper(_getche());
puts("");
return answer;
}
char getKeySilent()
{
char answer = (char)toupper(_getch());
return answer;
}
char getOneOf(char key1, char key2, char key3)
{
key1 = (char)toupper(key1);
key2 = (char)toupper(key2);
key3 = (char)toupper(key3);
loop:
char answer = (char)toupper(_getch());
if (answer == key1 || answer == key2 || (key3 != 0 && answer == key3))
{
_putch(answer);
puts("");
return answer;
}
else
{
Beep(1000, 10);
goto loop;
}
}
char* getString(char* buffer, int max)
{
int x, y;
size_t r = 0;
getPos(&x, &y);
if (y >= ROWS - 1)
{
puts("");
y--;
}
do
{
#ifdef HAVESAFE
setPos(x, y);
_cgets_s(buffer, max, &r);
#if _WIN32
char hack[4];
size_t hr = 0;
_cgets_s(hack, 4, &hr);
#endif
#else
gets(buffer);
r = strlen(buffer);
#endif
if (r == 0)
Beep(1000, 10);
} while (r == 0);
return buffer;
}