3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <SDL_image.h>
31 #include <SDL_opengl.h>
35 #include <sys/types.h>
49 #include "configfile.h"
54 #define mkdir(dir, mode) mkdir(dir)
55 // on win32 we typically don't want LFS paths
57 #define DATA_PREFIX "./data/"
60 /* Local function prototypes: */
63 void usage(char * prog, int ret);
65 /* Does the given file exist and is it accessible? */
66 int faccessible(const char *filename)
69 if (stat(filename, &filestat) == -1)
75 if(S_ISREG(filestat.st_mode))
82 /* Can we write to this location? */
83 int fwriteable(const char *filename)
86 fi = fopen(filename, "wa");
94 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
95 int fcreatedir(const char* relative_dir)
98 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
99 if(mkdir(path,0755) != 0)
101 snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
102 if(mkdir(path,0755) != 0)
117 FILE * opendata(const char * rel_filename, const char * mode)
119 char * filename = NULL;
122 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
123 strlen(rel_filename) + 1));
125 strcpy(filename, st_dir);
126 /* Open the high score file: */
128 strcat(filename, rel_filename);
130 /* Try opening the file: */
131 fi = fopen(filename, mode);
135 fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
137 if (strcmp(mode, "r") == 0)
138 fprintf(stderr, "for read!!!\n");
139 else if (strcmp(mode, "w") == 0)
140 fprintf(stderr, "for write!!!\n");
147 /* Get all names of sub-directories in a certain directory. */
148 /* Returns the number of sub-directories found. */
149 /* Note: The user has to free the allocated space. */
150 string_list_type dsubdirs(const char *rel_path,const char* expected_file)
153 struct dirent *direntp;
154 string_list_type sdirs;
158 string_list_init(&sdirs);
159 sprintf(path,"%s/%s",st_dir,rel_path);
160 if((dirStructP = opendir(path)) != NULL)
162 while((direntp = readdir(dirStructP)) != NULL)
164 char absolute_filename[1024];
167 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
169 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
171 if(expected_file != NULL)
173 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
174 if(!faccessible(filename))
178 string_list_add_item(&sdirs,direntp->d_name);
181 closedir(dirStructP);
184 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
185 if((dirStructP = opendir(path)) != NULL)
187 while((direntp = readdir(dirStructP)) != NULL)
189 char absolute_filename[1024];
192 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
194 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
196 if(expected_file != NULL)
198 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
199 if(!faccessible(filename))
205 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
206 if(faccessible(filename))
211 string_list_add_item(&sdirs,direntp->d_name);
214 closedir(dirStructP);
220 string_list_type dfiles(const char *rel_path, const char* glob, const char* exception_str)
223 struct dirent *direntp;
224 string_list_type sdirs;
227 string_list_init(&sdirs);
228 sprintf(path,"%s/%s",st_dir,rel_path);
229 if((dirStructP = opendir(path)) != NULL)
231 while((direntp = readdir(dirStructP)) != NULL)
233 char absolute_filename[1024];
236 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
238 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
240 if(exception_str != NULL)
242 if(strstr(direntp->d_name,exception_str) != NULL)
246 if(strstr(direntp->d_name,glob) == NULL)
249 string_list_add_item(&sdirs,direntp->d_name);
252 closedir(dirStructP);
255 sprintf(path,"%s/%s",datadir.c_str(),rel_path);
256 if((dirStructP = opendir(path)) != NULL)
258 while((direntp = readdir(dirStructP)) != NULL)
260 char absolute_filename[1024];
263 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
265 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
267 if(exception_str != NULL)
269 if(strstr(direntp->d_name,exception_str) != NULL)
273 if(strstr(direntp->d_name,glob) == NULL)
276 string_list_add_item(&sdirs,direntp->d_name);
279 closedir(dirStructP);
285 void free_strings(char **strings, int num)
288 for(i=0; i < num; ++i)
293 /* Set SuperTux configuration and save directories */
294 void st_directory_setup(void)
298 /* Get home directory (from $HOME variable)... if we can't determine it,
299 use the current directory ("."): */
300 if (getenv("HOME") != NULL)
301 home = getenv("HOME");
305 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
306 strlen("/.supertux") + 1));
307 strcpy(st_dir, home);
308 strcat(st_dir, "/.supertux");
310 /* Remove .supertux config-file from old SuperTux versions */
311 if(faccessible(st_dir))
317 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
319 strcpy(st_save_dir,st_dir);
320 strcat(st_save_dir,"/save");
322 /* Create them. In the case they exist they won't destroy anything. */
324 mkdir(st_save_dir, 0755);
326 sprintf(str, "%s/levels", st_dir);
329 // User has not that a datadir, so we try some magic
334 char exe_file[PATH_MAX];
335 if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
337 puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
338 datadir = DATA_PREFIX;
342 std::string exedir = std::string(dirname(exe_file)) + "/";
344 datadir = exedir + "../data/"; // SuperTux run from source dir
345 if (access(datadir.c_str(), F_OK) != 0)
347 datadir = exedir + "../share/supertux/"; // SuperTux run from PATH
348 if (access(datadir.c_str(), F_OK) != 0)
349 { // If all fails, fall back to compiled path
350 datadir = DATA_PREFIX;
355 datadir = DATA_PREFIX;
358 printf("Datadir: %s\n", datadir.c_str());
361 /* Create and setup menus. */
364 main_menu = new Menu();
365 options_menu = new Menu();
366 options_controls_menu = new Menu();
367 load_game_menu = new Menu();
368 save_game_menu = new Menu();
369 game_menu = new Menu();
370 highscore_menu = new Menu();
371 contrib_menu = new Menu();
372 contrib_subset_menu = new Menu();
373 worldmap_menu = new Menu();
375 main_menu->set_pos(screen->w/2, 335);
376 main_menu->additem(MN_GOTO, "Start Game",0,load_game_menu);
377 main_menu->additem(MN_GOTO, "Contrib Levels",0,contrib_menu);
378 main_menu->additem(MN_GOTO, "Options",0,options_menu);
379 main_menu->additem(MN_ACTION,"Level editor",0,0);
380 main_menu->additem(MN_ACTION,"Credits",0,0);
381 main_menu->additem(MN_ACTION,"Quit",0,0);
383 options_menu->additem(MN_LABEL,"Options",0,0);
384 options_menu->additem(MN_HL,"",0,0);
386 options_menu->additem(MN_TOGGLE,"OpenGL",use_gl,0);
388 options_menu->additem(MN_DEACTIVE,"OpenGL (not supported)",use_gl,0);
390 options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0);
393 options_menu->additem(MN_TOGGLE,"Sound ",use_sound,0);
394 options_menu->additem(MN_TOGGLE,"Music ",use_music,0);
398 options_menu->additem(MN_DEACTIVE,"Sound ",use_sound,0);
399 options_menu->additem(MN_DEACTIVE,"Music ",use_music,0);
401 options_menu->additem(MN_TOGGLE,"Show FPS ",show_fps,0);
402 options_menu->additem(MN_GOTO,"Controls ",0,options_controls_menu);
403 options_menu->additem(MN_HL,"",0,0);
404 options_menu->additem(MN_BACK,"Back",0,0);
406 options_controls_menu->additem(MN_LABEL,"Controls",0,0);
407 options_controls_menu->additem(MN_HL,"",0,0);
408 //FIXME:options_controls_menu->additem(MN_CONTROLFIELD,"Move Right", tux.keymap.right,0);
409 options_controls_menu->additem(MN_HL,"",0,0);
410 options_controls_menu->additem(MN_BACK,"Back",0,0);
412 load_game_menu->additem(MN_LABEL,"Start Game",0,0);
413 load_game_menu->additem(MN_HL,"",0,0);
414 load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
415 load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
416 load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
417 load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
418 load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
419 load_game_menu->additem(MN_HL,"",0,0);
420 load_game_menu->additem(MN_BACK,"Back",0,0);
422 save_game_menu->additem(MN_LABEL,"Save Game",0,0);
423 save_game_menu->additem(MN_HL,"",0,0);
424 save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0);
425 save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0);
426 save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0);
427 save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0);
428 save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0);
429 save_game_menu->additem(MN_HL,"",0,0);
430 save_game_menu->additem(MN_BACK,"Back",0,0);
432 game_menu->additem(MN_LABEL,"Pause",0,0);
433 game_menu->additem(MN_HL,"",0,0);
434 game_menu->additem(MN_ACTION,"Continue",0,0);
435 game_menu->additem(MN_GOTO,"Options",0,options_menu);
436 game_menu->additem(MN_HL,"",0,0);
437 game_menu->additem(MN_ACTION,"Abort Level",0,0);
439 worldmap_menu->additem(MN_LABEL,"Pause",0,0);
440 worldmap_menu->additem(MN_HL,"",0,0);
441 worldmap_menu->additem(MN_ACTION,"Continue",0,0);
442 worldmap_menu->additem(MN_ACTION,"Save",0,0);
443 worldmap_menu->additem(MN_GOTO,"Options",0,options_menu);
444 worldmap_menu->additem(MN_HL,"",0,0);
445 worldmap_menu->additem(MN_ACTION,"Quit Game",0,0);
447 highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
450 void update_load_save_game_menu(Menu* pmenu)
452 for(int i = 2; i < 7; ++i)
454 // FIXME: Insert a real savegame struct/class here instead of
455 // doing string vodoo
456 std::string tmp = slotinfo(i - 1);
457 pmenu->item[i].kind = MN_ACTION;
458 pmenu->item[i].change_text(tmp.c_str());
462 bool process_load_game_menu()
464 int slot = load_game_menu->check();
466 if(slot != -1 && load_game_menu->get_item(slot).kind == MN_ACTION)
468 WorldMapNS::WorldMap worldmap;
471 snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot-1);
473 // Load the game or at least set the savegame_file variable
474 worldmap.loadgame(slotfile);
478 Menu::set_current(main_menu);
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",