7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - January 1st, 2004
19 #include <SDL_image.h>
21 #include <SDL_opengl.h>
26 #include <sys/types.h>
37 /* Local function prototypes: */
40 void usage(char * prog, int ret);
42 /* Does the given file exist and is it accessible? */
43 int faccessible(char *filename)
46 if (stat(filename, &filestat) == -1)
55 void st_directory_setup(void)
58 /* Set SuperTux configuration and save directories */
60 /* Get home directory (from $HOME variable)... if we can't determine it,
61 use the current directory ("."): */
63 if (getenv("HOME") != NULL)
64 home = getenv("HOME");
68 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
69 strlen("/.supertux") + 1));
71 strcat(st_dir, "/.supertux");
73 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
75 strcpy(st_save_dir,st_dir);
76 strcat(st_save_dir,"/save");
78 /* Create them. In the case they exist it won't destroy anything. */
82 mkdir(st_save_dir, 0755);
92 void st_general_setup(void)
94 /* Seed random number generator: */
96 srand(SDL_GetTicks());
98 /* Load global images: */
100 letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
103 letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
108 letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
111 letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
115 /* Set icon image: */
121 void st_video_setup(void)
125 SDL_FreeSurface(screen);
127 /* Init SDL Video: */
129 if (SDL_Init(SDL_INIT_VIDEO) < 0)
132 "\nError: I could not initialize video!\n"
133 "The Simple DirectMedia error that occured was:\n"
134 "%s\n\n", SDL_GetError());
143 st_video_setup_sdl();
151 /* Set window manager stuff: */
153 SDL_WM_SetCaption("Super Tux", "Super Tux");
157 void st_video_setup_sdl(void)
159 if (use_fullscreen == YES)
161 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
165 "\nWarning: I could not set up fullscreen video for "
167 "The Simple DirectMedia error that occured was:\n"
168 "%s\n\n", SDL_GetError());
174 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
179 "\nError: I could not set up video for 640x480 mode.\n"
180 "The Simple DirectMedia error that occured was:\n"
181 "%s\n\n", SDL_GetError());
187 void st_video_setup_gl(void)
191 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
192 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
193 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
194 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
195 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
197 if (use_fullscreen == YES)
199 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL ) ; /* | SDL_HWSURFACE); */
203 "\nWarning: I could not set up fullscreen video for "
205 "The Simple DirectMedia error that occured was:\n"
206 "%s\n\n", SDL_GetError());
212 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
217 "\nError: I could not set up video for 640x480 mode.\n"
218 "The Simple DirectMedia error that occured was:\n"
219 "%s\n\n", SDL_GetError());
224 /* Initialisierung von OpenGL * /
225 glViewport(0, 0, screen->w, screen->h);
227 glMatrixMode(GL_PROJECTION);
229 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
231 glMatrixMode(GL_MODELVIEW);
235 * Set up OpenGL for 2D rendering.
237 glDisable(GL_DEPTH_TEST);
238 glDisable(GL_CULL_FACE);
240 glViewport(0, 0, screen->w, screen->h);
241 glMatrixMode(GL_PROJECTION);
243 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
245 glMatrixMode(GL_MODELVIEW);
247 glTranslatef(0.0f, 0.0f, 0.0f);
252 void st_joystick_setup(void)
260 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
262 fprintf(stderr, "Warning: I could not initialize joystick!\n"
263 "The Simple DirectMedia error that occured was:\n"
264 "%s\n\n", SDL_GetError());
272 if (SDL_NumJoysticks() <= 0)
274 fprintf(stderr, "Warning: No joysticks are available.\n");
280 js = SDL_JoystickOpen(0);
284 fprintf(stderr, "Warning: Could not open joystick 1.\n"
285 "The Simple DirectMedia error that occured was:\n"
286 "%s\n\n", SDL_GetError());
292 /* Check for proper joystick configuration: */
294 if (SDL_JoystickNumAxes(js) < 2)
297 "Warning: Joystick does not have enough axes!\n");
303 if (SDL_JoystickNumButtons(js) < 2)
307 "Joystick does not have enough buttons!\n");
319 void st_audio_setup(void)
322 /* Init SDL Audio silently even if --disable-sound : */
324 if (audio_device == YES)
326 if (SDL_Init(SDL_INIT_AUDIO) < 0)
328 /* only print out message if sound or music
329 was not disabled at command-line
331 if (use_sound == YES || use_music == YES)
334 "\nWarning: I could not initialize audio!\n"
335 "The Simple DirectMedia error that occured was:\n"
336 "%s\n\n", SDL_GetError());
338 /* keep the programming logic the same :-)
339 because in this case, use_sound & use_music' values are ignored
340 when there's no available audio device
349 /* Open sound silently regarless the value of "use_sound": */
351 if (audio_device == YES)
353 if (open_audio(44100, AUDIO_S16, 2, 512) < 0)
355 /* only print out message if sound or music
356 was not disabled at command-line
358 if ((use_sound == YES) || (use_music == YES))
361 "\nWarning: I could not set up audio for 44100 Hz "
363 "The Simple DirectMedia error that occured was:\n"
364 "%s\n\n", SDL_GetError());
375 /* --- SHUTDOWN --- */
377 void st_shutdown(void)
386 void st_abort(char * reason, char * details)
388 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
394 /* Set Icon (private) */
403 /* Load icon into a surface: */
405 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
409 "\nError: I could not load the icon image: %s\n"
410 "The Simple DirectMedia error that occured was:\n"
411 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
418 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
419 mask = malloc(masklen * sizeof(Uint8));
420 memset(mask, 0xFF, masklen);
425 SDL_WM_SetIcon(icon, mask);
428 /* Free icon surface & mask: */
431 SDL_FreeSurface(icon);
435 /* Parse command-line arguments: */
437 void parseargs(int argc, char * argv[])
461 /* Parse arguments: */
463 for (i = 1; i < argc; i++)
465 if (strcmp(argv[i], "--fullscreen") == 0 ||
466 strcmp(argv[i], "-f") == 0)
468 /* Use full screen: */
470 use_fullscreen = YES;
472 else if (strcmp(argv[i], "--opengl") == 0 ||
473 strcmp(argv[i], "-g") == 0)
481 else if (strcmp(argv[i], "--usage") == 0)
487 else if (strcmp(argv[i], "--version") == 0)
491 printf("Super Tux - version " VERSION "\n");
494 else if (strcmp(argv[i], "--disable-sound") == 0)
496 /* Disable the compiled in sound feature */
498 printf("Sounds disabled \n");
502 printf("Warning: Sounds feature is not compiled in \n");
506 else if (strcmp(argv[i], "--disable-music") == 0)
508 /* Disable the compiled in sound feature */
510 printf("Music disabled \n");
514 printf("Warning: Music feature is not compiled in \n");
518 else if (strcmp(argv[i], "--debug-mode") == 0)
520 /* Enable the debug-mode */
524 else if (strcmp(argv[i], "--help") == 0)
527 printf("Super Tux " VERSION "\n\n");
529 printf("---------- Command-line options ----------\n\n");
531 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
533 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
535 printf(" --disable-music - Like above, but this will disable music.\n\n");
537 printf(" --fullscreen - Run in fullscreen mode.\n\n");
539 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
541 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
543 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
545 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
548 printf("---------- License ----------\n\n");
549 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
550 printf(" This is free software, and you are welcome to redistribute\n");
551 printf(" or modify it under certain conditions. See the file \n");
552 printf(" \"COPYING.txt\" for more details.\n\n\n");
554 printf("---------- Game controls ----------\n\n");
555 printf(" Please see the file \"README.txt\"\n\n");
561 /* Unknown - complain! */
571 void usage(char * prog, int ret)
576 /* Determine which stream to write to: */
584 /* Display the usage message: */
586 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",