Small snake game in C
Commit 3b375ff2d7d9
tarball5 files changed+120-52
@@ -27,6 +27,7 @@ add_executable(snake
2727
source/parameters.c2828
source/settings_file/settings_file.c2929
source/animation/animation.c30
+ source/read_input/read_input.c3031
)3132
3233
target_link_libraries(snake PRIVATE @@ -34,6 +34,7 @@
3434
parameters.c \3535
settings_file/settings_file.c \3636
animation/animation.c \37
+ read_input/read_input.c \3738
-I${jag-parse-lib}/include \3839
-L${jag-parse-lib}/lib \3940
-ljag_parse \@@ -1,11 +1,14 @@
11
#include "../snake.h"2
+3
+#include "../read_input/read_input.h"4
+25
#include <jag_parse.h>3
-#include <poll.h>46
57
#define DELAY 1068
79
#define KEY_PROTECTION 2810
11
+912
bool start_animation() { 1013
1114
struct winsize current_size;@@ -62,16 +65,12 @@ bool start_animation() {
6265
6366
int32_t end = current_size.ws_col + width;6467
65
-66
- struct pollfd polling[1];67
-68
- polling[0].fd = STDIN_FILENO;69
- polling[0].events = POLLIN;70
-7168
uint8_t wait_incomplete = 0;7269
bool key_lock = false;7370
74
- while (end >= 0) {71
+ bool exit = false;72
+73
+ while (end >= 0 && !exit) {7574
7675
ioctl(STDOUT_FILENO, TIOCGWINSZ, ¤t_size);7776
@@ -106,61 +105,47 @@ bool start_animation() {
106105
107106
end--;108107
109
- keypress input[20] = {};110
- size_t input_len = 0;111
-112
- if (key_lock)113
- wait_incomplete++;114108
109
+ bool is_broken = false;110
+ 115111
if (wait_incomplete >= KEY_PROTECTION) {116
- input[0] = ESCAPE_KEY;117
- input_len++;118
-112
+ 119113
key_lock = false;120114
wait_incomplete = 0;121
-122
- break_unfinished();115
+ is_broken = true;123116
}124117
125
- int is_poll = poll(polling, 1, DELAY);126
-127
- char string[256] = {};128
- ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);129
- 130
- add_to_buf(string, length_string);131
-132
- 133
- size_t k = input_len;134
-135
- input[k] = UNKNOWN;136118
137
- while (input[k] != NO_KEY) {119
+ if (key_lock)120
+ wait_incomplete++;138121
139
- input[k] = parse_input();140122
141
- if (input[k] == INCOMPLETE) {142
- key_lock = true;143
- break;144
- } 123
+ 124
+ keypress *input;125
+ size_t input_len = 0;145126
146
- k++;147
- }127
+ if (read_input(&input, &input_len, &key_lock, is_broken, DELAY)) {128
+ 129
+ size_t k;130
+131
+ for (k = 0; k < input_len; k++) {132
+ switch (input[k]) {133
+ case Q_KEY:134
+ case ESCAPE_KEY:135
+ exit = true;136
+ return false;137
+ break;138
+ case ENTER_KEY:139
+ end = -1; 140
+ break;141
+ default:142
+ break;143
+ } 144
+ }148145
149
- input_len = k;150
-151
- for (k = 0; k < input_len; k++) {152
- switch (input[k]) {153
- case Q_KEY:154
- case ESCAPE_KEY:155
- return false;156
- break;157
- case ENTER_KEY:158
- end = -1; 159
- break;160
- default:161
- break;162
- } 146
+ free_input(input);163147
}164148
}165
- return true;149
+150
+ return exit ? false : true;166151
}@@ -0,0 +1,61 @@
1
+#include "read_input.h"2
+3
+uint8_t read_input(keypress **input, size_t *input_len, bool *lock, bool is_broken, uint64_t delay) {4
+5
+ size_t arr_len = ARR_LEN;6
+7
+ if (!(*input = malloc(sizeof(keypress) * arr_len)))8
+ return 0;9
+10
+ struct pollfd polling[1];11
+12
+ polling[0].fd = STDIN_FILENO;13
+ polling[0].events = POLLIN;14
+15
+ if(is_broken) {16
+ (*input)[0] = ESCAPE_KEY;17
+ (*input_len)++;18
+19
+ break_unfinished();20
+ }21
+22
+ int is_poll = poll(polling, 1, delay);23
+24
+ if (is_poll > 0 && polling[0].revents & POLLIN) {25
+26
+ char string[256] = {};27
+ ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);28
+ 29
+ if (add_to_buf(string, length_string)) {30
+31
+ do {32
+33
+ if (*input_len >= arr_len) {34
+ arr_len *= 2;35
+ keypress *tmp = realloc(*input, sizeof(keypress) * arr_len);36
+ if (tmp == NULL) {37
+ free(*input);38
+ return 0;39
+ }40
+ *input = tmp;41
+ }42
+43
+ (*input)[*input_len] = parse_input();44
+45
+ if ((*input)[*input_len] == INCOMPLETE) {46
+ *lock = true;47
+ break;48
+ } 49
+50
+ } while ((*input)[(*input_len)++] != NO_KEY);51
+ }52
+ }53
+54
+ return 1;55
+}56
+57
+void free_input(keypress *input) {58
+ free(input);59
+}60
+61
+@@ -0,0 +1,20 @@
1
+#ifndef READ_INPUT_H2
+#define READ_INPUT_H3
+4
+#include <stdio.h>5
+#include <stdlib.h>6
+#include <poll.h>7
+#include <unistd.h>8
+9
+#include <jag_parse.h>10
+11
+12
+#define ARR_LEN 213
+14
+15
+uint8_t read_input(keypress **input, size_t *input_len, bool *lock, bool is_broken, uint64_t delay);16
+17
+void free_input(keypress *input);18
+19
+20
+#endif