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_ACTION,"Start Game",0,0));
225 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
226 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
227 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
228 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
230 menu_init(&options_menu);
231 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
232 if(audio_device == YES)
234 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound",use_sound,0));
235 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music",use_music,0));
239 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound",use_sound,0));
240 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music",use_music,0));
242 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS",show_fps,0));
243 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
245 menu_init(&leveleditor_menu);
246 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Return To Level Editor",0,0));
247 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"New Level",0,0));
248 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Load Level",0,0));
249 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Save Level",0,0));
250 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Quit Level Editor",0,0));
252 menu_init(&load_game_menu);
253 menu_additem(&load_game_menu,menu_item_create(MN_LABEL,"Load Game",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_BACK,"Back",0,0));
261 menu_init(&save_game_menu);
262 menu_additem(&save_game_menu,menu_item_create(MN_LABEL,"Save Game",0,0));
263 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 1",0,0));
264 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 2",0,0));
265 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 3",0,0));
266 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 4",0,0));
267 menu_additem(&save_game_menu,menu_item_create(MN_DEACTIVE,"Slot 5",0,0));
268 menu_additem(&save_game_menu,menu_item_create(MN_BACK,"Back",0,0));
270 menu_init(&game_menu);
271 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
272 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Save Game",0,&save_game_menu));
273 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
274 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
275 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
277 menu_init(&highscore_menu);
278 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
282 void update_load_save_game_menu(menu_type* pmenu, int load)
286 for(i = 1; i < 6; ++i)
290 if(load && strlen(tmp) == strlen("Slot X - Free") )
291 pmenu->item[i].kind = MN_DEACTIVE;
293 pmenu->item[i].kind = MN_ACTION;
294 menu_item_change_text(&pmenu->item[i],tmp);
299 void process_save_load_game_menu(int save)
302 switch (slot = menu_check(save ? &save_game_menu : &load_game_menu))
313 if(game_started == NO)
314 gameloop("whatever",slot,ST_GL_LOAD_GAME);
318 st_pause_ticks_stop();
324 /* Handle changes made to global settings in the options menu. */
325 void process_options_menu(void)
327 switch (menu_check(&options_menu))
330 if(use_fullscreen != options_menu.item[0].toggled)
332 use_fullscreen = !use_fullscreen;
337 if(use_sound != options_menu.item[1].toggled)
338 use_sound = !use_sound;
341 if(use_music != options_menu.item[2].toggled)
354 if (!playing_music())
356 play_current_music();
362 if(show_fps != options_menu.item[3].toggled)
363 show_fps = !show_fps;
368 void st_general_setup(void)
370 /* Seed random number generator: */
372 srand(SDL_GetTicks());
374 /* Load global images: */
376 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
377 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
378 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
379 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
380 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
381 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
382 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
384 /* Load GUI/menu images: */
385 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
386 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
387 texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
389 /* Set icon image: */
392 SDL_EnableUNICODE(1);
396 void st_general_free(void)
399 /* Free global images: */
401 text_free(&black_text);
402 text_free(&gold_text);
403 text_free(&white_text);
404 text_free(&blue_text);
405 text_free(&red_text);
406 text_free(&white_small_text);
408 /* Free GUI/menu images: */
409 texture_free(&checkbox);
410 texture_free(&checkbox_checked);
415 menu_free(&main_menu);
416 menu_free(&game_menu);
417 menu_free(&options_menu);
418 menu_free(&leveleditor_menu);
419 menu_free(&highscore_menu);
423 void st_video_setup(void)
427 SDL_FreeSurface(screen);
429 /* Init SDL Video: */
431 if (SDL_Init(SDL_INIT_VIDEO) < 0)
434 "\nError: I could not initialize video!\n"
435 "The Simple DirectMedia error that occured was:\n"
436 "%s\n\n", SDL_GetError());
445 st_video_setup_sdl();
449 /* Set window manager stuff: */
451 SDL_WM_SetCaption("Super Tux", "Super Tux");
455 void st_video_setup_sdl(void)
457 SDL_FreeSurface(screen);
459 if (use_fullscreen == YES)
461 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
465 "\nWarning: I could not set up fullscreen video for "
467 "The Simple DirectMedia error that occured was:\n"
468 "%s\n\n", SDL_GetError());
474 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
479 "\nError: I could not set up video for 640x480 mode.\n"
480 "The Simple DirectMedia error that occured was:\n"
481 "%s\n\n", SDL_GetError());
487 void st_video_setup_gl(void)
491 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
492 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
493 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
494 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
495 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
497 if (use_fullscreen == YES)
499 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL | SDL_OPENGLBLIT ) ; /* | SDL_HWSURFACE); */
503 "\nWarning: I could not set up fullscreen video for "
505 "The Simple DirectMedia error that occured was:\n"
506 "%s\n\n", SDL_GetError());
512 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
517 "\nError: I could not set up video for 640x480 mode.\n"
518 "The Simple DirectMedia error that occured was:\n"
519 "%s\n\n", SDL_GetError());
525 * Set up OpenGL for 2D rendering.
527 glDisable(GL_DEPTH_TEST);
528 glDisable(GL_CULL_FACE);
530 glViewport(0, 0, screen->w, screen->h);
531 glMatrixMode(GL_PROJECTION);
533 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
535 glMatrixMode(GL_MODELVIEW);
537 glTranslatef(0.0f, 0.0f, 0.0f);
543 void st_joystick_setup(void)
551 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
553 fprintf(stderr, "Warning: I could not initialize joystick!\n"
554 "The Simple DirectMedia error that occured was:\n"
555 "%s\n\n", SDL_GetError());
563 if (SDL_NumJoysticks() <= 0)
565 fprintf(stderr, "Warning: No joysticks are available.\n");
571 js = SDL_JoystickOpen(0);
575 fprintf(stderr, "Warning: Could not open joystick 1.\n"
576 "The Simple DirectMedia error that occured was:\n"
577 "%s\n\n", SDL_GetError());
583 /* Check for proper joystick configuration: */
585 if (SDL_JoystickNumAxes(js) < 2)
588 "Warning: Joystick does not have enough axes!\n");
594 if (SDL_JoystickNumButtons(js) < 2)
598 "Joystick does not have enough buttons!\n");
610 void st_audio_setup(void)
613 /* Init SDL Audio silently even if --disable-sound : */
615 if (audio_device == YES)
617 if (SDL_Init(SDL_INIT_AUDIO) < 0)
619 /* only print out message if sound or music
620 was not disabled at command-line
622 if (use_sound == YES || use_music == YES)
625 "\nWarning: I could not initialize audio!\n"
626 "The Simple DirectMedia error that occured was:\n"
627 "%s\n\n", SDL_GetError());
629 /* keep the programming logic the same :-)
630 because in this case, use_sound & use_music' values are ignored
631 when there's no available audio device
640 /* Open sound silently regarless the value of "use_sound": */
642 if (audio_device == YES)
644 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
646 /* only print out message if sound or music
647 was not disabled at command-line
649 if ((use_sound == YES) || (use_music == YES))
652 "\nWarning: I could not set up audio for 44100 Hz "
654 "The Simple DirectMedia error that occured was:\n"
655 "%s\n\n", SDL_GetError());
666 /* --- SHUTDOWN --- */
668 void st_shutdown(void)
677 void st_abort(char * reason, char * details)
679 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
685 /* Set Icon (private) */
694 /* Load icon into a surface: */
696 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
700 "\nError: I could not load the icon image: %s\n"
701 "The Simple DirectMedia error that occured was:\n"
702 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
709 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
710 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
711 memset(mask, 0xFF, masklen);
716 SDL_WM_SetIcon(icon, mask);
719 /* Free icon surface & mask: */
722 SDL_FreeSurface(icon);
726 /* Parse command-line arguments: */
728 void parseargs(int argc, char * argv[])
752 /* Parse arguments: */
754 for (i = 1; i < argc; i++)
756 if (strcmp(argv[i], "--fullscreen") == 0 ||
757 strcmp(argv[i], "-f") == 0)
759 /* Use full screen: */
761 use_fullscreen = YES;
763 else if (strcmp(argv[i], "--show-fps") == 0)
765 /* Use full screen: */
769 else if (strcmp(argv[i], "--opengl") == 0 ||
770 strcmp(argv[i], "-gl") == 0)
779 else if (strcmp(argv[i], "--usage") == 0)
785 else if (strcmp(argv[i], "--version") == 0)
789 printf("Super Tux - version " VERSION "\n");
792 else if (strcmp(argv[i], "--disable-sound") == 0)
794 /* Disable the compiled in sound feature */
796 printf("Sounds disabled \n");
800 printf("Warning: Sounds feature is not compiled in \n");
804 else if (strcmp(argv[i], "--disable-music") == 0)
806 /* Disable the compiled in sound feature */
808 printf("Music disabled \n");
812 printf("Warning: Music feature is not compiled in \n");
816 else if (strcmp(argv[i], "--debug-mode") == 0)
818 /* Enable the debug-mode */
822 else if (strcmp(argv[i], "--help") == 0)
825 printf("Super Tux " VERSION "\n\n");
827 printf("---------- Command-line options ----------\n\n");
829 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
831 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
833 printf(" --disable-music - Like above, but this will disable music.\n\n");
835 printf(" --fullscreen - Run in fullscreen mode.\n\n");
837 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
839 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
841 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
843 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
846 printf("---------- License ----------\n\n");
847 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
848 printf(" This is free software, and you are welcome to redistribute\n");
849 printf(" or modify it under certain conditions. See the file \n");
850 printf(" \"COPYING.txt\" for more details.\n\n\n");
852 printf("---------- Game controls ----------\n\n");
853 printf(" Please see the file \"README.txt\"\n\n");
859 /* Unknown - complain! */
869 void usage(char * prog, int ret)
874 /* Determine which stream to write to: */
882 /* Display the usage message: */
884 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",