-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstage3.c
More file actions
213 lines (155 loc) · 4.45 KB
/
stage3.c
File metadata and controls
213 lines (155 loc) · 4.45 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <signal.h>
#include <string.h>
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX_X 60
#define MAX_Y 26
typedef struct {
int key;
int pos_x;
int pos_y;
char old_screen[MAX_Y][MAX_X];
char screen[MAX_Y][MAX_X];
} GameState;
static struct termios old_termios, new_termios;
void reset_terminal() {
printf("\e[m"); // reset color changes
printf("\e[?25h"); // show cursor
printf("\e[%d;%dH\n", MAX_Y + 3, MAX_X); // move cursor after game board
fflush(stdout);
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
}
void configure_terminal() {
tcgetattr(STDIN_FILENO, &old_termios);
new_termios = old_termios; // save it to be able to reset on exit
new_termios.c_lflag &= ~(ICANON | ECHO); // turn off echo + non-canonical mode
new_termios.c_cc[VMIN] = 0;
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
printf("\e[?25l"); // hide cursor
atexit(reset_terminal);
}
static int exit_loop;
void signal_handler(__attribute__((unused)) int signum) {
exit_loop = 1;
}
int read_key(char* buf, int k) {
if (buf[k] == '\033' && buf[k + 1] == '[') {
switch (buf[k + 2]) {
case 'A': return 1; // UP
case 'B': return 2; // DOWN
case 'C': return 3; // RIGHT
case 'D': return 4; // LEFT
}
}
return 0;
}
void read_input(GameState* state) {
char buf[4096]; // maximum input buffer
int n = read(STDIN_FILENO, buf, sizeof(buf));
int final_key = 0;
// it's okay if we miss some keys
// we will correct it on next frame
for (int k = 0; k <= n - 3; k += 3) {
int key = read_key(buf, k);
if (key == 0) continue;
final_key = key;
}
state->key = final_key;
}
void handle_player(GameState* state) {
switch (state->key) {
case 1:
if (state->pos_y > 1) {
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y - 1][state->pos_x] = '@';
--state->pos_y;
}
break;
case 2:
if (state->pos_y < MAX_Y - 2) {
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y + 1][state->pos_x] = '@';
++state->pos_y;
}
break;
case 3:
if (state->pos_x < MAX_X - 3) {
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y][state->pos_x + 1] = '@';
++state->pos_x;
}
break;
case 4:
if (state->pos_x > 1) {
state->screen[state->pos_y][state->pos_x] = ' ';
state->screen[state->pos_y][state->pos_x - 1] = '@';
--state->pos_x;
}
break;
default:
break;
}
}
void update(GameState* state) {
memcpy(state->screen, state->old_screen, sizeof(state->screen));
handle_player(state);
}
void render(GameState* state) {
for (int j = 0; j < MAX_Y; ++j) {
for (int i = 0; i < MAX_X; ++i) {
if (state->old_screen[j][i] != state->screen[j][i]) {
printf("\e[%d;%dH", j + 1, i + 1); // move cursor
printf("%c", state->screen[j][i]);
}
}
}
fflush(stdout);
}
void setup_board(GameState* state) {
memset(state->screen, ' ', sizeof(state->screen));
for (int i = 0; i < MAX_X - 1; ++i) {
state->screen[0][i] = 'X';
state->screen[MAX_Y - 1][i] = 'X';
}
for (int j = 0; j < MAX_Y; ++j) {
state->screen[j][0] = 'X';
state->screen[j][MAX_X - 2] = 'X';
state->screen[j][MAX_X - 1] = '\n';
}
state->screen[state->pos_y][state->pos_x] = '@';
render(state);
}
// Lower is faster
#define SPEED 0.1
int main() {
configure_terminal();
signal(SIGINT, signal_handler);
struct timespec req = {};
struct timespec rem = {};
GameState state = {
.pos_x = 5,
.pos_y = 5
};
printf("\e[2J");
setup_board(&state);
clock_t start, end;
while (!exit_loop) {
start = clock();
read_input(&state);
update(&state);
render(&state);
memcpy(state.old_screen, state.screen, sizeof(state.screen));
end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
if (time_taken > SPEED) continue;
req.tv_sec = 0;
req.tv_nsec = (SPEED - time_taken) * 1000000000; // 0.1 seconds
nanosleep(&req, &rem);
}
}