-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
117 lines (95 loc) · 2.46 KB
/
main.c
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
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include "nano/console.h"
#include "nano/io.h"
#include "nano/io_key.h"
#include "tetris.h"
/* -------------------------------------------------------------------------- */
static const key_command_t commands[] = {
{ "\002", KEY_LEFT },
{ "\006", KEY_RIGHT },
{ "\020", KEY_UP },
{ "\016", KEY_DOWN },
/* VT100 */
{ "\033[A", KEY_UP },
{ "\033[B", KEY_DOWN },
{ "\033[D", KEY_LEFT },
{ "\033[C", KEY_RIGHT },
/* VT52 */
{ "\033A", KEY_UP },
{ "\033B", KEY_DOWN },
{ "\033D", KEY_LEFT },
{ "\033C", KEY_RIGHT },
{ " ", KEY_DROP },
{ "w", KEY_UP },
{ "s", KEY_DOWN },
{ "a", KEY_LEFT },
{ "d", KEY_RIGHT },
{ "p", KEY_PAUSE },
{ "q", KEY_QUIT },
{ "", 0 }
};
/* -------------------------------------------------------------------------- */
static char *get_path_filename(char *full_path)
{
char *slash = strrchr(full_path, '/');
return slash ? slash + 1: full_path;
}
static tetris_t ntet;
/* -------------------------------------------------------------------------- */
static void free_all()
{
tetris_free(&ntet);
con_xy(1, 25);
con_uncol();
con_show_cursor(1);
}
/* -------------------------------------------------------------------------- */
static void main_key_handler(void *self, char const *seq, int code)
{
if (code == KEY_QUIT)
exit(0);
}
/* -------------------------------------------------------------------------- */
void start(int argc, char *argv[])
{
static struct option const long_options[] = {
/* name, has_arg, *flag, chr */
{ "nocolor", 0, 0, 'c' },
{ "bsp", 0, 0, 'b' },
{ "help", 0, 0, 'h' },
{ 0, 0, 0, 0 }
};
int to_options = 0; // like black screen pause
for (;;) {
int option_index;
switch (getopt_long(argc, argv, "?hcb", long_options, &option_index)) {
case -1:
goto _end_of_opts;
case 'c':
con_colors_enable(0);
break;
case 'b':
to_options |= TO_BLACK_SCREEN_PAUSE;
break;
case 'h':
case '?':
con_put(
"Usage: %s <options>\n\n\
options:\n\
-c, --nocolor\t: disable ANSI colors;\n\
-b, --bsp\t: enable black screen pause;\n\
-h\t\t: print this help and exit.\n\n", get_path_filename(argv[0]));
return;
}
}
_end_of_opts:
con_show_cursor(0);
io_key_set_commands(commands);
io_key_on(main_key_handler, NULL);
io_atexit(free_all);
tetris_init(&ntet, 0, 0, to_options);
tetris_start(&ntet);
}