debug-mode featuring backscrolling! Split up of st_setup.
[supertux.git] / src / setup.c
1 /*
2   setup.c
3   
4   Super Tux - Setup
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - November 7, 2001
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <ctype.h>
26 #endif
27
28 #include "defines.h"
29 #include "globals.h"
30 #include "setup.h"
31 #include "screen.h"
32 #include "sound.h"
33
34 /* Local function prototypes: */
35
36 void seticon(void);
37 void usage(char * prog, int ret);
38
39
40 /* --- SETUP --- */
41
42 void st_directory_setup(void)
43 {
44
45   /* Set SuperTux configuration and save directories */
46
47   /* Get home directory (from $HOME variable)... if we can't determine it,
48      use the current directory ("."): */
49   char *home;
50   if (getenv("HOME") != NULL)
51     home = getenv("HOME");
52   else
53     home = ".";
54
55   st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
56                            strlen("/.supertux") + 1));
57   strcpy(st_dir, home);
58   strcat(st_dir, "/.supertux");
59
60   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
61
62   strcpy(st_save_dir,st_dir);
63   strcat(st_save_dir,"/save");
64
65   /* Create them. In the case they exist it won't destroy anything. */
66   mkdir(st_dir, 0755);
67   mkdir(st_save_dir, 0755);
68 }
69
70 void st_general_setup(void)
71 {
72   /* Seed random number generator: */
73
74   srand(SDL_GetTicks());
75   
76   /* Load global images: */
77
78   letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
79                              USE_ALPHA);
80
81   letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
82                             USE_ALPHA);/*
83   if (tux_x < 0)
84     tux_x = 0;*/
85
86   letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
87                             USE_ALPHA);
88
89   letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
90                            USE_ALPHA);
91
92
93   /* Set icon image: */
94
95   seticon();
96
97 }
98
99 void st_video_setup(void)
100 {
101
102   /* Init SDL Video: */
103
104   if (SDL_Init(SDL_INIT_VIDEO) < 0)
105     {
106       fprintf(stderr,
107               "\nError: I could not initialize video!\n"
108               "The Simple DirectMedia error that occured was:\n"
109               "%s\n\n", SDL_GetError());
110       exit(1);
111     }
112
113   /* Open display: */
114
115   if (use_fullscreen == YES)
116     {
117       screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
118       if (screen == NULL)
119         {
120           fprintf(stderr,
121                   "\nWarning: I could not set up fullscreen video for "
122                   "640x480 mode.\n"
123                   "The Simple DirectMedia error that occured was:\n"
124                   "%s\n\n", SDL_GetError());
125           use_fullscreen = NO;
126         }
127     }
128
129   if (use_fullscreen == NO)
130     {
131       screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
132
133       if (screen == NULL)
134         {
135           fprintf(stderr,
136                   "\nError: I could not set up video for 640x480 mode.\n"
137                   "The Simple DirectMedia error that occured was:\n"
138                   "%s\n\n", SDL_GetError());
139           exit(1);
140         }
141     }
142
143   /* Set window manager stuff: */
144
145   SDL_WM_SetCaption("Super Tux", "Super Tux");
146     
147 }
148
149 void st_joystick_setup(void)
150 {
151
152   /* Init Joystick: */
153
154 #ifdef JOY_YES
155   use_joystick = YES;
156
157   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
158     {
159       fprintf(stderr, "Warning: I could not initialize joystick!\n"
160               "The Simple DirectMedia error that occured was:\n"
161               "%s\n\n", SDL_GetError());
162
163       use_joystick = NO;
164     }
165   else
166     {
167       /* Open joystick: */
168
169       if (SDL_NumJoysticks() <= 0)
170         {
171           fprintf(stderr, "Warning: No joysticks are available.\n");
172
173           use_joystick = NO;
174         }
175       else
176         {
177           js = SDL_JoystickOpen(0);
178
179           if (js == NULL)
180             {
181               fprintf(stderr, "Warning: Could not open joystick 1.\n"
182                       "The Simple DirectMedia error that occured was:\n"
183                       "%s\n\n", SDL_GetError());
184
185               use_joystick = NO;
186             }
187           else
188             {
189               /* Check for proper joystick configuration: */
190
191               if (SDL_JoystickNumAxes(js) < 2)
192                 {
193                   fprintf(stderr,
194                           "Warning: Joystick does not have enough axes!\n");
195
196                   use_joystick = NO;
197                 }
198               else
199                 {
200                   if (SDL_JoystickNumButtons(js) < 2)
201                     {
202                       fprintf(stderr,
203                               "Warning: "
204                               "Joystick does not have enough buttons!\n");
205
206                       use_joystick = NO;
207                     }
208                 }
209             }
210         }
211     }
212 #endif
213
214 }
215
216 void st_audio_setup(void)
217 {
218
219   /* Init SDL Audio silently even if --disable-sound : */
220
221   if (audio_device == YES)
222     {
223       if (SDL_Init(SDL_INIT_AUDIO) < 0)
224         {
225           /* only print out message if sound or music
226              was not disabled at command-line
227            */
228           if (use_sound == YES || use_music == YES)
229             {
230               fprintf(stderr,
231                       "\nWarning: I could not initialize audio!\n"
232                       "The Simple DirectMedia error that occured was:\n"
233                       "%s\n\n", SDL_GetError());
234             }
235           /* keep the programming logic the same :-)
236              because in this case, use_sound & use_music' values are ignored
237              when there's no available audio device
238           */
239           use_sound = NO;
240           use_music = NO;
241           audio_device = NO;
242         }
243     }
244
245
246   /* Open sound silently regarless the value of "use_sound": */
247
248   if (audio_device == YES)
249     {
250       if (open_audio(44100, AUDIO_S16, 2, 512) < 0)
251         {
252           /* only print out message if sound or music
253              was not disabled at command-line
254            */
255           if ((use_sound == YES) || (use_music == YES))
256             {
257               fprintf(stderr,
258                       "\nWarning: I could not set up audio for 44100 Hz "
259                       "16-bit stereo.\n"
260                       "The Simple DirectMedia error that occured was:\n"
261                       "%s\n\n", SDL_GetError());
262             }
263           use_sound = NO;
264           use_music = NO;
265           audio_device = NO;
266         }
267     }
268
269 }
270
271
272 /* --- SHUTDOWN --- */
273
274 void st_shutdown(void)
275 {
276   SDL_Quit();
277 }
278
279
280 /* --- ABORT! --- */
281
282 void st_abort(char * reason, char * details)
283 {
284   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
285   st_shutdown();
286   exit(1);
287 }
288
289
290 /* Set Icon (private) */
291
292 void seticon(void)
293 {
294   int masklen;
295   Uint8 * mask;
296   SDL_Surface * icon;
297
298
299   /* Load icon into a surface: */
300
301   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
302   if (icon == NULL)
303     {
304       fprintf(stderr,
305               "\nError: I could not load the icon image: %s\n"
306               "The Simple DirectMedia error that occured was:\n"
307               "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
308       exit(1);
309     }
310
311
312   /* Create mask: */
313
314   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
315   mask = malloc(masklen * sizeof(Uint8));
316   memset(mask, 0xFF, masklen);
317
318
319   /* Set icon: */
320
321   SDL_WM_SetIcon(icon, mask);
322
323
324   /* Free icon surface & mask: */
325
326   free(mask);
327   SDL_FreeSurface(icon);
328 }
329
330
331 /* Parse command-line arguments: */
332
333 void parseargs(int argc, char * argv[])
334 {
335   int i;
336
337
338   /* Set defaults: */
339
340   debug_mode = NO;
341   use_fullscreen = NO;
342 #ifndef NOSOUND
343
344   use_sound = YES;
345   use_music = YES;
346   audio_device = YES;
347 #else
348
349   use_sound = NO;
350   use_music = NO;
351   audio_device = NO;
352 #endif
353
354   /* Parse arguments: */
355
356   for (i = 1; i < argc; i++)
357     {
358       if (strcmp(argv[i], "--fullscreen") == 0 ||
359           strcmp(argv[i], "-f") == 0)
360         {
361           /* Use full screen: */
362
363           use_fullscreen = YES;
364         }
365       else if (strcmp(argv[i], "--usage") == 0)
366         {
367           /* Show usage: */
368
369           usage(argv[0], 0);
370         }
371       else if (strcmp(argv[i], "--version") == 0)
372         {
373           /* Show version: */
374
375           printf("Super Tux - version " VERSION "\n");
376           exit(0);
377         }
378       else if (strcmp(argv[i], "--disable-sound") == 0)
379         {
380           /* Disable the compiled in sound feature */
381 #ifndef NOSOUND
382           printf("Sounds disabled \n");
383           use_sound = NO;
384 #else
385
386           printf("Warning: Sounds feature is not compiled in \n");
387           printf("Warning: Sounds feature is not compiled in \n");
388 #endif
389         }
390       else if (strcmp(argv[i], "--disable-music") == 0)
391         {
392           /* Disable the compiled in sound feature */
393 #ifndef NOSOUND
394           printf("Music disabled \n");
395           use_music = NO;
396 #else
397           printf("Warning: Music feature is not compiled in \n");
398 #endif
399
400         }
401       else if (strcmp(argv[i], "--debug-mode") == 0)
402         {
403           /* Enable the debug-mode */
404         debug_mode = YES;
405
406         }
407       else if (strcmp(argv[i], "--help") == 0)
408         {         /* Show help: */
409
410           printf("Super Tux " VERSION "\n\n");
411
412           printf("----------  Command-line options  ----------\n\n");
413
414           printf("  --disable-sound     - If sound support was compiled in,  this will\n                        disable sound for this session of the game.\n\n");
415
416           printf("  --disable-music     - Like above, but this will disable music.\n\n");
417
418           printf("  --fullscreen        - Run in fullscreen mode.\n\n");
419
420           printf("  --debug-mode        - Enables the debug-mode, which is useful for developers.\n\n");
421           
422           printf("  --help              - Display a help message summarizing command-line\n                        options, license and game controls.\n\n");
423
424           printf("  --usage             - Display a brief message summarizing command-line options.\n\n");
425
426           printf("  --version           - Display the version of SuperTux you're running.\n\n\n");
427
428
429           printf("----------          License       ----------\n\n");
430           printf("  This program comes with ABSOLUTELY NO WARRANTY.\n");
431           printf("  This is free software, and you are welcome to redistribute\n");
432           printf("  or modify it under certain conditions. See the file \n");
433           printf("  \"COPYING.txt\" for more details.\n\n\n");
434
435           printf("----------      Game controls     ----------\n\n");
436           printf("  Please see the file \"README.txt\"\n\n");
437
438           exit(0);
439         }
440       else
441         {
442           /* Unknown - complain! */
443
444           usage(argv[0], 1);
445         }
446     }
447 }
448
449
450 /* Display usage: */
451
452 void usage(char * prog, int ret)
453 {
454   FILE * fi;
455
456
457   /* Determine which stream to write to: */
458
459   if (ret == 0)
460     fi = stdout;
461   else
462     fi = stderr;
463
464
465   /* Display the usage message: */
466
467   fprintf(fi, "Usage: %s [--fullscreen] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version]\n",
468            prog);
469
470
471   /* Quit! */
472
473   exit(ret);
474 }