forked from 0xminik/ctris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctris.c
121 lines (108 loc) · 2.45 KB
/
ctris.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
118
119
#include "ctris.h"
#include "screen.h"
#include "game.h"
char game_state = RUNNING_STATE, old_style_keys = 0, counterclockwise_rotation = 0, highscore_file_path[256], default_name[40];
unsigned long rseed = 1;
#ifndef HIDE_CURSOR_WORKAROUND
char hide_cursor_workaround = 0;
#else
char hide_cursor_workaround = 1;
#endif
void handle_quit_signal(const int code)
{
if(game_state != RUNNING_STATE && game_state != QUIT_STATE)
{
restore_screen();
exit(0);
}
game_state = SIGNALED_QUIT_STATE;
}
void handle_resize_signal(const int code)
{
pause_game();
}
unsigned char get_rand(const unsigned char i)
{
rseed = rseed * 1103515245 + 12345;
return (unsigned char)((rseed / 65536) % 32768) % i;
}
int main(int argc, char *argv[])
{
int optchr;
char *name;
rseed = time(NULL);
snprintf(highscore_file_path, 256, "%s/.ctris_highscore.dat", getenv("HOME"));
if((name = getenv("USER")) != NULL)
{
strncpy(default_name, getenv("USER"), 40);
}
else
{
default_name[0] = '\0';
}
signal(SIGKILL, handle_quit_signal);
signal(SIGWINCH, handle_resize_signal);
signal(SIGTERM, handle_quit_signal);
signal(SIGINT, handle_quit_signal);
signal(SIGQUIT, handle_quit_signal);
while((optchr = getopt(argc, argv, "hvroisc")) != EOF)
{
switch(optchr)
{
case 'h':
printf("Help:\n\
-h parameter list\n\
-o use old style keys\n\
-v version info\n\
-i enable hide cursor workaround\n\
-r remove highscore-file\n\
-c enable couterclockwise rotation\n");
game_state = QUIT_STATE;
break;
case 'v':
printf("\
ctris %s, built on %s.\n\
(c) Dominik Hackl [[email protected]]\n\
Homepage: http://www.hackl.dhs.org/ctris\n\
ALL PARTS OF CTRIS ARE UNDER THE TERMS OF GPL. SEE DOCUMENTATION FOR MORE INFO\n\
", VERSION, __DATE__);
game_state = QUIT_STATE;
break;
case 'r':
printf("removing highscore-file (\"%s\")...", highscore_file_path);
if(remove(highscore_file_path) == 0)
{
printf("done\n");
}
else
{
printf("failed\n");
}
game_state = QUIT_STATE;
break;
case 'o':
old_style_keys = 1;
break;
case 'i':
hide_cursor_workaround = 1;
case 'c':
counterclockwise_rotation = 1;
default:
break;
}
}
if(game_state != RUNNING_STATE)
{
return 0;
}
if(init_screen() != 0)
{
return 1;
}
do
{
start_game();
} while(game_state == QUIT_STATE && play_again() == 0);
restore_screen();
return 0;
}