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 if ( direntp->d_type == DT_DIR )
108 if(expected_file != NULL)
110 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
111 if(!faccessible(filename))
115 sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1));
116 sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 );
117 strcpy(sdirs[i],direntp->d_name);
121 closedir(dirStructP);
124 sprintf(path,"%s/%s",DATA_PREFIX,rel_path);
125 if((dirStructP = opendir(path)) != NULL)
127 while((direntp = readdir(dirStructP)) != NULL)
129 if ( direntp->d_type == DT_DIR )
131 if(expected_file != NULL)
133 sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
134 if(!faccessible(filename))
140 sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
141 if(faccessible(filename))
146 sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1));
147 sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 );
148 strcpy(sdirs[i],direntp->d_name);
152 closedir(dirStructP);
159 void free_strings(char **strings, int num)
162 for(i=0; i < num; ++i)
167 /* Set SuperTux configuration and save directories */
168 void st_directory_setup(void)
172 /* Get home directory (from $HOME variable)... if we can't determine it,
173 use the current directory ("."): */
174 if (getenv("HOME") != NULL)
175 home = getenv("HOME");
179 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
180 strlen("/.supertux") + 1));
181 strcpy(st_dir, home);
182 strcat(st_dir, "/.supertux");
184 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
186 strcpy(st_save_dir,st_dir);
187 strcat(st_save_dir,"/save");
189 /* Create them. In the case they exist they won't destroy anything. */
193 mkdir(st_save_dir, 0755);
195 sprintf(str, "%s/levels", st_dir);
202 sprintf(str, "%s/levels", st_dir);
209 /* Create and setup menus. */
213 menu_init(&main_menu);
214 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Start Game",0,0));
215 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Load Game",0,0));
216 menu_additem(&main_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
217 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Level editor",0,0));
218 menu_additem(&main_menu,menu_item_create(MN_ACTION,"Quit",0,0));
220 menu_init(&options_menu);
221 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Fullscreen",use_fullscreen,0));
222 if(audio_device == YES)
224 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Sound",use_sound,0));
225 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Music",use_music,0));
229 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Sound",use_sound,0));
230 menu_additem(&options_menu,menu_item_create(MN_DEACTIVE,"Music",use_music,0));
232 menu_additem(&options_menu,menu_item_create(MN_TOGGLE,"Show FPS",show_fps,0));
233 menu_additem(&options_menu,menu_item_create(MN_BACK,"Back",0,0));
235 menu_init(&leveleditor_menu);
236 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Return To Level Editor",0,0));
237 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"New Level",0,0));
238 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Load Level",0,0));
239 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Save Level",0,0));
240 menu_additem(&leveleditor_menu,menu_item_create(MN_ACTION,"Quit Level Editor",0,0));
242 menu_init(&game_menu);
243 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Return To Game",0,0));
244 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Save Game",0,0));
245 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Load Game",0,0));
246 menu_additem(&game_menu,menu_item_create(MN_GOTO,"Options",0,&options_menu));
247 menu_additem(&game_menu,menu_item_create(MN_ACTION,"Quit Game",0,0));
249 menu_init(&highscore_menu);
250 menu_additem(&highscore_menu,menu_item_create(MN_TEXTFIELD,"Enter your name:",0,0));
254 /* Handle changes made to global settings in the options menu. */
255 void process_options_menu(void)
257 switch (menu_check(&options_menu))
260 if(use_fullscreen != options_menu.item[0].toggled)
262 use_fullscreen = !use_fullscreen;
267 if(use_sound != options_menu.item[1].toggled)
268 use_sound = !use_sound;
271 if(use_music != options_menu.item[2].toggled)
284 if (!playing_music())
286 play_current_music();
292 if(show_fps != options_menu.item[3].toggled)
293 show_fps = !show_fps;
298 void st_general_setup(void)
300 /* Seed random number generator: */
302 srand(SDL_GetTicks());
304 /* Load global images: */
306 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png", TEXT_TEXT, 16,18);
307 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
308 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
309 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png", TEXT_TEXT, 16,18);
310 text_load(&white_text,DATA_PREFIX "/images/status/letters-white.png", TEXT_TEXT, 16,18);
311 text_load(&white_small_text,DATA_PREFIX "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
312 text_load(&yellow_nums,DATA_PREFIX "/images/status/numbers.png", TEXT_NUM, 32,32);
314 /* Load GUI/menu images: */
315 texture_load(&checkbox, DATA_PREFIX "/images/status/checkbox.png", USE_ALPHA);
316 texture_load(&checkbox_checked, DATA_PREFIX "/images/status/checkbox-checked.png", USE_ALPHA);
318 /* Set icon image: */
321 SDL_EnableUNICODE(1);
325 void st_general_free(void)
328 /* Free global images: */
330 text_free(&black_text);
331 text_free(&gold_text);
332 text_free(&white_text);
333 text_free(&blue_text);
334 text_free(&red_text);
335 text_free(&white_small_text);
337 /* Free GUI/menu images: */
338 texture_free(&checkbox);
339 texture_free(&checkbox_checked);
343 menu_free(&main_menu);
344 menu_free(&game_menu);
345 menu_free(&options_menu);
346 menu_free(&leveleditor_menu);
347 menu_free(&highscore_menu);
351 void st_video_setup(void)
355 SDL_FreeSurface(screen);
357 /* Init SDL Video: */
359 if (SDL_Init(SDL_INIT_VIDEO) < 0)
362 "\nError: I could not initialize video!\n"
363 "The Simple DirectMedia error that occured was:\n"
364 "%s\n\n", SDL_GetError());
373 st_video_setup_sdl();
377 /* Set window manager stuff: */
379 SDL_WM_SetCaption("Super Tux", "Super Tux");
383 void st_video_setup_sdl(void)
385 SDL_FreeSurface(screen);
387 if (use_fullscreen == YES)
389 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
393 "\nWarning: I could not set up fullscreen video for "
395 "The Simple DirectMedia error that occured was:\n"
396 "%s\n\n", SDL_GetError());
402 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
407 "\nError: I could not set up video for 640x480 mode.\n"
408 "The Simple DirectMedia error that occured was:\n"
409 "%s\n\n", SDL_GetError());
415 void st_video_setup_gl(void)
419 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
420 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
421 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
422 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
423 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
425 if (use_fullscreen == YES)
427 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL | SDL_OPENGLBLIT ) ; /* | SDL_HWSURFACE); */
431 "\nWarning: I could not set up fullscreen video for "
433 "The Simple DirectMedia error that occured was:\n"
434 "%s\n\n", SDL_GetError());
440 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
445 "\nError: I could not set up video for 640x480 mode.\n"
446 "The Simple DirectMedia error that occured was:\n"
447 "%s\n\n", SDL_GetError());
453 * Set up OpenGL for 2D rendering.
455 glDisable(GL_DEPTH_TEST);
456 glDisable(GL_CULL_FACE);
458 glViewport(0, 0, screen->w, screen->h);
459 glMatrixMode(GL_PROJECTION);
461 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
463 glMatrixMode(GL_MODELVIEW);
465 glTranslatef(0.0f, 0.0f, 0.0f);
471 void st_joystick_setup(void)
479 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
481 fprintf(stderr, "Warning: I could not initialize joystick!\n"
482 "The Simple DirectMedia error that occured was:\n"
483 "%s\n\n", SDL_GetError());
491 if (SDL_NumJoysticks() <= 0)
493 fprintf(stderr, "Warning: No joysticks are available.\n");
499 js = SDL_JoystickOpen(0);
503 fprintf(stderr, "Warning: Could not open joystick 1.\n"
504 "The Simple DirectMedia error that occured was:\n"
505 "%s\n\n", SDL_GetError());
511 /* Check for proper joystick configuration: */
513 if (SDL_JoystickNumAxes(js) < 2)
516 "Warning: Joystick does not have enough axes!\n");
522 if (SDL_JoystickNumButtons(js) < 2)
526 "Joystick does not have enough buttons!\n");
538 void st_audio_setup(void)
541 /* Init SDL Audio silently even if --disable-sound : */
543 if (audio_device == YES)
545 if (SDL_Init(SDL_INIT_AUDIO) < 0)
547 /* only print out message if sound or music
548 was not disabled at command-line
550 if (use_sound == YES || use_music == YES)
553 "\nWarning: I could not initialize audio!\n"
554 "The Simple DirectMedia error that occured was:\n"
555 "%s\n\n", SDL_GetError());
557 /* keep the programming logic the same :-)
558 because in this case, use_sound & use_music' values are ignored
559 when there's no available audio device
568 /* Open sound silently regarless the value of "use_sound": */
570 if (audio_device == YES)
572 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
574 /* only print out message if sound or music
575 was not disabled at command-line
577 if ((use_sound == YES) || (use_music == YES))
580 "\nWarning: I could not set up audio for 44100 Hz "
582 "The Simple DirectMedia error that occured was:\n"
583 "%s\n\n", SDL_GetError());
594 /* --- SHUTDOWN --- */
596 void st_shutdown(void)
605 void st_abort(char * reason, char * details)
607 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
613 /* Set Icon (private) */
622 /* Load icon into a surface: */
624 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
628 "\nError: I could not load the icon image: %s\n"
629 "The Simple DirectMedia error that occured was:\n"
630 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
637 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
638 mask = (Uint8*) malloc(masklen * sizeof(Uint8));
639 memset(mask, 0xFF, masklen);
644 SDL_WM_SetIcon(icon, mask);
647 /* Free icon surface & mask: */
650 SDL_FreeSurface(icon);
654 /* Parse command-line arguments: */
656 void parseargs(int argc, char * argv[])
680 /* Parse arguments: */
682 for (i = 1; i < argc; i++)
684 if (strcmp(argv[i], "--fullscreen") == 0 ||
685 strcmp(argv[i], "-f") == 0)
687 /* Use full screen: */
689 use_fullscreen = YES;
691 else if (strcmp(argv[i], "--show-fps") == 0)
693 /* Use full screen: */
697 else if (strcmp(argv[i], "--opengl") == 0 ||
698 strcmp(argv[i], "-gl") == 0)
707 else if (strcmp(argv[i], "--usage") == 0)
713 else if (strcmp(argv[i], "--version") == 0)
717 printf("Super Tux - version " VERSION "\n");
720 else if (strcmp(argv[i], "--disable-sound") == 0)
722 /* Disable the compiled in sound feature */
724 printf("Sounds disabled \n");
728 printf("Warning: Sounds feature is not compiled in \n");
732 else if (strcmp(argv[i], "--disable-music") == 0)
734 /* Disable the compiled in sound feature */
736 printf("Music disabled \n");
740 printf("Warning: Music feature is not compiled in \n");
744 else if (strcmp(argv[i], "--debug-mode") == 0)
746 /* Enable the debug-mode */
750 else if (strcmp(argv[i], "--help") == 0)
753 printf("Super Tux " VERSION "\n\n");
755 printf("---------- Command-line options ----------\n\n");
757 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
759 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
761 printf(" --disable-music - Like above, but this will disable music.\n\n");
763 printf(" --fullscreen - Run in fullscreen mode.\n\n");
765 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
767 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
769 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
771 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
774 printf("---------- License ----------\n\n");
775 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
776 printf(" This is free software, and you are welcome to redistribute\n");
777 printf(" or modify it under certain conditions. See the file \n");
778 printf(" \"COPYING.txt\" for more details.\n\n\n");
780 printf("---------- Game controls ----------\n\n");
781 printf(" Please see the file \"README.txt\"\n\n");
787 /* Unknown - complain! */
797 void usage(char * prog, int ret)
802 /* Determine which stream to write to: */
810 /* Display the usage message: */
812 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",