7 bill@newbreedsoftware.com
8 http://www.newbreedsoftware.com/supertux/
10 April 11, 2000 - January 1st, 2004
19 #include <SDL_image.h>
20 #include <SDL_opengl.h>
24 #include <sys/types.h>
34 /* Local function prototypes: */
37 void usage(char * prog, int ret);
39 /* Does the given file exist and is it accessible? */
40 int faccessible(char *filename)
43 if (stat(filename, &filestat) == -1)
52 void st_directory_setup(void)
55 /* Set SuperTux configuration and save directories */
57 /* Get home directory (from $HOME variable)... if we can't determine it,
58 use the current directory ("."): */
60 if (getenv("HOME") != NULL)
61 home = getenv("HOME");
65 st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
66 strlen("/.supertux") + 1));
68 strcat(st_dir, "/.supertux");
70 st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
72 strcpy(st_save_dir,st_dir);
73 strcat(st_save_dir,"/save");
75 /* Create them. In the case they exist it won't destroy anything. */
79 mkdir(st_save_dir, 0755);
89 void st_general_setup(void)
91 /* Seed random number generator: */
93 srand(SDL_GetTicks());
95 /* Load global images: */
97 letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
100 letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
105 letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
108 letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
112 /* Set icon image: */
118 void st_video_setup(void)
122 SDL_FreeSurface(screen);
124 /* Init SDL Video: */
126 if (SDL_Init(SDL_INIT_VIDEO) < 0)
129 "\nError: I could not initialize video!\n"
130 "The Simple DirectMedia error that occured was:\n"
131 "%s\n\n", SDL_GetError());
140 st_video_setup_sdl();
142 /* Set window manager stuff: */
144 SDL_WM_SetCaption("Super Tux", "Super Tux");
148 void st_video_setup_sdl(void)
150 if (use_fullscreen == YES)
152 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
156 "\nWarning: I could not set up fullscreen video for "
158 "The Simple DirectMedia error that occured was:\n"
159 "%s\n\n", SDL_GetError());
165 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
170 "\nError: I could not set up video for 640x480 mode.\n"
171 "The Simple DirectMedia error that occured was:\n"
172 "%s\n\n", SDL_GetError());
178 void st_video_setup_gl(void)
181 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
182 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
183 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
184 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
185 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
187 if (use_fullscreen == YES)
189 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL ) ; /* | SDL_HWSURFACE); */
193 "\nWarning: I could not set up fullscreen video for "
195 "The Simple DirectMedia error that occured was:\n"
196 "%s\n\n", SDL_GetError());
202 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
207 "\nError: I could not set up video for 640x480 mode.\n"
208 "The Simple DirectMedia error that occured was:\n"
209 "%s\n\n", SDL_GetError());
214 /* Initialisierung von OpenGL * /
215 glViewport(0, 0, screen->w, screen->h);
217 glMatrixMode(GL_PROJECTION);
219 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
221 glMatrixMode(GL_MODELVIEW);
225 * Set up OpenGL for 2D rendering.
227 glDisable(GL_DEPTH_TEST);
228 glDisable(GL_CULL_FACE);
230 glViewport(0, 0, screen->w, screen->h);
231 glMatrixMode(GL_PROJECTION);
233 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
235 glMatrixMode(GL_MODELVIEW);
237 glTranslatef(0.0f, 0.0f, 0.0f);
240 void st_joystick_setup(void)
248 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
250 fprintf(stderr, "Warning: I could not initialize joystick!\n"
251 "The Simple DirectMedia error that occured was:\n"
252 "%s\n\n", SDL_GetError());
260 if (SDL_NumJoysticks() <= 0)
262 fprintf(stderr, "Warning: No joysticks are available.\n");
268 js = SDL_JoystickOpen(0);
272 fprintf(stderr, "Warning: Could not open joystick 1.\n"
273 "The Simple DirectMedia error that occured was:\n"
274 "%s\n\n", SDL_GetError());
280 /* Check for proper joystick configuration: */
282 if (SDL_JoystickNumAxes(js) < 2)
285 "Warning: Joystick does not have enough axes!\n");
291 if (SDL_JoystickNumButtons(js) < 2)
295 "Joystick does not have enough buttons!\n");
307 void st_audio_setup(void)
310 /* Init SDL Audio silently even if --disable-sound : */
312 if (audio_device == YES)
314 if (SDL_Init(SDL_INIT_AUDIO) < 0)
316 /* only print out message if sound or music
317 was not disabled at command-line
319 if (use_sound == YES || use_music == YES)
322 "\nWarning: I could not initialize audio!\n"
323 "The Simple DirectMedia error that occured was:\n"
324 "%s\n\n", SDL_GetError());
326 /* keep the programming logic the same :-)
327 because in this case, use_sound & use_music' values are ignored
328 when there's no available audio device
337 /* Open sound silently regarless the value of "use_sound": */
339 if (audio_device == YES)
341 if (open_audio(44100, AUDIO_S16, 2, 512) < 0)
343 /* only print out message if sound or music
344 was not disabled at command-line
346 if ((use_sound == YES) || (use_music == YES))
349 "\nWarning: I could not set up audio for 44100 Hz "
351 "The Simple DirectMedia error that occured was:\n"
352 "%s\n\n", SDL_GetError());
363 /* --- SHUTDOWN --- */
365 void st_shutdown(void)
374 void st_abort(char * reason, char * details)
376 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
382 /* Set Icon (private) */
391 /* Load icon into a surface: */
393 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
397 "\nError: I could not load the icon image: %s\n"
398 "The Simple DirectMedia error that occured was:\n"
399 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
406 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
407 mask = malloc(masklen * sizeof(Uint8));
408 memset(mask, 0xFF, masklen);
413 SDL_WM_SetIcon(icon, mask);
416 /* Free icon surface & mask: */
419 SDL_FreeSurface(icon);
423 /* Parse command-line arguments: */
425 void parseargs(int argc, char * argv[])
446 /* Parse arguments: */
448 for (i = 1; i < argc; i++)
450 if (strcmp(argv[i], "--fullscreen") == 0 ||
451 strcmp(argv[i], "-f") == 0)
453 /* Use full screen: */
455 use_fullscreen = YES;
457 else if (strcmp(argv[i], "--opengl") == 0 ||
458 strcmp(argv[i], "-g") == 0)
460 /* Use full screen: */
464 else if (strcmp(argv[i], "--usage") == 0)
470 else if (strcmp(argv[i], "--version") == 0)
474 printf("Super Tux - version " VERSION "\n");
477 else if (strcmp(argv[i], "--disable-sound") == 0)
479 /* Disable the compiled in sound feature */
481 printf("Sounds disabled \n");
485 printf("Warning: Sounds feature is not compiled in \n");
489 else if (strcmp(argv[i], "--disable-music") == 0)
491 /* Disable the compiled in sound feature */
493 printf("Music disabled \n");
497 printf("Warning: Music feature is not compiled in \n");
501 else if (strcmp(argv[i], "--debug-mode") == 0)
503 /* Enable the debug-mode */
507 else if (strcmp(argv[i], "--help") == 0)
510 printf("Super Tux " VERSION "\n\n");
512 printf("---------- Command-line options ----------\n\n");
514 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
516 printf(" --disable-music - Like above, but this will disable music.\n\n");
518 printf(" --fullscreen - Run in fullscreen mode.\n\n");
520 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
522 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
524 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
526 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
529 printf("---------- License ----------\n\n");
530 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
531 printf(" This is free software, and you are welcome to redistribute\n");
532 printf(" or modify it under certain conditions. See the file \n");
533 printf(" \"COPYING.txt\" for more details.\n\n\n");
535 printf("---------- Game controls ----------\n\n");
536 printf(" Please see the file \"README.txt\"\n\n");
542 /* Unknown - complain! */
552 void usage(char * prog, int ret)
557 /* Determine which stream to write to: */
565 /* Display the usage message: */
567 fprintf(fi, "Usage: %s [--fullscreen] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",