Implemented --help and --disable-sound (patch by Duong-Khang NGUYEN <neoneurone@users...
[supertux.git] / src / setup.c
1 /*
2   setup.c
3   
4   Super Tux - Setup
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - November 7, 2001
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include "defines.h"
28 #include "globals.h"
29 #include "setup.h"
30 #include "screen.h"
31 #include "sound.h"
32
33
34 /* Local function prototypes: */
35
36 void seticon(void);
37 void usage(char * prog, int ret);
38
39
40 /* --- SETUP --- */
41
42 void st_setup(void)
43 {
44   /* Seed random number generator: */
45   
46   srand(SDL_GetTicks());
47   
48   
49   /* Init SDL Video: */
50   
51   if (SDL_Init(SDL_INIT_VIDEO) < 0)
52     {
53       fprintf(stderr,
54               "\nError: I could not initialize video!\n"
55               "The Simple DirectMedia error that occured was:\n"
56               "%s\n\n", SDL_GetError());
57       exit(1);
58     }
59
60
61   /* Init Joystick: */
62   
63 #ifdef JOY_YES
64   use_joystick = YES;
65   
66   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
67     {
68       fprintf(stderr, "Warning: I could not initialize joystick!\n"
69               "The Simple DirectMedia error that occured was:\n"
70               "%s\n\n", SDL_GetError());
71       
72       use_joystick = NO;
73     }
74   else
75     {
76       /* Open joystick: */
77       
78       if (SDL_NumJoysticks() <= 0)
79         {
80           fprintf(stderr, "Warning: No joysticks are available.\n");
81           
82           use_joystick = NO;
83         }
84       else
85         {
86           js = SDL_JoystickOpen(0);
87           
88           if (js == NULL)
89             {
90               fprintf(stderr, "Warning: Could not open joystick 1.\n"
91                       "The Simple DirectMedia error that occured was:\n"
92                       "%s\n\n", SDL_GetError());
93               
94               use_joystick = NO;
95             }
96           else
97             {
98               /* Check for proper joystick configuration: */
99               
100               if (SDL_JoystickNumAxes(js) < 2)
101                 {
102                   fprintf(stderr,
103                           "Warning: Joystick does not have enough axes!\n");
104                   
105                   use_joystick = NO;
106                 }
107               else
108                 {
109                   if (SDL_JoystickNumButtons(js) < 2)
110                     {
111                       fprintf(stderr,
112                               "Warning: "
113                               "Joystick does not have enough buttons!\n");
114                       
115                       use_joystick = NO;
116                     }
117                 }
118             }
119         }
120     }
121 #endif
122   
123   
124   /* Init SDL Audio: */
125   
126   if (use_sound == YES)
127     {
128       if (SDL_Init(SDL_INIT_AUDIO) < 0)
129         {
130           fprintf(stderr,
131                   "\nWarning: I could not initialize audio!\n"
132                   "The Simple DirectMedia error that occured was:\n"
133                   "%s\n\n", SDL_GetError());
134           use_sound = NO;
135         }
136     }
137   
138   
139   /* Open sound: */
140   
141   if (use_sound == YES)
142     {
143       if (Mix_OpenAudio(44100, AUDIO_S16, 2, 512) < 0)
144         {
145           fprintf(stderr,
146                   "\nWarning: I could not set up audio for 44100 Hz "
147                   "16-bit stereo.\n"
148                   "The Simple DirectMedia error that occured was:\n"
149                   "%s\n\n", SDL_GetError());
150           use_sound = NO;
151         }
152     }
153
154
155   /* Open display: */
156   
157   if (use_fullscreen == YES)
158     {
159       screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN) ; /* | SDL_HWSURFACE); */
160       if (screen == NULL)
161         {
162           fprintf(stderr,
163                   "\nWarning: I could not set up fullscreen video for "
164                   "640x480 mode.\n"
165                   "The Simple DirectMedia error that occured was:\n"
166                   "%s\n\n", SDL_GetError());
167           use_fullscreen = NO;
168         }
169     }
170   
171   if (use_fullscreen == NO)
172     {
173       screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
174       
175       if (screen == NULL)
176         {
177           fprintf(stderr,
178                   "\nError: I could not set up video for 640x480 mode.\n"
179                   "The Simple DirectMedia error that occured was:\n"
180                   "%s\n\n", SDL_GetError());
181           exit(1);
182         }
183     }
184   
185   
186   /* Load global images: */
187   
188   letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
189                              USE_ALPHA);
190
191   letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
192                              USE_ALPHA);
193
194   letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
195                              USE_ALPHA);
196
197   letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
198                            USE_ALPHA);
199   
200   
201   /* Set icon image: */
202   
203   seticon();
204   
205   
206   /* Set window manager stuff: */
207   
208   SDL_WM_SetCaption("Super Tux", "Super Tux");
209 }
210
211
212 /* --- SHUTDOWN --- */
213
214 void st_shutdown(void)
215 {
216   SDL_Quit();
217 }
218
219
220 /* --- ABORT! --- */
221
222 void st_abort(char * reason, char * details)
223 {
224   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
225   st_shutdown();
226   exit(1);
227 }
228
229
230 /* Set Icon (private) */
231
232 void seticon(void)
233 {
234   int masklen;
235   Uint8 * mask;
236   SDL_Surface * icon;
237   
238   
239   /* Load icon into a surface: */
240   
241   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
242   if (icon == NULL)
243     {
244       fprintf(stderr,
245               "\nError: I could not load the icon image: %s\n"
246               "The Simple DirectMedia error that occured was:\n"
247               "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
248       exit(1);
249     }
250   
251   
252   /* Create mask: */
253   
254   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
255   mask = malloc(masklen * sizeof(Uint8));
256   memset(mask, 0xFF, masklen);
257   
258   
259   /* Set icon: */
260   
261   SDL_WM_SetIcon(icon, mask);
262   
263   
264   /* Free icon surface & mask: */
265   
266   free(mask);
267   SDL_FreeSurface(icon);
268 }
269
270
271 /* Parse command-line arguments: */
272
273 void parseargs(int argc, char * argv[])
274 {
275   int i;
276   
277   
278   /* Set defaults: */
279   
280   use_fullscreen = NO;
281   #ifndef NOSOUND
282   use_sound = YES;
283   #else
284   use_sound = NO;
285   #endif
286   
287   /* Parse arguments: */
288   
289   for (i = 1; i < argc; i++)
290     {
291       if (strcmp(argv[i], "--fullscreen") == 0 ||
292           strcmp(argv[i], "-f") == 0)
293         {
294           /* Use full screen: */
295           
296           use_fullscreen = YES;
297         }
298       else if (strcmp(argv[i], "--usage") == 0)
299         {
300           /* Show usage: */
301           
302           usage(argv[0], 0);
303         }
304       else if (strcmp(argv[i], "--version") == 0)
305         {
306           /* Show version: */
307           
308           printf("Super Tux - version " VERSION "\n");
309           exit(0);
310         }
311        else if (strcmp(argv[i], "--disable-sound") == 0)
312         {
313           /* Disable the compiled in sound & music feature */
314  #ifndef NOSOUND
315           printf("Sounds and music disabled \n");
316           use_sound = NO;
317  #else
318           printf("Sounds and music feature is not compiled in \n");
319  #endif
320         }
321       else if (strcmp(argv[i], "--help") == 0)
322         {         /* Show help: */
323  
324           printf("Super Tux " VERSION "\n\n");
325  
326           printf("----------  Command-line options  ----------\n\n");
327  
328            printf("  --disable-sound     - If sound support was compiled in,  this will\n                        disable it for this session of the game.\n\n");
329  
330           printf("  --fullscreen        - Run in fullscreen mode.\n\n");
331  
332           printf("  --help              - Display a help message summarizing command-line\n                        options, license and game controls.\n\n");
333  
334           printf("  --usage             - Display a brief message summarizing command-line options.\n\n");
335  
336           printf("  --version           - Display the version of SuperTux you're running.\n\n\n");
337  
338  
339           printf("----------          License       ----------\n\n");
340           printf("  This program comes with ABSOLUTELY NO WARRANTY.\n");
341           printf("  This is free software, and you are welcome to redistribute\n");
342           printf("  or modify it under certain conditions. See the file \n");
343           printf("  \"COPYING.txt\" for more details.\n\n\n");
344  
345           printf("----------      Game controls     ----------\n\n");
346           printf("  Please see the file \"README.txt\"\n\n");
347  
348           exit(0);
349       }
350       else
351         {
352           /* Unknown - complain! */
353           
354           usage(argv[0], 1);
355         }
356     }
357 }
358
359
360 /* Display usage: */
361
362 void usage(char * prog, int ret)
363 {
364   FILE * fi;
365   
366   
367   /* Determine which stream to write to: */
368   
369   if (ret == 0)
370     fi = stdout;
371   else
372     fi = stderr;
373   
374   
375   /* Display the usage message: */
376   
377   fprintf(fi, "Usage: %s [--fullscreen] | [--disable-sound] |  [--usage | --help | --version]\n",
378           prog);
379   
380   
381   /* Quit! */
382
383   exit(ret);
384 }