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