-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
105 lines (94 loc) · 2.91 KB
/
Copy pathinput.c
File metadata and controls
105 lines (94 loc) · 2.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ezalos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/06 12:15:02 by ezalos #+# #+# */
/* Updated: 2019/09/08 21:35:51 by ezalos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/head.h"
#include <sys/select.h>
#include <curses.h>
#include <unistd.h>
#include <termios.h>
int get_input(t_snake *snake)
{
char *buffer;
int r_v;
// int nfds = 1;
// fd_set readfds;
// // fd_set writefds;
// // fd_set errorfds;
// struct timeval timeout;
// timeout.tv_sec = 1;
// timeout.tv_usec = REFRESH_TIME;
// FD_ZERO(&readfds);
// FD_SET(0, &readfds);
ft_printf("%s\n", __func__);
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 0;
old.c_cc[VTIME] = 1;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
// if (0 <= (r_v = select(nfds, &readfds, NULL, NULL, &timeout)) && ft_printf("%~{?}SELECT: %d\n", r_v))
// return (r_v);
// ft_printf("%~{?}GOOLECT: %d\n", r_v);
if ((r_v = get_next_line(0, &buffer)) < 0)
perror ("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror ("tcsetattr ~ICANON");
if (r_v <= 0)
return (r_v);
if (!ft_strncmp(buffer, S_UP, 3))//UP
{
// ft_printf("UP\n");
if (snake->dir_row != 1 || snake->row_history[0] == 1)
snake->dir_row = -1;
snake->dir_col = 0;
}
else if (!ft_strncmp(buffer, S_DOWN, 3))//DOWN
{
// ft_printf("DOWN\n");
if (snake->dir_row != -1 || snake->row_history[0] == 1)
snake->dir_row = 1;
snake->dir_col = 0;
}
else if (!ft_strncmp(buffer, S_RIGHT, 3))//RIGHT
{
// ft_printf("RIGHT\n");
snake->dir_row = 0;
if (snake->dir_col != -1 || snake->row_history[0] == 1)
snake->dir_col = 1;
}
else if (!ft_strncmp(buffer, S_LEFT, 3))//LEFT
{
// ft_printf("LEFT\n");
snake->dir_row = 0;
if (snake->dir_col != 1 || snake->row_history[0] == 1)
snake->dir_col = -1;
}
else if (!ft_strncmp(buffer, "0", 1))
{
r_v = 5;
while (r_v--)
{
calculate_head_pos(snake);
snake->board[snake->food.row][snake->food.col] == BODY;
snake_grow(snake, snake->first.row, snake->first.col);
}
}
else if (!ft_strncmp(buffer, "9", 1))
{
print_it(snake);
}
return (r_v);
}