7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - February 1st, 2004
19 #include <SDL_image.h>
21 #include <SDL_opengl.h>
27 #include <sys/types.h>
40 /* Local function prototypes: */
43 void usage(char * prog, int ret);
45 /* Does the given file exist and is it accessible? */
46 int faccessible(char *filename)
49 if (stat(filename, &filestat) == -1)
55 if(S_ISREG(filestat.st_mode))
62 /* Can we write to this location? */
63 int fwriteable(char *filename)
66 fi = fopen(filename, "wa");
74 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
75 int fcreatedir(char* relative_dir)
78 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
79 if(mkdir(path,0755) != 0)
81 snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
82 if(mkdir(path,0755) != 0)
97 /* Get all names of sub-directories in a certain directory. */
98 /* Returns the number of sub-directories found. */
99 /* Note: The user has to free the allocated space. */
100 string_list_type dsubdirs(char *rel_path, char* expected_file)
103 struct dirent *direntp;
105 string_list_type sdirs;
109 string_list_init(&sdirs);
110 sprintf(path,"%s/%s",st_dir,rel_path);
111 if((dirStructP = opendir(path)) != NULL)
113 while((direntp = readdir(dirStructP)) != NULL)
115 char absolute_filename[1024];
118 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
120 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
122 if(expected_file != NULL)
124 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
125 if(!faccessible(filename))
129 string_list_add_item(&sdirs,direntp->d_name);
132 closedir(dirStructP);
135 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
136 if((dirStructP = opendir(path)) != NULL)
138 while((direntp = readdir(dirStructP)) != NULL)
140 char absolute_filename[1024];
143 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
145 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
147 if(expected_file != NULL)
149 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
150 if(!faccessible(filename))
156 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
157 if(faccessible(filename))
162 string_list_add_item(&sdirs,direntp->d_name);
165 closedir(dirStructP);
171 string_list_type dfiles(char *rel_path, char* glob, char* exception_str)
174 struct dirent *direntp;
176 string_list_type sdirs;
180 string_list_init(&sdirs);
181 sprintf(path,"%s/%s",st_dir,rel_path);
182 if((dirStructP = opendir(path)) != NULL)
184 while((direntp = readdir(dirStructP)) != NULL)
186 char absolute_filename[1024];
189 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
191 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
193 if(exception_str != NULL)
195 if(strstr(direntp->d_name,exception_str) != NULL)
199 if(strstr(direntp->d_name,glob) == NULL)
202 string_list_add_item(&sdirs,direntp->d_name);
205 closedir(dirStructP);
208 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
209 if((dirStructP = opendir(path)) != NULL)
211 while((direntp = readdir(dirStructP)) != NULL)
213 char absolute_filename[1024];
216 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
218 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
220 if(exception_str != NULL)
222 if(strstr(direntp->d_name,exception_str) != NULL)
226 if(strstr(direntp->d_name,glob) == NULL)
229 string_list_add_item(&sdirs,direntp->d_name);
232 closedir(dirStructP);
238 void free_strings(char **strings, int num)
241 for(i=0; i < num; ++i)
246 /* Set SuperTux configuration and save directories */
247 void st_directory_setup(void)
251 /* Get home directory (from $HOME variable)... if we can't determine it,
252 use the current directory ("."): */
253 if (getenv("HOME") != NULL)
254 home = getenv("HOME");
258 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
259 strlen("/.supertux") + 1));
260 strcpy(st_dir, home);
261 strcat(st_dir, "/.supertux");
263 /* Remove .supertux config-file from old SuperTux versions */
264 if(faccessible(st_dir))
269 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
271 strcpy(st_save_dir,st_dir);
272 strcat(st_save_dir,"/save");
274 /* Create them. In the case they exist they won't destroy anything. */
278 mkdir(st_save_dir, 0755);
280 sprintf(str, "%s/levels", st_dir);
287 sprintf(str, "%s/levels", st_dir);
294 /* Create and setup menus. */
297 menu_init(&main_menu);
298 menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
299 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
300 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
301 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
302 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
303 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
304 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Credits",0,0));
305 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
306 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
308 menu_init(&options_menu);
309 menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
310 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
311 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
312 if(audio_device == YES)
314 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound ",use_sound,0));
315 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music ",use_music,0));
319 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound ",use_sound,0));
320 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music ",use_music,0));
322 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS ",show_fps,0));
323 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
324 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
326 menu_init(&load_game_menu);
327 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
328 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
329 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
330 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
331 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
332 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
333 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
334 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
335 menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
337 menu_init(&save_game_menu);
338 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
339 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
340 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
341 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
342 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
343 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
344 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
345 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
346 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
348 menu_init(&game_menu);
349 menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
350 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
351 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
352 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
353 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
354 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
355 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
356 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
358 menu_init(&highscore_menu);
359 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
363 void update_load_save_game_menu(menu_type* pmenu, int load)
367 for(i = 2; i < 7; ++i)
371 if(load && strlen(tmp) == strlen("Slot X - Free") )
372 pmenu->item[i].kind = MN_DEACTIVE;
374 pmenu->item[i].kind = MN_ACTION;
375 menu_item_change_text(&pmenu->item[i],tmp);
380 void process_save_load_game_menu(int save)
383 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
394 if(game_started == NO)
396 gameloop("default",slot,ST_GL_LOAD_GAME);
398 menu_set_current(&main_menu);
403 st_pause_ticks_stop();
409 /* Handle changes made to global settings in the options menu. */
410 void process_options_menu(void)
412 switch (menu_check(&options_menu))
415 if(use_fullscreen != options_menu.item[2].toggled)
417 use_fullscreen = !use_fullscreen;
422 if(use_sound != options_menu.item[3].toggled)
423 use_sound = !use_sound;
426 if(use_music != options_menu.item[4].toggled)
439 if (!playing_music())
441 play_current_music();
447 if(show_fps != options_menu.item[5].toggled)
448 show_fps = !show_fps;
453 void st_general_setup(void)
455 /* Seed random number generator: */
457 srand(SDL_GetTicks());
459 /* Load global images: */
461 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
462 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
463 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
464 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
465 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
466 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
467 text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
468 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
470 /* Load GUI/menu images: */
471 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
472 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
473 texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
474 texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
475 texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA);
477 /* Set icon image: */
480 SDL_EnableUNICODE(1);
484 void st_general_free(void)
487 /* Free global images: */
489 text_free(&black_text);
490 text_free(&gold_text);
491 text_free(&white_text);
492 text_free(&blue_text);
493 text_free(&red_text);
494 text_free(&white_small_text);
495 text_free(&white_big_text);
497 /* Free GUI/menu images: */
498 texture_free(&checkbox);
499 texture_free(&checkbox_checked);
501 texture_free(&arrow_left);
502 texture_free(&arrow_right);
506 menu_free(&main_menu);
507 menu_free(&game_menu);
508 menu_free(&options_menu);
509 menu_free(&highscore_menu);
510 menu_free(&save_game_menu);
511 menu_free(&load_game_menu);
515 void st_video_setup(void)
519 SDL_FreeSurface(screen);
521 /* Init SDL Video: */
523 if (SDL_Init(SDL_INIT_VIDEO) < 0)
526 "\nError: I could not initialize video!\n"
527 "The Simple DirectMedia error that occured was:\n"
528 "%s\n\n", SDL_GetError());
537 st_video_setup_sdl();
541 /* Set window manager stuff: */
543 SDL_WM_SetCaption("Super Tux", "Super Tux");
547 void st_video_setup_sdl(void)
549 SDL_FreeSurface(screen);
551 if (use_fullscreen == YES)
553 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
557 "\nWarning: I could not set up fullscreen video for "
559 "The Simple DirectMedia error that occured was:\n"
560 "%s\n\n", SDL_GetError());
566 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
571 "\nError: I could not set up video for 640x480 mode.\n"
572 "The Simple DirectMedia error that occured was:\n"
573 "%s\n\n", SDL_GetError());
579 void st_video_setup_gl(void)
583 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
584 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
585 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
586 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
587 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
589 if (use_fullscreen == YES)
591 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
595 "\nWarning: I could not set up fullscreen video for "
597 "The Simple DirectMedia error that occured was:\n"
598 "%s\n\n", SDL_GetError());
604 screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
609 "\nError: I could not set up video for 640x480 mode.\n"
610 "The Simple DirectMedia error that occured was:\n"
611 "%s\n\n", SDL_GetError());
617 * Set up OpenGL for 2D rendering.
619 glDisable(GL_DEPTH_TEST);
620 glDisable(GL_CULL_FACE);
622 glViewport(0, 0, screen->w, screen->h);
623 glMatrixMode(GL_PROJECTION);
625 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
627 glMatrixMode(GL_MODELVIEW);
629 glTranslatef(0.0f, 0.0f, 0.0f);
635 void st_joystick_setup(void)
643 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
645 fprintf(stderr, "Warning: I could not initialize joystick!\n"
646 "The Simple DirectMedia error that occured was:\n"
647 "%s\n\n", SDL_GetError());
655 if (SDL_NumJoysticks() <= 0)
657 fprintf(stderr, "Warning: No joysticks are available.\n");
663 js = SDL_JoystickOpen(0);
667 fprintf(stderr, "Warning: Could not open joystick 1.\n"
668 "The Simple DirectMedia error that occured was:\n"
669 "%s\n\n", SDL_GetError());
675 /* Check for proper joystick configuration: */
677 if (SDL_JoystickNumAxes(js) < 2)
680 "Warning: Joystick does not have enough axes!\n");
686 if (SDL_JoystickNumButtons(js) < 2)
690 "Joystick does not have enough buttons!\n");
702 void st_audio_setup(void)
705 /* Init SDL Audio silently even if --disable-sound : */
707 if (audio_device == YES)
709 if (SDL_Init(SDL_INIT_AUDIO) < 0)
711 /* only print out message if sound or music
712 was not disabled at command-line
714 if (use_sound == YES || use_music == YES)
717 "\nWarning: I could not initialize audio!\n"
718 "The Simple DirectMedia error that occured was:\n"
719 "%s\n\n", SDL_GetError());
721 /* keep the programming logic the same :-)
722 because in this case, use_sound & use_music' values are ignored
723 when there's no available audio device
732 /* Open sound silently regarless the value of "use_sound": */
734 if (audio_device == YES)
736 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
738 /* only print out message if sound or music
739 was not disabled at command-line
741 if ((use_sound == YES) || (use_music == YES))
744 "\nWarning: I could not set up audio for 44100 Hz "
746 "The Simple DirectMedia error that occured was:\n"
747 "%s\n\n", SDL_GetError());
758 /* --- SHUTDOWN --- */
760 void st_shutdown(void)
769 void st_abort(char * reason, char * details)
771 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
777 /* Set Icon (private) */
786 /* Load icon into a surface: */
788 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
792 "\nError: I could not load the icon image: %s\n"
793 "The Simple DirectMedia error that occured was:\n"
794 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
801 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
802 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
803 memset(mask, 0xFF, masklen);
808 SDL_WM_SetIcon(icon, mask);
811 /* Free icon surface & mask: */
814 SDL_FreeSurface(icon);
818 /* Parse command-line arguments: */
820 void parseargs(int argc, char * argv[])
844 /* Parse arguments: */
846 for (i = 1; i < argc; i++)
848 if (strcmp(argv[i], "--fullscreen") == 0 ||
849 strcmp(argv[i], "-f") == 0)
851 /* Use full screen: */
853 use_fullscreen = YES;
855 else if (strcmp(argv[i], "--show-fps") == 0)
857 /* Use full screen: */
861 else if (strcmp(argv[i], "--opengl") == 0 ||
862 strcmp(argv[i], "-gl") == 0)
871 else if (strcmp(argv[i], "--usage") == 0)
877 else if (strcmp(argv[i], "--version") == 0)
881 printf("Super Tux - version " VERSION "\n");
884 else if (strcmp(argv[i], "--disable-sound") == 0)
886 /* Disable the compiled in sound feature */
888 printf("Sounds disabled \n");
892 printf("Warning: Sounds feature is not compiled in \n");
896 else if (strcmp(argv[i], "--disable-music") == 0)
898 /* Disable the compiled in sound feature */
900 printf("Music disabled \n");
904 printf("Warning: Music feature is not compiled in \n");
908 else if (strcmp(argv[i], "--debug-mode") == 0)
910 /* Enable the debug-mode */
914 else if (strcmp(argv[i], "--help") == 0)
917 printf("Super Tux " VERSION "\n\n");
919 printf("---------- Command-line options ----------\n\n");
921 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
923 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
925 printf(" --disable-music - Like above, but this will disable music.\n\n");
927 printf(" --fullscreen - Run in fullscreen mode.\n\n");
929 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
931 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
933 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
935 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
938 printf("---------- License ----------\n\n");
939 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
940 printf(" This is free software, and you are welcome to redistribute\n");
941 printf(" or modify it under certain conditions. See the file \n");
942 printf(" \"COPYING.txt\" for more details.\n\n\n");
944 printf("---------- Game controls ----------\n\n");
945 printf(" Please see the file \"README.txt\"\n\n");
951 /* Unknown - complain! */
961 void usage(char * prog, int ret)
966 /* Determine which stream to write to: */
974 /* Display the usage message: */
976 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",