Skip to content

Commit 82685a6

Browse files
committed
support windows
1 parent 18abbc6 commit 82685a6

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
main
2-
spaceship
2+
spaceship
3+
*.out
4+
*.exe

main.c

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
1-
#include <time.h>
21
#include <stdio.h>
2+
#ifdef _WIN32
3+
#include <windows.h>
4+
#include <conio.h>
5+
#define UP 72
6+
#define DOWN 80
7+
#define RIGHT 77
8+
#define LEFT 75
9+
#define Read(u) u = kbhit() ? getch() : u
10+
#define t GetTickCount
11+
#define tcsetattr(a,b,c)
12+
#else
13+
#include <time.h>
314
#include <stdlib.h>
415
#include <string.h>
516
#include <unistd.h>
617
#include <termios.h>
718
#include <sys/ioctl.h>
19+
#define UP 65
20+
#define DOWN 66
21+
#define RIGHT 67
22+
#define LEFT 68
23+
#define Read(u) read(0, &u, 1)
24+
int t() {
25+
struct timespec t;
26+
timespec_get(&t, TIME_UTC);
27+
return t.tv_sec * 1000 + t.tv_nsec / 1000000;
28+
}
29+
#endif
830
#define g(x, y, u, v, s) for (int j = 0, X = x, Y = y; j < v && Y + j < h - X / w && Y >= 0 && X >= 0; ++j) memcpy(&f[Y + j][X], &s[j * u], u)
931
#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))
1032
#define f(x, y, z) y += z * d; strcpy(b, x); break
11-
int t() {
12-
struct timespec now;
13-
timespec_get(&now, TIME_UTC);
14-
return now.tv_sec * 1000 + now.tv_nsec / 1000000;
15-
}
1633
int main() {
34+
#ifdef _WIN32
35+
CONSOLE_SCREEN_BUFFER_INFO csbi; // console info
36+
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); // get console size
37+
int h = csbi.srWindow.Bottom - csbi.srWindow.Top + 1, w = csbi.srWindow.Right - csbi.srWindow.Left + 1;
38+
#else
1739
struct termios z, o;
1840
tcgetattr(0, &z); // get current terminal settings as a "backup"
1941
o = z; // save a copy
@@ -22,24 +44,26 @@ int main() {
2244
tcsetattr(0, TCSANOW, &z); // set new terminal settings
2345
struct winsize v; // terminal size
2446
ioctl(1, TIOCGWINSZ, &v); // get terminal size
25-
int h = v.ws_row, w = v.ws_col, A = w * h / 100, l = t(), g = 1;
47+
int h = v.ws_row, w = v.ws_col;
48+
#endif
49+
int A = w * h / 100, l = t(), g = 1;
2650
struct V {
2751
float x, y;
2852
} p = {w / 2, h / 2}, a[A], m[A];
2953
char u = 0, f[h + 1][w], b[13] = " /\\ / \\ vv ";
3054
while (1) {
3155
float d = (t() - l) * .001;
32-
read(0, &u, 1); // read the next move
56+
Read(u); // read input
3357
l = t();
3458
memset(f, ' ', h * w); // clear the screen
3559
switch (u) { // could be minified...
36-
case 65: // up
60+
case UP:
3761
f(" /\\ / \\ vv ", p.y, -20);
38-
case 66: // down
62+
case DOWN:
3963
f(" ^^ \\ / \\/ ", p.y, 20);
40-
case 67: // right
64+
case RIGHT:
4165
f("<\\ < ></ ", p.x, 40);
42-
case 68: // left
66+
case LEFT:
4367
f(" />< > \\>", p.x, -40);
4468
}
4569
p.x = p.x < 4 ? 4 : p.x >= w - 4 ? w - 4 : p.x; // make sure the player is in the screen (horizontally)

0 commit comments

Comments
 (0)