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 char ** dsubdirs(char *rel_path, char* expected_file, int* num)
96 struct dirent *direntp;
102 sprintf(path,"%s/%s",st_dir,rel_path);
103 if((dirStructP = opendir(path)) != NULL)
105 while((direntp = readdir(dirStructP)) != NULL)
107 char absolute_filename[1024];
110 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
112 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
114 if(expected_file != NULL)
116 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
117 if(!faccessible(filename))
121 sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1));
122 sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 );
123 strcpy(sdirs[i],direntp->d_name);
127 closedir(dirStructP);
130 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
131 if((dirStructP = opendir(path)) != NULL)
133 while((direntp = readdir(dirStructP)) != NULL)
135 char absolute_filename[1024];
138 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
140 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
142 if(expected_file != NULL)
144 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
145 if(!faccessible(filename))
151 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
152 if(faccessible(filename))
157 sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1));
158 sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 );
159 strcpy(sdirs[i],direntp->d_name);
163 closedir(dirStructP);
170 void free_strings(char **strings, int num)
173 for(i=0; i < num; ++i)
178 /* Set SuperTux configuration and save directories */
179 void st_directory_setup(void)
183 /* Get home directory (from $HOME variable)... if we can't determine it,
184 use the current directory ("."): */
185 if (getenv("HOME") != NULL)
186 home = getenv("HOME");
190 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
191 strlen("/.supertux") + 1));
192 strcpy(st_dir, home);
193 strcat(st_dir, "/.supertux");
195 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
197 strcpy(st_save_dir,st_dir);
198 strcat(st_save_dir,"/save");
200 /* Create them. In the case they exist they won't destroy anything. */
204 mkdir(st_save_dir, 0755);
206 sprintf(str, "%s/levels", st_dir);
213 sprintf(str, "%s/levels", st_dir);
220 /* Create and setup menus. */
223 menu_init(&main_menu);
224 menu_additem(&main_menu,menu_item_create(MN_LABEL,"Main Menu",0,0));
225 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
226 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
227 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
228 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
229 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
230 menu_additem(&main_menu,menu_item_create(MN_HL,"",0,0));
231 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
233 menu_init(&options_menu);
234 menu_additem(&options_menu,menu_item_create(MN_LABEL,"Options",0,0));
235 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
236 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
237 if(audio_device == YES)
239 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound ",use_sound,0));
240 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music ",use_music,0));
244 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound ",use_sound,0));
245 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music ",use_music,0));
247 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS ",show_fps,0));
248 menu_additem(&options_menu,menu_item_create(MN_HL,"",0,0));
249 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
251 menu_init(&load_game_menu);
252 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",0,0));
253 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
254 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
255 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
256 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
257 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
258 menu_additem(&load_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
259 menu_additem(&load_game_menu,menu_item_create(MN_HL,"",0,0));
260 menu_additem(&load_game_menu,menu_item_create(MN_BACK,"Back",0,0));
262 menu_init(&save_game_menu);
263 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
264 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
265 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
266 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
267 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
268 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
269 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
270 menu_additem(&save_game_menu,menu_item_create(MN_HL,"",0,0));
271 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
273 menu_init(&game_menu);
274 menu_additem(&game_menu,menu_item_create(MN_LABEL,"InGame Menu",0,0));
275 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
276 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
277 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
278 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
279 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
280 menu_additem(&game_menu,menu_item_create(MN_HL,"",0,0));
281 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
283 menu_init(&highscore_menu);
284 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
288 void update_load_save_game_menu(menu_type* pmenu, int load)
292 for(i = 2; i < 7; ++i)
296 if(load && strlen(tmp) == strlen("Slot X - Free") )
297 pmenu->item[i].kind = MN_DEACTIVE;
299 pmenu->item[i].kind = MN_ACTION;
300 menu_item_change_text(&pmenu->item[i],tmp);
305 void process_save_load_game_menu(int save)
308 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
319 if(game_started == NO)
320 gameloop("whatever",slot,ST_GL_LOAD_GAME);
324 st_pause_ticks_stop();
330 /* Handle changes made to global settings in the options menu. */
331 void process_options_menu(void)
333 switch (menu_check(&options_menu))
336 if(use_fullscreen != options_menu.item[2].toggled)
338 use_fullscreen = !use_fullscreen;
343 if(use_sound != options_menu.item[3].toggled)
344 use_sound = !use_sound;
347 if(use_music != options_menu.item[4].toggled)
360 if (!playing_music())
362 play_current_music();
368 if(show_fps != options_menu.item[5].toggled)
369 show_fps = !show_fps;
374 void st_general_setup(void)
376 /* Seed random number generator: */
378 srand(SDL_GetTicks());
380 /* Load global images: */
382 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
383 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
384 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
385 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
386 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
387 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
388 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
390 /* Load GUI/menu images: */
391 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
392 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
393 texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
395 /* Set icon image: */
398 SDL_EnableUNICODE(1);
402 void st_general_free(void)
405 /* Free global images: */
407 text_free(&black_text);
408 text_free(&gold_text);
409 text_free(&white_text);
410 text_free(&blue_text);
411 text_free(&red_text);
412 text_free(&white_small_text);
414 /* Free GUI/menu images: */
415 texture_free(&checkbox);
416 texture_free(&checkbox_checked);
421 menu_free(&main_menu);
422 menu_free(&game_menu);
423 menu_free(&options_menu);
424 menu_free(&highscore_menu);
428 void st_video_setup(void)
432 SDL_FreeSurface(screen);
434 /* Init SDL Video: */
436 if (SDL_Init(SDL_INIT_VIDEO) < 0)
439 "\nError: I could not initialize video!\n"
440 "The Simple DirectMedia error that occured was:\n"
441 "%s\n\n", SDL_GetError());
450 st_video_setup_sdl();
454 /* Set window manager stuff: */
456 SDL_WM_SetCaption("Super Tux", "Super Tux");
460 void st_video_setup_sdl(void)
462 SDL_FreeSurface(screen);
464 if (use_fullscreen == YES)
466 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
470 "\nWarning: I could not set up fullscreen video for "
472 "The Simple DirectMedia error that occured was:\n"
473 "%s\n\n", SDL_GetError());
479 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
484 "\nError: I could not set up video for 640x480 mode.\n"
485 "The Simple DirectMedia error that occured was:\n"
486 "%s\n\n", SDL_GetError());
492 void st_video_setup_gl(void)
496 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
497 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
498 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
499 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
500 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
502 if (use_fullscreen == YES)
504 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
508 "\nWarning: I could not set up fullscreen video for "
510 "The Simple DirectMedia error that occured was:\n"
511 "%s\n\n", SDL_GetError());
517 screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
522 "\nError: I could not set up video for 640x480 mode.\n"
523 "The Simple DirectMedia error that occured was:\n"
524 "%s\n\n", SDL_GetError());
530 * Set up OpenGL for 2D rendering.
532 glDisable(GL_DEPTH_TEST);
533 glDisable(GL_CULL_FACE);
535 glViewport(0, 0, screen->w, screen->h);
536 glMatrixMode(GL_PROJECTION);
538 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
540 glMatrixMode(GL_MODELVIEW);
542 glTranslatef(0.0f, 0.0f, 0.0f);
548 void st_joystick_setup(void)
556 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
558 fprintf(stderr, "Warning: I could not initialize joystick!\n"
559 "The Simple DirectMedia error that occured was:\n"
560 "%s\n\n", SDL_GetError());
568 if (SDL_NumJoysticks() <= 0)
570 fprintf(stderr, "Warning: No joysticks are available.\n");
576 js = SDL_JoystickOpen(0);
580 fprintf(stderr, "Warning: Could not open joystick 1.\n"
581 "The Simple DirectMedia error that occured was:\n"
582 "%s\n\n", SDL_GetError());
588 /* Check for proper joystick configuration: */
590 if (SDL_JoystickNumAxes(js) < 2)
593 "Warning: Joystick does not have enough axes!\n");
599 if (SDL_JoystickNumButtons(js) < 2)
603 "Joystick does not have enough buttons!\n");
615 void st_audio_setup(void)
618 /* Init SDL Audio silently even if --disable-sound : */
620 if (audio_device == YES)
622 if (SDL_Init(SDL_INIT_AUDIO) < 0)
624 /* only print out message if sound or music
625 was not disabled at command-line
627 if (use_sound == YES || use_music == YES)
630 "\nWarning: I could not initialize audio!\n"
631 "The Simple DirectMedia error that occured was:\n"
632 "%s\n\n", SDL_GetError());
634 /* keep the programming logic the same :-)
635 because in this case, use_sound & use_music' values are ignored
636 when there's no available audio device
645 /* Open sound silently regarless the value of "use_sound": */
647 if (audio_device == YES)
649 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
651 /* only print out message if sound or music
652 was not disabled at command-line
654 if ((use_sound == YES) || (use_music == YES))
657 "\nWarning: I could not set up audio for 44100 Hz "
659 "The Simple DirectMedia error that occured was:\n"
660 "%s\n\n", SDL_GetError());
671 /* --- SHUTDOWN --- */
673 void st_shutdown(void)
682 void st_abort(char * reason, char * details)
684 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
690 /* Set Icon (private) */
699 /* Load icon into a surface: */
701 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
705 "\nError: I could not load the icon image: %s\n"
706 "The Simple DirectMedia error that occured was:\n"
707 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
714 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
715 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
716 memset(mask, 0xFF, masklen);
721 SDL_WM_SetIcon(icon, mask);
724 /* Free icon surface & mask: */
727 SDL_FreeSurface(icon);
731 /* Parse command-line arguments: */
733 void parseargs(int argc, char * argv[])
757 /* Parse arguments: */
759 for (i = 1; i < argc; i++)
761 if (strcmp(argv[i], "--fullscreen") == 0 ||
762 strcmp(argv[i], "-f") == 0)
764 /* Use full screen: */
766 use_fullscreen = YES;
768 else if (strcmp(argv[i], "--show-fps") == 0)
770 /* Use full screen: */
774 else if (strcmp(argv[i], "--opengl") == 0 ||
775 strcmp(argv[i], "-gl") == 0)
784 else if (strcmp(argv[i], "--usage") == 0)
790 else if (strcmp(argv[i], "--version") == 0)
794 printf("Super Tux - version " VERSION "\n");
797 else if (strcmp(argv[i], "--disable-sound") == 0)
799 /* Disable the compiled in sound feature */
801 printf("Sounds disabled \n");
805 printf("Warning: Sounds feature is not compiled in \n");
809 else if (strcmp(argv[i], "--disable-music") == 0)
811 /* Disable the compiled in sound feature */
813 printf("Music disabled \n");
817 printf("Warning: Music feature is not compiled in \n");
821 else if (strcmp(argv[i], "--debug-mode") == 0)
823 /* Enable the debug-mode */
827 else if (strcmp(argv[i], "--help") == 0)
830 printf("Super Tux " VERSION "\n\n");
832 printf("---------- Command-line options ----------\n\n");
834 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
836 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
838 printf(" --disable-music - Like above, but this will disable music.\n\n");
840 printf(" --fullscreen - Run in fullscreen mode.\n\n");
842 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
844 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
846 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
848 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
851 printf("---------- License ----------\n\n");
852 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
853 printf(" This is free software, and you are welcome to redistribute\n");
854 printf(" or modify it under certain conditions. See the file \n");
855 printf(" \"COPYING.txt\" for more details.\n\n\n");
857 printf("---------- Game controls ----------\n\n");
858 printf(" Please see the file \"README.txt\"\n\n");
864 /* Unknown - complain! */
874 void usage(char * prog, int ret)
879 /* Determine which stream to write to: */
887 /* Display the usage message: */
889 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",