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>
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 text_load(&black_text,DATA_PREFIX "/images/status/letters-black.png");
101 text_load(&gold_text,DATA_PREFIX "/images/status/letters-gold.png");
102 text_load(&blue_text,DATA_PREFIX "/images/status/letters-blue.png");
103 text_load(&red_text,DATA_PREFIX "/images/status/letters-red.png");
105 /* Set icon image: */
108 SDL_EnableUNICODE(1);
112 void st_video_setup(void)
116 SDL_FreeSurface(screen);
118 /* Init SDL Video: */
120 if (SDL_Init(SDL_INIT_VIDEO) < 0)
123 "\nError: I could not initialize video!\n"
124 "The Simple DirectMedia error that occured was:\n"
125 "%s\n\n", SDL_GetError());
134 st_video_setup_sdl();
138 /* Set window manager stuff: */
140 SDL_WM_SetCaption("Super Tux", "Super Tux");
144 void st_video_setup_sdl(void)
146 if (use_fullscreen == YES)
148 screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
152 "\nWarning: I could not set up fullscreen video for "
154 "The Simple DirectMedia error that occured was:\n"
155 "%s\n\n", SDL_GetError());
161 screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF );
166 "\nError: I could not set up video for 640x480 mode.\n"
167 "The Simple DirectMedia error that occured was:\n"
168 "%s\n\n", SDL_GetError());
174 void st_video_setup_gl(void)
178 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
179 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
180 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
181 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
182 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
184 if (use_fullscreen == YES)
186 screen = SDL_SetVideoMode(640, 480, 32, SDL_FULLSCREEN | SDL_OPENGL ) ; /* | SDL_HWSURFACE); */
190 "\nWarning: I could not set up fullscreen video for "
192 "The Simple DirectMedia error that occured was:\n"
193 "%s\n\n", SDL_GetError());
199 screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_OPENGL | SDL_OPENGLBLIT );
204 "\nError: I could not set up video for 640x480 mode.\n"
205 "The Simple DirectMedia error that occured was:\n"
206 "%s\n\n", SDL_GetError());
212 * Set up OpenGL for 2D rendering.
214 glDisable(GL_DEPTH_TEST);
215 glDisable(GL_CULL_FACE);
217 glViewport(0, 0, screen->w, screen->h);
218 glMatrixMode(GL_PROJECTION);
220 glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
222 glMatrixMode(GL_MODELVIEW);
224 glTranslatef(0.0f, 0.0f, 0.0f);
229 void st_joystick_setup(void)
237 if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
239 fprintf(stderr, "Warning: I could not initialize joystick!\n"
240 "The Simple DirectMedia error that occured was:\n"
241 "%s\n\n", SDL_GetError());
249 if (SDL_NumJoysticks() <= 0)
251 fprintf(stderr, "Warning: No joysticks are available.\n");
257 js = SDL_JoystickOpen(0);
261 fprintf(stderr, "Warning: Could not open joystick 1.\n"
262 "The Simple DirectMedia error that occured was:\n"
263 "%s\n\n", SDL_GetError());
269 /* Check for proper joystick configuration: */
271 if (SDL_JoystickNumAxes(js) < 2)
274 "Warning: Joystick does not have enough axes!\n");
280 if (SDL_JoystickNumButtons(js) < 2)
284 "Joystick does not have enough buttons!\n");
296 void st_audio_setup(void)
299 /* Init SDL Audio silently even if --disable-sound : */
301 if (audio_device == YES)
303 if (SDL_Init(SDL_INIT_AUDIO) < 0)
305 /* only print out message if sound or music
306 was not disabled at command-line
308 if (use_sound == YES || use_music == YES)
311 "\nWarning: I could not initialize audio!\n"
312 "The Simple DirectMedia error that occured was:\n"
313 "%s\n\n", SDL_GetError());
315 /* keep the programming logic the same :-)
316 because in this case, use_sound & use_music' values are ignored
317 when there's no available audio device
326 /* Open sound silently regarless the value of "use_sound": */
328 if (audio_device == YES)
330 if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
332 /* only print out message if sound or music
333 was not disabled at command-line
335 if ((use_sound == YES) || (use_music == YES))
338 "\nWarning: I could not set up audio for 44100 Hz "
340 "The Simple DirectMedia error that occured was:\n"
341 "%s\n\n", SDL_GetError());
352 /* --- SHUTDOWN --- */
354 void st_shutdown(void)
363 void st_abort(char * reason, char * details)
365 fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
371 /* Set Icon (private) */
380 /* Load icon into a surface: */
382 icon = IMG_Load(DATA_PREFIX "/images/icon.png");
386 "\nError: I could not load the icon image: %s\n"
387 "The Simple DirectMedia error that occured was:\n"
388 "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
395 masklen = (((icon -> w) + 7) / 8) * (icon -> h);
396 mask = malloc(masklen * sizeof(Uint8));
397 memset(mask, 0xFF, masklen);
402 SDL_WM_SetIcon(icon, mask);
405 /* Free icon surface & mask: */
408 SDL_FreeSurface(icon);
412 /* Parse command-line arguments: */
414 void parseargs(int argc, char * argv[])
438 /* Parse arguments: */
440 for (i = 1; i < argc; i++)
442 if (strcmp(argv[i], "--fullscreen") == 0 ||
443 strcmp(argv[i], "-f") == 0)
445 /* Use full screen: */
447 use_fullscreen = YES;
449 else if (strcmp(argv[i], "--show-fps") == 0)
451 /* Use full screen: */
455 else if (strcmp(argv[i], "--opengl") == 0 ||
456 strcmp(argv[i], "-gl") == 0)
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(" --opengl - If opengl support was compiled in, this will enable the EXPERIMENTAL OpenGL mode.\n\n");
516 printf(" --disable-sound - If sound support was compiled in, this will\n disable sound for this session of the game.\n\n");
518 printf(" --disable-music - Like above, but this will disable music.\n\n");
520 printf(" --fullscreen - Run in fullscreen mode.\n\n");
522 printf(" --debug-mode - Enables the debug-mode, which is useful for developers.\n\n");
524 printf(" --help - Display a help message summarizing command-line\n options, license and game controls.\n\n");
526 printf(" --usage - Display a brief message summarizing command-line options.\n\n");
528 printf(" --version - Display the version of SuperTux you're running.\n\n\n");
531 printf("---------- License ----------\n\n");
532 printf(" This program comes with ABSOLUTELY NO WARRANTY.\n");
533 printf(" This is free software, and you are welcome to redistribute\n");
534 printf(" or modify it under certain conditions. See the file \n");
535 printf(" \"COPYING.txt\" for more details.\n\n\n");
537 printf("---------- Game controls ----------\n\n");
538 printf(" Please see the file \"README.txt\"\n\n");
544 /* Unknown - complain! */
554 void usage(char * prog, int ret)
559 /* Determine which stream to write to: */
567 /* Display the usage message: */
569 fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",