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>
40 #include "configfile.h"
44 #define mkdir(dir, mode) mkdir(dir)
45 // on win32 we typically don't want LFS paths
47 #define DATA_PREFIX "./data/"
50 /* Local function prototypes: */
53 void usage(char * prog, int ret);
55 /* Does the given file exist and is it accessible? */
56 int faccessible(const char *filename)
59 if (stat(filename, &filestat) == -1)
65 if(S_ISREG(filestat.st_mode))
72 /* Can we write to this location? */
73 int fwriteable(const char *filename)
76 fi = fopen(filename, "wa");
84 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
85 int fcreatedir(const char* relative_dir)
88 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
89 if(mkdir(path,0755) != 0)
91 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
92 if(mkdir(path,0755) != 0)
107 FILE * opendata(const char * rel_filename, const char * mode)
109 char * filename = NULL;
112 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
113 strlen(rel_filename) + 1));
115 strcpy(filename, st_dir);
116 /* Open the high score file: */
118 strcat(filename, rel_filename);
120 /* Try opening the file: */
121 fi = fopen(filename, mode);
125 fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
127 if (strcmp(mode, "r") == 0)
128 fprintf(stderr, "for read!!!\n");
129 else if (strcmp(mode, "w") == 0)
130 fprintf(stderr, "for write!!!\n");
137 /* Get all names of sub-directories in a certain directory. */
138 /* Returns the number of sub-directories found. */
139 /* Note: The user has to free the allocated space. */
140 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
143 struct dirent *direntp;
144 string_list_type sdirs;
148 string_list_init(&sdirs);
149 sprintf(path,"%s/%s",st_dir,rel_path);
150 if((dirStructP = opendir(path)) != NULL)
152 while((direntp = readdir(dirStructP)) != NULL)
154 char absolute_filename[1024];
157 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
159 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
161 if(expected_file != NULL)
163 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
164 if(!faccessible(filename))
168 string_list_add_item(&sdirs,direntp->d_name);
171 closedir(dirStructP);
174 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
175 if((dirStructP = opendir(path)) != NULL)
177 while((direntp = readdir(dirStructP)) != NULL)
179 char absolute_filename[1024];
182 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
184 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
186 if(expected_file != NULL)
188 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
189 if(!faccessible(filename))
195 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
196 if(faccessible(filename))
201 string_list_add_item(&sdirs,direntp->d_name);
204 closedir(dirStructP);
210 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
213 struct dirent *direntp;
214 string_list_type sdirs;
217 string_list_init(&sdirs);
218 sprintf(path,"%s/%s",st_dir,rel_path);
219 if((dirStructP = opendir(path)) != NULL)
221 while((direntp = readdir(dirStructP)) != NULL)
223 char absolute_filename[1024];
226 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
228 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
230 if(exception_str != NULL)
232 if(strstr(direntp->d_name,exception_str) != NULL)
236 if(strstr(direntp->d_name,glob) == NULL)
239 string_list_add_item(&sdirs,direntp->d_name);
242 closedir(dirStructP);
245 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
246 if((dirStructP = opendir(path)) != NULL)
248 while((direntp = readdir(dirStructP)) != NULL)
250 char absolute_filename[1024];
253 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
255 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
257 if(exception_str != NULL)
259 if(strstr(direntp->d_name,exception_str) != NULL)
263 if(strstr(direntp->d_name,glob) == NULL)
266 string_list_add_item(&sdirs,direntp->d_name);
269 closedir(dirStructP);
275 void free_strings(char **strings, int num)
278 for(i=0; i < num; ++i)
283 /* Set SuperTux configuration and save directories */
284 void st_directory_setup(void)
288 /* Get home directory (from $HOME variable)... if we can't determine it,
289 use the current directory ("."): */
290 if (getenv("HOME") != NULL)
291 home = getenv("HOME");
295 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
296 strlen("/.supertux") + 1));
297 strcpy(st_dir, home);
298 strcat(st_dir, "/.supertux");
300 /* Remove .supertux config-file from old SuperTux versions */
301 if(faccessible(st_dir))
307 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
309 strcpy(st_save_dir,st_dir);
310 strcat(st_save_dir,"/save");
312 /* Create them. In the case they exist they won't destroy anything. */
314 mkdir(st_save_dir, 0755);
316 sprintf(str, "%s/levels", st_dir);
319 // User has not that a datadir, so we try some magic
324 char exe_file[PATH_MAX];
325 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
327 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
328 datadir = DATA_PREFIX;
332 std::string exedir = std::string(dirname(exe_file)) + "/";
334 datadir = exedir + "../data/"; // SuperTux run from source dir
335 if (access(datadir.c_str(), F_OK) != 0)
337 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
338 if (access(datadir.c_str(), F_OK) != 0)
339 { // If all fails, fall back to compiled path
340 datadir = DATA_PREFIX;
345 datadir = DATA_PREFIX;
348 printf("Datadir: %s\n", datadir.c_str());
351 /* Create and setup menus. */
354 main_menu = new Menu();
355 options_menu = new Menu();
356 options_controls_menu = new Menu();
357 load_game_menu = new Menu();
358 save_game_menu = new Menu();
359 game_menu = new Menu();
360 highscore_menu = new Menu();
362 main_menu->additem(MN_LABEL,"Main Menu",0,0);
363 main_menu->additem(MN_HL,"",0,0);
364 main_menu->additem(MN_ACTION,"Start Game",0,0);
365 main_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
366 main_menu->additem(MN_GOTO,"Options",0,options_menu);
367 main_menu->additem(MN_ACTION,"Level editor",0,0);
368 main_menu->additem(MN_ACTION,"Credits",0,0);
369 main_menu->additem(MN_HL,"",0,0);
370 main_menu->additem(MN_ACTION,"Quit",0,0);
372 options_menu->additem(MN_LABEL,"Options",0,0);
373 options_menu->additem(MN_HL,"",0,0);
374 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
377 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
378 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
382 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
383 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
385 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
386 options_menu->additem(MN_GOTO,"Controls ",0,options_controls_menu);
387 options_menu->additem(MN_HL,"",0,0);
388 options_menu->additem(MN_BACK,"Back",0,0);
390 options_controls_menu->additem(MN_LABEL,"Controls",0,0);
391 options_controls_menu->additem(MN_HL,"",0,0);
392 options_controls_menu->additem(MN_CONTROLFIELD,"Move Right",tux.keymap.right,0);
393 options_controls_menu->additem(MN_HL,"",0,0);
394 options_controls_menu->additem(MN_BACK,"Back",0,0);
396 load_game_menu->additem(MN_LABEL,"Load Game",0,0);
397 load_game_menu->additem(MN_HL,"",0,0);
398 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
399 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
400 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
401 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
402 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
403 load_game_menu->additem(MN_HL,"",0,0);
404 load_game_menu->additem(MN_BACK,"Back",0,0);
406 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
407 save_game_menu->additem(MN_HL,"",0,0);
408 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
409 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
410 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
411 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
412 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
413 save_game_menu->additem(MN_HL,"",0,0);
414 save_game_menu->additem(MN_BACK,"Back",0,0);
416 game_menu->additem(MN_LABEL,"InGame Menu",0,0);
417 game_menu->additem(MN_HL,"",0,0);
418 game_menu->additem(MN_ACTION,"Return To Game",0,0);
419 game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
420 game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
421 game_menu->additem(MN_GOTO,"Options",0,options_menu);
422 game_menu->additem(MN_HL,"",0,0);
423 game_menu->additem(MN_ACTION,"Quit Game",0,0);
425 highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
428 void update_load_save_game_menu(Menu* pmenu, int load)
430 for(int i = 2; i < 7; ++i)
434 if(load && strlen(tmp) == strlen("Slot X - Free") )
435 pmenu->item[i].kind = MN_DEACTIVE;
437 pmenu->item[i].kind = MN_ACTION;
438 menu_item_change_text(&pmenu->item[i],tmp);
443 void process_save_load_game_menu(int save)
446 switch (slot = (save ? save_game_menu->check() : load_game_menu->check()))
459 gameloop("default",slot - 1,ST_GL_LOAD_GAME);
461 Menu::set_current(main_menu);
466 st_pause_ticks_stop();
472 /* Handle changes made to global settings in the options menu. */
473 void process_options_menu(void)
475 switch (options_menu->check())
478 if(use_fullscreen != options_menu->item[2].toggled)
480 use_fullscreen = !use_fullscreen;
485 if(use_sound != options_menu->item[3].toggled)
486 use_sound = !use_sound;
489 if(use_music != options_menu->item[4].toggled)
502 if (!playing_music())
504 play_current_music();
510 if(show_fps != options_menu->item[5].toggled)
511 show_fps = !show_fps;
516 void st_general_setup(void)
518 /* Seed random number generator: */
520 srand(SDL_GetTicks());
522 /* Set icon image: */
526 /* Unicode needed for input handling: */
528 SDL_EnableUNICODE(1);
530 /* Load global images: */
532 text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
533 text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
534 text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
535 text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
536 text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
537 text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
538 text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
539 text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
541 /* Load GUI/menu images: */
542 texture_load(&checkbox, datadir + "/images/status/checkbox.png", USE_ALPHA);
543 texture_load(&checkbox_checked, datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
544 texture_load(&back, datadir + "/images/status/back.png", USE_ALPHA);
545 texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA);
546 texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA);
548 /* Load the mouse-cursor */
549 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
553 void st_general_free(void)
556 /* Free global images: */
558 text_free(&black_text);
559 text_free(&gold_text);
560 text_free(&white_text);
561 text_free(&blue_text);
562 text_free(&red_text);
563 text_free(&white_small_text);
564 text_free(&white_big_text);
566 /* Free GUI/menu images: */
567 texture_free(&checkbox);
568 texture_free(&checkbox_checked);
570 texture_free(&arrow_left);
571 texture_free(&arrow_right);
573 /* Free mouse-cursor */
580 delete highscore_menu;
581 delete save_game_menu;
582 delete load_game_menu;
585 void st_video_setup(void)
589 SDL_FreeSurface(screen);
591 /* Init SDL Video: */
593 if (SDL_Init(SDL_INIT_VIDEO) < 0)
596 "\nError: I could not initialize video!\n"
597 "The Simple DirectMedia error that occured was:\n"
598 "%s\n\n", SDL_GetError());
607 st_video_setup_sdl();
611 /* Set window manager stuff: */
613 SDL_WM_SetCaption("Super Tux", "Super Tux");
617 void st_video_setup_sdl(void)
619 SDL_FreeSurface(screen);
623 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
627 "\nWarning: I could not set up fullscreen video for "
629 "The Simple DirectMedia error that occured was:\n"
630 "%s\n\n", SDL_GetError());
631 use_fullscreen = false;
636 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
641 "\nError: I could not set up video for 640x480 mode.\n"
642 "The Simple DirectMedia error that occured was:\n"
643 "%s\n\n", SDL_GetError());
649 void st_video_setup_gl(void)
653 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
654 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
655 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
656 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
657 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
661 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
665 "\nWarning: I could not set up fullscreen video for "
667 "The Simple DirectMedia error that occured was:\n"
668 "%s\n\n", SDL_GetError());
669 use_fullscreen = false;
674 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
679 "\nError: I could not set up video for 640x480 mode.\n"
680 "The Simple DirectMedia error that occured was:\n"
681 "%s\n\n", SDL_GetError());
687 * Set up OpenGL for 2D rendering.
689 glDisable(GL_DEPTH_TEST);
690 glDisable(GL_CULL_FACE);
692 glViewport(0, 0, screen->w, screen->h);
693 glMatrixMode(GL_PROJECTION);
695 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
697 glMatrixMode(GL_MODELVIEW);
699 glTranslatef(0.0f, 0.0f, 0.0f);
705 void st_joystick_setup(void)
712 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
714 fprintf(stderr, "Warning: I could not initialize joystick!\n"
715 "The Simple DirectMedia error that occured was:\n"
716 "%s\n\n", SDL_GetError());
718 use_joystick = false;
723 if (SDL_NumJoysticks() <= 0)
725 fprintf(stderr, "Warning: No joysticks are available.\n");
727 use_joystick = false;
731 js = SDL_JoystickOpen(joystick_num);
735 fprintf(stderr, "Warning: Could not open joystick %d.\n"
736 "The Simple DirectMedia error that occured was:\n"
737 "%s\n\n", joystick_num, SDL_GetError());
739 use_joystick = false;
743 /* Check for proper joystick configuration: */
745 if (SDL_JoystickNumAxes(js) < 2)
748 "Warning: Joystick does not have enough axes!\n");
750 use_joystick = false;
754 if (SDL_JoystickNumButtons(js) < 2)
758 "Joystick does not have enough buttons!\n");
760 use_joystick = false;
768 void st_audio_setup(void)
771 /* Init SDL Audio silently even if --disable-sound : */
775 if (SDL_Init(SDL_INIT_AUDIO) < 0)
777 /* only print out message if sound or music
778 was not disabled at command-line
780 if (use_sound || use_music)
783 "\nWarning: I could not initialize audio!\n"
784 "The Simple DirectMedia error that occured was:\n"
785 "%s\n\n", SDL_GetError());
787 /* keep the programming logic the same :-)
788 because in this case, use_sound & use_music' values are ignored
789 when there's no available audio device
793 audio_device = false;
798 /* Open sound silently regarless the value of "use_sound": */
802 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
804 /* only print out message if sound or music
805 was not disabled at command-line
807 if (use_sound || use_music)
810 "\nWarning: I could not set up audio for 44100 Hz "
812 "The Simple DirectMedia error that occured was:\n"
813 "%s\n\n", SDL_GetError());
817 audio_device = false;
824 /* --- SHUTDOWN --- */
826 void st_shutdown(void)
835 void st_abort(const std::string& reason, const std::string& details)
837 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
843 /* Set Icon (private) */
852 /* Load icon into a surface: */
854 icon = IMG_Load((datadir + "/images/icon.png").c_str());
858 "\nError: I could not load the icon image: %s%s\n"
859 "The Simple DirectMedia error that occured was:\n"
860 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
867 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
868 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
869 memset(mask, 0xFF, masklen);
874 SDL_WM_SetIcon(icon, mask);
877 /* Free icon surface & mask: */
880 SDL_FreeSurface(icon);
884 /* Parse command-line arguments: */
886 void parseargs(int argc, char * argv[])
892 /* Parse arguments: */
894 for (i = 1; i < argc; i++)
896 if (strcmp(argv[i], "--fullscreen") == 0 ||
897 strcmp(argv[i], "-f") == 0)
899 /* Use full screen: */
901 use_fullscreen = true;
903 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
906 joystick_num = atoi(argv[++i]);
908 else if (strcmp(argv[i], "--worldmap") == 0)
910 launch_worldmap_mode = true;
912 else if (strcmp(argv[i], "--datadir") == 0
913 || strcmp(argv[i], "-d") == 0 )
918 else if (strcmp(argv[i], "--show-fps") == 0)
920 /* Use full screen: */
924 else if (strcmp(argv[i], "--opengl") == 0 ||
925 strcmp(argv[i], "-gl") == 0)
934 else if (strcmp(argv[i], "--usage") == 0)
940 else if (strcmp(argv[i], "--version") == 0)
944 printf("Super Tux - version " VERSION "\n");
947 else if (strcmp(argv[i], "--disable-sound") == 0)
949 /* Disable the compiled in sound feature */
950 printf("Sounds disabled \n");
953 else if (strcmp(argv[i], "--disable-music") == 0)
955 /* Disable the compiled in sound feature */
956 printf("Music disabled \n");
959 else if (strcmp(argv[i], "--debug-mode") == 0)
961 /* Enable the debug-mode */
965 else if (strcmp(argv[i], "--help") == 0)
967 puts("Super Tux " VERSION "\n"
968 " Please see the file \"README.txt\" for more details.\n");
969 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
970 puts("Display Options:\n"
971 " --fullscreen Run in fullscreen mode.\n"
972 " --opengl If opengl support was compiled in, this will enable\n"
973 " the EXPERIMENTAL OpenGL mode.\n"
976 " --disable-sound If sound support was compiled in, this will\n"
977 " disable sound for this session of the game.\n"
978 " --disable-music Like above, but this will disable music.\n"
981 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
982 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
983 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
984 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
985 " --help Display a help message summarizing command-line\n"
986 " options, license and game controls.\n"
987 " --usage Display a brief message summarizing command-line options.\n"
988 " --version Display the version of SuperTux you're running.\n\n"
992 else if (argv[i][0] != '-')
994 level_startup_file = argv[i];
998 /* Unknown - complain! */
1006 /* Display usage: */
1008 void usage(char * prog, int ret)
1013 /* Determine which stream to write to: */
1021 /* Display the usage message: */
1023 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",