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/", DATA_PREFIX, 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",DATA_PREFIX,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",DATA_PREFIX,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);
299 std::string exedir = std::string(dirname(exe_file)) + "/";
301 datadir = exedir + "../data/"; // SuperTux run from source dir
302 if (access(datadir.c_str(), F_OK) != 0)
304 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
305 if (access(datadir.c_str(), F_OK) != 0)
306 { // If all fails, fall back to compiled path
307 datadir = DATA_PREFIX;
312 printf("Datadir: %s\n", datadir.c_str());
315 /* Create and setup menus. */
318 menu_init(&main_menu);
319 menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
320 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
321 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
322 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
323 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
324 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
325 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
326 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
327 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
329 menu_init(&options_menu);
330 menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
331 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
332 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
333 if(audio_device == YES)
335 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound ",use_sound,0));
336 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music ",use_music,0));
340 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound ",use_sound,0));
341 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music ",use_music,0));
343 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS ",show_fps,0));
344 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
345 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
347 menu_init(&load_game_menu);
348 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
349 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
350 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
351 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
352 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
353 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
354 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
355 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
356 menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
358 menu_init(&save_game_menu);
359 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
360 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
361 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
362 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
363 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
364 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
365 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
366 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
367 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
369 menu_init(&game_menu);
370 menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
371 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
372 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
373 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
374 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
375 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
376 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
377 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
379 menu_init(&highscore_menu);
380 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
384 void update_load_save_game_menu(menu_type* pmenu, int load)
388 for(i = 2; i < 7; ++i)
392 if(load && strlen(tmp) == strlen("Slot X - Free") )
393 pmenu->item[i].kind = MN_DEACTIVE;
395 pmenu->item[i].kind = MN_ACTION;
396 menu_item_change_text(&pmenu->item[i],tmp);
401 void process_save_load_game_menu(int save)
404 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
415 if(game_started == NO)
417 gameloop("default",slot - 1,ST_GL_LOAD_GAME);
419 menu_set_current(&main_menu);
424 st_pause_ticks_stop();
430 /* Handle changes made to global settings in the options menu. */
431 void process_options_menu(void)
433 switch (menu_check(&options_menu))
436 if(use_fullscreen != options_menu.item[2].toggled)
438 use_fullscreen = !use_fullscreen;
443 if(use_sound != options_menu.item[3].toggled)
444 use_sound = !use_sound;
447 if(use_music != options_menu.item[4].toggled)
460 if (!playing_music())
462 play_current_music();
468 if(show_fps != options_menu.item[5].toggled)
469 show_fps = !show_fps;
474 void st_general_setup(void)
476 /* Seed random number generator: */
478 srand(SDL_GetTicks());
480 /* Set icon image: */
484 /* Unicode needed for input handling: */
486 SDL_EnableUNICODE(1);
488 /* Load global images: */
490 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
491 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
492 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
493 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
494 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
495 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
496 text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
497 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
499 /* Load GUI/menu images: */
500 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
501 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
502 texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
503 texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
504 texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA);
508 void st_general_free(void)
511 /* Free global images: */
513 text_free(&black_text);
514 text_free(&gold_text);
515 text_free(&white_text);
516 text_free(&blue_text);
517 text_free(&red_text);
518 text_free(&white_small_text);
519 text_free(&white_big_text);
521 /* Free GUI/menu images: */
522 texture_free(&checkbox);
523 texture_free(&checkbox_checked);
525 texture_free(&arrow_left);
526 texture_free(&arrow_right);
530 menu_free(&main_menu);
531 menu_free(&game_menu);
532 menu_free(&options_menu);
533 menu_free(&highscore_menu);
534 menu_free(&save_game_menu);
535 menu_free(&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 == YES)
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());
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 == YES)
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());
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)
667 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
669 fprintf(stderr, "Warning: I could not initialize joystick!\n"
670 "The Simple DirectMedia error that occured was:\n"
671 "%s\n\n", SDL_GetError());
679 if (SDL_NumJoysticks() <= 0)
681 fprintf(stderr, "Warning: No joysticks are available.\n");
687 js = SDL_JoystickOpen(0);
691 fprintf(stderr, "Warning: Could not open joystick 1.\n"
692 "The Simple DirectMedia error that occured was:\n"
693 "%s\n\n", SDL_GetError());
699 /* Check for proper joystick configuration: */
701 if (SDL_JoystickNumAxes(js) < 2)
704 "Warning: Joystick does not have enough axes!\n");
710 if (SDL_JoystickNumButtons(js) < 2)
714 "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 char * reason,const char * details)
795 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
801 /* Set Icon (private) */
810 /* Load icon into a surface: */
812 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
816 "\nError: I could not load the icon image: %s\n"
817 "The Simple DirectMedia error that occured was:\n"
818 "%s\n\n", DATA_PREFIX "/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[])
868 /* Parse arguments: */
870 for (i = 1; i < argc; i++)
872 if (strcmp(argv[i], "--fullscreen") == 0 ||
873 strcmp(argv[i], "-f") == 0)
875 /* Use full screen: */
877 use_fullscreen = YES;
879 else if (strcmp(argv[i], "--worldmap") == 0)
881 launch_worldmap_mode = true;
883 else if (strcmp(argv[i], "--datadir") == 0
884 || strcmp(argv[i], "-d") == 0 )
889 else if (strcmp(argv[i], "--show-fps") == 0)
891 /* Use full screen: */
895 else if (strcmp(argv[i], "--opengl") == 0 ||
896 strcmp(argv[i], "-gl") == 0)
905 else if (strcmp(argv[i], "--usage") == 0)
911 else if (strcmp(argv[i], "--version") == 0)
915 printf("Super Tux - version " VERSION "\n");
918 else if (strcmp(argv[i], "--disable-sound") == 0)
920 /* Disable the compiled in sound feature */
922 printf("Sounds disabled \n");
926 printf("Warning: Sound capability has not been compiled into this build.\n");
930 else if (strcmp(argv[i], "--disable-music") == 0)
932 /* Disable the compiled in sound feature */
934 printf("Music disabled \n");
938 printf("Warning: Music feature is not compiled in \n");
942 else if (strcmp(argv[i], "--debug-mode") == 0)
944 /* Enable the debug-mode */
948 else if (strcmp(argv[i], "--help") == 0)
950 puts("Super Tux " VERSION "\n"
951 " Please see the file \"README.txt\" for more details.\n");
952 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
953 puts("Display Options:\n"
954 " --fullscreen Run in fullscreen mode.\n"
955 " --opengl If opengl support was compiled in, this will enable\n"
956 " the EXPERIMENTAL OpenGL mode.\n"
959 " --disable-sound If sound support was compiled in, this will\n"
960 " disable sound for this session of the game.\n"
961 " --disable-music Like above, but this will disable music.\n"
964 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
965 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
966 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
967 " --help Display a help message summarizing command-line\n"
968 " options, license and game controls.\n"
969 " --usage Display a brief message summarizing command-line options.\n"
970 " --version Display the version of SuperTux you're running.\n\n"
974 else if (argv[i][0] != '-')
976 level_startup_file = argv[i];
980 /* Unknown - complain! */
990 void usage(char * prog, int ret)
995 /* Determine which stream to write to: */
1003 /* Display the usage message: */
1005 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",