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"
43 #define mkdir(dir, mode) mkdir(dir)
44 // on win32 we typically don't want LFS paths
46 #define DATA_PREFIX "./data"
49 /* Local function prototypes: */
52 void usage(char * prog, int ret);
54 /* Does the given file exist and is it accessible? */
55 int faccessible(const char *filename)
58 if (stat(filename, &filestat) == -1)
64 if(S_ISREG(filestat.st_mode))
71 /* Can we write to this location? */
72 int fwriteable(const char *filename)
75 fi = fopen(filename, "wa");
83 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
84 int fcreatedir(const char* relative_dir)
87 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
88 if(mkdir(path,0755) != 0)
90 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
91 if(mkdir(path,0755) != 0)
106 FILE * opendata(const char * rel_filename, const char * mode)
108 char * filename = NULL;
111 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
112 strlen(rel_filename) + 1));
114 strcpy(filename, st_dir);
115 /* Open the high score file: */
117 strcat(filename, rel_filename);
119 /* Try opening the file: */
120 fi = fopen(filename, mode);
124 fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
126 if (strcmp(mode, "r") == 0)
127 fprintf(stderr, "for read!!!\n");
128 else if (strcmp(mode, "w") == 0)
129 fprintf(stderr, "for write!!!\n");
136 /* Get all names of sub-directories in a certain directory. */
137 /* Returns the number of sub-directories found. */
138 /* Note: The user has to free the allocated space. */
139 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
142 struct dirent *direntp;
143 string_list_type sdirs;
147 string_list_init(&sdirs);
148 sprintf(path,"%s/%s",st_dir,rel_path);
149 if((dirStructP = opendir(path)) != NULL)
151 while((direntp = readdir(dirStructP)) != NULL)
153 char absolute_filename[1024];
156 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
158 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
160 if(expected_file != NULL)
162 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
163 if(!faccessible(filename))
167 string_list_add_item(&sdirs,direntp->d_name);
170 closedir(dirStructP);
173 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
174 if((dirStructP = opendir(path)) != NULL)
176 while((direntp = readdir(dirStructP)) != NULL)
178 char absolute_filename[1024];
181 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
183 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
185 if(expected_file != NULL)
187 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
188 if(!faccessible(filename))
194 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
195 if(faccessible(filename))
200 string_list_add_item(&sdirs,direntp->d_name);
203 closedir(dirStructP);
209 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
212 struct dirent *direntp;
213 string_list_type sdirs;
216 string_list_init(&sdirs);
217 sprintf(path,"%s/%s",st_dir,rel_path);
218 if((dirStructP = opendir(path)) != NULL)
220 while((direntp = readdir(dirStructP)) != NULL)
222 char absolute_filename[1024];
225 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
227 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
229 if(exception_str != NULL)
231 if(strstr(direntp->d_name,exception_str) != NULL)
235 if(strstr(direntp->d_name,glob) == NULL)
238 string_list_add_item(&sdirs,direntp->d_name);
241 closedir(dirStructP);
244 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
245 if((dirStructP = opendir(path)) != NULL)
247 while((direntp = readdir(dirStructP)) != NULL)
249 char absolute_filename[1024];
252 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
254 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
256 if(exception_str != NULL)
258 if(strstr(direntp->d_name,exception_str) != NULL)
262 if(strstr(direntp->d_name,glob) == NULL)
265 string_list_add_item(&sdirs,direntp->d_name);
268 closedir(dirStructP);
274 void free_strings(char **strings, int num)
277 for(i=0; i < num; ++i)
282 /* Set SuperTux configuration and save directories */
283 void st_directory_setup(void)
287 /* Get home directory (from $HOME variable)... if we can't determine it,
288 use the current directory ("."): */
289 if (getenv("HOME") != NULL)
290 home = getenv("HOME");
294 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
295 strlen("/.supertux") + 1));
296 strcpy(st_dir, home);
297 strcat(st_dir, "/.supertux");
299 /* Remove .supertux config-file from old SuperTux versions */
300 if(faccessible(st_dir))
306 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
308 strcpy(st_save_dir,st_dir);
309 strcat(st_save_dir,"/save");
311 /* Create them. In the case they exist they won't destroy anything. */
313 mkdir(st_save_dir, 0755);
315 sprintf(str, "%s/levels", st_dir);
318 // User has not that a datadir, so we try some magic
323 char exe_file[PATH_MAX];
324 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
326 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
327 datadir = DATA_PREFIX;
331 std::string exedir = std::string(dirname(exe_file)) + "/";
333 datadir = exedir + "../data/"; // SuperTux run from source dir
334 if (access(datadir.c_str(), F_OK) != 0)
336 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
337 if (access(datadir.c_str(), F_OK) != 0)
338 { // If all fails, fall back to compiled path
339 datadir = DATA_PREFIX;
344 datadir = DATA_PREFIX;
347 printf("Datadir: %s\n", datadir.c_str());
350 /* Create and setup menus. */
353 main_menu = new Menu();
354 options_menu = new Menu();
355 load_game_menu = new Menu();
356 save_game_menu = new Menu();
357 game_menu = new Menu();
358 highscore_menu = new Menu();
360 main_menu->additem(MN_LABEL,"Main Menu",0,0);
361 main_menu->additem(MN_HL,"",0,0);
362 main_menu->additem(MN_ACTION,"Start Game",0,0);
363 main_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
364 main_menu->additem(MN_GOTO,"Options",0,options_menu);
365 main_menu->additem(MN_ACTION,"Level editor",0,0);
366 main_menu->additem(MN_ACTION,"Credits",0,0);
367 main_menu->additem(MN_HL,"",0,0);
368 main_menu->additem(MN_ACTION,"Quit",0,0);
370 options_menu->additem(MN_LABEL,"Options",0,0);
371 options_menu->additem(MN_HL,"",0,0);
372 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
375 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
376 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
380 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
381 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
383 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
384 options_menu->additem(MN_HL,"",0,0);
385 options_menu->additem(MN_BACK,"Back",0,0);
387 load_game_menu->additem(MN_LABEL,"Load Game",0,0);
388 load_game_menu->additem(MN_HL,"",0,0);
389 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
390 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
391 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
392 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
393 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
394 load_game_menu->additem(MN_HL,"",0,0);
395 load_game_menu->additem(MN_BACK,"Back",0,0);
397 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
398 save_game_menu->additem(MN_HL,"",0,0);
399 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
400 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
401 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
402 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
403 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
404 save_game_menu->additem(MN_HL,"",0,0);
405 save_game_menu->additem(MN_BACK,"Back",0,0);
407 game_menu->additem(MN_LABEL,"InGame Menu",0,0);
408 game_menu->additem(MN_HL,"",0,0);
409 game_menu->additem(MN_ACTION,"Return To Game",0,0);
410 game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
411 game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
412 game_menu->additem(MN_GOTO,"Options",0,options_menu);
413 game_menu->additem(MN_HL,"",0,0);
414 game_menu->additem(MN_ACTION,"Quit Game",0,0);
416 highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
419 void update_load_save_game_menu(Menu* pmenu, int load)
421 for(int i = 2; i < 7; ++i)
425 if(load && strlen(tmp) == strlen("Slot X - Free") )
426 pmenu->item[i].kind = MN_DEACTIVE;
428 pmenu->item[i].kind = MN_ACTION;
429 menu_item_change_text(&pmenu->item[i],tmp);
434 void process_save_load_game_menu(int save)
437 switch (slot = (save ? save_game_menu->check() : load_game_menu->check()))
450 gameloop("default",slot - 1,ST_GL_LOAD_GAME);
452 Menu::set_current(main_menu);
457 st_pause_ticks_stop();
463 /* Handle changes made to global settings in the options menu. */
464 void process_options_menu(void)
466 switch (options_menu->check())
469 if(use_fullscreen != options_menu->item[2].toggled)
471 use_fullscreen = !use_fullscreen;
476 if(use_sound != options_menu->item[3].toggled)
477 use_sound = !use_sound;
480 if(use_music != options_menu->item[4].toggled)
493 if (!playing_music())
495 play_current_music();
501 if(show_fps != options_menu->item[5].toggled)
502 show_fps = !show_fps;
507 void st_general_setup(void)
509 /* Seed random number generator: */
511 srand(SDL_GetTicks());
513 /* Set icon image: */
517 /* Unicode needed for input handling: */
519 SDL_EnableUNICODE(1);
521 /* Load global images: */
523 text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
524 text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
525 text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
526 text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
527 text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
528 text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
529 text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
530 text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
532 /* Load GUI/menu images: */
533 texture_load(&checkbox, datadir + "/images/status/checkbox.png", USE_ALPHA);
534 texture_load(&checkbox_checked, datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
535 texture_load(&back, datadir + "/images/status/back.png", USE_ALPHA);
536 texture_load(&arrow_left, datadir + "/images/icons/left.png", USE_ALPHA);
537 texture_load(&arrow_right, datadir + "/images/icons/right.png", USE_ALPHA);
541 void st_general_free(void)
544 /* Free global images: */
546 text_free(&black_text);
547 text_free(&gold_text);
548 text_free(&white_text);
549 text_free(&blue_text);
550 text_free(&red_text);
551 text_free(&white_small_text);
552 text_free(&white_big_text);
554 /* Free GUI/menu images: */
555 texture_free(&checkbox);
556 texture_free(&checkbox_checked);
558 texture_free(&arrow_left);
559 texture_free(&arrow_right);
565 delete highscore_menu;
566 delete save_game_menu;
567 delete load_game_menu;
570 void st_video_setup(void)
574 SDL_FreeSurface(screen);
576 /* Init SDL Video: */
578 if (SDL_Init(SDL_INIT_VIDEO) < 0)
581 "\nError: I could not initialize video!\n"
582 "The Simple DirectMedia error that occured was:\n"
583 "%s\n\n", SDL_GetError());
592 st_video_setup_sdl();
596 /* Set window manager stuff: */
598 SDL_WM_SetCaption("Super Tux", "Super Tux");
602 void st_video_setup_sdl(void)
604 SDL_FreeSurface(screen);
608 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
612 "\nWarning: I could not set up fullscreen video for "
614 "The Simple DirectMedia error that occured was:\n"
615 "%s\n\n", SDL_GetError());
616 use_fullscreen = false;
621 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
626 "\nError: I could not set up video for 640x480 mode.\n"
627 "The Simple DirectMedia error that occured was:\n"
628 "%s\n\n", SDL_GetError());
634 void st_video_setup_gl(void)
638 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
639 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
640 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
641 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
642 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
646 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
650 "\nWarning: I could not set up fullscreen video for "
652 "The Simple DirectMedia error that occured was:\n"
653 "%s\n\n", SDL_GetError());
654 use_fullscreen = false;
659 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
664 "\nError: I could not set up video for 640x480 mode.\n"
665 "The Simple DirectMedia error that occured was:\n"
666 "%s\n\n", SDL_GetError());
672 * Set up OpenGL for 2D rendering.
674 glDisable(GL_DEPTH_TEST);
675 glDisable(GL_CULL_FACE);
677 glViewport(0, 0, screen->w, screen->h);
678 glMatrixMode(GL_PROJECTION);
680 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
682 glMatrixMode(GL_MODELVIEW);
684 glTranslatef(0.0f, 0.0f, 0.0f);
690 void st_joystick_setup(void)
697 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
699 fprintf(stderr, "Warning: I could not initialize joystick!\n"
700 "The Simple DirectMedia error that occured was:\n"
701 "%s\n\n", SDL_GetError());
703 use_joystick = false;
708 if (SDL_NumJoysticks() <= 0)
710 fprintf(stderr, "Warning: No joysticks are available.\n");
712 use_joystick = false;
716 js = SDL_JoystickOpen(joystick_num);
720 fprintf(stderr, "Warning: Could not open joystick %d.\n"
721 "The Simple DirectMedia error that occured was:\n"
722 "%s\n\n", joystick_num, SDL_GetError());
724 use_joystick = false;
728 /* Check for proper joystick configuration: */
730 if (SDL_JoystickNumAxes(js) < 2)
733 "Warning: Joystick does not have enough axes!\n");
735 use_joystick = false;
739 if (SDL_JoystickNumButtons(js) < 2)
743 "Joystick does not have enough buttons!\n");
745 use_joystick = false;
753 void st_audio_setup(void)
756 /* Init SDL Audio silently even if --disable-sound : */
760 if (SDL_Init(SDL_INIT_AUDIO) < 0)
762 /* only print out message if sound or music
763 was not disabled at command-line
765 if (use_sound || use_music)
768 "\nWarning: I could not initialize audio!\n"
769 "The Simple DirectMedia error that occured was:\n"
770 "%s\n\n", SDL_GetError());
772 /* keep the programming logic the same :-)
773 because in this case, use_sound & use_music' values are ignored
774 when there's no available audio device
778 audio_device = false;
783 /* Open sound silently regarless the value of "use_sound": */
787 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
789 /* only print out message if sound or music
790 was not disabled at command-line
792 if (use_sound || use_music)
795 "\nWarning: I could not set up audio for 44100 Hz "
797 "The Simple DirectMedia error that occured was:\n"
798 "%s\n\n", SDL_GetError());
802 audio_device = false;
809 /* --- SHUTDOWN --- */
811 void st_shutdown(void)
820 void st_abort(const std::string& reason, const std::string& details)
822 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
828 /* Set Icon (private) */
837 /* Load icon into a surface: */
839 icon = IMG_Load((datadir + "/images/icon.png").c_str());
843 "\nError: I could not load the icon image: %s%s\n"
844 "The Simple DirectMedia error that occured was:\n"
845 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
852 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
853 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
854 memset(mask, 0xFF, masklen);
859 SDL_WM_SetIcon(icon, mask);
862 /* Free icon surface & mask: */
865 SDL_FreeSurface(icon);
869 /* Parse command-line arguments: */
871 void parseargs(int argc, char * argv[])
877 /* Parse arguments: */
879 for (i = 1; i < argc; i++)
881 if (strcmp(argv[i], "--fullscreen") == 0 ||
882 strcmp(argv[i], "-f") == 0)
884 /* Use full screen: */
886 use_fullscreen = true;
888 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
891 joystick_num = atoi(argv[++i]);
893 else if (strcmp(argv[i], "--worldmap") == 0)
895 launch_worldmap_mode = true;
897 else if (strcmp(argv[i], "--datadir") == 0
898 || strcmp(argv[i], "-d") == 0 )
903 else if (strcmp(argv[i], "--show-fps") == 0)
905 /* Use full screen: */
909 else if (strcmp(argv[i], "--opengl") == 0 ||
910 strcmp(argv[i], "-gl") == 0)
919 else if (strcmp(argv[i], "--usage") == 0)
925 else if (strcmp(argv[i], "--version") == 0)
929 printf("Super Tux - version " VERSION "\n");
932 else if (strcmp(argv[i], "--disable-sound") == 0)
934 /* Disable the compiled in sound feature */
935 printf("Sounds disabled \n");
938 else if (strcmp(argv[i], "--disable-music") == 0)
940 /* Disable the compiled in sound feature */
941 printf("Music disabled \n");
944 else if (strcmp(argv[i], "--debug-mode") == 0)
946 /* Enable the debug-mode */
950 else if (strcmp(argv[i], "--help") == 0)
952 puts("Super Tux " VERSION "\n"
953 " Please see the file \"README.txt\" for more details.\n");
954 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
955 puts("Display Options:\n"
956 " --fullscreen Run in fullscreen mode.\n"
957 " --opengl If opengl support was compiled in, this will enable\n"
958 " the EXPERIMENTAL OpenGL mode.\n"
961 " --disable-sound If sound support was compiled in, this will\n"
962 " disable sound for this session of the game.\n"
963 " --disable-music Like above, but this will disable music.\n"
966 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
967 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
968 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
969 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
970 " --help Display a help message summarizing command-line\n"
971 " options, license and game controls.\n"
972 " --usage Display a brief message summarizing command-line options.\n"
973 " --version Display the version of SuperTux you're running.\n\n"
977 else if (argv[i][0] != '-')
979 level_startup_file = argv[i];
983 /* Unknown - complain! */
993 void usage(char * prog, int ret)
998 /* Determine which stream to write to: */
1006 /* Display the usage message: */
1008 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",