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();
147 /* Set window manager stuff: */
149 SDL_WM_SetCaption("Super Tux", "Super Tux");
153 void st_video_setup_sdl(void)
155 if (use_fullscreen == YES)
157 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
161 "\nWarning: I could not set up fullscreen video for "
163 "The Simple DirectMedia error that occured was:\n"
164 "%s\n\n", SDL_GetError());
170 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
175 "\nError: I could not set up video for 640x480 mode.\n"
176 "The Simple DirectMedia error that occured was:\n"
177 "%s\n\n", SDL_GetError());
183 void st_video_setup_gl(void)
187 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
188 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
189 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
190 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
191 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
193 if (use_fullscreen == YES)
195 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL ) ; /* | SDL_HWSURFACE); */
199 "\nWarning: I could not set up fullscreen video for "
201 "The Simple DirectMedia error that occured was:\n"
202 "%s\n\n", SDL_GetError());
208 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
213 "\nError: I could not set up video for 640x480 mode.\n"
214 "The Simple DirectMedia error that occured was:\n"
215 "%s\n\n", SDL_GetError());
220 /* Initialisierung von OpenGL * /
221 glViewport(0, 0, screen->w, screen->h);
223 glMatrixMode(GL_PROJECTION);
225 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
227 glMatrixMode(GL_MODELVIEW);
231 * Set up OpenGL for 2D rendering.
233 glDisable(GL_DEPTH_TEST);
234 glDisable(GL_CULL_FACE);
236 glViewport(0, 0, screen->w, screen->h);
237 glMatrixMode(GL_PROJECTION);
239 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
241 glMatrixMode(GL_MODELVIEW);
243 glTranslatef(0.0f, 0.0f, 0.0f);
248 void st_joystick_setup(void)
256 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
258 fprintf(stderr, "Warning: I could not initialize joystick!\n"
259 "The Simple DirectMedia error that occured was:\n"
260 "%s\n\n", SDL_GetError());
268 if (SDL_NumJoysticks() <= 0)
270 fprintf(stderr, "Warning: No joysticks are available.\n");
276 js = SDL_JoystickOpen(0);
280 fprintf(stderr, "Warning: Could not open joystick 1.\n"
281 "The Simple DirectMedia error that occured was:\n"
282 "%s\n\n", SDL_GetError());
288 /* Check for proper joystick configuration: */
290 if (SDL_JoystickNumAxes(js) < 2)
293 "Warning: Joystick does not have enough axes!\n");
299 if (SDL_JoystickNumButtons(js) < 2)
303 "Joystick does not have enough buttons!\n");
315 void st_audio_setup(void)
318 /* Init SDL Audio silently even if --disable-sound : */
320 if (audio_device == YES)
322 if (SDL_Init(SDL_INIT_AUDIO) < 0)
324 /* only print out message if sound or music
325 was not disabled at command-line
327 if (use_sound == YES || use_music == YES)
330 "\nWarning: I could not initialize audio!\n"
331 "The Simple DirectMedia error that occured was:\n"
332 "%s\n\n", SDL_GetError());
334 /* keep the programming logic the same :-)
335 because in this case, use_sound & use_music' values are ignored
336 when there's no available audio device
345 /* Open sound silently regarless the value of "use_sound": */
347 if (audio_device == YES)
349 if (open_audio(44100, AUDIO_S16, 2, 512) < 0)
351 /* only print out message if sound or music
352 was not disabled at command-line
354 if ((use_sound == YES) || (use_music == YES))
357 "\nWarning: I could not set up audio for 44100 Hz "
359 "The Simple DirectMedia error that occured was:\n"
360 "%s\n\n", SDL_GetError());
371 /* --- SHUTDOWN --- */
373 void st_shutdown(void)
382 void st_abort(char * reason, char * details)
384 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
390 /* Set Icon (private) */
399 /* Load icon into a surface: */
401 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
405 "\nError: I could not load the icon image: %s\n"
406 "The Simple DirectMedia error that occured was:\n"
407 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
414 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
415 mask = malloc(masklen * sizeof(Uint8));
416 memset(mask, 0xFF, masklen);
421 SDL_WM_SetIcon(icon, mask);
424 /* Free icon surface & mask: */
427 SDL_FreeSurface(icon);
431 /* Parse command-line arguments: */
433 void parseargs(int argc, char * argv[])
457 /* Parse arguments: */
459 for (i = 1; i < argc; i++)
461 if (strcmp(argv[i], "--fullscreen") == 0 ||
462 strcmp(argv[i], "-f") == 0)
464 /* Use full screen: */
466 use_fullscreen = YES;
468 else if (strcmp(argv[i], "--show-fps") == 0)
470 /* Use full screen: */
474 else if (strcmp(argv[i], "--opengl") == 0 ||
475 strcmp(argv[i], "-gl") == 0)
483 else if (strcmp(argv[i], "--usage") == 0)
489 else if (strcmp(argv[i], "--version") == 0)
493 printf("Super Tux - version " VERSION "\n");
496 else if (strcmp(argv[i], "--disable-sound") == 0)
498 /* Disable the compiled in sound feature */
500 printf("Sounds disabled \n");
504 printf("Warning: Sounds feature is not compiled in \n");
508 else if (strcmp(argv[i], "--disable-music") == 0)
510 /* Disable the compiled in sound feature */
512 printf("Music disabled \n");
516 printf("Warning: Music feature is not compiled in \n");
520 else if (strcmp(argv[i], "--debug-mode") == 0)
522 /* Enable the debug-mode */
526 else if (strcmp(argv[i], "--help") == 0)
529 printf("Super Tux " VERSION "\n\n");
531 printf("---------- Command-line options ----------\n\n");
533 printf(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
535 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
537 printf(" --disable-music - Like above, but this will disable music.\n\n");
539 printf(" --fullscreen - Run in fullscreen mode.\n\n");
541 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
543 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
545 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
547 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
550 printf("---------- License ----------\n\n");
551 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
552 printf(" This is free software, and you are welcome to redistribute\n");
553 printf(" or modify it under certain conditions. See the file \n");
554 printf(" \"COPYING.txt\" for more details.\n\n\n");
556 printf("---------- Game controls ----------\n\n");
557 printf(" Please see the file \"README.txt\"\n\n");
563 /* Unknown - complain! */
573 void usage(char * prog, int ret)
578 /* Determine which stream to write to: */
586 /* Display the usage message: */
588 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",