7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - March 15, 2004
21 #include <SDL_image.h>
23 #include <SDL_opengl.h>
27 #include <sys/types.h>
41 #include "configfile.h"
46 #define mkdir(dir, mode) mkdir(dir)
47 // on win32 we typically don't want LFS paths
49 #define DATA_PREFIX "./data/"
52 /* Local function prototypes: */
55 void usage(char * prog, int ret);
57 /* Does the given file exist and is it accessible? */
58 int faccessible(const char *filename)
61 if (stat(filename, &filestat) == -1)
67 if(S_ISREG(filestat.st_mode))
74 /* Can we write to this location? */
75 int fwriteable(const char *filename)
78 fi = fopen(filename, "wa");
86 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
87 int fcreatedir(const char* relative_dir)
90 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
91 if(mkdir(path,0755) != 0)
93 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
94 if(mkdir(path,0755) != 0)
109 FILE * opendata(const char * rel_filename, const char * mode)
111 char * filename = NULL;
114 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
115 strlen(rel_filename) + 1));
117 strcpy(filename, st_dir);
118 /* Open the high score file: */
120 strcat(filename, rel_filename);
122 /* Try opening the file: */
123 fi = fopen(filename, mode);
127 fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
129 if (strcmp(mode, "r") == 0)
130 fprintf(stderr, "for read!!!\n");
131 else if (strcmp(mode, "w") == 0)
132 fprintf(stderr, "for write!!!\n");
139 /* Get all names of sub-directories in a certain directory. */
140 /* Returns the number of sub-directories found. */
141 /* Note: The user has to free the allocated space. */
142 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
145 struct dirent *direntp;
146 string_list_type sdirs;
150 string_list_init(&sdirs);
151 sprintf(path,"%s/%s",st_dir,rel_path);
152 if((dirStructP = opendir(path)) != NULL)
154 while((direntp = readdir(dirStructP)) != NULL)
156 char absolute_filename[1024];
159 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
161 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
163 if(expected_file != NULL)
165 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
166 if(!faccessible(filename))
170 string_list_add_item(&sdirs,direntp->d_name);
173 closedir(dirStructP);
176 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
177 if((dirStructP = opendir(path)) != NULL)
179 while((direntp = readdir(dirStructP)) != NULL)
181 char absolute_filename[1024];
184 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
186 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
188 if(expected_file != NULL)
190 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
191 if(!faccessible(filename))
197 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
198 if(faccessible(filename))
203 string_list_add_item(&sdirs,direntp->d_name);
206 closedir(dirStructP);
212 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
215 struct dirent *direntp;
216 string_list_type sdirs;
219 string_list_init(&sdirs);
220 sprintf(path,"%s/%s",st_dir,rel_path);
221 if((dirStructP = opendir(path)) != NULL)
223 while((direntp = readdir(dirStructP)) != NULL)
225 char absolute_filename[1024];
228 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
230 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
232 if(exception_str != NULL)
234 if(strstr(direntp->d_name,exception_str) != NULL)
238 if(strstr(direntp->d_name,glob) == NULL)
241 string_list_add_item(&sdirs,direntp->d_name);
244 closedir(dirStructP);
247 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
248 if((dirStructP = opendir(path)) != NULL)
250 while((direntp = readdir(dirStructP)) != NULL)
252 char absolute_filename[1024];
255 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
257 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
259 if(exception_str != NULL)
261 if(strstr(direntp->d_name,exception_str) != NULL)
265 if(strstr(direntp->d_name,glob) == NULL)
268 string_list_add_item(&sdirs,direntp->d_name);
271 closedir(dirStructP);
277 void free_strings(char **strings, int num)
280 for(i=0; i < num; ++i)
285 /* Set SuperTux configuration and save directories */
286 void st_directory_setup(void)
290 /* Get home directory (from $HOME variable)... if we can't determine it,
291 use the current directory ("."): */
292 if (getenv("HOME") != NULL)
293 home = getenv("HOME");
297 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
298 strlen("/.supertux") + 1));
299 strcpy(st_dir, home);
300 strcat(st_dir, "/.supertux");
302 /* Remove .supertux config-file from old SuperTux versions */
303 if(faccessible(st_dir))
309 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
311 strcpy(st_save_dir,st_dir);
312 strcat(st_save_dir,"/save");
314 /* Create them. In the case they exist they won't destroy anything. */
316 mkdir(st_save_dir, 0755);
318 sprintf(str, "%s/levels", st_dir);
321 // User has not that a datadir, so we try some magic
326 char exe_file[PATH_MAX];
327 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
329 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
330 datadir = DATA_PREFIX;
334 std::string exedir = std::string(dirname(exe_file)) + "/";
336 datadir = exedir + "../data/"; // SuperTux run from source dir
337 if (access(datadir.c_str(), F_OK) != 0)
339 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
340 if (access(datadir.c_str(), F_OK) != 0)
341 { // If all fails, fall back to compiled path
342 datadir = DATA_PREFIX;
347 datadir = DATA_PREFIX;
350 printf("Datadir: %s\n", datadir.c_str());
353 /* Create and setup menus. */
356 main_menu = new Menu();
357 options_menu = new Menu();
358 options_controls_menu = new Menu();
359 load_game_menu = new Menu();
360 save_game_menu = new Menu();
361 game_menu = new Menu();
362 highscore_menu = new Menu();
363 contrib_menu = new Menu();
364 contrib_subset_menu = new Menu();
365 worldmap_menu = new Menu();
367 main_menu->set_pos(screen->w/2, 335);
368 main_menu->additem(MN_GOTO, "Start Game",0,load_game_menu);
369 main_menu->additem(MN_GOTO, "Contrib Levels",0,contrib_menu);
370 main_menu->additem(MN_GOTO, "Options",0,options_menu);
371 main_menu->additem(MN_ACTION,"Level editor",0,0);
372 main_menu->additem(MN_ACTION,"Credits",0,0);
373 main_menu->additem(MN_ACTION,"Quit",0,0);
375 options_menu->additem(MN_LABEL,"Options",0,0);
376 options_menu->additem(MN_HL,"",0,0);
378 options_menu->additem(MN_TOGGLE,"OpenGL",use_gl,0);
380 options_menu->additem(MN_DEACTIVE,"OpenGL (not supported)",use_gl,0);
382 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
385 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
386 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
390 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
391 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
393 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
394 options_menu->additem(MN_GOTO,"Controls ",0,options_controls_menu);
395 options_menu->additem(MN_HL,"",0,0);
396 options_menu->additem(MN_BACK,"Back",0,0);
398 options_controls_menu->additem(MN_LABEL,"Controls",0,0);
399 options_controls_menu->additem(MN_HL,"",0,0);
400 //FIXME:options_controls_menu->additem(MN_CONTROLFIELD,"Move Right", tux.keymap.right,0);
401 options_controls_menu->additem(MN_HL,"",0,0);
402 options_controls_menu->additem(MN_BACK,"Back",0,0);
404 load_game_menu->additem(MN_LABEL,"Start Game",0,0);
405 load_game_menu->additem(MN_HL,"",0,0);
406 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
407 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
408 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
409 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
410 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
411 load_game_menu->additem(MN_HL,"",0,0);
412 load_game_menu->additem(MN_BACK,"Back",0,0);
414 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
415 save_game_menu->additem(MN_HL,"",0,0);
416 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
417 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
418 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
419 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
420 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
421 save_game_menu->additem(MN_HL,"",0,0);
422 save_game_menu->additem(MN_BACK,"Back",0,0);
424 game_menu->additem(MN_LABEL,"InGame Menu",0,0);
425 game_menu->additem(MN_HL,"",0,0);
426 game_menu->additem(MN_ACTION,"Return To Game",0,0);
427 game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
428 game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
429 game_menu->additem(MN_GOTO,"Options",0,options_menu);
430 game_menu->additem(MN_HL,"",0,0);
431 game_menu->additem(MN_ACTION,"Quit Game",0,0);
433 worldmap_menu->additem(MN_LABEL,"Worldmap Menu",0,0);
434 worldmap_menu->additem(MN_HL,"",0,0);
435 worldmap_menu->additem(MN_ACTION,"Return To Game",0,0);
436 worldmap_menu->additem(MN_GOTO,"Options",0,options_menu);
437 worldmap_menu->additem(MN_HL,"",0,0);
438 worldmap_menu->additem(MN_ACTION,"Quit Game",0,0);
440 highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
443 void update_load_save_game_menu(Menu* pmenu, int load)
445 for(int i = 2; i < 7; ++i)
447 // FIXME: Insert a real savegame struct/class here instead of
448 // doing string vodoo
449 std::string tmp = slotinfo(i-1);
451 if(load && tmp.length() == strlen("Slot X - Free"))
452 pmenu->item[i].kind = MN_ACTION;
454 pmenu->item[i].kind = MN_ACTION;
455 pmenu->item[i].change_text(tmp.c_str());
459 bool process_load_game_menu()
461 int slot = load_game_menu->check();
465 // FIXME: Insert a real savegame struct/class here instead of
466 // doing string vodoo
467 std::string tmp = slotinfo(slot-1);
469 if (tmp.length() == strlen("Slot X - Free"))
470 { // Slot is free, so start a new game
474 Menu::set_current(main_menu);
478 puts("Warning: Loading games isn't supported at the moment");
480 st_pause_ticks_stop();
489 /* Handle changes made to global settings in the options menu. */
490 void process_options_menu(void)
492 switch (options_menu->check())
496 if(use_gl != options_menu->item[2].toggled)
504 if(use_fullscreen != options_menu->item[3].toggled)
506 use_fullscreen = !use_fullscreen;
511 if(use_sound != options_menu->item[4].toggled)
512 use_sound = !use_sound;
515 if(use_music != options_menu->item[5].toggled)
528 if (!playing_music())
530 play_current_music();
536 if(show_fps != options_menu->item[6].toggled)
537 show_fps = !show_fps;
542 void st_general_setup(void)
544 /* Seed random number generator: */
546 srand(SDL_GetTicks());
548 /* Set icon image: */
552 /* Unicode needed for input handling: */
554 SDL_EnableUNICODE(1);
556 /* Load global images: */
558 black_text = new Text(datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
559 gold_text = new Text(datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
560 blue_text = new Text(datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
561 red_text = new Text(datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
562 white_text = new Text(datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
563 white_small_text = new Text(datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
564 white_big_text = new Text(datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
565 yellow_nums = new Text(datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
567 /* Load GUI/menu images: */
568 checkbox = new Surface(datadir + "/images/status/checkbox.png", USE_ALPHA);
569 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
570 back = new Surface(datadir + "/images/status/back.png", USE_ALPHA);
571 arrow_left = new Surface(datadir + "/images/icons/left.png", USE_ALPHA);
572 arrow_right = new Surface(datadir + "/images/icons/right.png", USE_ALPHA);
574 /* Load the mouse-cursor */
575 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
579 void st_general_free(void)
582 /* Free global images: */
589 delete white_small_text;
590 delete white_big_text;
592 /* Free GUI/menu images: */
594 delete checkbox_checked;
599 /* Free mouse-cursor */
606 delete highscore_menu;
607 delete save_game_menu;
608 delete load_game_menu;
611 void st_video_setup(void)
614 SDL_FreeSurface(screen);
616 /* Init SDL Video: */
618 if (SDL_Init(SDL_INIT_VIDEO) < 0)
621 "\nError: I could not initialize video!\n"
622 "The Simple DirectMedia error that occured was:\n"
623 "%s\n\n", SDL_GetError());
631 st_video_setup_sdl();
633 Surface::reload_all();
635 /* Set window manager stuff: */
636 SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux");
639 void st_video_setup_sdl(void)
641 SDL_FreeSurface(screen);
645 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
649 "\nWarning: I could not set up fullscreen video for "
651 "The Simple DirectMedia error that occured was:\n"
652 "%s\n\n", SDL_GetError());
653 use_fullscreen = false;
658 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
663 "\nError: I could not set up video for 640x480 mode.\n"
664 "The Simple DirectMedia error that occured was:\n"
665 "%s\n\n", SDL_GetError());
671 void st_video_setup_gl(void)
675 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
676 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
677 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
678 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
679 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
683 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
687 "\nWarning: I could not set up fullscreen video for "
689 "The Simple DirectMedia error that occured was:\n"
690 "%s\n\n", SDL_GetError());
691 use_fullscreen = false;
696 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
701 "\nError: I could not set up video for 640x480 mode.\n"
702 "The Simple DirectMedia error that occured was:\n"
703 "%s\n\n", SDL_GetError());
709 * Set up OpenGL for 2D rendering.
711 glDisable(GL_DEPTH_TEST);
712 glDisable(GL_CULL_FACE);
714 glViewport(0, 0, screen->w, screen->h);
715 glMatrixMode(GL_PROJECTION);
717 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
719 glMatrixMode(GL_MODELVIEW);
721 glTranslatef(0.0f, 0.0f, 0.0f);
727 void st_joystick_setup(void)
734 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
736 fprintf(stderr, "Warning: I could not initialize joystick!\n"
737 "The Simple DirectMedia error that occured was:\n"
738 "%s\n\n", SDL_GetError());
740 use_joystick = false;
745 if (SDL_NumJoysticks() <= 0)
747 fprintf(stderr, "Warning: No joysticks are available.\n");
749 use_joystick = false;
753 js = SDL_JoystickOpen(joystick_num);
757 fprintf(stderr, "Warning: Could not open joystick %d.\n"
758 "The Simple DirectMedia error that occured was:\n"
759 "%s\n\n", joystick_num, SDL_GetError());
761 use_joystick = false;
765 /* Check for proper joystick configuration: */
767 if (SDL_JoystickNumAxes(js) < 2)
770 "Warning: Joystick does not have enough axes!\n");
772 use_joystick = false;
776 if (SDL_JoystickNumButtons(js) < 2)
780 "Joystick does not have enough buttons!\n");
782 use_joystick = false;
790 void st_audio_setup(void)
793 /* Init SDL Audio silently even if --disable-sound : */
797 if (SDL_Init(SDL_INIT_AUDIO) < 0)
799 /* only print out message if sound or music
800 was not disabled at command-line
802 if (use_sound || use_music)
805 "\nWarning: I could not initialize audio!\n"
806 "The Simple DirectMedia error that occured was:\n"
807 "%s\n\n", SDL_GetError());
809 /* keep the programming logic the same :-)
810 because in this case, use_sound & use_music' values are ignored
811 when there's no available audio device
815 audio_device = false;
820 /* Open sound silently regarless the value of "use_sound": */
824 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
826 /* only print out message if sound or music
827 was not disabled at command-line
829 if (use_sound || use_music)
832 "\nWarning: I could not set up audio for 44100 Hz "
834 "The Simple DirectMedia error that occured was:\n"
835 "%s\n\n", SDL_GetError());
839 audio_device = false;
846 /* --- SHUTDOWN --- */
848 void st_shutdown(void)
857 void st_abort(const std::string& reason, const std::string& details)
859 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
865 /* Set Icon (private) */
874 /* Load icon into a surface: */
876 icon = IMG_Load((datadir + "/images/icon.png").c_str());
880 "\nError: I could not load the icon image: %s%s\n"
881 "The Simple DirectMedia error that occured was:\n"
882 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
889 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
890 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
891 memset(mask, 0xFF, masklen);
896 SDL_WM_SetIcon(icon, mask);
899 /* Free icon surface & mask: */
902 SDL_FreeSurface(icon);
906 /* Parse command-line arguments: */
908 void parseargs(int argc, char * argv[])
914 /* Parse arguments: */
916 for (i = 1; i < argc; i++)
918 if (strcmp(argv[i], "--fullscreen") == 0 ||
919 strcmp(argv[i], "-f") == 0)
921 /* Use full screen: */
923 use_fullscreen = true;
925 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
928 joystick_num = atoi(argv[++i]);
930 else if (strcmp(argv[i], "--worldmap") == 0)
932 launch_worldmap_mode = true;
934 else if (strcmp(argv[i], "--datadir") == 0
935 || strcmp(argv[i], "-d") == 0 )
940 else if (strcmp(argv[i], "--show-fps") == 0)
942 /* Use full screen: */
946 else if (strcmp(argv[i], "--opengl") == 0 ||
947 strcmp(argv[i], "-gl") == 0)
955 else if (strcmp(argv[i], "--sdl") == 0)
959 else if (strcmp(argv[i], "--usage") == 0)
965 else if (strcmp(argv[i], "--version") == 0)
968 printf("SuperTux " VERSION "\n");
971 else if (strcmp(argv[i], "--disable-sound") == 0)
973 /* Disable the compiled in sound feature */
974 printf("Sounds disabled \n");
977 else if (strcmp(argv[i], "--disable-music") == 0)
979 /* Disable the compiled in sound feature */
980 printf("Music disabled \n");
983 else if (strcmp(argv[i], "--debug-mode") == 0)
985 /* Enable the debug-mode */
989 else if (strcmp(argv[i], "--help") == 0)
991 puts("Super Tux " VERSION "\n"
992 " Please see the file \"README.txt\" for more details.\n");
993 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
994 puts("Display Options:\n"
995 " --fullscreen Run in fullscreen mode.\n"
996 " --opengl If opengl support was compiled in, this will enable\n"
997 " the EXPERIMENTAL OpenGL mode.\n"
998 " --sdl Use non-opengl renderer\n"
1001 " --disable-sound If sound support was compiled in, this will\n"
1002 " disable sound for this session of the game.\n"
1003 " --disable-music Like above, but this will disable music.\n"
1006 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
1007 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
1008 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
1009 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
1010 " --help Display a help message summarizing command-line\n"
1011 " options, license and game controls.\n"
1012 " --usage Display a brief message summarizing command-line options.\n"
1013 " --version Display the version of SuperTux you're running.\n\n"
1017 else if (argv[i][0] != '-')
1019 level_startup_file = argv[i];
1023 /* Unknown - complain! */
1031 /* Display usage: */
1033 void usage(char * prog, int ret)
1038 /* Determine which stream to write to: */
1046 /* Display the usage message: */
1048 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",