1 //  $Id: setup.cpp 2702 2005-07-08 12:18:16Z wansti $
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 
20 #include <assert.h>
21 #include <stdio.h>
22 #include <iostream>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <SDL.h>
29 #include <SDL_image.h>
30 #ifndef NOOPENGL
31 #include <SDL_opengl.h>
32 #endif
33 
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37 #ifndef WIN32
38 #include <libgen.h>
39 #endif
40 #include <ctype.h>
41 
42 #include "defines.h"
43 #include "globals.h"
44 #include "setup.h"
45 #include "screen.h"
46 #include "texture.h"
47 #include "menu.h"
48 #include "gameloop.h"
49 #include "configfile.h"
50 #include "scene.h"
51 #include "worldmap.h"
52 #include "resources.h"
53 #include "intro.h"
54 #include "music_manager.h"
55 
56 #include "player.h"
57 
58 #ifdef WIN32
59 #define mkdir(dir, mode)    mkdir(dir)
60 // on win32 we typically don't want LFS paths
61 #undef DATA_PREFIX
62 #define DATA_PREFIX "./data/"
63 #endif
64 
65 /* Screen proprities: */
66 /* Don't use this to test for the actual screen sizes. Use screen->w/h instead! */
67 #define SCREEN_W 640
68 #define SCREEN_H 480
69 
70 /* Local function prototypes: */
71 
72 void seticon(void);
73 void usage(char * prog, int ret);
74 
75 /* Does the given file exist and is it accessible? */
faccessible(const char * filename)76 int faccessible(const char *filename)
77 {
78   struct stat filestat;
79   if (stat(filename, &filestat) == -1)
80     {
81       return false;
82     }
83   else
84     {
85       if(S_ISREG(filestat.st_mode))
86         return true;
87       else
88         return false;
89     }
90 }
91 
92 /* Can we write to this location? */
fwriteable(const char * filename)93 int fwriteable(const char *filename)
94 {
95   FILE* fi;
96   fi = fopen(filename, "wa");
97   if (fi == NULL)
98     {
99       return false;
100     }
101   fclose(fi);
102   return true;
103 }
104 
105 /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/
fcreatedir(const char * relative_dir)106 int fcreatedir(const char* relative_dir)
107 {
108   char path[1024];
109   snprintf(path, 1024, "%s/%s/", st_dir, relative_dir);
110   if(mkdir(path,0755) != 0)
111     {
112       snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir);
113       if(mkdir(path,0755) != 0)
114         {
115           return false;
116         }
117       else
118         {
119           return true;
120         }
121     }
122   else
123     {
124       return true;
125     }
126 }
127 
opendata(const char * rel_filename,const char * mode)128 FILE * opendata(const char * rel_filename, const char * mode)
129 {
130   char * filename = NULL;
131   FILE * fi;
132 
133   filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
134                                              strlen(rel_filename) + 1));
135 
136   strcpy(filename, st_dir);
137   /* Open the high score file: */
138 
139   strcat(filename, rel_filename);
140 
141   /* Try opening the file: */
142   fi = fopen(filename, mode);
143 
144   if (fi == NULL)
145     {
146       fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename);
147 
148       if (strcmp(mode, "r") == 0)
149         fprintf(stderr, "for read!!!\n");
150       else if (strcmp(mode, "w") == 0)
151         fprintf(stderr, "for write!!!\n");
152     }
153   free( filename );
154 
155   return(fi);
156 }
157 
158 /* Get all names of sub-directories in a certain directory. */
159 /* Returns the number of sub-directories found. */
160 /* Note: The user has to free the allocated space. */
dsubdirs(const char * rel_path,const char * expected_file)161 string_list_type dsubdirs(const char *rel_path,const  char* expected_file)
162 {
163   DIR *dirStructP;
164   struct dirent *direntp;
165   string_list_type sdirs;
166   char filename[1024];
167   char path[1024];
168 
169   string_list_init(&sdirs);
170   sprintf(path,"%s/%s",st_dir,rel_path);
171   if((dirStructP = opendir(path)) != NULL)
172     {
173       while((direntp = readdir(dirStructP)) != NULL)
174         {
175           char absolute_filename[1024];
176           struct stat buf;
177 
178           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
179 
180           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
181             {
182               if(expected_file != NULL)
183                 {
184                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
185                   if(!faccessible(filename))
186                     continue;
187                 }
188 
189               string_list_add_item(&sdirs,direntp->d_name);
190             }
191         }
192       closedir(dirStructP);
193     }
194 
195   sprintf(path,"%s/%s",datadir.c_str(),rel_path);
196   if((dirStructP = opendir(path)) != NULL)
197     {
198       while((direntp = readdir(dirStructP)) != NULL)
199         {
200           char absolute_filename[1024];
201           struct stat buf;
202 
203           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
204 
205           if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode))
206             {
207               if(expected_file != NULL)
208                 {
209                   sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file);
210                   if(!faccessible(filename))
211                     {
212                       continue;
213                     }
214                   else
215                     {
216                       sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file);
217                       if(faccessible(filename))
218                         continue;
219                     }
220                 }
221 
222               string_list_add_item(&sdirs,direntp->d_name);
223             }
224         }
225       closedir(dirStructP);
226     }
227 
228   return sdirs;
229 }
230 
dfiles(const char * rel_path,const char * glob,const char * exception_str)231 string_list_type dfiles(const char *rel_path, const  char* glob, const  char* exception_str)
232 {
233   DIR *dirStructP;
234   struct dirent *direntp;
235   string_list_type sdirs;
236   char path[1024];
237 
238   string_list_init(&sdirs);
239   sprintf(path,"%s/%s",st_dir,rel_path);
240   if((dirStructP = opendir(path)) != NULL)
241     {
242       while((direntp = readdir(dirStructP)) != NULL)
243         {
244           char absolute_filename[1024];
245           struct stat buf;
246 
247           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
248 
249           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
250             {
251               if(exception_str != NULL)
252                 {
253                   if(strstr(direntp->d_name,exception_str) != NULL)
254                     continue;
255                 }
256               if(glob != NULL)
257                 if(strstr(direntp->d_name,glob) == NULL)
258                   continue;
259 
260               string_list_add_item(&sdirs,direntp->d_name);
261             }
262         }
263       closedir(dirStructP);
264     }
265 
266   sprintf(path,"%s/%s",datadir.c_str(),rel_path);
267   if((dirStructP = opendir(path)) != NULL)
268     {
269       while((direntp = readdir(dirStructP)) != NULL)
270         {
271           char absolute_filename[1024];
272           struct stat buf;
273 
274           sprintf(absolute_filename, "%s/%s", path, direntp->d_name);
275 
276           if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode))
277             {
278               if(exception_str != NULL)
279                 {
280                   if(strstr(direntp->d_name,exception_str) != NULL)
281                     continue;
282                 }
283               if(glob != NULL)
284                 if(strstr(direntp->d_name,glob) == NULL)
285                   continue;
286 
287               string_list_add_item(&sdirs,direntp->d_name);
288             }
289         }
290       closedir(dirStructP);
291     }
292 
293   return sdirs;
294 }
295 
free_strings(char ** strings,int num)296 void free_strings(char **strings, int num)
297 {
298   int i;
299   for(i=0; i < num; ++i)
300     free(strings[i]);
301 }
302 
303 /* --- SETUP --- */
304 /* Set SuperTux configuration and save directories */
st_directory_setup(void)305 void st_directory_setup(void)
306 {
307   char *home;
308   char str[1024];
309   /* Get home directory (from $HOME variable)... if we can't determine it,
310      use the current directory ("."): */
311   if (getenv("HOME") != NULL)
312     home = getenv("HOME");
313   else
314     home = ".";
315 
316   st_dir = (char *) malloc(sizeof(char) * (strlen(home) +
317                                            strlen("/.supertux") + 1));
318   strcpy(st_dir, home);
319   strcat(st_dir, "/.supertux");
320 
321   /* Remove .supertux config-file from old SuperTux versions */
322   if(faccessible(st_dir))
323     {
324       remove
325         (st_dir);
326     }
327 
328   st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1));
329 
330   strcpy(st_save_dir,st_dir);
331   strcat(st_save_dir,"/save");
332 
333   /* Create them. In the case they exist they won't destroy anything. */
334   mkdir(st_dir, 0755);
335   mkdir(st_save_dir, 0755);
336 
337   sprintf(str, "%s/levels", st_dir);
338   mkdir(str, 0755);
339 
340   // User has not that a datadir, so we try some magic
341   if (datadir.empty())
342     {
343 #ifndef WIN32
344       // Detect datadir
345       char exe_file[PATH_MAX];
346       if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0)
347         {
348           puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX);
349           datadir = DATA_PREFIX;
350         }
351       else
352         {
353           std::string exedir = std::string(dirname(exe_file)) + "/";
354 
355           datadir = exedir + "../data"; // SuperTux run from source dir
356           if (access(datadir.c_str(), F_OK) != 0)
357             {
358               datadir = exedir + "../share/supertux"; // SuperTux run from PATH
359               if (access(datadir.c_str(), F_OK) != 0)
360                 { // If all fails, fall back to compiled path
361                   datadir = DATA_PREFIX;
362                 }
363             }
364         }
365 #else
366   datadir = DATA_PREFIX;
367 #endif
368     }
369   printf("Datadir: %s\n", datadir.c_str());
370 }
371 
372 /* Create and setup menus. */
st_menu(void)373 void st_menu(void)
374 {
375   main_menu      = new Menu();
376   options_menu   = new Menu();
377   options_keys_menu     = new Menu();
378   options_joystick_menu = new Menu();
379   load_game_menu = new Menu();
380   save_game_menu = new Menu();
381   game_menu      = new Menu();
382   highscore_menu = new Menu();
383   contrib_menu   = new Menu();
384   contrib_subset_menu   = new Menu();
385   worldmap_menu  = new Menu();
386 
387   main_menu->set_pos(screen->w/2, 335);
388   main_menu->additem(MN_GOTO, "Start Game",0,load_game_menu, MNID_STARTGAME);
389   main_menu->additem(MN_GOTO, "Bonus Levels",0,contrib_menu, MNID_CONTRIB);
390   main_menu->additem(MN_GOTO, "Options",0,options_menu, MNID_OPTIONMENU);
391   main_menu->additem(MN_ACTION,"Level Editor",0,0, MNID_LEVELEDITOR);
392   main_menu->additem(MN_ACTION,"Credits",0,0, MNID_CREDITS);
393   main_menu->additem(MN_ACTION,"Quit",0,0, MNID_QUITMAINMENU);
394 
395   options_menu->additem(MN_LABEL,"Options",0,0);
396   options_menu->additem(MN_HL,"",0,0);
397 #ifndef NOOPENGL
398   options_menu->additem(MN_TOGGLE,"OpenGL",use_gl,0, MNID_OPENGL);
399 #else
400   options_menu->additem(MN_DEACTIVE,"OpenGL (not supported)",use_gl, 0, MNID_OPENGL);
401 #endif
402   options_menu->additem(MN_TOGGLE,"Fullscreen",use_fullscreen,0, MNID_FULLSCREEN);
403   if(audio_device)
404     {
405       options_menu->additem(MN_TOGGLE,"Sound     ", use_sound,0, MNID_SOUND);
406       options_menu->additem(MN_TOGGLE,"Music     ", use_music,0, MNID_MUSIC);
407     }
408   else
409     {
410       options_menu->additem(MN_DEACTIVE,"Sound     ", false,0, MNID_SOUND);
411       options_menu->additem(MN_DEACTIVE,"Music     ", false,0, MNID_MUSIC);
412     }
413   options_menu->additem(MN_TOGGLE,"Show FPS  ",show_fps,0, MNID_SHOWFPS);
414   options_menu->additem(MN_GOTO,"Keyboard Setup",0,options_keys_menu);
415 
416   //if(use_joystick)
417   //  options_menu->additem(MN_GOTO,"Joystick Setup",0,options_joystick_menu);
418 
419   options_menu->additem(MN_HL,"",0,0);
420   options_menu->additem(MN_BACK,"Back",0,0);
421 
422   options_keys_menu->additem(MN_LABEL,"Key Setup",0,0);
423   options_keys_menu->additem(MN_HL,"",0,0);
424   options_keys_menu->additem(MN_CONTROLFIELD,"Left move", 0,0, 0,&keymap.left);
425   options_keys_menu->additem(MN_CONTROLFIELD,"Right move", 0,0, 0,&keymap.right);
426   options_keys_menu->additem(MN_CONTROLFIELD,"Jump", 0,0, 0,&keymap.jump);
427   options_keys_menu->additem(MN_CONTROLFIELD,"Duck", 0,0, 0,&keymap.duck);
428   options_keys_menu->additem(MN_CONTROLFIELD,"Power/Run", 0,0, 0,&keymap.fire);
429   options_keys_menu->additem(MN_HL,"",0,0);
430   options_keys_menu->additem(MN_BACK,"Back",0,0);
431 
432   if(use_joystick)
433     {
434     options_joystick_menu->additem(MN_LABEL,"Joystick Setup",0,0);
435     options_joystick_menu->additem(MN_HL,"",0,0);
436     options_joystick_menu->additem(MN_CONTROLFIELD,"X axis", 0,0, 0,&joystick_keymap.x_axis);
437     options_joystick_menu->additem(MN_CONTROLFIELD,"Y axis", 0,0, 0,&joystick_keymap.y_axis);
438     options_joystick_menu->additem(MN_CONTROLFIELD,"A button", 0,0, 0,&joystick_keymap.a_button);
439     options_joystick_menu->additem(MN_CONTROLFIELD,"B button", 0,0, 0,&joystick_keymap.b_button);
440     options_joystick_menu->additem(MN_CONTROLFIELD,"Start", 0,0, 0,&joystick_keymap.start_button);
441     options_joystick_menu->additem(MN_CONTROLFIELD,"DeadZone", 0,0, 0,&joystick_keymap.dead_zone);
442     options_joystick_menu->additem(MN_HL,"",0,0);
443     options_joystick_menu->additem(MN_BACK,"Back",0,0);
444     }
445 
446   load_game_menu->additem(MN_LABEL,"Start Game",0,0);
447   load_game_menu->additem(MN_HL,"",0,0);
448   load_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0, 1);
449   load_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0, 2);
450   load_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0, 3);
451   load_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0, 4);
452   load_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0, 5);
453   load_game_menu->additem(MN_HL,"",0,0);
454   load_game_menu->additem(MN_BACK,"Back",0,0);
455 
456   save_game_menu->additem(MN_LABEL,"Save Game",0,0);
457   save_game_menu->additem(MN_HL,"",0,0);
458   save_game_menu->additem(MN_DEACTIVE,"Slot 1",0,0, 1);
459   save_game_menu->additem(MN_DEACTIVE,"Slot 2",0,0, 2);
460   save_game_menu->additem(MN_DEACTIVE,"Slot 3",0,0, 3);
461   save_game_menu->additem(MN_DEACTIVE,"Slot 4",0,0, 4);
462   save_game_menu->additem(MN_DEACTIVE,"Slot 5",0,0, 5);
463   save_game_menu->additem(MN_HL,"",0,0);
464   save_game_menu->additem(MN_BACK,"Back",0,0);
465 
466   game_menu->additem(MN_LABEL,"Pause",0,0);
467   game_menu->additem(MN_HL,"",0,0);
468   game_menu->additem(MN_ACTION,"Continue",0,0,MNID_CONTINUE);
469   game_menu->additem(MN_GOTO,"Options",0,options_menu);
470   game_menu->additem(MN_HL,"",0,0);
471   game_menu->additem(MN_ACTION,"Abort Level",0,0,MNID_ABORTLEVEL);
472 
473   worldmap_menu->additem(MN_LABEL,"Pause",0,0);
474   worldmap_menu->additem(MN_HL,"",0,0);
475   worldmap_menu->additem(MN_ACTION,"Continue",0,0,MNID_RETURNWORLDMAP);
476   worldmap_menu->additem(MN_GOTO,"Options",0,options_menu);
477   worldmap_menu->additem(MN_HL,"",0,0);
478   worldmap_menu->additem(MN_ACTION,"Quit Game",0,0,MNID_QUITWORLDMAP);
479 
480   highscore_menu->additem(MN_TEXTFIELD,"Enter your name:",0,0);
481 }
482 
update_load_save_game_menu(Menu * pmenu)483 void update_load_save_game_menu(Menu* pmenu)
484 {
485   for(int i = 2; i < 7; ++i)
486     {
487       // FIXME: Insert a real savegame struct/class here instead of
488       // doing string vodoo
489       std::string tmp = slotinfo(i - 1);
490       pmenu->item[i].kind = MN_ACTION;
491       pmenu->item[i].change_text(tmp.c_str());
492     }
493 }
494 
process_load_game_menu()495 bool process_load_game_menu()
496 {
497   int slot = load_game_menu->check();
498 
499   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
500     {
501       char slotfile[1024];
502       snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot);
503 
504       if (access(slotfile, F_OK) != 0)
505         {
506           draw_intro();
507         }
508 
509       fadeout();
510       WorldMapNS::WorldMap worldmap;
511 
512       //TODO: Define the circumstances under which BonusIsland is chosen
513       worldmap.set_map_file("world1.stwm");
514       worldmap.load_map();
515 
516       // Load the game or at least set the savegame_file variable
517       worldmap.loadgame(slotfile);
518 
519       worldmap.display();
520 
521       Menu::set_current(main_menu);
522 
523       st_pause_ticks_stop();
524       return true;
525     }
526   else
527     {
528       return false;
529     }
530 }
531 
532 /* Handle changes made to global settings in the options menu. */
process_options_menu(void)533 void process_options_menu(void)
534 {
535   switch (options_menu->check())
536     {
537     case MNID_OPENGL:
538 #ifndef NOOPENGL
539       if(use_gl != options_menu->isToggled(MNID_OPENGL))
540         {
541           use_gl = !use_gl;
542           st_video_setup();
543         }
544 #else
545       options_menu->get_item_by_id(MNID_OPENGL).toggled = false;
546 #endif
547       break;
548     case MNID_FULLSCREEN:
549       if(use_fullscreen != options_menu->isToggled(MNID_FULLSCREEN))
550         {
551           use_fullscreen = !use_fullscreen;
552           st_video_setup();
553         }
554       break;
555     case MNID_SOUND:
556       if(use_sound != options_menu->isToggled(MNID_SOUND))
557         use_sound = !use_sound;
558       break;
559     case MNID_MUSIC:
560       if(use_music != options_menu->isToggled(MNID_MUSIC))
561         {
562           use_music = !use_music;
563           music_manager->enable_music(use_music);
564         }
565       break;
566     case MNID_SHOWFPS:
567       if(show_fps != options_menu->isToggled(MNID_SHOWFPS))
568         show_fps = !show_fps;
569       break;
570     }
571 }
572 
st_general_setup(void)573 void st_general_setup(void)
574 {
575   /* Seed random number generator: */
576 
577   srand(SDL_GetTicks());
578 
579   /* Set icon image: */
580 
581   seticon();
582 
583   /* Unicode needed for input handling: */
584 
585   SDL_EnableUNICODE(1);
586 
587   /* Load global images: */
588 
589   black_text  = new Text(datadir + "/images/status/letters-black.png", TEXT_TEXT, 16,18);
590   gold_text   = new Text(datadir + "/images/status/letters-gold.png", TEXT_TEXT, 16,18);
591   silver_text = new Text(datadir + "/images/status/letters-silver.png", TEXT_TEXT, 16,18);
592   blue_text   = new Text(datadir + "/images/status/letters-blue.png", TEXT_TEXT, 16,18);
593   red_text    = new Text(datadir + "/images/status/letters-red.png", TEXT_TEXT, 16,18);
594   green_text  = new Text(datadir + "/images/status/letters-green.png", TEXT_TEXT, 16,18);
595   white_text  = new Text(datadir + "/images/status/letters-white.png", TEXT_TEXT, 16,18);
596   white_small_text = new Text(datadir + "/images/status/letters-white-small.png", TEXT_TEXT, 8,9);
597   white_big_text   = new Text(datadir + "/images/status/letters-white-big.png", TEXT_TEXT, 20,22);
598   yellow_nums = new Text(datadir + "/images/status/numbers.png", TEXT_NUM, 32,32);
599 
600   /* Load GUI/menu images: */
601   checkbox = new Surface(datadir + "/images/status/checkbox.png", USE_ALPHA);
602   checkbox_checked = new Surface(datadir + "/images/status/checkbox-checked.png", USE_ALPHA);
603   back = new Surface(datadir + "/images/status/back.png", USE_ALPHA);
604   arrow_left = new Surface(datadir + "/images/icons/left.png", USE_ALPHA);
605   arrow_right = new Surface(datadir + "/images/icons/right.png", USE_ALPHA);
606 
607   /* Load the mouse-cursor */
608   mouse_cursor = new MouseCursor( datadir + "/images/status/mousecursor.png",1);
609   MouseCursor::set_current(mouse_cursor);
610 
611 }
612 
st_general_free(void)613 void st_general_free(void)
614 {
615 
616   /* Free global images: */
617   delete black_text;
618   delete gold_text;
619   delete silver_text;
620   delete white_text;
621   delete blue_text;
622   delete red_text;
623   delete green_text;
624   delete white_small_text;
625   delete white_big_text;
626   delete yellow_nums;
627 
628   /* Free GUI/menu images: */
629   delete checkbox;
630   delete checkbox_checked;
631   delete back;
632   delete arrow_left;
633   delete arrow_right;
634 
635   /* Free mouse-cursor */
636   delete mouse_cursor;
637 
638   /* Free menus */
639   delete main_menu;
640   delete game_menu;
641   delete options_menu;
642   delete highscore_menu;
643   delete save_game_menu;
644   delete load_game_menu;
645 }
646 
st_video_setup(void)647 void st_video_setup(void)
648 {
649   /* Init SDL Video: */
650   if (SDL_Init(SDL_INIT_VIDEO) < 0)
651     {
652       fprintf(stderr,
653               "\nError: I could not initialize video!\n"
654               "The Simple DirectMedia error that occured was:\n"
655               "%s\n\n", SDL_GetError());
656       exit(1);
657     }
658 
659   /* Open display: */
660   if(use_gl)
661     st_video_setup_gl();
662   else
663     st_video_setup_sdl();
664 
665   Surface::reload_all();
666 
667   /* Set window manager stuff: */
668   SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux");
669 }
670 
st_video_setup_sdl(void)671 void st_video_setup_sdl(void)
672 {
673   if (use_fullscreen)
674     {
675       screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_FULLSCREEN ) ; /* | SDL_HWSURFACE); */
676       if (screen == NULL)
677         {
678           fprintf(stderr,
679                   "\nWarning: I could not set up fullscreen video for "
680                   "640x480 mode.\n"
681                   "The Simple DirectMedia error that occured was:\n"
682                   "%s\n\n", SDL_GetError());
683           use_fullscreen = false;
684         }
685     }
686   else
687     {
688       screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_HWSURFACE | SDL_DOUBLEBUF );
689 
690       if (screen == NULL)
691         {
692           fprintf(stderr,
693                   "\nError: I could not set up video for 640x480 mode.\n"
694                   "The Simple DirectMedia error that occured was:\n"
695                   "%s\n\n", SDL_GetError());
696           exit(1);
697         }
698     }
699 }
700 
st_video_setup_gl(void)701 void st_video_setup_gl(void)
702 {
703 #ifndef NOOPENGL
704 
705   SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
706   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
707   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
708   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
709   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
710 
711   if (use_fullscreen)
712     {
713       screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_FULLSCREEN | SDL_OPENGL) ; /* | SDL_HWSURFACE); */
714       if (screen == NULL)
715         {
716           fprintf(stderr,
717                   "\nWarning: I could not set up fullscreen video for "
718                   "640x480 mode.\n"
719                   "The Simple DirectMedia error that occured was:\n"
720                   "%s\n\n", SDL_GetError());
721           use_fullscreen = false;
722         }
723     }
724   else
725     {
726       screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, SDL_OPENGL);
727 
728       if (screen == NULL)
729         {
730           fprintf(stderr,
731                   "\nError: I could not set up video for 640x480 mode.\n"
732                   "The Simple DirectMedia error that occured was:\n"
733                   "%s\n\n", SDL_GetError());
734           exit(1);
735         }
736     }
737 
738   /*
739    * Set up OpenGL for 2D rendering.
740    */
741   glDisable(GL_DEPTH_TEST);
742   glDisable(GL_CULL_FACE);
743 
744   glViewport(0, 0, screen->w, screen->h);
745   glMatrixMode(GL_PROJECTION);
746   glLoadIdentity();
747   glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);
748 
749   glMatrixMode(GL_MODELVIEW);
750   glLoadIdentity();
751   glTranslatef(0.0f, 0.0f, 0.0f);
752 
753 #endif
754 
755 }
756 
st_joystick_setup(void)757 void st_joystick_setup(void)
758 {
759 
760   /* Init Joystick: */
761 
762   use_joystick = true;
763 
764   if (SDL_Init(SDL_INIT_JOYSTICK) < 0)
765     {
766       fprintf(stderr, "Warning: I could not initialize joystick!\n"
767               "The Simple DirectMedia error that occured was:\n"
768               "%s\n\n", SDL_GetError());
769 
770       use_joystick = false;
771     }
772   else
773     {
774       /* Open joystick: */
775       if (SDL_NumJoysticks() <= 0)
776         {
777           fprintf(stderr, "Warning: No joysticks are available.\n");
778 
779           use_joystick = false;
780         }
781       else
782         {
783           js = SDL_JoystickOpen(joystick_num);
784 
785           if (js == NULL)
786             {
787               fprintf(stderr, "Warning: Could not open joystick %d.\n"
788                       "The Simple DirectMedia error that occured was:\n"
789                       "%s\n\n", joystick_num, SDL_GetError());
790 
791               use_joystick = false;
792             }
793           else
794             {
795               if (SDL_JoystickNumAxes(js) < 2)
796                 {
797                   fprintf(stderr,
798                           "Warning: Joystick does not have enough axes!\n");
799 
800                   use_joystick = false;
801                 }
802               else
803                 {
804                   if (SDL_JoystickNumButtons(js) < 2)
805                     {
806                       fprintf(stderr,
807                               "Warning: "
808                               "Joystick does not have enough buttons!\n");
809 
810                       use_joystick = false;
811                     }
812                 }
813             }
814         }
815     }
816 }
817 
st_audio_setup(void)818 void st_audio_setup(void)
819 {
820 
821   /* Init SDL Audio silently even if --disable-sound : */
822 
823   if (audio_device)
824     {
825       if (SDL_Init(SDL_INIT_AUDIO) < 0)
826         {
827           /* only print out message if sound or music
828              was not disabled at command-line
829            */
830           if (use_sound || use_music)
831             {
832               fprintf(stderr,
833                       "\nWarning: I could not initialize audio!\n"
834                       "The Simple DirectMedia error that occured was:\n"
835                       "%s\n\n", SDL_GetError());
836             }
837           /* keep the programming logic the same :-)
838              because in this case, use_sound & use_music' values are ignored
839              when there's no available audio device
840           */
841           use_sound = false;
842           use_music = false;
843           audio_device = false;
844         }
845     }
846 
847 
848   /* Open sound silently regarless the value of "use_sound": */
849 
850   if (audio_device)
851     {
852       if (open_audio(44100, AUDIO_S16, 2, 2048) < 0)
853         {
854           /* only print out message if sound or music
855              was not disabled at command-line
856            */
857           if (use_sound || use_music)
858             {
859               fprintf(stderr,
860                       "\nWarning: I could not set up audio for 44100 Hz "
861                       "16-bit stereo.\n"
862                       "The Simple DirectMedia error that occured was:\n"
863                       "%s\n\n", SDL_GetError());
864             }
865           use_sound = false;
866           use_music = false;
867           audio_device = false;
868         }
869     }
870 
871 }
872 
873 
874 /* --- SHUTDOWN --- */
875 
st_shutdown(void)876 void st_shutdown(void)
877 {
878   close_audio();
879   SDL_Quit();
880   saveconfig();
881 }
882 
883 /* --- ABORT! --- */
884 
st_abort(const std::string & reason,const std::string & details)885 void st_abort(const std::string& reason, const std::string& details)
886 {
887   fprintf(stderr, "\nError: %s\n%s\n\n", reason.c_str(), details.c_str());
888   st_shutdown();
889   abort();
890 }
891 
892 /* Set Icon (private) */
893 
seticon(void)894 void seticon(void)
895 {
896 //  int masklen;
897 //  Uint8 * mask;
898   SDL_Surface * icon;
899 
900 
901   /* Load icon into a surface: */
902 
903   icon = IMG_Load((datadir + "/images/icon.xpm").c_str());
904   if (icon == NULL)
905     {
906       fprintf(stderr,
907               "\nError: I could not load the icon image: %s%s\n"
908               "The Simple DirectMedia error that occured was:\n"
909               "%s\n\n", datadir.c_str(), "/images/icon.xpm", SDL_GetError());
910       exit(1);
911     }
912 
913 
914   /* Create mask: */
915 /*
916   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
917   mask = (Uint8*) malloc(masklen * sizeof(Uint8));
918   memset(mask, 0xFF, masklen);
919 */
920 
921   /* Set icon: */
922 
923   SDL_WM_SetIcon(icon, NULL);//mask);
924 
925 
926   /* Free icon surface & mask: */
927 
928 //  free(mask);
929   SDL_FreeSurface(icon);
930 }
931 
932 
933 /* Parse command-line arguments: */
934 
parseargs(int argc,char * argv[])935 void parseargs(int argc, char * argv[])
936 {
937   int i;
938 
939   loadconfig();
940 
941   /* Parse arguments: */
942 
943   for (i = 1; i < argc; i++)
944     {
945       if (strcmp(argv[i], "--fullscreen") == 0 ||
946           strcmp(argv[i], "-f") == 0)
947         {
948           /* Use full screen: */
949 
950           use_fullscreen = true;
951         }
952       else if (strcmp(argv[i], "--joystick") == 0 || strcmp(argv[i], "-j") == 0)
953         {
954           assert(i+1 < argc);
955           joystick_num = atoi(argv[++i]);
956         }
957       else if (strcmp(argv[i], "--joymap") == 0)
958         {
959           assert(i+1 < argc);
960           if (sscanf(argv[++i],
961                      "%d:%d:%d:%d:%d",
962                      &joystick_keymap.x_axis,
963                      &joystick_keymap.y_axis,
964                      &joystick_keymap.a_button,
965                      &joystick_keymap.b_button,
966                      &joystick_keymap.start_button) != 5)
967             {
968               puts("Warning: Invalid or incomplete joymap, should be: 'XAXIS:YAXIS:A:B:START'");
969             }
970           else
971             {
972               std::cout << "Using new joymap:\n"
973                         << "  X-Axis:       " << joystick_keymap.x_axis << "\n"
974                         << "  Y-Axis:       " << joystick_keymap.y_axis << "\n"
975                         << "  A-Button:     " << joystick_keymap.a_button << "\n"
976                         << "  B-Button:     " << joystick_keymap.b_button << "\n"
977                         << "  Start-Button: " << joystick_keymap.start_button << std::endl;
978             }
979         }
980       else if (strcmp(argv[i], "--leveleditor") == 0)
981         {
982           launch_leveleditor_mode = true;
983         }
984       else if (strcmp(argv[i], "--datadir") == 0
985                || strcmp(argv[i], "-d") == 0 )
986         {
987           assert(i+1 < argc);
988           datadir = argv[++i];
989         }
990       else if (strcmp(argv[i], "--show-fps") == 0)
991         {
992           /* Use full screen: */
993 
994           show_fps = true;
995         }
996       else if (strcmp(argv[i], "--opengl") == 0 ||
997                strcmp(argv[i], "-gl") == 0)
998         {
999 #ifndef NOOPENGL
1000           /* Use OpengGL: */
1001 
1002           use_gl = true;
1003 #endif
1004         }
1005       else if (strcmp(argv[i], "--sdl") == 0)
1006           {
1007             use_gl = false;
1008           }
1009       else if (strcmp(argv[i], "--usage") == 0)
1010         {
1011           /* Show usage: */
1012 
1013           usage(argv[0], 0);
1014         }
1015       else if (strcmp(argv[i], "--version") == 0)
1016         {
1017           /* Show version: */
1018           printf("SuperTux " VERSION "\n");
1019           exit(0);
1020         }
1021       else if (strcmp(argv[i], "--disable-sound") == 0)
1022         {
1023           /* Disable the compiled in sound feature */
1024           printf("Sounds disabled \n");
1025           use_sound = false;
1026           audio_device = false;
1027         }
1028       else if (strcmp(argv[i], "--disable-music") == 0)
1029         {
1030           /* Disable the compiled in sound feature */
1031           printf("Music disabled \n");
1032           use_music = false;
1033         }
1034       else if (strcmp(argv[i], "--debug-mode") == 0)
1035         {
1036           /* Enable the debug-mode */
1037           debug_mode = true;
1038 
1039         }
1040       else if (strcmp(argv[i], "--help") == 0)
1041         {     /* Show help: */
1042           puts("Super Tux " VERSION "\n"
1043                "  Please see the file \"README.txt\" for more details.\n");
1044           printf("Usage: %s [OPTIONS] FILENAME\n\n", argv[0]);
1045           puts("Display Options:\n"
1046                "  --fullscreen        Run in fullscreen mode.\n"
1047                "  --opengl            If opengl support was compiled in, this will enable\n"
1048                "                      the OpenGL mode.\n"
1049                "  --sdl               Use non-opengl renderer\n"
1050                "\n"
1051                "Sound Options:\n"
1052                "  --disable-sound     If sound support was compiled in,  this will\n"
1053                "                      disable sound for this session of the game.\n"
1054                "  --disable-music     Like above, but this will disable music.\n"
1055                "\n"
1056                "Misc Options:\n"
1057                "  -j, --joystick NUM  Use joystick NUM (default: 0)\n"
1058                "  --joymap XAXIS:YAXIS:A:B:START\n"
1059                "  --leveleditor       Opens the leveleditor in a file. (Only works when a file is provided.)\n"
1060                "                      Define how joystick buttons and axis should be mapped\n"
1061                "  -d, --datadir DIR   Load Game data from DIR (default: automatic)\n"
1062                "  --debug-mode        Enables the debug-mode, which is useful for developers.\n"
1063                "  --help              Display a help message summarizing command-line\n"
1064                "                      options, license and game controls.\n"
1065                "  --usage             Display a brief message summarizing command-line options.\n"
1066                "  --version           Display the version of SuperTux you're running.\n\n"
1067                );
1068           exit(0);
1069         }
1070       else if (argv[i][0] != '-')
1071         {
1072           level_startup_file = argv[i];
1073         }
1074       else
1075         {
1076           /* Unknown - complain! */
1077 
1078           usage(argv[0], 1);
1079         }
1080     }
1081 }
1082 
1083 
1084 /* Display usage: */
1085 
usage(char * prog,int ret)1086 void usage(char * prog, int ret)
1087 {
1088   FILE * fi;
1089 
1090 
1091   /* Determine which stream to write to: */
1092 
1093   if (ret == 0)
1094     fi = stdout;
1095   else
1096     fi = stderr;
1097 
1098 
1099   /* Display the usage message: */
1100 
1101   fprintf(fi, "Usage: %s [--fullscreen] [--opengl] [--disable-sound] [--disable-music] [--debug-mode] | [--usage | --help | --version] FILENAME\n",
1102           prog);
1103 
1104 
1105   /* Quit! */
1106 
1107   exit(ret);
1108 }
1109 
1110