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>
39 /* Local function prototypes: */
42 void usage(char * prog, int ret);
44 /* Does the given file exist and is it accessible? */
45 int faccessible(char *filename)
48 if (stat(filename, &filestat) == -1)
54 /* Can we write to this location? */
55 int fwriteable(char *filename)
58 fi = fopen(filename, "wa");
66 /* Makes sure a directory is created in either the SuperTux base directory or the SuperTux base directory.*/
67 int fcreatedir(char* relative_dir)
70 snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
71 if(mkdir(path,0755) != 0)
73 snprintf(path, 1024, "%s/%s/", DATA_PREFIX, relative_dir);
74 if(mkdir(path,0755) != 0)
89 /* Get all names of sub-directories in a certain directory. */
90 /* Returns the number of sub-directories found. */
91 /* Note: The user has to free the allocated space. */
92 char ** dsubdirs(char *rel_path, char* expected_file, int* num)
95 struct dirent *direntp;
101 sprintf(path,"%s/%s",st_dir,rel_path);
102 if((dirStructP = opendir(path)) != NULL)
104 while((direntp = readdir(dirStructP)) != NULL)
106 char absolute_filename[1024];
109 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
111 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
113 if(expected_file != NULL)
115 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
116 if(!faccessible(filename))
120 sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1));
121 sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 );
122 strcpy(sdirs[i],direntp->d_name);
126 closedir(dirStructP);
129 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
130 if((dirStructP = opendir(path)) != NULL)
132 while((direntp = readdir(dirStructP)) != NULL)
134 char absolute_filename[1024];
137 sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
139 if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
141 if(expected_file != NULL)
143 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
144 if(!faccessible(filename))
150 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
151 if(faccessible(filename))
156 sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1));
157 sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 );
158 strcpy(sdirs[i],direntp->d_name);
162 closedir(dirStructP);
169 void free_strings(char **strings, int num)
172 for(i=0; i < num; ++i)
177 /* Set SuperTux configuration and save directories */
178 void st_directory_setup(void)
182 /* Get home directory (from $HOME variable)... if we can't determine it,
183 use the current directory ("."): */
184 if (getenv("HOME") != NULL)
185 home = getenv("HOME");
189 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
190 strlen("/.supertux") + 1));
191 strcpy(st_dir, home);
192 strcat(st_dir, "/.supertux");
194 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
196 strcpy(st_save_dir,st_dir);
197 strcat(st_save_dir,"/save");
199 /* Create them. In the case they exist they won't destroy anything. */
203 mkdir(st_save_dir, 0755);
205 sprintf(str, "%s/levels", st_dir);
212 sprintf(str, "%s/levels", st_dir);
219 /* 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));
255 menu_init(&game_menu);
256 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
257 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Save Game",0,0));
258 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Load Game",0,&load_game_menu));
259 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
260 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
262 menu_init(&highscore_menu);
263 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
267 /* Handle changes made to global settings in the options menu. */
268 void process_options_menu(void)
270 switch (menu_check(&options_menu))
273 if(use_fullscreen != options_menu.item[0].toggled)
275 use_fullscreen = !use_fullscreen;
280 if(use_sound != options_menu.item[1].toggled)
281 use_sound = !use_sound;
284 if(use_music != options_menu.item[2].toggled)
297 if (!playing_music())
299 play_current_music();
305 if(show_fps != options_menu.item[3].toggled)
306 show_fps = !show_fps;
311 void st_general_setup(void)
313 /* Seed random number generator: */
315 srand(SDL_GetTicks());
317 /* Load global images: */
319 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
320 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
321 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
322 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
323 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
324 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
325 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
327 /* Load GUI/menu images: */
328 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
329 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
330 texture_load(&back, DATA_PREFIX "/images/status/back.png", USE_ALPHA);
332 /* Set icon image: */
335 SDL_EnableUNICODE(1);
339 void st_general_free(void)
342 /* Free global images: */
344 text_free(&black_text);
345 text_free(&gold_text);
346 text_free(&white_text);
347 text_free(&blue_text);
348 text_free(&red_text);
349 text_free(&white_small_text);
351 /* Free GUI/menu images: */
352 texture_free(&checkbox);
353 texture_free(&checkbox_checked);
358 menu_free(&main_menu);
359 menu_free(&game_menu);
360 menu_free(&options_menu);
361 menu_free(&leveleditor_menu);
362 menu_free(&highscore_menu);
366 void st_video_setup(void)
370 SDL_FreeSurface(screen);
372 /* Init SDL Video: */
374 if (SDL_Init(SDL_INIT_VIDEO) < 0)
377 "\nError: I could not initialize video!\n"
378 "The Simple DirectMedia error that occured was:\n"
379 "%s\n\n", SDL_GetError());
388 st_video_setup_sdl();
392 /* Set window manager stuff: */
394 SDL_WM_SetCaption("Super Tux", "Super Tux");
398 void st_video_setup_sdl(void)
400 SDL_FreeSurface(screen);
402 if (use_fullscreen == YES)
404 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
408 "\nWarning: I could not set up fullscreen video for "
410 "The Simple DirectMedia error that occured was:\n"
411 "%s\n\n", SDL_GetError());
417 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
422 "\nError: I could not set up video for 640x480 mode.\n"
423 "The Simple DirectMedia error that occured was:\n"
424 "%s\n\n", SDL_GetError());
430 void st_video_setup_gl(void)
434 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
435 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
436 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
437 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
438 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
440 if (use_fullscreen == YES)
442 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL | SDL_OPENGLBLIT ) ; /* | SDL_HWSURFACE); */
446 "\nWarning: I could not set up fullscreen video for "
448 "The Simple DirectMedia error that occured was:\n"
449 "%s\n\n", SDL_GetError());
455 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
460 "\nError: I could not set up video for 640x480 mode.\n"
461 "The Simple DirectMedia error that occured was:\n"
462 "%s\n\n", SDL_GetError());
468 * Set up OpenGL for 2D rendering.
470 glDisable(GL_DEPTH_TEST);
471 glDisable(GL_CULL_FACE);
473 glViewport(0, 0, screen->w, screen->h);
474 glMatrixMode(GL_PROJECTION);
476 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
478 glMatrixMode(GL_MODELVIEW);
480 glTranslatef(0.0f, 0.0f, 0.0f);
486 void st_joystick_setup(void)
494 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
496 fprintf(stderr, "Warning: I could not initialize joystick!\n"
497 "The Simple DirectMedia error that occured was:\n"
498 "%s\n\n", SDL_GetError());
506 if (SDL_NumJoysticks() <= 0)
508 fprintf(stderr, "Warning: No joysticks are available.\n");
514 js = SDL_JoystickOpen(0);
518 fprintf(stderr, "Warning: Could not open joystick 1.\n"
519 "The Simple DirectMedia error that occured was:\n"
520 "%s\n\n", SDL_GetError());
526 /* Check for proper joystick configuration: */
528 if (SDL_JoystickNumAxes(js) < 2)
531 "Warning: Joystick does not have enough axes!\n");
537 if (SDL_JoystickNumButtons(js) < 2)
541 "Joystick does not have enough buttons!\n");
553 void st_audio_setup(void)
556 /* Init SDL Audio silently even if --disable-sound : */
558 if (audio_device == YES)
560 if (SDL_Init(SDL_INIT_AUDIO) < 0)
562 /* only print out message if sound or music
563 was not disabled at command-line
565 if (use_sound == YES || use_music == YES)
568 "\nWarning: I could not initialize audio!\n"
569 "The Simple DirectMedia error that occured was:\n"
570 "%s\n\n", SDL_GetError());
572 /* keep the programming logic the same :-)
573 because in this case, use_sound & use_music' values are ignored
574 when there's no available audio device
583 /* Open sound silently regarless the value of "use_sound": */
585 if (audio_device == YES)
587 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
589 /* only print out message if sound or music
590 was not disabled at command-line
592 if ((use_sound == YES) || (use_music == YES))
595 "\nWarning: I could not set up audio for 44100 Hz "
597 "The Simple DirectMedia error that occured was:\n"
598 "%s\n\n", SDL_GetError());
609 /* --- SHUTDOWN --- */
611 void st_shutdown(void)
620 void st_abort(char * reason, char * details)
622 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
628 /* Set Icon (private) */
637 /* Load icon into a surface: */
639 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
643 "\nError: I could not load the icon image: %s\n"
644 "The Simple DirectMedia error that occured was:\n"
645 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
652 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
653 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
654 memset(mask, 0xFF, masklen);
659 SDL_WM_SetIcon(icon, mask);
662 /* Free icon surface & mask: */
665 SDL_FreeSurface(icon);
669 /* Parse command-line arguments: */
671 void parseargs(int argc, char * argv[])
695 /* Parse arguments: */
697 for (i = 1; i < argc; i++)
699 if (strcmp(argv[i], "--fullscreen") == 0 ||
700 strcmp(argv[i], "-f") == 0)
702 /* Use full screen: */
704 use_fullscreen = YES;
706 else if (strcmp(argv[i], "--show-fps") == 0)
708 /* Use full screen: */
712 else if (strcmp(argv[i], "--opengl") == 0 ||
713 strcmp(argv[i], "-gl") == 0)
722 else if (strcmp(argv[i], "--usage") == 0)
728 else if (strcmp(argv[i], "--version") == 0)
732 printf("Super Tux - version " VERSION "\n");
735 else if (strcmp(argv[i], "--disable-sound") == 0)
737 /* Disable the compiled in sound feature */
739 printf("Sounds disabled \n");
743 printf("Warning: Sounds feature is not compiled in \n");
747 else if (strcmp(argv[i], "--disable-music") == 0)
749 /* Disable the compiled in sound feature */
751 printf("Music disabled \n");
755 printf("Warning: Music feature is not compiled in \n");
759 else if (strcmp(argv[i], "--debug-mode") == 0)
761 /* Enable the debug-mode */
765 else if (strcmp(argv[i], "--help") == 0)
768 printf("Super Tux " VERSION "\n\n");
770 printf("---------- Command-line options ----------\n\n");
772 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
774 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
776 printf(" --disable-music - Like above, but this will disable music.\n\n");
778 printf(" --fullscreen - Run in fullscreen mode.\n\n");
780 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
782 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
784 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
786 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
789 printf("---------- License ----------\n\n");
790 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
791 printf(" This is free software, and you are welcome to redistribute\n");
792 printf(" or modify it under certain conditions. See the file \n");
793 printf(" \"COPYING.txt\" for more details.\n\n\n");
795 printf("---------- Game controls ----------\n\n");
796 printf(" Please see the file \"README.txt\"\n\n");
802 /* Unknown - complain! */
812 void usage(char * prog, int ret)
817 /* Determine which stream to write to: */
825 /* Display the usage message: */
827 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",