0c09c085eba3d54289d1dbc777780433fb1fdfd0
[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 #ifndef NOSOUND
22 #include <SDL_mixer.h>
23 #endif
24
25 #ifdef LINUX
26 #include <pwd.h>
27 #include <sys/types.h>
28 #include <ctype.h>
29 #endif
30
31 #include "defines.h"
32 #include "globals.h"
33 #include "setup.h"
34 #include "screen.h"
35
36
37 /* Local function prototypes: */
38
39 void seticon(void);
40 void usage(char * prog, int ret);
41
42
43 /* --- SETUP --- */
44
45 void st_setup(void)
46 {
47   /* Seed random number generator: */
48   
49   srand(SDL_GetTicks());
50   
51   
52   /* Init SDL Video: */
53   
54   if (SDL_Init(SDL_INIT_VIDEO) < 0)
55     {
56       fprintf(stderr,
57               "\nError: I could not initialize video!\n"
58               "The Simple DirectMedia error that occured was:\n"
59               "%s\n\n", SDL_GetError());
60       exit(1);
61     }
62
63
64   /* Init Joystick: */
65   
66 #ifdef JOY_YES
67   use_joystick = YES;
68   
69   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
70     {
71       fprintf(stderr, "Warning: I could not initialize joystick!\n"
72               "The Simple DirectMedia error that occured was:\n"
73               "%s\n\n", SDL_GetError());
74       
75       use_joystick = NO;
76     }
77   else
78     {
79       /* Open joystick: */
80       
81       if (SDL_NumJoysticks() <= 0)
82         {
83           fprintf(stderr, "Warning: No joysticks are available.\n");
84           
85           use_joystick = NO;
86         }
87       else
88         {
89           js = SDL_JoystickOpen(0);
90           
91           if (js == NULL)
92             {
93               fprintf(stderr, "Warning: Could not open joystick 1.\n"
94                       "The Simple DirectMedia error that occured was:\n"
95                       "%s\n\n", SDL_GetError());
96               
97               use_joystick = NO;
98             }
99           else
100             {
101               /* Check for proper joystick configuration: */
102               
103               if (SDL_JoystickNumAxes(js) < 2)
104                 {
105                   fprintf(stderr,
106                           "Warning: Joystick does not have enough axes!\n");
107                   
108                   use_joystick = NO;
109                 }
110               else
111                 {
112                   if (SDL_JoystickNumButtons(js) < 2)
113                     {
114                       fprintf(stderr,
115                               "Warning: "
116                               "Joystick does not have enough buttons!\n");
117                       
118                       use_joystick = NO;
119                     }
120                 }
121             }
122         }
123     }
124 #endif
125   
126   
127   /* Init SDL Audio: */
128   
129   if (use_sound == YES)
130     {
131       if (SDL_Init(SDL_INIT_AUDIO) < 0)
132         {
133           fprintf(stderr,
134                   "\nWarning: I could not initialize audio!\n"
135                   "The Simple DirectMedia error that occured was:\n"
136                   "%s\n\n", SDL_GetError());
137           use_sound = NO;
138         }
139     }
140   
141   
142   /* Open sound: */
143   
144 #ifndef NOSOUND
145   if (use_sound == YES)
146     {
147       if (Mix_OpenAudio(11025, AUDIO_S16, 2, 512) < 0)
148         {
149           fprintf(stderr,
150                   "\nWarning: I could not set up audio for 11025 Hz "
151                   "16-bit stereo.\n"
152                   "The Simple DirectMedia error that occured was:\n"
153                   "%s\n\n", SDL_GetError());
154           use_sound = 0;
155         }
156     }
157 #endif
158
159
160   /* Open display: */
161   
162   if (use_fullscreen == YES)
163     {
164       screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN) ; /* | SDL_HWSURFACE); */
165       if (screen == NULL)
166         {
167           fprintf(stderr,
168                   "\nWarning: I could not set up fullscreen video for "
169                   "640x480 mode.\n"
170                   "The Simple DirectMedia error that occured was:\n"
171                   "%s\n\n", SDL_GetError());
172           use_fullscreen = NO;
173         }
174     }
175   
176   if (use_fullscreen == NO)
177     {
178       screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
179       
180       if (screen == NULL)
181         {
182           fprintf(stderr,
183                   "\nError: I could not set up video for 640x480 mode.\n"
184                   "The Simple DirectMedia error that occured was:\n"
185                   "%s\n\n", SDL_GetError());
186           exit(1);
187         }
188     }
189   
190   
191   /* Load global images: */
192   
193   letters_black = load_image(DATA_PREFIX "/images/status/letters-black.png",
194                              USE_ALPHA);
195
196   letters_gold = load_image(DATA_PREFIX "/images/status/letters-gold.png",
197                              USE_ALPHA);
198
199   letters_blue = load_image(DATA_PREFIX "/images/status/letters-blue.png",
200                              USE_ALPHA);
201
202   letters_red = load_image(DATA_PREFIX "/images/status/letters-red.png",
203                            USE_ALPHA);
204   
205   
206   /* Set icon image: */
207   
208   seticon();
209   
210   
211   /* Set window manager stuff: */
212   
213   SDL_WM_SetCaption("Super Tux", "Super Tux");
214 }
215
216
217 /* --- SHUTDOWN --- */
218
219 void st_shutdown(void)
220 {
221   SDL_Quit();
222 }
223
224
225 /* --- ABORT! --- */
226
227 void st_abort(char * reason, char * details)
228 {
229   fprintf(stderr, "\nError: %s\n%s\n\n", reason, details);
230   st_shutdown();
231   exit(1);
232 }
233
234
235 /* Set Icon (private) */
236
237 void seticon(void)
238 {
239   int masklen;
240   Uint8 * mask;
241   SDL_Surface * icon;
242   
243   
244   /* Load icon into a surface: */
245   
246   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
247   if (icon == NULL)
248     {
249       fprintf(stderr,
250               "\nError: I could not load the icon image: %s\n"
251               "The Simple DirectMedia error that occured was:\n"
252               "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
253       exit(1);
254     }
255   
256   
257   /* Create mask: */
258   
259   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
260   mask = malloc(masklen * sizeof(Uint8));
261   memset(mask, 0xFF, masklen);
262   
263   
264   /* Set icon: */
265   
266   SDL_WM_SetIcon(icon, mask);
267   
268   
269   /* Free icon surface & mask: */
270   
271   free(mask);
272   SDL_FreeSurface(icon);
273 }
274
275
276 /* Parse command-line arguments: */
277
278 void parseargs(int argc, char * argv[])
279 {
280   int i;
281   
282   
283   /* Set defaults: */
284   
285   use_fullscreen = NO;
286   use_sound = YES;
287   
288   
289   /* Parse arguments: */
290   
291   for (i = 1; i < argc; i++)
292     {
293       if (strcmp(argv[i], "--fullscreen") == 0 ||
294           strcmp(argv[i], "-f") == 0)
295         {
296           /* Use full screen: */
297           
298           use_fullscreen = YES;
299         }
300       else if (strcmp(argv[i], "--usage") == 0)
301         {
302           /* Show usage: */
303           
304           usage(argv[0], 0);
305         }
306       else if (strcmp(argv[i], "--version") == 0)
307         {
308           /* Show version: */
309           
310           printf("Super Tux - version " VERSION "\n");
311           exit(0);
312         }
313       else if (strcmp(argv[i], "--help") == 0)
314         {
315           /* Show version: */
316           
317           printf("Super Tux - Help summary\n");
318           printf("[ under construction ]\n");
319           exit(0);
320         }
321       else
322         {
323           /* Unknown - complain! */
324           
325           usage(argv[0], 1);
326         }
327     }
328 }
329
330
331 /* Display usage: */
332
333 void usage(char * prog, int ret)
334 {
335   FILE * fi;
336   
337   
338   /* Determine which stream to write to: */
339   
340   if (ret == 0)
341     fi = stdout;
342   else
343     fi = stderr;
344   
345   
346   /* Display the usage message: */
347   
348   fprintf(fi, "Usage: %s [--fullscreen] | [--usage | --help | --version]\n",
349           prog);
350   
351   
352   /* Quit! */
353
354   exit(ret);
355 }