1 /* main.c - moon-buggy main file
2 *
3 * Copyright 1999, 2000, 2004, 2006 Jochen Voss */
4
5 static const char rcsid[] = "$Id: main.c 6825 2006-03-19 19:18:39Z voss $";
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdio.h>
12 #include <string.h>
13 #ifdef HAVE_GETOPT_H
14 #include <getopt.h>
15 #else
16 #include <unistd.h>
17 extern char *optarg;
18 extern int optind;
19 #endif
20 #ifdef HAVE_LOCALE_H
21 #include <locale.h>
22 #endif
23
24 #include "moon-buggy.h"
25
26
27 const char *my_name;
28 static int curses_initialised;
29 static int mesg_flag;
30 WINDOW *moon, *status, *message;
31
32 int car_base;
33
34
35 void
print_message(const char * str)36 print_message (const char *str)
37 /* Display STR in the message line. */
38 {
39 if (curses_initialised) {
40 werase (message);
41 waddstr (message, str);
42 wnoutrefresh (message);
43 } else {
44 fprintf (stderr, "%s\n", str);
45 }
46 }
47
48 void
print_hint(const char * str)49 print_hint (const char *str)
50 /* Display STR in the message line. */
51 {
52 if (curses_initialised) {
53 int len = strlen (str);
54 int cols = COLS;
55 int pos;
56
57 if (car_base+3+len/2 >= cols) {
58 pos = cols - len - 1;
59 } else {
60 pos = car_base+3-len/2;
61 }
62 wmove (moon, LINES-11, 0);
63 wclrtoeol (moon);
64 mvwaddstr (moon, LINES-11, pos, str);
65 wnoutrefresh (moon);
66 }
67 }
68
69 void
prepare_screen(void)70 prepare_screen (void)
71 /* Prepare the screen for the program.
72 * Calls to `prepare_screen' and `prepare_for_exit' must be paired. */
73 {
74 cbreak ();
75 noecho ();
76 hide_cursor ();
77 term_prepare (mesg_flag);
78 }
79
80 void
prepare_for_exit(void)81 prepare_for_exit (void)
82 /* Prepare the screen to exit from the program.
83 * Calls to `prepare_screen' and `prepare_for_exit' must be paired. */
84 {
85 if (! curses_initialised) return;
86 term_restore ();
87 show_cursor ();
88 wrefresh (moon);
89 wrefresh (message);
90 werase (status);
91 wmove (status, 0, 0);
92 wrefresh (status);
93 endwin ();
94 fflush (NULL);
95 }
96
97 void
allocate_windows(void)98 allocate_windows (void)
99 /* Create the curses windows. */
100 {
101 initscr ();
102
103 moon = newwin (LINES-2, 0, 0, 0);
104 keypad (moon, TRUE);
105 intrflush (moon, FALSE);
106
107 status = newwin (1, 0, LINES-1, 0);
108 keypad (status, TRUE);
109 intrflush (status, FALSE);
110
111 message = newwin (1, 0, LINES-2, 0);
112 keypad (message, TRUE);
113 intrflush (message, FALSE);
114 }
115
116 void
clear_windows(void)117 clear_windows (void)
118 {
119 wclear (moon); wnoutrefresh (moon);
120 wclear (status); wnoutrefresh (status);
121 wclear (message); wnoutrefresh (message);
122 }
123
124 /************************************************************
125 * main procedure
126 */
127
128 int
main(int argc,char ** argv)129 main (int argc, char **argv)
130 {
131 #ifdef HAVE_GETOPT_LONG
132 struct option long_options [] = {
133 { "create-scores", no_argument, 0, 'c' },
134 { "help", no_argument, 0, 'h' },
135 { "mesg", no_argument, 0, 'm' },
136 { "no-title", no_argument, 0, 'n' },
137 { "show-scores", no_argument, 0, 's' },
138 { "version", no_argument, 0, 'V' },
139 { NULL, 0, NULL, 0}
140 };
141 #endif
142 #define MB_SHORT_OPTIONS "chmnsV"
143 int help_flag = 0;
144 int highscore_flag = 0;
145 int title_flag = 1;
146 int version_flag = 0;
147 int error_flag = 0;
148
149 initialise_persona ();
150 set_persona (pers_USER);
151
152 #ifdef HAVE_SETLOCALE
153 setlocale (LC_CTYPE, "");
154 #endif
155
156 /* `basename' seems to be non-standard. So we avoid it. */
157 my_name = strrchr (argv[0], '/');
158 my_name = xstrdup (my_name ? my_name+1 : argv[0]);
159
160 do {
161 int c;
162 #ifdef HAVE_GETOPT_LONG
163 int ind;
164 c = getopt_long (argc, argv, MB_SHORT_OPTIONS, long_options, &ind);
165 #else
166 c = getopt (argc, argv, MB_SHORT_OPTIONS);
167 #endif
168 if (c == -1) break;
169 switch (c) {
170 case 'c':
171 highscore_flag = 2;
172 break;
173 case 'h':
174 help_flag = 1;
175 break;
176 case 'm':
177 mesg_flag = 1;
178 break;
179 case 'n':
180 title_flag = 0;
181 break;
182 case 's':
183 highscore_flag = 1;
184 break;
185 case 'V':
186 version_flag = 1;
187 break;
188 default:
189 error_flag = 1;
190 }
191 } while (! error_flag);
192
193 if (argc != optind) {
194 fputs ("too many arguments\n", stderr);
195 error_flag = 1;
196 }
197
198 if (version_flag) {
199 puts ("Moon-Buggy " VERSION);
200 puts ("Copyright 1998-2006 Jochen Voss");
201 puts ("\
202 Moon-Buggy comes with NO WARRANTY, to the extent permitted by law.");
203 puts ("\
204 You may redistribute copies of Moon-Buggy under the terms of the GNU\n\
205 General Public License. For more information about these matters, see\n\
206 the file named COPYING or press `c' at Moon-Buggy's title screen.");
207 if (! error_flag) exit (0);
208 }
209 if (error_flag || help_flag) {
210 #ifdef HAVE_GETOPT_LONG
211 #define OPT(short,long) " " short ", " long " "
212 #else
213 #define OPT(short,long) " " short " "
214 #endif
215 FILE *out = error_flag ? stderr : stdout;
216 fprintf (out, "usage: %s [options]\n\n", my_name);
217 fputs ("The options are\n", out);
218 /* --create-scores: create the highscore file (internal use only) */
219 fputs (OPT("-h","--help ") "show this message and exit\n", out);
220 fputs (OPT("-m","--mesg ") "imply the effect of \"mesg n\"\n",
221 out);
222 fputs (OPT("-n","--no-title ") "omit the title screen\n", out);
223 fputs (OPT("-s","--show-scores ") "only show the highscore list\n", out);
224 fputs (OPT("-V","--version ") "show the version number and exit\n\n",
225 out);
226 fputs ("Please report bugs to <voss@seehuhn.de>.\n", out);
227 exit (error_flag);
228 }
229
230 init_rnd ();
231
232 if (highscore_flag) {
233 if (highscore_flag == 1) {
234 show_highscores ();
235 } else {
236 create_highscores ();
237 }
238 exit (0);
239 }
240
241 initialise_signals ();
242
243 allocate_windows ();
244 curses_initialised = 1;
245 prepare_screen ();
246
247 install_keys ();
248 setup_title_mode ();
249 setup_pager_mode ();
250 setup_game_mode ();
251 setup_highscore_mode ();
252
253 clear_windows ();
254
255 resize_ground (1);
256 initialise_buggy ();
257
258 if (title_flag) {
259 mode_change (title_mode, 0);
260 } else {
261 mode_change (game_mode, 0);
262 }
263 main_loop ();
264 mode_change (NULL, 0);
265
266 prepare_for_exit ();
267 return 0;
268 }
269