- renamed *.c to *.cxx
[supertux.git] / src / setup.cpp
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 - March 15, 2004
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 #ifndef NOOPENGL
21 #include <SDL_opengl.h>
22 #endif
23
24 #ifdef LINUX
25 #include <pwd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <ctype.h>
30 #endif
31
32 #include "defines.h"
33 #include "globals.h"
34 #include "setup.h"
35 #include "screen.h"
36 #include "texture.h"
37 #include "menu.h"
38 #include "gameloop.h"
39
40 /* Local function prototypes: */
41
42 void seticon(void);
43 void usage(char * prog, int ret);
44
45 /* Does the given file exist and is it accessible? */
46 int faccessible(char *filename)
47 {
48   struct stat filestat;
49   if (stat(filename, &filestat) == -1)
50     {
51       return NO;
52     }
53   else
54     {
55       if(S_ISREG(filestat.st_mode))
56         return YES;
57       else
58         return NO;
59     }
60 }
61
62 /* Can we write to this location? */
63 int fwriteable(char *filename)
64 {
65   FILE* fi;
66   fi = fopen(filename, "wa");
67   if (fi == NULL)
68     {
69       return NO;
70     }
71   return YES;
72 }
73
74 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
75 int fcreatedir(char* relative_dir)
76 {
77   char path[1024];
78   snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
79   if(mkdir(path,0755) != 0)
80     {
81       snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
82       if(mkdir(path,0755) != 0)
83         {
84           return NO;
85         }
86       else
87         {
88           return YES;
89         }
90     }
91   else
92     {
93       return YES;
94     }
95 }
96
97 /* Get all names of sub-directories in a certain directory. */
98 /* Returns the number of sub-directories found. */
99 /* Note: The user has to free the allocated space. */
100 string_list_type dsubdirs(char *rel_path, char* expected_file)
101 {
102   DIR *dirStructP;
103   struct dirent *direntp;
104   int i = 0;
105   string_list_type sdirs;
106   char filename[1024];
107   char path[1024];
108
109   string_list_init(&sdirs);
110   sprintf(path,"%s/%s",st_dir,rel_path);
111   if((dirStructP = opendir(path)) != NULL)
112     {
113       while((direntp = readdir(dirStructP)) != NULL)
114         {
115           char absolute_filename[1024];
116           struct stat buf;
117
118           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
119
120           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
121             {
122               if(expected_file != NULL)
123                 {
124                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
125                   if(!faccessible(filename))
126                     continue;
127                 }
128
129               string_list_add_item(&sdirs,direntp->d_name);
130             }
131         }
132       closedir(dirStructP);
133     }
134
135   sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
136   if((dirStructP = opendir(path)) != NULL)
137     {
138       while((direntp = readdir(dirStructP)) != NULL)
139         {
140           char absolute_filename[1024];
141           struct stat buf;
142
143           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
144
145           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
146             {
147               if(expected_file != NULL)
148                 {
149                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
150                   if(!faccessible(filename))
151                     {
152                       continue;
153                     }
154                   else
155                     {
156                       sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
157                       if(faccessible(filename))
158                         continue;
159                     }
160                 }
161
162               string_list_add_item(&sdirs,direntp->d_name);
163             }
164         }
165       closedir(dirStructP);
166     }
167
168   return sdirs;
169 }
170
171 string_list_type dfiles(char *rel_path, char* glob, char* exception_str)
172 {
173   DIR *dirStructP;
174   struct dirent *direntp;
175   int i = 0;
176   string_list_type sdirs;
177   char filename[1024];
178   char path[1024];
179
180   string_list_init(&sdirs);
181   sprintf(path,"%s/%s",st_dir,rel_path);
182   if((dirStructP = opendir(path)) != NULL)
183     {
184       while((direntp = readdir(dirStructP)) != NULL)
185         {
186           char absolute_filename[1024];
187           struct stat buf;
188
189           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
190
191           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
192             {
193               if(exception_str != NULL)
194                 {
195                   if(strstr(direntp->d_name,exception_str) != NULL)
196                     continue;
197                 }
198               if(glob != NULL)
199                 if(strstr(direntp->d_name,glob) == NULL)
200                   continue;
201
202               string_list_add_item(&sdirs,direntp->d_name);
203             }
204         }
205       closedir(dirStructP);
206     }
207
208   sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
209   if((dirStructP = opendir(path)) != NULL)
210     {
211       while((direntp = readdir(dirStructP)) != NULL)
212         {
213           char absolute_filename[1024];
214           struct stat buf;
215
216           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
217
218           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
219             {
220               if(exception_str != NULL)
221                 {
222                   if(strstr(direntp->d_name,exception_str) != NULL)
223                     continue;
224                 }
225               if(glob != NULL)
226                 if(strstr(direntp->d_name,glob) == NULL)
227                   continue;
228
229               string_list_add_item(&sdirs,direntp->d_name);
230             }
231         }
232       closedir(dirStructP);
233     }
234
235   return sdirs;
236 }
237
238 void free_strings(char **strings, int num)
239 {
240   int i;
241   for(i=0; i < num; ++i)
242     free(strings[i]);
243 }
244
245 /* --- SETUP --- */
246 /* Set SuperTux configuration and save directories */
247 void st_directory_setup(void)
248 {
249   char *home;
250   char str[1024];
251   /* Get home directory (from $HOME variable)... if we can't determine it,
252      use the current directory ("."): */
253   if (getenv("HOME") != NULL)
254     home = getenv("HOME");
255   else
256     home = ".";
257
258   st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
259                            strlen("/.supertux") + 1));
260   strcpy(st_dir, home);
261   strcat(st_dir, "/.supertux");
262
263   /* Remove .supertux config-file from old SuperTux versions */
264   if(faccessible(st_dir))
265     {
266       remove
267         (st_dir);
268     }
269
270   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
271
272   strcpy(st_save_dir,st_dir);
273   strcat(st_save_dir,"/save");
274
275   /* Create them. In the case they exist they won't destroy anything. */
276 #ifdef LINUX
277
278   mkdir(st_dir, 0755);
279   mkdir(st_save_dir, 0755);
280
281   sprintf(str, "%s/levels", st_dir);
282   mkdir(str, 0755);
283 #else
284   #ifdef WIN32
285
286   mkdir(st_dir);
287   mkdir(st_save_dir);
288   sprintf(str, "%s/levels", st_dir);
289   mkdir(str);
290 #endif
291 #endif
292
293 }
294
295 /* Create and setup menus. */
296 void st_menu(void)
297 {
298   menu_init(&main_menu);
299   menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
300   menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
301   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
302   menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
303   menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
304   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
305   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
306   menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
307   menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
308
309   menu_init(&options_menu);
310   menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
311   menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
312   menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
313   if(audio_device == YES)
314     {
315       menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound     ",use_sound,0));
316       menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music     ",use_music,0));
317     }
318   else
319     {
320       menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound     ",use_sound,0));
321       menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music     ",use_music,0));
322     }
323   menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS  ",show_fps,0));
324   menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
325   menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
326
327   menu_init(&load_game_menu);
328   menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
329   menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
330   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
331   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
332   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
333   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
334   menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
335   menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
336   menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
337
338   menu_init(&save_game_menu);
339   menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
340   menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
341   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
342   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
343   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
344   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
345   menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
346   menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
347   menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
348
349   menu_init(&game_menu);
350   menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
351   menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
352   menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
353   menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
354   menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
355   menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
356   menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
357   menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
358
359   menu_init(&highscore_menu);
360   menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
361
362 }
363
364 void update_load_save_game_menu(menu_type* pmenu, int load)
365 {
366   int i;
367
368   for(i = 2; i < 7; ++i)
369     {
370       char *tmp;
371       slotinfo(&tmp,i-1);
372       if(load && strlen(tmp) == strlen("Slot X - Free") )
373         pmenu->item[i].kind = MN_DEACTIVE;
374       else
375         pmenu->item[i].kind = MN_ACTION;
376       menu_item_change_text(&pmenu->item[i],tmp);
377       free(tmp);
378     }
379 }
380
381 void process_save_load_game_menu(int save)
382 {
383   int slot;
384   switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
385     {
386     default:
387       if(slot != -1)
388         {
389           if(save == YES)
390             {
391               savegame(slot - 1);
392             }
393           else
394             {
395               if(game_started == NO)
396                 {
397                   gameloop("default",slot - 1,ST_GL_LOAD_GAME);
398                   show_menu = YES;
399                   menu_set_current(&main_menu);
400                 }
401               else
402                 loadgame(slot - 1);
403             }
404           st_pause_ticks_stop();
405         }
406       break;
407     }
408 }
409
410 /* Handle changes made to global settings in the options menu. */
411 void process_options_menu(void)
412 {
413   switch (menu_check(&options_menu))
414     {
415     case 2:
416       if(use_fullscreen != options_menu.item[2].toggled)
417         {
418           use_fullscreen = !use_fullscreen;
419           st_video_setup();
420         }
421       break;
422     case 3:
423       if(use_sound != options_menu.item[3].toggled)
424         use_sound = !use_sound;
425       break;
426     case 4:
427       if(use_music != options_menu.item[4].toggled)
428         {
429           if(use_music == YES)
430             {
431               if(playing_music())
432                 {
433                   halt_music();
434                 }
435               use_music = NO;
436             }
437           else
438             {
439               use_music = YES;
440               if (!playing_music())
441                 {
442                   play_current_music();
443                 }
444             }
445         }
446       break;
447     case 5:
448       if(show_fps != options_menu.item[5].toggled)
449         show_fps = !show_fps;
450       break;
451     }
452 }
453
454 void st_general_setup(void)
455 {
456   /* Seed random number generator: */
457
458   srand(SDL_GetTicks());
459
460   /* Set icon image: */
461
462   seticon();
463
464   /* Unicode needed for input handling: */
465
466   SDL_EnableUNICODE(1);
467
468   /* Load global images: */
469
470   text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
471   text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
472   text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
473   text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
474   text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
475   text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
476   text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
477   text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
478
479   /* Load GUI/menu images: */
480   texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
481   texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
482   texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
483   texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
484   texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA);
485
486 }
487
488 void st_general_free(void)
489 {
490
491   /* Free global images: */
492
493   text_free(&black_text);
494   text_free(&gold_text);
495   text_free(&white_text);
496   text_free(&blue_text);
497   text_free(&red_text);
498   text_free(&white_small_text);
499   text_free(&white_big_text);
500
501   /* Free GUI/menu images: */
502   texture_free(&checkbox);
503   texture_free(&checkbox_checked);
504   texture_free(&back);
505   texture_free(&arrow_left);
506   texture_free(&arrow_right);
507
508   /* Free menus */
509
510   menu_free(&main_menu);
511   menu_free(&game_menu);
512   menu_free(&options_menu);
513   menu_free(&highscore_menu);
514   menu_free(&save_game_menu);
515   menu_free(&load_game_menu);
516
517 }
518
519 void st_video_setup(void)
520 {
521
522   if(screen != NULL)
523     SDL_FreeSurface(screen);
524
525   /* Init SDL Video: */
526
527   if (SDL_Init(SDL_INIT_VIDEO) < 0)
528     {
529       fprintf(stderr,
530               "\nError: I could not initialize video!\n"
531               "The Simple DirectMedia error that occured was:\n"
532               "%s\n\n", SDL_GetError());
533       exit(1);
534     }
535
536   /* Open display: */
537
538   if(use_gl)
539     st_video_setup_gl();
540   else
541     st_video_setup_sdl();
542
543   texture_setup();
544
545   /* Set window manager stuff: */
546
547   SDL_WM_SetCaption("Super Tux", "Super Tux");
548
549 }
550
551 void st_video_setup_sdl(void)
552 {
553   SDL_FreeSurface(screen);
554
555   if (use_fullscreen == YES)
556     {
557       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
558       if (screen == NULL)
559         {
560           fprintf(stderr,
561                   "\nWarning: I could not set up fullscreen video for "
562                   "640x480 mode.\n"
563                   "The Simple DirectMedia error that occured was:\n"
564                   "%s\n\n", SDL_GetError());
565           use_fullscreen = NO;
566         }
567     }
568   else
569     {
570       screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
571
572       if (screen == NULL)
573         {
574           fprintf(stderr,
575                   "\nError: I could not set up video for 640x480 mode.\n"
576                   "The Simple DirectMedia error that occured was:\n"
577                   "%s\n\n", SDL_GetError());
578           exit(1);
579         }
580     }
581 }
582
583 void st_video_setup_gl(void)
584 {
585 #ifndef NOOPENGL
586
587   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
588   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
589   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
590   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
591   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
592
593   if (use_fullscreen == YES)
594     {
595       screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
596       if (screen == NULL)
597         {
598           fprintf(stderr,
599                   "\nWarning: I could not set up fullscreen video for "
600                   "640x480 mode.\n"
601                   "The Simple DirectMedia error that occured was:\n"
602                   "%s\n\n", SDL_GetError());
603           use_fullscreen = NO;
604         }
605     }
606   else
607     {
608       screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
609
610       if (screen == NULL)
611         {
612           fprintf(stderr,
613                   "\nError: I could not set up video for 640x480 mode.\n"
614                   "The Simple DirectMedia error that occured was:\n"
615                   "%s\n\n", SDL_GetError());
616           exit(1);
617         }
618     }
619
620   /*
621    * Set up OpenGL for 2D rendering.
622    */
623   glDisable(GL_DEPTH_TEST);
624   glDisable(GL_CULL_FACE);
625
626   glViewport(0, 0, screen->w, screen->h);
627   glMatrixMode(GL_PROJECTION);
628   glLoadIdentity();
629   glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
630
631   glMatrixMode(GL_MODELVIEW);
632   glLoadIdentity();
633   glTranslatef(0.0f, 0.0f, 0.0f);
634
635 #endif
636
637 }
638
639 void st_joystick_setup(void)
640 {
641
642   /* Init Joystick: */
643
644 #ifdef JOY_YES
645   use_joystick = YES;
646
647   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
648     {
649       fprintf(stderr, "Warning: I could not initialize joystick!\n"
650               "The Simple DirectMedia error that occured was:\n"
651               "%s\n\n", SDL_GetError());
652
653       use_joystick = NO;
654     }
655   else
656     {
657       /* Open joystick: */
658
659       if (SDL_NumJoysticks() <= 0)
660         {
661           fprintf(stderr, "Warning: No joysticks are available.\n");
662
663           use_joystick = NO;
664         }
665       else
666         {
667           js = SDL_JoystickOpen(0);
668
669           if (js == NULL)
670             {
671               fprintf(stderr, "Warning: Could not open joystick 1.\n"
672                       "The Simple DirectMedia error that occured was:\n"
673                       "%s\n\n", SDL_GetError());
674
675               use_joystick = NO;
676             }
677           else
678             {
679               /* Check for proper joystick configuration: */
680
681               if (SDL_JoystickNumAxes(js) < 2)
682                 {
683                   fprintf(stderr,
684                           "Warning: Joystick does not have enough axes!\n");
685
686                   use_joystick = NO;
687                 }
688               else
689                 {
690                   if (SDL_JoystickNumButtons(js) < 2)
691                     {
692                       fprintf(stderr,
693                               "Warning: "
694                               "Joystick does not have enough buttons!\n");
695
696                       use_joystick = NO;
697                     }
698                 }
699             }
700         }
701     }
702 #endif
703
704 }
705
706 void st_audio_setup(void)
707 {
708
709   /* Init SDL Audio silently even if --disable-sound : */
710
711   if (audio_device == YES)
712     {
713       if (SDL_Init(SDL_INIT_AUDIO) < 0)
714         {
715           /* only print out message if sound or music
716              was not disabled at command-line
717            */
718           if (use_sound == YES || use_music == YES)
719             {
720               fprintf(stderr,
721                       "\nWarning: I could not initialize audio!\n"
722                       "The Simple DirectMedia error that occured was:\n"
723                       "%s\n\n", SDL_GetError());
724             }
725           /* keep the programming logic the same :-)
726              because in this case, use_sound & use_music' values are ignored
727              when there's no available audio device
728           */
729           use_sound = NO;
730           use_music = NO;
731           audio_device = NO;
732         }
733     }
734
735
736   /* Open sound silently regarless the value of "use_sound": */
737
738   if (audio_device == YES)
739     {
740       if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
741         {
742           /* only print out message if sound or music
743              was not disabled at command-line
744            */
745           if ((use_sound == YES) || (use_music == YES))
746             {
747               fprintf(stderr,
748                       "\nWarning: I could not set up audio for 44100 Hz "
749                       "16-bit stereo.\n"
750                       "The Simple DirectMedia error that occured was:\n"
751                       "%s\n\n", SDL_GetError());
752             }
753           use_sound = NO;
754           use_music = NO;
755           audio_device = NO;
756         }
757     }
758
759 }
760
761
762 /* --- SHUTDOWN --- */
763
764 void st_shutdown(void)
765 {
766   close_audio();
767   SDL_Quit();
768 }
769
770
771 /* --- ABORT! --- */
772
773 void st_abort(char * reason, char * details)
774 {
775   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
776   st_shutdown();
777   exit(1);
778 }
779
780
781 /* Set Icon (private) */
782
783 void seticon(void)
784 {
785   int masklen;
786   Uint8 * mask;
787   SDL_Surface * icon;
788
789
790   /* Load icon into a surface: */
791
792   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
793   if (icon == NULL)
794     {
795       fprintf(stderr,
796               "\nError: I could not load the icon image: %s\n"
797               "The Simple DirectMedia error that occured was:\n"
798               "%s\n\n", DATA_PREFIX "/images/icon.png", SDL_GetError());
799       exit(1);
800     }
801
802
803   /* Create mask: */
804
805   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
806   mask = (Uint8*) malloc(masklen * sizeof(Uint8));
807   memset(mask, 0xFF, masklen);
808
809
810   /* Set icon: */
811
812   SDL_WM_SetIcon(icon, mask);
813
814
815   /* Free icon surface & mask: */
816
817   free(mask);
818   SDL_FreeSurface(icon);
819 }
820
821
822 /* Parse command-line arguments: */
823
824 void parseargs(int argc, char * argv[])
825 {
826   int i;
827
828   /* Set defaults: */
829
830
831   debug_mode = NO;
832   use_fullscreen = NO;
833   show_fps = NO;
834   use_gl = NO;
835
836 #ifndef NOSOUND
837
838   use_sound = YES;
839   use_music = YES;
840   audio_device = YES;
841 #else
842
843   use_sound = NO;
844   use_music = NO;
845   audio_device = NO;
846 #endif
847
848   /* Parse arguments: */
849
850   for (i = 1; i < argc; i++)
851     {
852       if (strcmp(argv[i], "--fullscreen") == 0 ||
853           strcmp(argv[i], "-f") == 0)
854         {
855           /* Use full screen: */
856
857           use_fullscreen = YES;
858         }
859       else if (strcmp(argv[i], "--show-fps") == 0)
860         {
861           /* Use full screen: */
862
863           show_fps = YES;
864         }
865       else if (strcmp(argv[i], "--opengl") == 0 ||
866                strcmp(argv[i], "-gl") == 0)
867         {
868 #ifndef NOOPENGL
869           /* Use OpengGL: */
870
871           use_gl = YES;
872 #endif
873
874         }
875       else if (strcmp(argv[i], "--usage") == 0)
876         {
877           /* Show usage: */
878
879           usage(argv[0], 0);
880         }
881       else if (strcmp(argv[i], "--version") == 0)
882         {
883           /* Show version: */
884
885           printf("Super Tux - version " VERSION "\n");
886           exit(0);
887         }
888       else if (strcmp(argv[i], "--disable-sound") == 0)
889         {
890           /* Disable the compiled in sound feature */
891 #ifndef NOSOUND
892           printf("Sounds disabled \n");
893           use_sound = NO;
894 #else
895
896           printf("Warning: Sound capability has not been compiled into this build.\n");
897 #endif
898
899         }
900       else if (strcmp(argv[i], "--disable-music") == 0)
901         {
902           /* Disable the compiled in sound feature */
903 #ifndef NOSOUND
904           printf("Music disabled \n");
905           use_music = NO;
906 #else
907
908           printf("Warning: Music feature is not compiled in \n");
909 #endif
910
911         }
912       else if (strcmp(argv[i], "--debug-mode") == 0)
913         {
914           /* Enable the debug-mode */
915           debug_mode = YES;
916
917         }
918       else if (strcmp(argv[i], "--help") == 0)
919         {         /* Show help: */
920
921           printf("Super Tux " VERSION "\n\n");
922
923           printf("----------  Command-line options  ----------\n\n");
924
925           printf("  --opengl            - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
926
927           printf("  --disable-sound     - If sound support was compiled in,  this will\n                        disable sound for this session of the game.\n\n");
928
929           printf("  --disable-music     - Like above, but this will disable music.\n\n");
930
931           printf("  --fullscreen        - Run in fullscreen mode.\n\n");
932
933           printf("  --debug-mode        - Enables the debug-mode, which is useful for developers.\n\n");
934
935           printf("  --help              - Display a help message summarizing command-line\n                        options, license and game controls.\n\n");
936
937           printf("  --usage             - Display a brief message summarizing command-line options.\n\n");
938
939           printf("  --version           - Display the version of SuperTux you're running.\n\n\n");
940
941
942           printf("----------          License       ----------\n\n");
943           printf("  This program comes with ABSOLUTELY NO WARRANTY.\n");
944           printf("  This is free software, and you are welcome to redistribute\n");
945           printf("  or modify it under certain conditions. See the file \n");
946           printf("  \"COPYING.txt\" for more details.\n\n\n");
947
948           printf("----------      Game controls     ----------\n\n");
949           printf("  Please see the file \"README.txt\"\n\n");
950
951           exit(0);
952         }
953       else
954         {
955           /* Unknown - complain! */
956
957           usage(argv[0], 1);
958         }
959     }
960 }
961
962
963 /* Display usage: */
964
965 void usage(char * prog, int ret)
966 {
967   FILE * fi;
968
969
970   /* Determine which stream to write to: */
971
972   if (ret == 0)
973     fi = stdout;
974   else
975     fi = stderr;
976
977
978   /* Display the usage message: */
979
980   fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",
981           prog);
982
983
984   /* Quit! */
985
986   exit(ret);
987 }
988