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