7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - March 15, 2004
20 #include <SDL_image.h>
22 #include <SDL_opengl.h>
26 #include <sys/types.h>
42 #define mkdir(dir, mode) mkdir(dir)
43 // on win32 we typically don't want LFS paths
45 #define DATA_PREFIX "./data"
48 /* Local function prototypes: */
51 void usage(char * prog, int ret);
53 /* Does the given file exist and is it accessible? */
54 int faccessible(const char *filename)
57 if (stat(filename, &filestat) == -1)
63 if(S_ISREG(filestat.st_mode))
70 /* Can we write to this location? */
71 int fwriteable(const char *filename)
74 fi = fopen(filename, "wa");
82 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
83 int fcreatedir(const char* relative_dir)
86 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
87 if(mkdir(path,0755) != 0)
89 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
90 if(mkdir(path,0755) != 0)
105 /* Get all names of sub-directories in a certain directory. */
106 /* Returns the number of sub-directories found. */
107 /* Note: The user has to free the allocated space. */
108 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
111 struct dirent *direntp;
112 string_list_type sdirs;
116 string_list_init(&sdirs);
117 sprintf(path,"%s/%s",st_dir,rel_path);
118 if((dirStructP = opendir(path)) != NULL)
120 while((direntp = readdir(dirStructP)) != NULL)
122 char absolute_filename[1024];
125 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
127 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
129 if(expected_file != NULL)
131 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
132 if(!faccessible(filename))
136 string_list_add_item(&sdirs,direntp->d_name);
139 closedir(dirStructP);
142 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
143 if((dirStructP = opendir(path)) != NULL)
145 while((direntp = readdir(dirStructP)) != NULL)
147 char absolute_filename[1024];
150 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
152 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
154 if(expected_file != NULL)
156 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
157 if(!faccessible(filename))
163 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
164 if(faccessible(filename))
169 string_list_add_item(&sdirs,direntp->d_name);
172 closedir(dirStructP);
178 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
181 struct dirent *direntp;
182 string_list_type sdirs;
185 string_list_init(&sdirs);
186 sprintf(path,"%s/%s",st_dir,rel_path);
187 if((dirStructP = opendir(path)) != NULL)
189 while((direntp = readdir(dirStructP)) != NULL)
191 char absolute_filename[1024];
194 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
196 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
198 if(exception_str != NULL)
200 if(strstr(direntp->d_name,exception_str) != NULL)
204 if(strstr(direntp->d_name,glob) == NULL)
207 string_list_add_item(&sdirs,direntp->d_name);
210 closedir(dirStructP);
213 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
214 if((dirStructP = opendir(path)) != NULL)
216 while((direntp = readdir(dirStructP)) != NULL)
218 char absolute_filename[1024];
221 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
223 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
225 if(exception_str != NULL)
227 if(strstr(direntp->d_name,exception_str) != NULL)
231 if(strstr(direntp->d_name,glob) == NULL)
234 string_list_add_item(&sdirs,direntp->d_name);
237 closedir(dirStructP);
243 void free_strings(char **strings, int num)
246 for(i=0; i < num; ++i)
251 /* Set SuperTux configuration and save directories */
252 void st_directory_setup(void)
256 /* Get home directory (from $HOME variable)... if we can't determine it,
257 use the current directory ("."): */
258 if (getenv("HOME") != NULL)
259 home = getenv("HOME");
263 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
264 strlen("/.supertux") + 1));
265 strcpy(st_dir, home);
266 strcat(st_dir, "/.supertux");
268 /* Remove .supertux config-file from old SuperTux versions */
269 if(faccessible(st_dir))
275 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
277 strcpy(st_save_dir,st_dir);
278 strcat(st_save_dir,"/save");
280 /* Create them. In the case they exist they won't destroy anything. */
282 mkdir(st_save_dir, 0755);
284 sprintf(str, "%s/levels", st_dir);
287 // User has not that a datadir, so we try some magic
292 char exe_file[PATH_MAX];
293 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
295 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
296 datadir = DATA_PREFIX;
300 std::string exedir = std::string(dirname(exe_file)) + "/";
302 datadir = exedir + "../data/"; // SuperTux run from source dir
303 if (access(datadir.c_str(), F_OK) != 0)
305 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
306 if (access(datadir.c_str(), F_OK) != 0)
307 { // If all fails, fall back to compiled path
308 datadir = DATA_PREFIX;
313 datadir = DATA_PREFIX;
316 printf("Datadir: %s\n", datadir.c_str());
319 /* Create and setup menus. */
322 main_menu = new Menu();
323 options_menu = new Menu();
324 load_game_menu = new Menu();
325 save_game_menu = new Menu();
326 game_menu = new Menu();
327 highscore_menu = new Menu();
329 main_menu->additem(MN_LABEL,"Main Menu",0,0);
330 main_menu->additem(MN_HL,"",0,0);
331 main_menu->additem(MN_ACTION,"Start Game",0,0);
332 main_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
333 main_menu->additem(MN_GOTO,"Options",0,options_menu);
334 main_menu->additem(MN_ACTION,"Level editor",0,0);
335 main_menu->additem(MN_ACTION,"Credits",0,0);
336 main_menu->additem(MN_HL,"",0,0);
337 main_menu->additem(MN_ACTION,"Quit",0,0);
339 options_menu->additem(MN_LABEL,"Options",0,0);
340 options_menu->additem(MN_HL,"",0,0);
341 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
344 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
345 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
349 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
350 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
352 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
353 options_menu->additem(MN_HL,"",0,0);
354 options_menu->additem(MN_BACK,"Back",0,0);
356 load_game_menu->additem(MN_LABEL,"Load Game",0,0);
357 load_game_menu->additem(MN_HL,"",0,0);
358 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
359 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
360 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
361 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
362 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
363 load_game_menu->additem(MN_HL,"",0,0);
364 load_game_menu->additem(MN_BACK,"Back",0,0);
366 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
367 save_game_menu->additem(MN_HL,"",0,0);
368 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
369 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
370 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
371 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
372 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
373 save_game_menu->additem(MN_HL,"",0,0);
374 save_game_menu->additem(MN_BACK,"Back",0,0);
376 game_menu->additem(MN_LABEL,"InGame Menu",0,0);
377 game_menu->additem(MN_HL,"",0,0);
378 game_menu->additem(MN_ACTION,"Return To Game",0,0);
379 game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
380 game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
381 game_menu->additem(MN_GOTO,"Options",0,options_menu);
382 game_menu->additem(MN_HL,"",0,0);
383 game_menu->additem(MN_ACTION,"Quit Game",0,0);
385 highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
388 void update_load_save_game_menu(Menu* pmenu, int load)
390 for(int i = 2; i < 7; ++i)
394 if(load && strlen(tmp) == strlen("Slot X - Free") )
395 pmenu->item[i].kind = MN_DEACTIVE;
397 pmenu->item[i].kind = MN_ACTION;
398 menu_item_change_text(&pmenu->item[i],tmp);
403 void process_save_load_game_menu(int save)
406 switch (slot = (save ? save_game_menu->check() : load_game_menu->check()))
417 if(game_started == false)
419 gameloop("default",slot - 1,ST_GL_LOAD_GAME);
421 Menu::set_current(main_menu);
426 st_pause_ticks_stop();
432 /* Handle changes made to global settings in the options menu. */
433 void process_options_menu(void)
435 switch (options_menu->check())
438 if(use_fullscreen != options_menu->item[2].toggled)
440 use_fullscreen = !use_fullscreen;
445 if(use_sound != options_menu->item[3].toggled)
446 use_sound = !use_sound;
449 if(use_music != options_menu->item[4].toggled)
451 if(use_music == true)
462 if (!playing_music())
464 play_current_music();
470 if(show_fps != options_menu->item[5].toggled)
471 show_fps = !show_fps;
476 void st_general_setup(void)
478 /* Seed random number generator: */
480 srand(SDL_GetTicks());
482 /* Set icon image: */
486 /* Unicode needed for input handling: */
488 SDL_EnableUNICODE(1);
490 /* Load global images: */
492 text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
493 text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
494 text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
495 text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
496 text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
497 text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
498 text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
499 text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
501 /* Load GUI/menu images: */
502 texture_load(&checkbox, datadir + "/images/status/checkbox.png", USE_ALPHA);
503 texture_load(&checkbox_checked, datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
504 texture_load(&back, datadir + "/images/status/back.png", USE_ALPHA);
505 texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA);
506 texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA);
510 void st_general_free(void)
513 /* Free global images: */
515 text_free(&black_text);
516 text_free(&gold_text);
517 text_free(&white_text);
518 text_free(&blue_text);
519 text_free(&red_text);
520 text_free(&white_small_text);
521 text_free(&white_big_text);
523 /* Free GUI/menu images: */
524 texture_free(&checkbox);
525 texture_free(&checkbox_checked);
527 texture_free(&arrow_left);
528 texture_free(&arrow_right);
534 delete highscore_menu;
535 delete save_game_menu;
536 delete load_game_menu;
539 void st_video_setup(void)
543 SDL_FreeSurface(screen);
545 /* Init SDL Video: */
547 if (SDL_Init(SDL_INIT_VIDEO) < 0)
550 "\nError: I could not initialize video!\n"
551 "The Simple DirectMedia error that occured was:\n"
552 "%s\n\n", SDL_GetError());
561 st_video_setup_sdl();
565 /* Set window manager stuff: */
567 SDL_WM_SetCaption("Super Tux", "Super Tux");
571 void st_video_setup_sdl(void)
573 SDL_FreeSurface(screen);
575 if (use_fullscreen == true)
577 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
581 "\nWarning: I could not set up fullscreen video for "
583 "The Simple DirectMedia error that occured was:\n"
584 "%s\n\n", SDL_GetError());
585 use_fullscreen = false;
590 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
595 "\nError: I could not set up video for 640x480 mode.\n"
596 "The Simple DirectMedia error that occured was:\n"
597 "%s\n\n", SDL_GetError());
603 void st_video_setup_gl(void)
607 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
608 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
609 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
610 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
611 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
613 if (use_fullscreen == true)
615 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
619 "\nWarning: I could not set up fullscreen video for "
621 "The Simple DirectMedia error that occured was:\n"
622 "%s\n\n", SDL_GetError());
623 use_fullscreen = false;
628 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
633 "\nError: I could not set up video for 640x480 mode.\n"
634 "The Simple DirectMedia error that occured was:\n"
635 "%s\n\n", SDL_GetError());
641 * Set up OpenGL for 2D rendering.
643 glDisable(GL_DEPTH_TEST);
644 glDisable(GL_CULL_FACE);
646 glViewport(0, 0, screen->w, screen->h);
647 glMatrixMode(GL_PROJECTION);
649 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
651 glMatrixMode(GL_MODELVIEW);
653 glTranslatef(0.0f, 0.0f, 0.0f);
659 void st_joystick_setup(void)
666 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
668 fprintf(stderr, "Warning: I could not initialize joystick!\n"
669 "The Simple DirectMedia error that occured was:\n"
670 "%s\n\n", SDL_GetError());
672 use_joystick = false;
677 if (SDL_NumJoysticks() <= 0)
679 fprintf(stderr, "Warning: No joysticks are available.\n");
681 use_joystick = false;
685 js = SDL_JoystickOpen(joystick_num);
689 fprintf(stderr, "Warning: Could not open joystick %d.\n"
690 "The Simple DirectMedia error that occured was:\n"
691 "%s\n\n", joystick_num, SDL_GetError());
693 use_joystick = false;
697 /* Check for proper joystick configuration: */
699 if (SDL_JoystickNumAxes(js) < 2)
702 "Warning: Joystick does not have enough axes!\n");
704 use_joystick = false;
708 if (SDL_JoystickNumButtons(js) < 2)
712 "Joystick does not have enough buttons!\n");
714 use_joystick = false;
722 void st_audio_setup(void)
725 /* Init SDL Audio silently even if --disable-sound : */
727 if (audio_device == true)
729 if (SDL_Init(SDL_INIT_AUDIO) < 0)
731 /* only print out message if sound or music
732 was not disabled at command-line
734 if (use_sound || use_music)
737 "\nWarning: I could not initialize audio!\n"
738 "The Simple DirectMedia error that occured was:\n"
739 "%s\n\n", SDL_GetError());
741 /* keep the programming logic the same :-)
742 because in this case, use_sound & use_music' values are ignored
743 when there's no available audio device
747 audio_device = false;
752 /* Open sound silently regarless the value of "use_sound": */
754 if (audio_device == true)
756 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
758 /* only print out message if sound or music
759 was not disabled at command-line
761 if ((use_sound == true) || (use_music == true))
764 "\nWarning: I could not set up audio for 44100 Hz "
766 "The Simple DirectMedia error that occured was:\n"
767 "%s\n\n", SDL_GetError());
771 audio_device = false;
778 /* --- SHUTDOWN --- */
780 void st_shutdown(void)
789 void st_abort(const std::string& reason, const std::string& details)
791 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
797 /* Set Icon (private) */
806 /* Load icon into a surface: */
808 icon = IMG_Load((datadir + "/images/icon.png").c_str());
812 "\nError: I could not load the icon image: %s%s\n"
813 "The Simple DirectMedia error that occured was:\n"
814 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
821 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
822 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
823 memset(mask, 0xFF, masklen);
828 SDL_WM_SetIcon(icon, mask);
831 /* Free icon surface & mask: */
834 SDL_FreeSurface(icon);
838 /* Parse command-line arguments: */
840 void parseargs(int argc, char * argv[])
848 use_fullscreen = false;
856 /* Parse arguments: */
858 for (i = 1; i < argc; i++)
860 if (strcmp(argv[i], "--fullscreen") == 0 ||
861 strcmp(argv[i], "-f") == 0)
863 /* Use full screen: */
865 use_fullscreen = true;
867 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
870 joystick_num = atoi(argv[++i]);
872 else if (strcmp(argv[i], "--worldmap") == 0)
874 launch_worldmap_mode = true;
876 else if (strcmp(argv[i], "--datadir") == 0
877 || strcmp(argv[i], "-d") == 0 )
882 else if (strcmp(argv[i], "--show-fps") == 0)
884 /* Use full screen: */
888 else if (strcmp(argv[i], "--opengl") == 0 ||
889 strcmp(argv[i], "-gl") == 0)
898 else if (strcmp(argv[i], "--usage") == 0)
904 else if (strcmp(argv[i], "--version") == 0)
908 printf("Super Tux - version " VERSION "\n");
911 else if (strcmp(argv[i], "--disable-sound") == 0)
913 /* Disable the compiled in sound feature */
914 printf("Sounds disabled \n");
917 else if (strcmp(argv[i], "--disable-music") == 0)
919 /* Disable the compiled in sound feature */
920 printf("Music disabled \n");
923 else if (strcmp(argv[i], "--debug-mode") == 0)
925 /* Enable the debug-mode */
929 else if (strcmp(argv[i], "--help") == 0)
931 puts("Super Tux " VERSION "\n"
932 " Please see the file \"README.txt\" for more details.\n");
933 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
934 puts("Display Options:\n"
935 " --fullscreen Run in fullscreen mode.\n"
936 " --opengl If opengl support was compiled in, this will enable\n"
937 " the EXPERIMENTAL OpenGL mode.\n"
940 " --disable-sound If sound support was compiled in, this will\n"
941 " disable sound for this session of the game.\n"
942 " --disable-music Like above, but this will disable music.\n"
945 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
946 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
947 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
948 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
949 " --help Display a help message summarizing command-line\n"
950 " options, license and game controls.\n"
951 " --usage Display a brief message summarizing command-line options.\n"
952 " --version Display the version of SuperTux you're running.\n\n"
956 else if (argv[i][0] != '-')
958 level_startup_file = argv[i];
962 /* Unknown - complain! */
972 void usage(char * prog, int ret)
977 /* Determine which stream to write to: */
985 /* Display the usage message: */
987 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",