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>
28 #include <sys/types.h>
42 /* Local function prototypes: */
45 void usage(char * prog, int ret);
47 /* Does the given file exist and is it accessible? */
48 int faccessible(const char *filename)
51 if (stat(filename, &filestat) == -1)
57 if(S_ISREG(filestat.st_mode))
64 /* Can we write to this location? */
65 int fwriteable(const char *filename)
68 fi = fopen(filename, "wa");
76 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
77 int fcreatedir(const char* relative_dir)
80 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
81 if(mkdir(path,0755) != 0)
83 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
84 if(mkdir(path,0755) != 0)
99 /* Get all names of sub-directories in a certain directory. */
100 /* Returns the number of sub-directories found. */
101 /* Note: The user has to free the allocated space. */
102 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
105 struct dirent *direntp;
106 string_list_type sdirs;
110 string_list_init(&sdirs);
111 sprintf(path,"%s/%s",st_dir,rel_path);
112 if((dirStructP = opendir(path)) != NULL)
114 while((direntp = readdir(dirStructP)) != NULL)
116 char absolute_filename[1024];
119 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
121 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
123 if(expected_file != NULL)
125 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
126 if(!faccessible(filename))
130 string_list_add_item(&sdirs,direntp->d_name);
133 closedir(dirStructP);
136 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
137 if((dirStructP = opendir(path)) != NULL)
139 while((direntp = readdir(dirStructP)) != NULL)
141 char absolute_filename[1024];
144 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
146 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
148 if(expected_file != NULL)
150 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
151 if(!faccessible(filename))
157 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
158 if(faccessible(filename))
163 string_list_add_item(&sdirs,direntp->d_name);
166 closedir(dirStructP);
172 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
175 struct dirent *direntp;
176 string_list_type sdirs;
179 string_list_init(&sdirs);
180 sprintf(path,"%s/%s",st_dir,rel_path);
181 if((dirStructP = opendir(path)) != NULL)
183 while((direntp = readdir(dirStructP)) != NULL)
185 char absolute_filename[1024];
188 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
190 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
192 if(exception_str != NULL)
194 if(strstr(direntp->d_name,exception_str) != NULL)
198 if(strstr(direntp->d_name,glob) == NULL)
201 string_list_add_item(&sdirs,direntp->d_name);
204 closedir(dirStructP);
207 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
208 if((dirStructP = opendir(path)) != NULL)
210 while((direntp = readdir(dirStructP)) != NULL)
212 char absolute_filename[1024];
215 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
217 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
219 if(exception_str != NULL)
221 if(strstr(direntp->d_name,exception_str) != NULL)
225 if(strstr(direntp->d_name,glob) == NULL)
228 string_list_add_item(&sdirs,direntp->d_name);
231 closedir(dirStructP);
237 void free_strings(char **strings, int num)
240 for(i=0; i < num; ++i)
245 /* Set SuperTux configuration and save directories */
246 void st_directory_setup(void)
250 /* Get home directory (from $HOME variable)... if we can't determine it,
251 use the current directory ("."): */
252 if (getenv("HOME") != NULL)
253 home = getenv("HOME");
257 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
258 strlen("/.supertux") + 1));
259 strcpy(st_dir, home);
260 strcat(st_dir, "/.supertux");
262 /* Remove .supertux config-file from old SuperTux versions */
263 if(faccessible(st_dir))
269 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
271 strcpy(st_save_dir,st_dir);
272 strcat(st_save_dir,"/save");
274 /* Create them. In the case they exist they won't destroy anything. */
277 mkdir(st_save_dir, 0755);
279 sprintf(str, "%s/levels", st_dir);
284 sprintf(str, "%s/levels", st_dir);
288 // 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 printf("Datadir: %s\n", datadir.c_str());
316 /* Create and setup menus. */
319 menu_init(&main_menu);
320 menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
321 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
322 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
323 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
324 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
325 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
326 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
327 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
328 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
330 menu_init(&options_menu);
331 menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
332 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
333 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
334 if(audio_device == YES)
336 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound ",use_sound,0));
337 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music ",use_music,0));
341 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound ",use_sound,0));
342 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music ",use_music,0));
344 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS ",show_fps,0));
345 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
346 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
348 menu_init(&load_game_menu);
349 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
350 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
351 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
352 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
353 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
354 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
355 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
356 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
357 menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
359 menu_init(&save_game_menu);
360 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
361 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
362 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
363 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
364 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
365 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
366 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
367 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
368 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
370 menu_init(&game_menu);
371 menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
372 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
373 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
374 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
375 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
376 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
377 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
378 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
380 menu_init(&highscore_menu);
381 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
385 void update_load_save_game_menu(menu_type* pmenu, int load)
389 for(i = 2; i < 7; ++i)
393 if(load && strlen(tmp) == strlen("Slot X - Free") )
394 pmenu->item[i].kind = MN_DEACTIVE;
396 pmenu->item[i].kind = MN_ACTION;
397 menu_item_change_text(&pmenu->item[i],tmp);
402 void process_save_load_game_menu(int save)
405 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
416 if(game_started == NO)
418 gameloop("default",slot - 1,ST_GL_LOAD_GAME);
420 menu_set_current(&main_menu);
425 st_pause_ticks_stop();
431 /* Handle changes made to global settings in the options menu. */
432 void process_options_menu(void)
434 switch (menu_check(&options_menu))
437 if(use_fullscreen != options_menu.item[2].toggled)
439 use_fullscreen = !use_fullscreen;
444 if(use_sound != options_menu.item[3].toggled)
445 use_sound = !use_sound;
448 if(use_music != options_menu.item[4].toggled)
461 if (!playing_music())
463 play_current_music();
469 if(show_fps != options_menu.item[5].toggled)
470 show_fps = !show_fps;
475 void st_general_setup(void)
477 /* Seed random number generator: */
479 srand(SDL_GetTicks());
481 /* Set icon image: */
485 /* Unicode needed for input handling: */
487 SDL_EnableUNICODE(1);
489 /* Load global images: */
491 text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
492 text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
493 text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
494 text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
495 text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
496 text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
497 text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
498 text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
500 /* Load GUI/menu images: */
501 texture_load(&checkbox, datadir + "/images/status/checkbox.png", USE_ALPHA);
502 texture_load(&checkbox_checked, datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
503 texture_load(&back, datadir + "/images/status/back.png", USE_ALPHA);
504 texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA);
505 texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA);
509 void st_general_free(void)
512 /* Free global images: */
514 text_free(&black_text);
515 text_free(&gold_text);
516 text_free(&white_text);
517 text_free(&blue_text);
518 text_free(&red_text);
519 text_free(&white_small_text);
520 text_free(&white_big_text);
522 /* Free GUI/menu images: */
523 texture_free(&checkbox);
524 texture_free(&checkbox_checked);
526 texture_free(&arrow_left);
527 texture_free(&arrow_right);
531 menu_free(&main_menu);
532 menu_free(&game_menu);
533 menu_free(&options_menu);
534 menu_free(&highscore_menu);
535 menu_free(&save_game_menu);
536 menu_free(&load_game_menu);
540 void st_video_setup(void)
544 SDL_FreeSurface(screen);
546 /* Init SDL Video: */
548 if (SDL_Init(SDL_INIT_VIDEO) < 0)
551 "\nError: I could not initialize video!\n"
552 "The Simple DirectMedia error that occured was:\n"
553 "%s\n\n", SDL_GetError());
562 st_video_setup_sdl();
566 /* Set window manager stuff: */
568 SDL_WM_SetCaption("Super Tux", "Super Tux");
572 void st_video_setup_sdl(void)
574 SDL_FreeSurface(screen);
576 if (use_fullscreen == YES)
578 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
582 "\nWarning: I could not set up fullscreen video for "
584 "The Simple DirectMedia error that occured was:\n"
585 "%s\n\n", SDL_GetError());
591 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
596 "\nError: I could not set up video for 640x480 mode.\n"
597 "The Simple DirectMedia error that occured was:\n"
598 "%s\n\n", SDL_GetError());
604 void st_video_setup_gl(void)
608 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
609 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
610 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
611 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
612 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
614 if (use_fullscreen == YES)
616 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
620 "\nWarning: I could not set up fullscreen video for "
622 "The Simple DirectMedia error that occured was:\n"
623 "%s\n\n", SDL_GetError());
629 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
634 "\nError: I could not set up video for 640x480 mode.\n"
635 "The Simple DirectMedia error that occured was:\n"
636 "%s\n\n", SDL_GetError());
642 * Set up OpenGL for 2D rendering.
644 glDisable(GL_DEPTH_TEST);
645 glDisable(GL_CULL_FACE);
647 glViewport(0, 0, screen->w, screen->h);
648 glMatrixMode(GL_PROJECTION);
650 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
652 glMatrixMode(GL_MODELVIEW);
654 glTranslatef(0.0f, 0.0f, 0.0f);
660 void st_joystick_setup(void)
668 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
670 fprintf(stderr, "Warning: I could not initialize joystick!\n"
671 "The Simple DirectMedia error that occured was:\n"
672 "%s\n\n", SDL_GetError());
680 if (SDL_NumJoysticks() <= 0)
682 fprintf(stderr, "Warning: No joysticks are available.\n");
688 js = SDL_JoystickOpen(0);
692 fprintf(stderr, "Warning: Could not open joystick 1.\n"
693 "The Simple DirectMedia error that occured was:\n"
694 "%s\n\n", SDL_GetError());
700 /* Check for proper joystick configuration: */
702 if (SDL_JoystickNumAxes(js) < 2)
705 "Warning: Joystick does not have enough axes!\n");
711 if (SDL_JoystickNumButtons(js) < 2)
715 "Joystick does not have enough buttons!\n");
727 void st_audio_setup(void)
730 /* Init SDL Audio silently even if --disable-sound : */
732 if (audio_device == YES)
734 if (SDL_Init(SDL_INIT_AUDIO) < 0)
736 /* only print out message if sound or music
737 was not disabled at command-line
739 if (use_sound == YES || use_music == YES)
742 "\nWarning: I could not initialize audio!\n"
743 "The Simple DirectMedia error that occured was:\n"
744 "%s\n\n", SDL_GetError());
746 /* keep the programming logic the same :-)
747 because in this case, use_sound & use_music' values are ignored
748 when there's no available audio device
757 /* Open sound silently regarless the value of "use_sound": */
759 if (audio_device == YES)
761 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
763 /* only print out message if sound or music
764 was not disabled at command-line
766 if ((use_sound == YES) || (use_music == YES))
769 "\nWarning: I could not set up audio for 44100 Hz "
771 "The Simple DirectMedia error that occured was:\n"
772 "%s\n\n", SDL_GetError());
783 /* --- SHUTDOWN --- */
785 void st_shutdown(void)
794 void st_abort(const char * reason,const char * details)
796 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
802 /* Set Icon (private) */
811 /* Load icon into a surface: */
813 icon = IMG_Load((datadir + "/images/icon.png").c_str());
817 "\nError: I could not load the icon image: %s%s\n"
818 "The Simple DirectMedia error that occured was:\n"
819 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
826 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
827 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
828 memset(mask, 0xFF, masklen);
833 SDL_WM_SetIcon(icon, mask);
836 /* Free icon surface & mask: */
839 SDL_FreeSurface(icon);
843 /* Parse command-line arguments: */
845 void parseargs(int argc, char * argv[])
869 /* Parse arguments: */
871 for (i = 1; i < argc; i++)
873 if (strcmp(argv[i], "--fullscreen") == 0 ||
874 strcmp(argv[i], "-f") == 0)
876 /* Use full screen: */
878 use_fullscreen = YES;
880 else if (strcmp(argv[i], "--worldmap") == 0)
882 launch_worldmap_mode = true;
884 else if (strcmp(argv[i], "--datadir") == 0
885 || strcmp(argv[i], "-d") == 0 )
890 else if (strcmp(argv[i], "--show-fps") == 0)
892 /* Use full screen: */
896 else if (strcmp(argv[i], "--opengl") == 0 ||
897 strcmp(argv[i], "-gl") == 0)
906 else if (strcmp(argv[i], "--usage") == 0)
912 else if (strcmp(argv[i], "--version") == 0)
916 printf("Super Tux - version " VERSION "\n");
919 else if (strcmp(argv[i], "--disable-sound") == 0)
921 /* Disable the compiled in sound feature */
923 printf("Sounds disabled \n");
927 printf("Warning: Sound capability has not been compiled into this build.\n");
931 else if (strcmp(argv[i], "--disable-music") == 0)
933 /* Disable the compiled in sound feature */
935 printf("Music disabled \n");
939 printf("Warning: Music feature is not compiled in \n");
943 else if (strcmp(argv[i], "--debug-mode") == 0)
945 /* Enable the debug-mode */
949 else if (strcmp(argv[i], "--help") == 0)
951 puts("Super Tux " VERSION "\n"
952 " Please see the file \"README.txt\" for more details.\n");
953 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
954 puts("Display Options:\n"
955 " --fullscreen Run in fullscreen mode.\n"
956 " --opengl If opengl support was compiled in, this will enable\n"
957 " the EXPERIMENTAL OpenGL mode.\n"
960 " --disable-sound If sound support was compiled in, this will\n"
961 " disable sound for this session of the game.\n"
962 " --disable-music Like above, but this will disable music.\n"
965 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
966 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
967 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
968 " --help Display a help message summarizing command-line\n"
969 " options, license and game controls.\n"
970 " --usage Display a brief message summarizing command-line options.\n"
971 " --version Display the version of SuperTux you're running.\n\n"
975 else if (argv[i][0] != '-')
977 level_startup_file = argv[i];
981 /* Unknown - complain! */
991 void usage(char * prog, int ret)
996 /* Determine which stream to write to: */
1004 /* Display the usage message: */
1006 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",