Skip to content

Commit b8e203e

Browse files
committed
enable escape sequence in cmd
1 parent fef7a0d commit b8e203e

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

main.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#define LEFT 75
99
#define Read(u) u = kbhit() ? getch() : u
1010
#define tcsetattr(a, b, c)
11-
#define PrintError(x) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4); puts(x); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); exit(1)
11+
#define SetupConsole() \
12+
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
1213
void getSize(int *x, int *y) {
1314
CONSOLE_SCREEN_BUFFER_INFO csbi;
1415
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
@@ -27,8 +28,13 @@ void getSize(int *x, int *y) {
2728
#define RIGHT 67
2829
#define LEFT 68
2930
#define Read(u) read(0, &u, 1)
30-
#define puts(x) printf("\33[0;4H%s", x)
31-
#define PrintError(x) exit(printf("\33[0;31m%s\33[0m\n", x))
31+
#define SetupConsole() \
32+
struct termios z, o; \
33+
tcgetattr(0, &z); \
34+
o = z; \
35+
z.c_lflag &= ~ICANON; \
36+
z.c_cc[VMIN] = 0; \
37+
tcsetattr(0, TCSANOW, &z);
3238
int GetTickCount() {
3339
struct timespec t;
3440
timespec_get(&t, TIME_UTC);
@@ -45,14 +51,8 @@ void getSize(int *x, int *y) {
4551
#define l(x, y, w, h, a, b, c, d) !((x - a > c && x >= a) || (a - x > w && a >= x) || (y - b > d && y >= b) || (b - y > h && b >= y))
4652
#define f(x, y, z) y += z * d; strcpy(b, x); break
4753
int main() {
48-
#ifndef _WIN32
49-
struct termios z, o;
50-
tcgetattr(0, &z); // get current terminal settings as a "backup"
51-
o = z; // save a copy
52-
z.c_lflag &= ~ICANON; // disable canonical mode
53-
z.c_cc[VMIN] = 0; // minimum number of characters to read
54-
tcsetattr(0, TCSANOW, &z); // set new terminal settings
55-
#endif
54+
SetupConsole();
55+
puts("\x1b[?25l"); // hide cursor
5656
int w, W, h, H;
5757
getSize(&w, &h);
5858
int A = w * h / 100, l = GetTickCount(), g = 1, start = l;
@@ -89,12 +89,12 @@ int main() {
8989
};
9090
g(e->x, e->y, 3, 2, "OOOOOO"); // draw the asteroid
9191
}
92-
g(p.x, p.y, 4, 3, b); // draw the player
93-
for (int i = 2, j = 1000; i >= -2; i--, j *= 10) f[1][w / 2 + i] = '0' + (l - start) / j % 10; // draw the score
94-
getSize(&W, &H); // get the new screen size
95-
if (W != w || H < h) {PrintError("\nPlease don't resize the screen, it will break the program");} // make sure the screen is not resized besides getting higher
96-
else puts(&f[0][4]); // print the screen
97-
while (GetTickCount() - l < 10); // wait for a bit
92+
g(p.x, p.y, 4, 3, b); // draw the player
93+
for (int i = 2, j = 1000; i >= -2; i--, j *= 10) f[1][w / 2 + i] = '0' + (l - start) / j % 10; // draw the score
94+
getSize(&W, &H); // get the new screen size
95+
if (W != w || H < h) exit(printf("\033[0;31m\nPlease don't resize the screen, it will break the program\033[0m\n")); // make sure the screen is not resized besides getting higher
96+
else printf("\033[0;4H%s", &f[0][4]); // print the screen
97+
while (GetTickCount() - l < 10); // wait for a bit
9898
g = 0;
9999
}
100100
}

0 commit comments

Comments
 (0)