7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - March 15, 2004
22 #include <SDL_image.h>
24 #include <SDL_opengl.h>
28 #include <sys/types.h>
42 #include "configfile.h"
47 #define mkdir(dir, mode) mkdir(dir)
48 // on win32 we typically don't want LFS paths
50 #define DATA_PREFIX "./data/"
53 /* Local function prototypes: */
56 void usage(char * prog, int ret);
58 /* Does the given file exist and is it accessible? */
59 int faccessible(const char *filename)
62 if (stat(filename, &filestat) == -1)
68 if(S_ISREG(filestat.st_mode))
75 /* Can we write to this location? */
76 int fwriteable(const char *filename)
79 fi = fopen(filename, "wa");
87 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
88 int fcreatedir(const char* relative_dir)
91 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
92 if(mkdir(path,0755) != 0)
94 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
95 if(mkdir(path,0755) != 0)
110 FILE * opendata(const char * rel_filename, const char * mode)
112 char * filename = NULL;
115 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
116 strlen(rel_filename) + 1));
118 strcpy(filename, st_dir);
119 /* Open the high score file: */
121 strcat(filename, rel_filename);
123 /* Try opening the file: */
124 fi = fopen(filename, mode);
128 fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
130 if (strcmp(mode, "r") == 0)
131 fprintf(stderr, "for read!!!\n");
132 else if (strcmp(mode, "w") == 0)
133 fprintf(stderr, "for write!!!\n");
140 /* Get all names of sub-directories in a certain directory. */
141 /* Returns the number of sub-directories found. */
142 /* Note: The user has to free the allocated space. */
143 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
146 struct dirent *direntp;
147 string_list_type sdirs;
151 string_list_init(&sdirs);
152 sprintf(path,"%s/%s",st_dir,rel_path);
153 if((dirStructP = opendir(path)) != NULL)
155 while((direntp = readdir(dirStructP)) != NULL)
157 char absolute_filename[1024];
160 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
162 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
164 if(expected_file != NULL)
166 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
167 if(!faccessible(filename))
171 string_list_add_item(&sdirs,direntp->d_name);
174 closedir(dirStructP);
177 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
178 if((dirStructP = opendir(path)) != NULL)
180 while((direntp = readdir(dirStructP)) != NULL)
182 char absolute_filename[1024];
185 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
187 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
189 if(expected_file != NULL)
191 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
192 if(!faccessible(filename))
198 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
199 if(faccessible(filename))
204 string_list_add_item(&sdirs,direntp->d_name);
207 closedir(dirStructP);
213 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
216 struct dirent *direntp;
217 string_list_type sdirs;
220 string_list_init(&sdirs);
221 sprintf(path,"%s/%s",st_dir,rel_path);
222 if((dirStructP = opendir(path)) != NULL)
224 while((direntp = readdir(dirStructP)) != NULL)
226 char absolute_filename[1024];
229 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
231 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
233 if(exception_str != NULL)
235 if(strstr(direntp->d_name,exception_str) != NULL)
239 if(strstr(direntp->d_name,glob) == NULL)
242 string_list_add_item(&sdirs,direntp->d_name);
245 closedir(dirStructP);
248 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
249 if((dirStructP = opendir(path)) != NULL)
251 while((direntp = readdir(dirStructP)) != NULL)
253 char absolute_filename[1024];
256 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
258 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
260 if(exception_str != NULL)
262 if(strstr(direntp->d_name,exception_str) != NULL)
266 if(strstr(direntp->d_name,glob) == NULL)
269 string_list_add_item(&sdirs,direntp->d_name);
272 closedir(dirStructP);
278 void free_strings(char **strings, int num)
281 for(i=0; i < num; ++i)
286 /* Set SuperTux configuration and save directories */
287 void st_directory_setup(void)
291 /* Get home directory (from $HOME variable)... if we can't determine it,
292 use the current directory ("."): */
293 if (getenv("HOME") != NULL)
294 home = getenv("HOME");
298 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
299 strlen("/.supertux") + 1));
300 strcpy(st_dir, home);
301 strcat(st_dir, "/.supertux");
303 /* Remove .supertux config-file from old SuperTux versions */
304 if(faccessible(st_dir))
310 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
312 strcpy(st_save_dir,st_dir);
313 strcat(st_save_dir,"/save");
315 /* Create them. In the case they exist they won't destroy anything. */
317 mkdir(st_save_dir, 0755);
319 sprintf(str, "%s/levels", st_dir);
322 // User has not that a datadir, so we try some magic
327 char exe_file[PATH_MAX];
328 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
330 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
331 datadir = DATA_PREFIX;
335 std::string exedir = std::string(dirname(exe_file)) + "/";
337 datadir = exedir + "../data/"; // SuperTux run from source dir
338 if (access(datadir.c_str(), F_OK) != 0)
340 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
341 if (access(datadir.c_str(), F_OK) != 0)
342 { // If all fails, fall back to compiled path
343 datadir = DATA_PREFIX;
348 datadir = DATA_PREFIX;
351 printf("Datadir: %s\n", datadir.c_str());
354 /* Create and setup menus. */
357 main_menu = new Menu();
358 options_menu = new Menu();
359 options_controls_menu = new Menu();
360 load_game_menu = new Menu();
361 save_game_menu = new Menu();
362 game_menu = new Menu();
363 highscore_menu = new Menu();
364 contrib_menu = new Menu();
365 contrib_subset_menu = new Menu();
366 worldmap_menu = new Menu();
368 main_menu->set_pos(screen->w/2, 335);
369 main_menu->additem(MN_GOTO, "Start Game",0,load_game_menu);
370 main_menu->additem(MN_GOTO, "Contrib Levels",0,contrib_menu);
371 main_menu->additem(MN_GOTO, "Options",0,options_menu);
372 main_menu->additem(MN_ACTION,"Level editor",0,0);
373 main_menu->additem(MN_ACTION,"Credits",0,0);
374 main_menu->additem(MN_ACTION,"Quit",0,0);
376 options_menu->additem(MN_LABEL,"Options",0,0);
377 options_menu->additem(MN_HL,"",0,0);
379 options_menu->additem(MN_TOGGLE,"OpenGL",use_gl,0);
381 options_menu->additem(MN_DEACTIVE,"OpenGL (not supported)",use_gl,0);
383 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
386 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
387 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
391 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
392 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
394 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
395 options_menu->additem(MN_GOTO,"Controls ",0,options_controls_menu);
396 options_menu->additem(MN_HL,"",0,0);
397 options_menu->additem(MN_BACK,"Back",0,0);
399 options_controls_menu->additem(MN_LABEL,"Controls",0,0);
400 options_controls_menu->additem(MN_HL,"",0,0);
401 //FIXME:options_controls_menu->additem(MN_CONTROLFIELD,"Move Right", tux.keymap.right,0);
402 options_controls_menu->additem(MN_HL,"",0,0);
403 options_controls_menu->additem(MN_BACK,"Back",0,0);
405 load_game_menu->additem(MN_LABEL,"Start Game",0,0);
406 load_game_menu->additem(MN_HL,"",0,0);
407 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
408 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
409 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
410 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
411 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
412 load_game_menu->additem(MN_HL,"",0,0);
413 load_game_menu->additem(MN_BACK,"Back",0,0);
415 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
416 save_game_menu->additem(MN_HL,"",0,0);
417 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
418 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
419 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
420 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
421 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
422 save_game_menu->additem(MN_HL,"",0,0);
423 save_game_menu->additem(MN_BACK,"Back",0,0);
425 game_menu->additem(MN_LABEL,"Pause",0,0);
426 game_menu->additem(MN_HL,"",0,0);
427 game_menu->additem(MN_ACTION,"Continue",0,0);
428 game_menu->additem(MN_GOTO,"Options",0,options_menu);
429 game_menu->additem(MN_HL,"",0,0);
430 game_menu->additem(MN_ACTION,"Abort Level",0,0);
432 worldmap_menu->additem(MN_LABEL,"Pause",0,0);
433 worldmap_menu->additem(MN_HL,"",0,0);
434 worldmap_menu->additem(MN_ACTION,"Continue",0,0);
435 worldmap_menu->additem(MN_ACTION,"Save",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)
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);
450 pmenu->item[i].kind = MN_ACTION;
451 pmenu->item[i].change_text(tmp.c_str());
455 bool process_load_game_menu()
457 int slot = load_game_menu->check();
461 WorldMapNS::WorldMap worldmap;
464 snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot-1);
466 // Load the game or at least set the savegame_file variable
467 worldmap.loadgame(slotfile);
471 Menu::set_current(main_menu);
473 st_pause_ticks_stop();
482 /* Handle changes made to global settings in the options menu. */
483 void process_options_menu(void)
485 switch (options_menu->check())
489 if(use_gl != options_menu->item[2].toggled)
497 if(use_fullscreen != options_menu->item[3].toggled)
499 use_fullscreen = !use_fullscreen;
504 if(use_sound != options_menu->item[4].toggled)
505 use_sound = !use_sound;
508 if(use_music != options_menu->item[5].toggled)
521 if (!playing_music())
523 play_current_music();
529 if(show_fps != options_menu->item[6].toggled)
530 show_fps = !show_fps;
535 void st_general_setup(void)
537 /* Seed random number generator: */
539 srand(SDL_GetTicks());
541 /* Set icon image: */
545 /* Unicode needed for input handling: */
547 SDL_EnableUNICODE(1);
549 /* Load global images: */
551 black_text = new Text(datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
552 gold_text = new Text(datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
553 blue_text = new Text(datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
554 red_text = new Text(datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
555 white_text = new Text(datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
556 white_small_text = new Text(datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
557 white_big_text = new Text(datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
558 yellow_nums = new Text(datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
560 /* Load GUI/menu images: */
561 checkbox = new Surface(datadir + "/images/status/checkbox.png", USE_ALPHA);
562 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
563 back = new Surface(datadir + "/images/status/back.png", USE_ALPHA);
564 arrow_left = new Surface(datadir + "/images/icons/left.png", USE_ALPHA);
565 arrow_right = new Surface(datadir + "/images/icons/right.png", USE_ALPHA);
567 /* Load the mouse-cursor */
568 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
572 void st_general_free(void)
575 /* Free global images: */
582 delete white_small_text;
583 delete white_big_text;
585 /* Free GUI/menu images: */
587 delete checkbox_checked;
592 /* Free mouse-cursor */
599 delete highscore_menu;
600 delete save_game_menu;
601 delete load_game_menu;
604 void st_video_setup(void)
607 SDL_FreeSurface(screen);
609 /* Init SDL Video: */
611 if (SDL_Init(SDL_INIT_VIDEO) < 0)
614 "\nError: I could not initialize video!\n"
615 "The Simple DirectMedia error that occured was:\n"
616 "%s\n\n", SDL_GetError());
624 st_video_setup_sdl();
626 Surface::reload_all();
628 /* Set window manager stuff: */
629 SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux");
632 void st_video_setup_sdl(void)
634 SDL_FreeSurface(screen);
638 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
642 "\nWarning: I could not set up fullscreen video for "
644 "The Simple DirectMedia error that occured was:\n"
645 "%s\n\n", SDL_GetError());
646 use_fullscreen = false;
651 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
656 "\nError: I could not set up video for 640x480 mode.\n"
657 "The Simple DirectMedia error that occured was:\n"
658 "%s\n\n", SDL_GetError());
664 void st_video_setup_gl(void)
668 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
669 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
670 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
671 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
672 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
676 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
680 "\nWarning: I could not set up fullscreen video for "
682 "The Simple DirectMedia error that occured was:\n"
683 "%s\n\n", SDL_GetError());
684 use_fullscreen = false;
689 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
694 "\nError: I could not set up video for 640x480 mode.\n"
695 "The Simple DirectMedia error that occured was:\n"
696 "%s\n\n", SDL_GetError());
702 * Set up OpenGL for 2D rendering.
704 glDisable(GL_DEPTH_TEST);
705 glDisable(GL_CULL_FACE);
707 glViewport(0, 0, screen->w, screen->h);
708 glMatrixMode(GL_PROJECTION);
710 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
712 glMatrixMode(GL_MODELVIEW);
714 glTranslatef(0.0f, 0.0f, 0.0f);
720 void st_joystick_setup(void)
727 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
729 fprintf(stderr, "Warning: I could not initialize joystick!\n"
730 "The Simple DirectMedia error that occured was:\n"
731 "%s\n\n", SDL_GetError());
733 use_joystick = false;
738 if (SDL_NumJoysticks() <= 0)
740 fprintf(stderr, "Warning: No joysticks are available.\n");
742 use_joystick = false;
746 js = SDL_JoystickOpen(joystick_num);
750 fprintf(stderr, "Warning: Could not open joystick %d.\n"
751 "The Simple DirectMedia error that occured was:\n"
752 "%s\n\n", joystick_num, SDL_GetError());
754 use_joystick = false;
758 /* Check for proper joystick configuration: */
760 if (SDL_JoystickNumAxes(js) < 2)
763 "Warning: Joystick does not have enough axes!\n");
765 use_joystick = false;
769 if (SDL_JoystickNumButtons(js) < 2)
773 "Joystick does not have enough buttons!\n");
775 use_joystick = false;
783 void st_audio_setup(void)
786 /* Init SDL Audio silently even if --disable-sound : */
790 if (SDL_Init(SDL_INIT_AUDIO) < 0)
792 /* only print out message if sound or music
793 was not disabled at command-line
795 if (use_sound || use_music)
798 "\nWarning: I could not initialize audio!\n"
799 "The Simple DirectMedia error that occured was:\n"
800 "%s\n\n", SDL_GetError());
802 /* keep the programming logic the same :-)
803 because in this case, use_sound & use_music' values are ignored
804 when there's no available audio device
808 audio_device = false;
813 /* Open sound silently regarless the value of "use_sound": */
817 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
819 /* only print out message if sound or music
820 was not disabled at command-line
822 if (use_sound || use_music)
825 "\nWarning: I could not set up audio for 44100 Hz "
827 "The Simple DirectMedia error that occured was:\n"
828 "%s\n\n", SDL_GetError());
832 audio_device = false;
839 /* --- SHUTDOWN --- */
841 void st_shutdown(void)
850 void st_abort(const std::string& reason, const std::string& details)
852 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
858 /* Set Icon (private) */
867 /* Load icon into a surface: */
869 icon = IMG_Load((datadir + "/images/icon.png").c_str());
873 "\nError: I could not load the icon image: %s%s\n"
874 "The Simple DirectMedia error that occured was:\n"
875 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
882 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
883 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
884 memset(mask, 0xFF, masklen);
889 SDL_WM_SetIcon(icon, mask);
892 /* Free icon surface & mask: */
895 SDL_FreeSurface(icon);
899 /* Parse command-line arguments: */
901 void parseargs(int argc, char * argv[])
907 /* Parse arguments: */
909 for (i = 1; i < argc; i++)
911 if (strcmp(argv[i], "--fullscreen") == 0 ||
912 strcmp(argv[i], "-f") == 0)
914 /* Use full screen: */
916 use_fullscreen = true;
918 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
921 joystick_num = atoi(argv[++i]);
923 else if (strcmp(argv[i], "--worldmap") == 0)
925 launch_worldmap_mode = true;
927 else if (strcmp(argv[i], "--datadir") == 0
928 || strcmp(argv[i], "-d") == 0 )
933 else if (strcmp(argv[i], "--show-fps") == 0)
935 /* Use full screen: */
939 else if (strcmp(argv[i], "--opengl") == 0 ||
940 strcmp(argv[i], "-gl") == 0)
948 else if (strcmp(argv[i], "--sdl") == 0)
952 else if (strcmp(argv[i], "--usage") == 0)
958 else if (strcmp(argv[i], "--version") == 0)
961 printf("SuperTux " VERSION "\n");
964 else if (strcmp(argv[i], "--disable-sound") == 0)
966 /* Disable the compiled in sound feature */
967 printf("Sounds disabled \n");
970 else if (strcmp(argv[i], "--disable-music") == 0)
972 /* Disable the compiled in sound feature */
973 printf("Music disabled \n");
976 else if (strcmp(argv[i], "--debug-mode") == 0)
978 /* Enable the debug-mode */
982 else if (strcmp(argv[i], "--help") == 0)
984 puts("Super Tux " VERSION "\n"
985 " Please see the file \"README.txt\" for more details.\n");
986 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
987 puts("Display Options:\n"
988 " --fullscreen Run in fullscreen mode.\n"
989 " --opengl If opengl support was compiled in, this will enable\n"
990 " the EXPERIMENTAL OpenGL mode.\n"
991 " --sdl Use non-opengl renderer\n"
994 " --disable-sound If sound support was compiled in, this will\n"
995 " disable sound for this session of the game.\n"
996 " --disable-music Like above, but this will disable music.\n"
999 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
1000 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
1001 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
1002 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
1003 " --help Display a help message summarizing command-line\n"
1004 " options, license and game controls.\n"
1005 " --usage Display a brief message summarizing command-line options.\n"
1006 " --version Display the version of SuperTux you're running.\n\n"
1010 else if (argv[i][0] != '-')
1012 level_startup_file = argv[i];
1016 /* Unknown - complain! */
1024 /* Display usage: */
1026 void usage(char * prog, int ret)
1031 /* Determine which stream to write to: */
1039 /* Display the usage message: */
1041 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",