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