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 /* Can we write to this location? */
56 int fwriteable(char *filename)
59 fi = fopen(filename, "wa");
67 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
68 int fcreatedir(char* relative_dir)
71 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
72 if(mkdir(path,0755) != 0)
74 snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
75 if(mkdir(path,0755) != 0)
90 /* Get all names of sub-directories in a certain directory. */
91 /* Returns the number of sub-directories found. */
92 /* Note: The user has to free the allocated space. */
93 string_list_type dsubdirs(char *rel_path, char* expected_file)
96 struct dirent *direntp;
98 string_list_type sdirs;
102 string_list_init(&sdirs);
103 sprintf(path,"%s/%s",st_dir,rel_path);
104 if((dirStructP = opendir(path)) != NULL)
106 while((direntp = readdir(dirStructP)) != NULL)
108 char absolute_filename[1024];
111 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
113 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
115 if(expected_file != NULL)
117 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
118 if(!faccessible(filename))
122 string_list_add_item(&sdirs,direntp->d_name);
125 closedir(dirStructP);
128 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
129 if((dirStructP = opendir(path)) != NULL)
131 while((direntp = readdir(dirStructP)) != NULL)
133 char absolute_filename[1024];
136 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
138 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
140 if(expected_file != NULL)
142 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
143 if(!faccessible(filename))
149 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
150 if(faccessible(filename))
155 string_list_add_item(&sdirs,direntp->d_name);
158 closedir(dirStructP);
164 string_list_type dfiles(char *rel_path, char* glob, char* exception_str)
167 struct dirent *direntp;
169 string_list_type sdirs;
173 string_list_init(&sdirs);
174 sprintf(path,"%s/%s",st_dir,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_ISREG(buf.st_mode))
186 if(exception_str != NULL)
188 if(strstr(direntp->d_name,exception_str) != NULL)
192 if(strstr(direntp->d_name,glob) == NULL)
195 string_list_add_item(&sdirs,direntp->d_name);
198 closedir(dirStructP);
201 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
202 if((dirStructP = opendir(path)) != NULL)
204 while((direntp = readdir(dirStructP)) != NULL)
206 char absolute_filename[1024];
209 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
211 if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
213 if(exception_str != NULL)
215 if(strstr(direntp->d_name,exception_str) != NULL)
219 if(strstr(direntp->d_name,glob) == NULL)
222 string_list_add_item(&sdirs,direntp->d_name);
225 closedir(dirStructP);
231 void free_strings(char **strings, int num)
234 for(i=0; i < num; ++i)
239 /* Set SuperTux configuration and save directories */
240 void st_directory_setup(void)
244 /* Get home directory (from $HOME variable)... if we can't determine it,
245 use the current directory ("."): */
246 if (getenv("HOME") != NULL)
247 home = getenv("HOME");
251 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
252 strlen("/.supertux") + 1));
253 strcpy(st_dir, home);
254 strcat(st_dir, "/.supertux");
256 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
258 strcpy(st_save_dir,st_dir);
259 strcat(st_save_dir,"/save");
261 /* Create them. In the case they exist they won't destroy anything. */
265 mkdir(st_save_dir, 0755);
267 sprintf(str, "%s/levels", st_dir);
274 sprintf(str, "%s/levels", st_dir);
281 /* Create and setup menus. */
284 menu_init(&main_menu);
285 menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
286 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
287 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
288 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
289 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
290 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
291 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
292 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
294 menu_init(&options_menu);
295 menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
296 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
297 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
298 if(audio_device == YES)
300 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound ",use_sound,0));
301 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music ",use_music,0));
305 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound ",use_sound,0));
306 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music ",use_music,0));
308 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS ",show_fps,0));
309 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
310 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
312 menu_init(&load_game_menu);
313 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
314 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
315 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
316 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
317 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
318 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
319 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
320 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
321 menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
323 menu_init(&save_game_menu);
324 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
325 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
326 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
327 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
328 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
329 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
330 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
331 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
332 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
334 menu_init(&game_menu);
335 menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
336 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
337 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
338 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
339 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
340 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
341 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
342 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
344 menu_init(&highscore_menu);
345 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
349 void update_load_save_game_menu(menu_type* pmenu, int load)
353 for(i = 2; i < 7; ++i)
357 if(load && strlen(tmp) == strlen("Slot X - Free") )
358 pmenu->item[i].kind = MN_DEACTIVE;
360 pmenu->item[i].kind = MN_ACTION;
361 menu_item_change_text(&pmenu->item[i],tmp);
366 void process_save_load_game_menu(int save)
369 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
380 if(game_started == NO)
381 gameloop("whatever",slot,ST_GL_LOAD_GAME);
385 st_pause_ticks_stop();
391 /* Handle changes made to global settings in the options menu. */
392 void process_options_menu(void)
394 switch (menu_check(&options_menu))
397 if(use_fullscreen != options_menu.item[2].toggled)
399 use_fullscreen = !use_fullscreen;
404 if(use_sound != options_menu.item[3].toggled)
405 use_sound = !use_sound;
408 if(use_music != options_menu.item[4].toggled)
421 if (!playing_music())
423 play_current_music();
429 if(show_fps != options_menu.item[5].toggled)
430 show_fps = !show_fps;
435 void st_general_setup(void)
437 /* Seed random number generator: */
439 srand(SDL_GetTicks());
441 /* Load global images: */
443 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
444 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
445 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
446 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
447 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
448 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
449 text_load(&white_big_text,DATA_PREFIX "/images/status/letters-white-big.png", TEXT_TEXT, 20,23);
450 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
452 /* Load GUI/menu images: */
453 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
454 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
455 texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
456 texture_load(&arrow_left, DATA_PREFIX "/images/icons/left.png", USE_ALPHA);
457 texture_load(&arrow_right, DATA_PREFIX "/images/icons/right.png", USE_ALPHA);
459 /* Set icon image: */
462 SDL_EnableUNICODE(1);
466 void st_general_free(void)
469 /* Free global images: */
471 text_free(&black_text);
472 text_free(&gold_text);
473 text_free(&white_text);
474 text_free(&blue_text);
475 text_free(&red_text);
476 text_free(&white_small_text);
477 text_free(&white_big_text);
479 /* Free GUI/menu images: */
480 texture_free(&checkbox);
481 texture_free(&checkbox_checked);
483 texture_free(&arrow_left);
484 texture_free(&arrow_right);
488 menu_free(&main_menu);
489 menu_free(&game_menu);
490 menu_free(&options_menu);
491 menu_free(&highscore_menu);
492 menu_free(&save_game_menu);
493 menu_free(&load_game_menu);
497 void st_video_setup(void)
501 SDL_FreeSurface(screen);
503 /* Init SDL Video: */
505 if (SDL_Init(SDL_INIT_VIDEO) < 0)
508 "\nError: I could not initialize video!\n"
509 "The Simple DirectMedia error that occured was:\n"
510 "%s\n\n", SDL_GetError());
519 st_video_setup_sdl();
523 /* Set window manager stuff: */
525 SDL_WM_SetCaption("Super Tux", "Super Tux");
529 void st_video_setup_sdl(void)
531 SDL_FreeSurface(screen);
533 if (use_fullscreen == YES)
535 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
539 "\nWarning: I could not set up fullscreen video for "
541 "The Simple DirectMedia error that occured was:\n"
542 "%s\n\n", SDL_GetError());
548 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
553 "\nError: I could not set up video for 640x480 mode.\n"
554 "The Simple DirectMedia error that occured was:\n"
555 "%s\n\n", SDL_GetError());
561 void st_video_setup_gl(void)
565 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
566 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
567 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
568 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
569 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
571 if (use_fullscreen == YES)
573 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
577 "\nWarning: I could not set up fullscreen video for "
579 "The Simple DirectMedia error that occured was:\n"
580 "%s\n\n", SDL_GetError());
586 screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
591 "\nError: I could not set up video for 640x480 mode.\n"
592 "The Simple DirectMedia error that occured was:\n"
593 "%s\n\n", SDL_GetError());
599 * Set up OpenGL for 2D rendering.
601 glDisable(GL_DEPTH_TEST);
602 glDisable(GL_CULL_FACE);
604 glViewport(0, 0, screen->w, screen->h);
605 glMatrixMode(GL_PROJECTION);
607 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
609 glMatrixMode(GL_MODELVIEW);
611 glTranslatef(0.0f, 0.0f, 0.0f);
617 void st_joystick_setup(void)
625 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
627 fprintf(stderr, "Warning: I could not initialize joystick!\n"
628 "The Simple DirectMedia error that occured was:\n"
629 "%s\n\n", SDL_GetError());
637 if (SDL_NumJoysticks() <= 0)
639 fprintf(stderr, "Warning: No joysticks are available.\n");
645 js = SDL_JoystickOpen(0);
649 fprintf(stderr, "Warning: Could not open joystick 1.\n"
650 "The Simple DirectMedia error that occured was:\n"
651 "%s\n\n", SDL_GetError());
657 /* Check for proper joystick configuration: */
659 if (SDL_JoystickNumAxes(js) < 2)
662 "Warning: Joystick does not have enough axes!\n");
668 if (SDL_JoystickNumButtons(js) < 2)
672 "Joystick does not have enough buttons!\n");
684 void st_audio_setup(void)
687 /* Init SDL Audio silently even if --disable-sound : */
689 if (audio_device == YES)
691 if (SDL_Init(SDL_INIT_AUDIO) < 0)
693 /* only print out message if sound or music
694 was not disabled at command-line
696 if (use_sound == YES || use_music == YES)
699 "\nWarning: I could not initialize audio!\n"
700 "The Simple DirectMedia error that occured was:\n"
701 "%s\n\n", SDL_GetError());
703 /* keep the programming logic the same :-)
704 because in this case, use_sound & use_music' values are ignored
705 when there's no available audio device
714 /* Open sound silently regarless the value of "use_sound": */
716 if (audio_device == YES)
718 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
720 /* only print out message if sound or music
721 was not disabled at command-line
723 if ((use_sound == YES) || (use_music == YES))
726 "\nWarning: I could not set up audio for 44100 Hz "
728 "The Simple DirectMedia error that occured was:\n"
729 "%s\n\n", SDL_GetError());
740 /* --- SHUTDOWN --- */
742 void st_shutdown(void)
751 void st_abort(char * reason, char * details)
753 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
759 /* Set Icon (private) */
768 /* Load icon into a surface: */
770 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
774 "\nError: I could not load the icon image: %s\n"
775 "The Simple DirectMedia error that occured was:\n"
776 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
783 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
784 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
785 memset(mask, 0xFF, masklen);
790 SDL_WM_SetIcon(icon, mask);
793 /* Free icon surface & mask: */
796 SDL_FreeSurface(icon);
800 /* Parse command-line arguments: */
802 void parseargs(int argc, char * argv[])
826 /* Parse arguments: */
828 for (i = 1; i < argc; i++)
830 if (strcmp(argv[i], "--fullscreen") == 0 ||
831 strcmp(argv[i], "-f") == 0)
833 /* Use full screen: */
835 use_fullscreen = YES;
837 else if (strcmp(argv[i], "--show-fps") == 0)
839 /* Use full screen: */
843 else if (strcmp(argv[i], "--opengl") == 0 ||
844 strcmp(argv[i], "-gl") == 0)
853 else if (strcmp(argv[i], "--usage") == 0)
859 else if (strcmp(argv[i], "--version") == 0)
863 printf("Super Tux - version " VERSION "\n");
866 else if (strcmp(argv[i], "--disable-sound") == 0)
868 /* Disable the compiled in sound feature */
870 printf("Sounds disabled \n");
874 printf("Warning: Sounds feature is not compiled in \n");
878 else if (strcmp(argv[i], "--disable-music") == 0)
880 /* Disable the compiled in sound feature */
882 printf("Music disabled \n");
886 printf("Warning: Music feature is not compiled in \n");
890 else if (strcmp(argv[i], "--debug-mode") == 0)
892 /* Enable the debug-mode */
896 else if (strcmp(argv[i], "--help") == 0)
899 printf("Super Tux " VERSION "\n\n");
901 printf("---------- Command-line options ----------\n\n");
903 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
905 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
907 printf(" --disable-music - Like above, but this will disable music.\n\n");
909 printf(" --fullscreen - Run in fullscreen mode.\n\n");
911 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
913 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
915 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
917 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
920 printf("---------- License ----------\n\n");
921 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
922 printf(" This is free software, and you are welcome to redistribute\n");
923 printf(" or modify it under certain conditions. See the file \n");
924 printf(" \"COPYING.txt\" for more details.\n\n\n");
926 printf("---------- Game controls ----------\n\n");
927 printf(" Please see the file \"README.txt\"\n\n");
933 /* Unknown - complain! */
943 void usage(char * prog, int ret)
948 /* Determine which stream to write to: */
956 /* Display the usage message: */
958 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",