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 menu_init(&main_menu);
323 menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
324 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
325 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
326 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
327 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
328 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
329 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
330 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
331 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
333 menu_init(&options_menu);
334 menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
335 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
336 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
337 if(audio_device == YES)
339 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound ",use_sound,0));
340 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music ",use_music,0));
344 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound ",use_sound,0));
345 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music ",use_music,0));
347 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS ",show_fps,0));
348 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
349 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
351 menu_init(&load_game_menu);
352 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
353 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
354 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
355 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
356 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
357 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
358 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
359 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
360 menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
362 menu_init(&save_game_menu);
363 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
364 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
365 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
366 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
367 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
368 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
369 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
370 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
371 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
373 menu_init(&game_menu);
374 menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
375 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
376 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
377 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
378 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
379 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
380 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
381 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
383 menu_init(&highscore_menu);
384 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
388 void update_load_save_game_menu(menu_type* pmenu, int load)
392 for(i = 2; i < 7; ++i)
396 if(load && strlen(tmp) == strlen("Slot X - Free") )
397 pmenu->item[i].kind = MN_DEACTIVE;
399 pmenu->item[i].kind = MN_ACTION;
400 menu_item_change_text(&pmenu->item[i],tmp);
405 void process_save_load_game_menu(int save)
408 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
419 if(game_started == NO)
421 gameloop("default",slot - 1,ST_GL_LOAD_GAME);
423 menu_set_current(&main_menu);
428 st_pause_ticks_stop();
434 /* Handle changes made to global settings in the options menu. */
435 void process_options_menu(void)
437 switch (menu_check(&options_menu))
440 if(use_fullscreen != options_menu.item[2].toggled)
442 use_fullscreen = !use_fullscreen;
447 if(use_sound != options_menu.item[3].toggled)
448 use_sound = !use_sound;
451 if(use_music != options_menu.item[4].toggled)
464 if (!playing_music())
466 play_current_music();
472 if(show_fps != options_menu.item[5].toggled)
473 show_fps = !show_fps;
478 void st_general_setup(void)
480 /* Seed random number generator: */
482 srand(SDL_GetTicks());
484 /* Set icon image: */
488 /* Unicode needed for input handling: */
490 SDL_EnableUNICODE(1);
492 /* Load global images: */
494 text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
495 text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
496 text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
497 text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
498 text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
499 text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
500 text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
501 text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
503 /* Load GUI/menu images: */
504 texture_load(&checkbox, datadir + "/images/status/checkbox.png", USE_ALPHA);
505 texture_load(&checkbox_checked, datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
506 texture_load(&back, datadir + "/images/status/back.png", USE_ALPHA);
507 texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA);
508 texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA);
512 void st_general_free(void)
515 /* Free global images: */
517 text_free(&black_text);
518 text_free(&gold_text);
519 text_free(&white_text);
520 text_free(&blue_text);
521 text_free(&red_text);
522 text_free(&white_small_text);
523 text_free(&white_big_text);
525 /* Free GUI/menu images: */
526 texture_free(&checkbox);
527 texture_free(&checkbox_checked);
529 texture_free(&arrow_left);
530 texture_free(&arrow_right);
534 menu_free(&main_menu);
535 menu_free(&game_menu);
536 menu_free(&options_menu);
537 menu_free(&highscore_menu);
538 menu_free(&save_game_menu);
539 menu_free(&load_game_menu);
543 void st_video_setup(void)
547 SDL_FreeSurface(screen);
549 /* Init SDL Video: */
551 if (SDL_Init(SDL_INIT_VIDEO) < 0)
554 "\nError: I could not initialize video!\n"
555 "The Simple DirectMedia error that occured was:\n"
556 "%s\n\n", SDL_GetError());
565 st_video_setup_sdl();
569 /* Set window manager stuff: */
571 SDL_WM_SetCaption("Super Tux", "Super Tux");
575 void st_video_setup_sdl(void)
577 SDL_FreeSurface(screen);
579 if (use_fullscreen == YES)
581 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
585 "\nWarning: I could not set up fullscreen video for "
587 "The Simple DirectMedia error that occured was:\n"
588 "%s\n\n", SDL_GetError());
594 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
599 "\nError: I could not set up video for 640x480 mode.\n"
600 "The Simple DirectMedia error that occured was:\n"
601 "%s\n\n", SDL_GetError());
607 void st_video_setup_gl(void)
611 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
612 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
613 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
614 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
615 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
617 if (use_fullscreen == YES)
619 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
623 "\nWarning: I could not set up fullscreen video for "
625 "The Simple DirectMedia error that occured was:\n"
626 "%s\n\n", SDL_GetError());
632 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
637 "\nError: I could not set up video for 640x480 mode.\n"
638 "The Simple DirectMedia error that occured was:\n"
639 "%s\n\n", SDL_GetError());
645 * Set up OpenGL for 2D rendering.
647 glDisable(GL_DEPTH_TEST);
648 glDisable(GL_CULL_FACE);
650 glViewport(0, 0, screen->w, screen->h);
651 glMatrixMode(GL_PROJECTION);
653 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
655 glMatrixMode(GL_MODELVIEW);
657 glTranslatef(0.0f, 0.0f, 0.0f);
663 void st_joystick_setup(void)
670 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
672 fprintf(stderr, "Warning: I could not initialize joystick!\n"
673 "The Simple DirectMedia error that occured was:\n"
674 "%s\n\n", SDL_GetError());
681 if (SDL_NumJoysticks() <= 0)
683 fprintf(stderr, "Warning: No joysticks are available.\n");
689 js = SDL_JoystickOpen(0);
693 fprintf(stderr, "Warning: Could not open joystick 1.\n"
694 "The Simple DirectMedia error that occured was:\n"
695 "%s\n\n", SDL_GetError());
701 /* Check for proper joystick configuration: */
703 if (SDL_JoystickNumAxes(js) < 2)
706 "Warning: Joystick does not have enough axes!\n");
712 if (SDL_JoystickNumButtons(js) < 2)
716 "Joystick does not have enough buttons!\n");
726 void st_audio_setup(void)
729 /* Init SDL Audio silently even if --disable-sound : */
731 if (audio_device == YES)
733 if (SDL_Init(SDL_INIT_AUDIO) < 0)
735 /* only print out message if sound or music
736 was not disabled at command-line
738 if (use_sound == YES || use_music == YES)
741 "\nWarning: I could not initialize audio!\n"
742 "The Simple DirectMedia error that occured was:\n"
743 "%s\n\n", SDL_GetError());
745 /* keep the programming logic the same :-)
746 because in this case, use_sound & use_music' values are ignored
747 when there's no available audio device
756 /* Open sound silently regarless the value of "use_sound": */
758 if (audio_device == YES)
760 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
762 /* only print out message if sound or music
763 was not disabled at command-line
765 if ((use_sound == YES) || (use_music == YES))
768 "\nWarning: I could not set up audio for 44100 Hz "
770 "The Simple DirectMedia error that occured was:\n"
771 "%s\n\n", SDL_GetError());
782 /* --- SHUTDOWN --- */
784 void st_shutdown(void)
793 void st_abort(const std::string& reason, const std::string& details)
795 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
801 /* Set Icon (private) */
810 /* Load icon into a surface: */
812 icon = IMG_Load((datadir + "/images/icon.png").c_str());
816 "\nError: I could not load the icon image: %s%s\n"
817 "The Simple DirectMedia error that occured was:\n"
818 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
825 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
826 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
827 memset(mask, 0xFF, masklen);
832 SDL_WM_SetIcon(icon, mask);
835 /* Free icon surface & mask: */
838 SDL_FreeSurface(icon);
842 /* Parse command-line arguments: */
844 void parseargs(int argc, char * argv[])
860 /* Parse arguments: */
862 for (i = 1; i < argc; i++)
864 if (strcmp(argv[i], "--fullscreen") == 0 ||
865 strcmp(argv[i], "-f") == 0)
867 /* Use full screen: */
869 use_fullscreen = YES;
871 else if (strcmp(argv[i], "--worldmap") == 0)
873 launch_worldmap_mode = true;
875 else if (strcmp(argv[i], "--datadir") == 0
876 || strcmp(argv[i], "-d") == 0 )
881 else if (strcmp(argv[i], "--show-fps") == 0)
883 /* Use full screen: */
887 else if (strcmp(argv[i], "--opengl") == 0 ||
888 strcmp(argv[i], "-gl") == 0)
897 else if (strcmp(argv[i], "--usage") == 0)
903 else if (strcmp(argv[i], "--version") == 0)
907 printf("Super Tux - version " VERSION "\n");
910 else if (strcmp(argv[i], "--disable-sound") == 0)
912 /* Disable the compiled in sound feature */
913 printf("Sounds disabled \n");
916 else if (strcmp(argv[i], "--disable-music") == 0)
918 /* Disable the compiled in sound feature */
919 printf("Music disabled \n");
922 else if (strcmp(argv[i], "--debug-mode") == 0)
924 /* Enable the debug-mode */
928 else if (strcmp(argv[i], "--help") == 0)
930 puts("Super Tux " VERSION "\n"
931 " Please see the file \"README.txt\" for more details.\n");
932 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
933 puts("Display Options:\n"
934 " --fullscreen Run in fullscreen mode.\n"
935 " --opengl If opengl support was compiled in, this will enable\n"
936 " the EXPERIMENTAL OpenGL mode.\n"
939 " --disable-sound If sound support was compiled in, this will\n"
940 " disable sound for this session of the game.\n"
941 " --disable-music Like above, but this will disable music.\n"
944 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
945 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
946 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
947 " --help Display a help message summarizing command-line\n"
948 " options, license and game controls.\n"
949 " --usage Display a brief message summarizing command-line options.\n"
950 " --version Display the version of SuperTux you're running.\n\n"
954 else if (argv[i][0] != '-')
956 level_startup_file = argv[i];
960 /* Unknown - complain! */
970 void usage(char * prog, int ret)
975 /* Determine which stream to write to: */
983 /* Display the usage message: */
985 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",