-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.cpp
267 lines (214 loc) · 4.62 KB
/
console.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
264
265
266
267
#ifdef _WIN32
#include <Windows.h>
#include <conio.h>
#endif
#ifdef __APPLE__
#include <ncurses.h>
#include <unistd.h>
#endif
#include "console.h"
#include <chrono>
#include <cstdio>
#include <vector>
namespace console {
template <typename T> using Vec = std::vector<T>;
using String = std::string;
// Cross-platform codes
#ifdef _WIN32
void sleep(int milliseconds) { Sleep(milliseconds); }
void setCursorPosition(int x, int y) {
COORD coord = {(short)x, (short)y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Key key() {
if (!_kbhit())
return K_NONE;
int c = _getch();
// 방향키
if (c == 224) {
c = _getch();
switch (c) {
case 75:
return K_LEFT;
case 77:
return K_RIGHT;
case 72:
return K_UP;
case 80:
return K_DOWN;
}
}
if (c == 27)
return K_ESC;
if (c == 13)
return K_ENTER;
return K_OTHER;
}
void cls() { system("cls"); }
void setCodePage() {
system("chcp 65001"); // UTF-8
system("cls");
}
#endif
#ifdef __APPLE__
void sleep(int milliseconds) { usleep(milliseconds * 1000); }
void setCursorPosition(int x, int y) { printf("\033[%d;%dH", y + 1, x + 1); }
int kbhit(void) {
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
Key key() {
if (!kbhit())
return K_NONE;
int c = getch();
// 방향키
if (c == 27) {
c = getch();
if (c == 91) {
c = getch();
switch (c) {
case 68:
return K_LEFT;
case 67:
return K_RIGHT;
case 65:
return K_UP;
case 66:
return K_DOWN;
}
} else if (c == ERR) {
return K_ESC;
}
}
if (c == 10)
return K_ENTER;
return K_OTHER;
}
void setCodePage() {}
void cls() {
printf("\033[2J");
printf("\033[1;1H");
}
#endif
Vec<Key> pressed;
Vec<Vec<bool>> updated;
Vec<Vec<String>> screen;
int frame = 0;
std::chrono::time_point<std::chrono::system_clock> startTime;
void init() {
setCodePage();
cls();
#ifdef __APPLE__
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
#endif
updated = Vec<Vec<bool>>(SCREEN_WIDTH, Vec<bool>(SCREEN_HEIGHT, false));
screen = Vec<Vec<String>>(SCREEN_WIDTH, Vec<String>(SCREEN_HEIGHT, " "));
}
void wait() {
pressed.clear();
for (int i = 0; i < SCREEN_WIDTH; i++) {
for (int j = 0; j < SCREEN_HEIGHT; j++) {
if (updated[i][j]) {
setCursorPosition(i, j);
printf("%s", screen[i][j].c_str());
updated[i][j] = false;
}
}
}
setCursorPosition(SCREEN_WIDTH, SCREEN_HEIGHT);
fflush(stdout);
// sleep until next frame
if (startTime.time_since_epoch().count() == 0)
startTime = std::chrono::system_clock::now();
frame += 1;
auto nextFrame = startTime + std::chrono::milliseconds(frame * 1000 / 60);
auto sleepTime = nextFrame - std::chrono::system_clock::now();
if (sleepTime.count() > 0) {
console::sleep(
std::chrono::duration_cast<std::chrono::milliseconds>(sleepTime)
.count());
}
}
size_t countUtf8CodePoint(const char *s) {
size_t i = 0;
size_t j = 0;
while (s[i]) {
if ((s[i] & 0xC0) != 0x80)
j++;
i++;
}
return j;
}
int getUTF8CodePoint(const char *s, char *buffer) {
if (s[0] == '\0')
return 0;
int numBytes = 0;
if ((s[0] & 0x80) == 0)
numBytes = 1;
if ((s[0] & 0xE0) == 0xC0)
numBytes = 2;
else if ((s[0] & 0xF0) == 0xE0)
numBytes = 3;
for (int i = 0; i < numBytes; i++)
buffer[i] = s[i];
buffer[numBytes] = '\0';
return numBytes;
}
char *getUTF8CodePoint(const char *s, int index) {
static char buffer[5];
int skip = 0;
for (int i = 0; i <= index; i++) {
auto bytes = getUTF8CodePoint(s + skip, buffer);
if (bytes == 0)
return nullptr;
skip += bytes;
}
return buffer;
}
void draw(int x, int y, const char *s, int index) {
char *c = getUTF8CodePoint(s, index);
if (x < 0 || y < 0 || x >= SCREEN_WIDTH || y >= SCREEN_HEIGHT) {
return;
}
if (screen[x][y] == c)
return;
screen[x][y] = c;
updated[x][y] = true;
}
void draw(int x, int y, const char *s) {
for (size_t i = 0; i < countUtf8CodePoint(s); i++) {
draw(x + i, y, s, i);
}
}
void draw(int x, int y, String s) { draw(x, y, s.c_str()); }
void clear() {
for (int i = 0; i < SCREEN_WIDTH; i++) {
for (int j = 0; j < SCREEN_HEIGHT; j++) {
draw(i, j, " ");
}
}
}
void updatePressed() {
Key k = key();
while (k != K_NONE) {
pressed.push_back(k);
k = key();
}
}
bool key(Key k) {
updatePressed();
for (auto key : pressed)
if (key == k)
return true;
return false;
}
} // namespace console