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"
45 #define mkdir(dir, mode) mkdir(dir)
46 // on win32 we typically don't want LFS paths
48 #define DATA_PREFIX "./data/"
51 /* Local function prototypes: */
54 void usage(char * prog, int ret);
56 /* Does the given file exist and is it accessible? */
57 int faccessible(const char *filename)
60 if (stat(filename, &filestat) == -1)
66 if(S_ISREG(filestat.st_mode))
73 /* Can we write to this location? */
74 int fwriteable(const char *filename)
77 fi = fopen(filename, "wa");
85 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
86 int fcreatedir(const char* relative_dir)
89 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
90 if(mkdir(path,0755) != 0)
92 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
93 if(mkdir(path,0755) != 0)
108 FILE * opendata(const char * rel_filename, const char * mode)
110 char * filename = NULL;
113 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
114 strlen(rel_filename) + 1));
116 strcpy(filename, st_dir);
117 /* Open the high score file: */
119 strcat(filename, rel_filename);
121 /* Try opening the file: */
122 fi = fopen(filename, mode);
126 fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
128 if (strcmp(mode, "r") == 0)
129 fprintf(stderr, "for read!!!\n");
130 else if (strcmp(mode, "w") == 0)
131 fprintf(stderr, "for write!!!\n");
138 /* Get all names of sub-directories in a certain directory. */
139 /* Returns the number of sub-directories found. */
140 /* Note: The user has to free the allocated space. */
141 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
144 struct dirent *direntp;
145 string_list_type sdirs;
149 string_list_init(&sdirs);
150 sprintf(path,"%s/%s",st_dir,rel_path);
151 if((dirStructP = opendir(path)) != NULL)
153 while((direntp = readdir(dirStructP)) != NULL)
155 char absolute_filename[1024];
158 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
160 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
162 if(expected_file != NULL)
164 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
165 if(!faccessible(filename))
169 string_list_add_item(&sdirs,direntp->d_name);
172 closedir(dirStructP);
175 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
176 if((dirStructP = opendir(path)) != NULL)
178 while((direntp = readdir(dirStructP)) != NULL)
180 char absolute_filename[1024];
183 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
185 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
187 if(expected_file != NULL)
189 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
190 if(!faccessible(filename))
196 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
197 if(faccessible(filename))
202 string_list_add_item(&sdirs,direntp->d_name);
205 closedir(dirStructP);
211 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
214 struct dirent *direntp;
215 string_list_type sdirs;
218 string_list_init(&sdirs);
219 sprintf(path,"%s/%s",st_dir,rel_path);
220 if((dirStructP = opendir(path)) != NULL)
222 while((direntp = readdir(dirStructP)) != NULL)
224 char absolute_filename[1024];
227 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
229 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
231 if(exception_str != NULL)
233 if(strstr(direntp->d_name,exception_str) != NULL)
237 if(strstr(direntp->d_name,glob) == NULL)
240 string_list_add_item(&sdirs,direntp->d_name);
243 closedir(dirStructP);
246 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
247 if((dirStructP = opendir(path)) != NULL)
249 while((direntp = readdir(dirStructP)) != NULL)
251 char absolute_filename[1024];
254 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
256 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
258 if(exception_str != NULL)
260 if(strstr(direntp->d_name,exception_str) != NULL)
264 if(strstr(direntp->d_name,glob) == NULL)
267 string_list_add_item(&sdirs,direntp->d_name);
270 closedir(dirStructP);
276 void free_strings(char **strings, int num)
279 for(i=0; i < num; ++i)
284 /* Set SuperTux configuration and save directories */
285 void st_directory_setup(void)
289 /* Get home directory (from $HOME variable)... if we can't determine it,
290 use the current directory ("."): */
291 if (getenv("HOME") != NULL)
292 home = getenv("HOME");
296 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
297 strlen("/.supertux") + 1));
298 strcpy(st_dir, home);
299 strcat(st_dir, "/.supertux");
301 /* Remove .supertux config-file from old SuperTux versions */
302 if(faccessible(st_dir))
308 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
310 strcpy(st_save_dir,st_dir);
311 strcat(st_save_dir,"/save");
313 /* Create them. In the case they exist they won't destroy anything. */
315 mkdir(st_save_dir, 0755);
317 sprintf(str, "%s/levels", st_dir);
320 // User has not that a datadir, so we try some magic
325 char exe_file[PATH_MAX];
326 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
328 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
329 datadir = DATA_PREFIX;
333 std::string exedir = std::string(dirname(exe_file)) + "/";
335 datadir = exedir + "../data/"; // SuperTux run from source dir
336 if (access(datadir.c_str(), F_OK) != 0)
338 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
339 if (access(datadir.c_str(), F_OK) != 0)
340 { // If all fails, fall back to compiled path
341 datadir = DATA_PREFIX;
346 datadir = DATA_PREFIX;
349 printf("Datadir: %s\n", datadir.c_str());
352 /* Create and setup menus. */
355 main_menu = new Menu();
356 options_menu = new Menu();
357 options_controls_menu = new Menu();
358 load_game_menu = new Menu();
359 save_game_menu = new Menu();
360 game_menu = new Menu();
361 highscore_menu = new Menu();
362 contrib_menu = new Menu();
363 contrib_subset_menu = new Menu();
364 worldmap_menu = new Menu();
366 main_menu->set_pos(screen->w/2, 335);
367 main_menu->additem(MN_GOTO, "Start Game",0,load_game_menu);
368 main_menu->additem(MN_GOTO, "Contrib Levels",0,contrib_menu);
369 main_menu->additem(MN_GOTO, "Options",0,options_menu);
370 main_menu->additem(MN_ACTION,"Level editor",0,0);
371 main_menu->additem(MN_ACTION,"Credits",0,0);
372 main_menu->additem(MN_ACTION,"Quit",0,0);
374 options_menu->additem(MN_LABEL,"Options",0,0);
375 options_menu->additem(MN_HL,"",0,0);
376 options_menu->additem(MN_TOGGLE,"OpenGL",use_gl,0);
377 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
380 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
381 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
385 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
386 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
388 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
389 options_menu->additem(MN_GOTO,"Controls ",0,options_controls_menu);
390 options_menu->additem(MN_HL,"",0,0);
391 options_menu->additem(MN_BACK,"Back",0,0);
393 options_controls_menu->additem(MN_LABEL,"Controls",0,0);
394 options_controls_menu->additem(MN_HL,"",0,0);
395 //FIXME:options_controls_menu->additem(MN_CONTROLFIELD,"Move Right", tux.keymap.right,0);
396 options_controls_menu->additem(MN_HL,"",0,0);
397 options_controls_menu->additem(MN_BACK,"Back",0,0);
399 load_game_menu->additem(MN_LABEL,"Start Game",0,0);
400 load_game_menu->additem(MN_HL,"",0,0);
401 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
402 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
403 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
404 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
405 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
406 load_game_menu->additem(MN_HL,"",0,0);
407 load_game_menu->additem(MN_BACK,"Back",0,0);
409 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
410 save_game_menu->additem(MN_HL,"",0,0);
411 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
412 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
413 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
414 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
415 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
416 save_game_menu->additem(MN_HL,"",0,0);
417 save_game_menu->additem(MN_BACK,"Back",0,0);
419 game_menu->additem(MN_LABEL,"InGame Menu",0,0);
420 game_menu->additem(MN_HL,"",0,0);
421 game_menu->additem(MN_ACTION,"Return To Game",0,0);
422 game_menu->additem(MN_GOTO,"Save Game",0,save_game_menu);
423 game_menu->additem(MN_GOTO,"Load Game",0,load_game_menu);
424 game_menu->additem(MN_GOTO,"Options",0,options_menu);
425 game_menu->additem(MN_HL,"",0,0);
426 game_menu->additem(MN_ACTION,"Quit Game",0,0);
428 worldmap_menu->additem(MN_LABEL,"Worldmap Menu",0,0);
429 worldmap_menu->additem(MN_HL,"",0,0);
430 worldmap_menu->additem(MN_ACTION,"Return To Game",0,0);
431 worldmap_menu->additem(MN_GOTO,"Options",0,options_menu);
432 worldmap_menu->additem(MN_HL,"",0,0);
433 worldmap_menu->additem(MN_ACTION,"Quit Game",0,0);
435 highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
438 void update_load_save_game_menu(Menu* pmenu, int load)
440 for(int i = 2; i < 7; ++i)
442 // FIXME: Insert a real savegame struct/class here instead of
443 // doing string vodoo
444 std::string tmp = slotinfo(i-1);
446 if(load && tmp.length() == strlen("Slot X - Free"))
447 pmenu->item[i].kind = MN_ACTION;
449 pmenu->item[i].kind = MN_ACTION;
450 pmenu->item[i].change_text(tmp.c_str());
454 void process_save_game_menu()
456 int slot = save_game_menu->check();
458 GameSession::current()->savegame(slot - 1);
461 bool process_load_game_menu()
463 int slot = load_game_menu->check();
467 // FIXME: Insert a real savegame struct/class here instead of
468 // doing string vodoo
469 std::string tmp = slotinfo(slot-1);
471 if (tmp.length() == strlen("Slot X - Free"))
472 { // Slot is free, so start a new game
473 GameSession session("default", 1, ST_GL_PLAY);
477 Menu::set_current(main_menu);
481 puts("Warning: Loading games isn't supported at the moment");
483 // Slot contains a level, so load it
486 GameSession session("default",slot - 1,ST_GL_LOAD_GAME);
490 Menu::set_current(main_menu);
494 //loadgame(slot - 1);
498 st_pause_ticks_stop();
507 /* Handle changes made to global settings in the options menu. */
508 void process_options_menu(void)
510 switch (options_menu->check())
513 if(use_gl != options_menu->item[2].toggled)
522 if(use_fullscreen != options_menu->item[3].toggled)
524 use_fullscreen = !use_fullscreen;
529 if(use_sound != options_menu->item[4].toggled)
530 use_sound = !use_sound;
533 if(use_music != options_menu->item[5].toggled)
546 if (!playing_music())
548 play_current_music();
554 if(show_fps != options_menu->item[6].toggled)
555 show_fps = !show_fps;
560 void st_general_setup(void)
562 /* Seed random number generator: */
564 srand(SDL_GetTicks());
566 /* Set icon image: */
570 /* Unicode needed for input handling: */
572 SDL_EnableUNICODE(1);
574 /* Load global images: */
576 text_load(&black_text, datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
577 text_load(&gold_text,datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
578 text_load(&blue_text,datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
579 text_load(&red_text,datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
580 text_load(&white_text,datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
581 text_load(&white_small_text,datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
582 text_load(&white_big_text,datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
583 text_load(&yellow_nums,datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
585 /* Load GUI/menu images: */
586 checkbox = new Surface(datadir + "/images/status/checkbox.png", USE_ALPHA);
587 checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
588 back = new Surface(datadir + "/images/status/back.png", USE_ALPHA);
589 arrow_left = new Surface(datadir + "/images/icons/left.png", USE_ALPHA);
590 arrow_right = new Surface(datadir + "/images/icons/right.png", USE_ALPHA);
592 /* Load the mouse-cursor */
593 mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
597 void st_general_free(void)
600 /* Free global images: */
602 text_free(&black_text);
603 text_free(&gold_text);
604 text_free(&white_text);
605 text_free(&blue_text);
606 text_free(&red_text);
607 text_free(&white_small_text);
608 text_free(&white_big_text);
610 /* Free GUI/menu images: */
612 delete checkbox_checked;
617 /* Free mouse-cursor */
624 delete highscore_menu;
625 delete save_game_menu;
626 delete load_game_menu;
629 void st_video_setup(void)
633 SDL_FreeSurface(screen);
635 /* Init SDL Video: */
637 if (SDL_Init(SDL_INIT_VIDEO) < 0)
640 "\nError: I could not initialize video!\n"
641 "The Simple DirectMedia error that occured was:\n"
642 "%s\n\n", SDL_GetError());
650 st_video_setup_sdl();
652 /* Set window manager stuff: */
653 SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux");
657 void st_video_setup_sdl(void)
659 SDL_FreeSurface(screen);
663 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
667 "\nWarning: I could not set up fullscreen video for "
669 "The Simple DirectMedia error that occured was:\n"
670 "%s\n\n", SDL_GetError());
671 use_fullscreen = false;
676 screen = SDL_SetVideoMode(640, 480, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
681 "\nError: I could not set up video for 640x480 mode.\n"
682 "The Simple DirectMedia error that occured was:\n"
683 "%s\n\n", SDL_GetError());
689 void st_video_setup_gl(void)
693 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
694 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
695 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
696 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
697 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
701 screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
705 "\nWarning: I could not set up fullscreen video for "
707 "The Simple DirectMedia error that occured was:\n"
708 "%s\n\n", SDL_GetError());
709 use_fullscreen = false;
714 screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
719 "\nError: I could not set up video for 640x480 mode.\n"
720 "The Simple DirectMedia error that occured was:\n"
721 "%s\n\n", SDL_GetError());
727 * Set up OpenGL for 2D rendering.
729 glDisable(GL_DEPTH_TEST);
730 glDisable(GL_CULL_FACE);
732 glViewport(0, 0, screen->w, screen->h);
733 glMatrixMode(GL_PROJECTION);
735 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
737 glMatrixMode(GL_MODELVIEW);
739 glTranslatef(0.0f, 0.0f, 0.0f);
745 void st_joystick_setup(void)
752 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
754 fprintf(stderr, "Warning: I could not initialize joystick!\n"
755 "The Simple DirectMedia error that occured was:\n"
756 "%s\n\n", SDL_GetError());
758 use_joystick = false;
763 if (SDL_NumJoysticks() <= 0)
765 fprintf(stderr, "Warning: No joysticks are available.\n");
767 use_joystick = false;
771 js = SDL_JoystickOpen(joystick_num);
775 fprintf(stderr, "Warning: Could not open joystick %d.\n"
776 "The Simple DirectMedia error that occured was:\n"
777 "%s\n\n", joystick_num, SDL_GetError());
779 use_joystick = false;
783 /* Check for proper joystick configuration: */
785 if (SDL_JoystickNumAxes(js) < 2)
788 "Warning: Joystick does not have enough axes!\n");
790 use_joystick = false;
794 if (SDL_JoystickNumButtons(js) < 2)
798 "Joystick does not have enough buttons!\n");
800 use_joystick = false;
808 void st_audio_setup(void)
811 /* Init SDL Audio silently even if --disable-sound : */
815 if (SDL_Init(SDL_INIT_AUDIO) < 0)
817 /* only print out message if sound or music
818 was not disabled at command-line
820 if (use_sound || use_music)
823 "\nWarning: I could not initialize audio!\n"
824 "The Simple DirectMedia error that occured was:\n"
825 "%s\n\n", SDL_GetError());
827 /* keep the programming logic the same :-)
828 because in this case, use_sound & use_music' values are ignored
829 when there's no available audio device
833 audio_device = false;
838 /* Open sound silently regarless the value of "use_sound": */
842 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
844 /* only print out message if sound or music
845 was not disabled at command-line
847 if (use_sound || use_music)
850 "\nWarning: I could not set up audio for 44100 Hz "
852 "The Simple DirectMedia error that occured was:\n"
853 "%s\n\n", SDL_GetError());
857 audio_device = false;
864 /* --- SHUTDOWN --- */
866 void st_shutdown(void)
875 void st_abort(const std::string& reason, const std::string& details)
877 fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
883 /* Set Icon (private) */
892 /* Load icon into a surface: */
894 icon = IMG_Load((datadir + "/images/icon.png").c_str());
898 "\nError: I could not load the icon image: %s%s\n"
899 "The Simple DirectMedia error that occured was:\n"
900 "%s\n\n", datadir.c_str(), "/images/icon.png", SDL_GetError());
907 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
908 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
909 memset(mask, 0xFF, masklen);
914 SDL_WM_SetIcon(icon, mask);
917 /* Free icon surface & mask: */
920 SDL_FreeSurface(icon);
924 /* Parse command-line arguments: */
926 void parseargs(int argc, char * argv[])
932 /* Parse arguments: */
934 for (i = 1; i < argc; i++)
936 if (strcmp(argv[i], "--fullscreen") == 0 ||
937 strcmp(argv[i], "-f") == 0)
939 /* Use full screen: */
941 use_fullscreen = true;
943 else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
946 joystick_num = atoi(argv[++i]);
948 else if (strcmp(argv[i], "--worldmap") == 0)
950 launch_worldmap_mode = true;
952 else if (strcmp(argv[i], "--datadir") == 0
953 || strcmp(argv[i], "-d") == 0 )
958 else if (strcmp(argv[i], "--show-fps") == 0)
960 /* Use full screen: */
964 else if (strcmp(argv[i], "--opengl") == 0 ||
965 strcmp(argv[i], "-gl") == 0)
973 else if (strcmp(argv[i], "--sdl") == 0)
977 else if (strcmp(argv[i], "--usage") == 0)
983 else if (strcmp(argv[i], "--version") == 0)
986 printf("SuperTux " VERSION "\n");
989 else if (strcmp(argv[i], "--disable-sound") == 0)
991 /* Disable the compiled in sound feature */
992 printf("Sounds disabled \n");
995 else if (strcmp(argv[i], "--disable-music") == 0)
997 /* Disable the compiled in sound feature */
998 printf("Music disabled \n");
1001 else if (strcmp(argv[i], "--debug-mode") == 0)
1003 /* Enable the debug-mode */
1007 else if (strcmp(argv[i], "--help") == 0)
1009 puts("Super Tux " VERSION "\n"
1010 " Please see the file \"README.txt\" for more details.\n");
1011 printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
1012 puts("Display Options:\n"
1013 " --fullscreen Run in fullscreen mode.\n"
1014 " --opengl If opengl support was compiled in, this will enable\n"
1015 " the EXPERIMENTAL OpenGL mode.\n"
1016 " --sdl Use non-opengl renderer\n"
1019 " --disable-sound If sound support was compiled in, this will\n"
1020 " disable sound for this session of the game.\n"
1021 " --disable-music Like above, but this will disable music.\n"
1024 " -j, --joystick NUM Use joystick NUM (default: 0)\n"
1025 " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n"
1026 " -d, --datadir DIR Load Game data from DIR (default: automatic)\n"
1027 " --debug-mode Enables the debug-mode, which is useful for developers.\n"
1028 " --help Display a help message summarizing command-line\n"
1029 " options, license and game controls.\n"
1030 " --usage Display a brief message summarizing command-line options.\n"
1031 " --version Display the version of SuperTux you're running.\n\n"
1035 else if (argv[i][0] != '-')
1037 level_startup_file = argv[i];
1041 /* Unknown - complain! */
1049 /* Display usage: */
1051 void usage(char * prog, int ret)
1056 /* Determine which stream to write to: */
1064 /* Display the usage message: */
1066 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] [--worldmap] FILENAME\n",