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