1 /*
2  * GNUjump
3  * =======
4  *
5  * Copyright (C) 2005-2008, Juan Pedro Bolivar Puente
6  *
7  * GNUjump is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GNUjump is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <locale.h>
22 
23 #include "gnujump.h"
24 
25 #include "setup.h"
26 #include "game.h"
27 #include "menu.h"
28 #include "records.h"
29 #include "replay.h"
30 
31 SDL_Surface *screen = NULL;
32 L_gblOptions gblOps;
33 
34 void displayHelp ();
35 
36 void displayInfo ();
37 
38 int parseArgs (int argc, char *argv[], char **replay);
39 
40 int
main(int argc,char * argv[])41 main (int argc, char *argv[])
42 {
43   data_t gfxdata;
44   char *cfgFile;
45   char *hscFile;
46   char *homeDir;
47   char *replay = NULL;
48 
49   /* Init gettext */
50   setlocale (LC_ALL, "");
51   textdomain (PACKAGE);
52   bindtextdomain (PACKAGE, LOCALEDIR);
53 
54   /* Get the config file name */
55 #ifndef WIN32
56   homeDir = getenv ("HOME");
57   cfgFile =
58     malloc (sizeof (char) *
59 	    (strlen (homeDir) + strlen (CONFDIR) + strlen (CFGFILE) + 3));
60   hscFile =
61     malloc (sizeof (char) *
62 	    (strlen (homeDir) + strlen (CONFDIR) + strlen (HSCFILE) + 3));
63   sprintf (cfgFile, "%s/" CONFDIR "/", homeDir);
64   sprintf (hscFile, "%s/" CONFDIR "/", homeDir);
65 #else
66   cfgFile = malloc (sizeof (char) * strlen (CFGFILE) + 1);
67   hscFile = malloc (sizeof (char) * strlen (HSCFILE) + 1);
68   cfgFile[0] = hscFile[0] = '\0';
69 #endif
70   strcat (cfgFile, CFGFILE);
71   strcat (hscFile, HSCFILE);
72 
73   if (!loadConfigFile (cfgFile))
74     {
75       initGblOps ();		/* Set default options */
76     }
77   if (!loadRecords (hscFile, gblOps.records))
78     {
79       defaultRecords (gblOps.records);
80     }
81 
82   /* Parse args */
83   if (parseArgs (argc, argv, &replay))
84     {
85       return 1;
86     }
87 
88   EngineInit ();
89 
90   gfxdata.soundloaded = FALSE;
91   if (!loadGraphics (&gfxdata, gblOps.dataDir))
92     return 1;
93 
94   //gblOps.fps = 20;
95   if (replay != NULL)
96     loadReplay (&gfxdata, replay);
97   else
98     mainMenu (&gfxdata);
99 
100   writeConfigFile (cfgFile);
101   writeRecords (hscFile, gblOps.records);
102 
103   /* Free some things */
104   freeGraphics (&gfxdata);
105   freeSounds (&gfxdata);
106 
107   cleanGblOps ();
108 
109   Mix_CloseAudio ();
110 
111   free (cfgFile);
112   free (hscFile);
113 
114   printf (_("\nHave a nice day!\n\n"));
115 
116   return 0;
117 }
118 
119 void
displayHelp()120 displayHelp ()
121 {
122   printf (_("GNUjump, an xjump clone. By Juan Pedro Bolivar Puente.\n\
123 This software can be redistributed and modified under the terms of the GPL.\n\n\
124 Usage: sdljump [REPLAY_FILE] [OPTIONS] \n\
125 Availible options:\n\
126 -b <int>  --bpp <int>     Sets the screen bitdepth to <int>. \n\
127 -f        --fullscreen    Force fullscreen mode.\n\
128 -s        --software      Force software rendering.\n\
129 -o        --opengl        Force OpenGL rendering.\n\
130 -a        --antialias     Force antialiasing for rotating sprites. \n\
131 -n        --no-aa         Disables antialiasing for rotating sprites. \n\
132 -?        --help          Displays this help screen.\n\
133 \nExample: sdljump myRep.rep -o -f\n"));
134 }
135 
136 void
displayInfo()137 displayInfo ()
138 {
139   printf
140     ("\n*********************************************************************"
141      "\n*                            GNUjump                                *"
142      "\n*********************************************************************"
143      "\nCopyright (C) 2005, Juan Pedro Bolivar Puente\n");
144 
145   printf ("\nVERSION: %s", VERSION);
146 
147   printf
148     ("\n\n GNUjump is free software; you can redistribute it and/or modify\n"
149      "it under the terms of the GNU General Public License as published by\n"
150      "the Free Software Foundation; either version 3 of the License, or\n"
151      "(at your option) any later version.\n\n"
152      "SDLjump is distributed in the hope that it will be useful,\n"
153      "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
154      "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
155      "GNU General Public License for more details.\n\n"
156      "You should have received a copy of the GNU General Public License\n"
157      "along with GNUjump; if not, write to the Free Software\n"
158      "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n");
159 }
160 
161 int
parseArgs(int argc,char * argv[],char ** replay)162 parseArgs (int argc, char *argv[], char **replay)
163 {
164   int i;
165   for (i = 1; i < argc; i++)
166     {
167       if (argv[i][0] == '-')
168 	{
169 	  if (argv[i][1] != '-')
170 	    {			/* Short arg */
171 	      switch (argv[i][1])
172 		{
173 		case 'w':
174 		  if ((i + 1) < argc)
175 		    {
176 		      gblOps.w = atoi (argv[i + 1]);
177 		      i++;
178 		    }
179 		  break;
180 		case 'h':
181 		  if ((i + 1) < argc)
182 		    {
183 		      gblOps.h = atoi (argv[i + 1]);
184 		      i++;
185 		    }
186 		  break;
187 		case 'b':
188 		  if ((i + 1) < argc)
189 		    {
190 		      gblOps.bpp = atoi (argv[i + 1]);
191 		      i++;
192 		    }
193 		  break;
194 		case 'f':
195 		  gblOps.fullsc = TRUE;
196 		  break;
197 		case 'o':
198 		  gblOps.useGL = TRUE;
199 		  break;
200 		case 's':
201 		  gblOps.useGL = FALSE;
202 		  break;
203 		case 'a':
204 		  gblOps.aa = 1;
205 		  break;
206 		case 'n':
207 		  gblOps.aa = 0;
208 		  break;
209 		case '?':
210 		  displayHelp ();
211 		  return 1;
212 		  break;
213 		}
214 	    }
215 	  else
216 	    {			/* Long arg */
217 	      if (strcmp (argv[i], "--width") == 0)
218 		{
219 		  if ((i + 1) < argc)
220 		    {
221 		      gblOps.w = atoi (argv[i + 1]);
222 		      i++;
223 		    }
224 		}
225 	      if (strcmp (argv[i], "--height") == 0)
226 		{
227 		  if ((i + 1) < argc)
228 		    {
229 		      gblOps.h = atoi (argv[i + 1]);
230 		      i++;
231 		    }
232 		}
233 	      if (strcmp (argv[i], "--bpp") == 0)
234 		{
235 		  if ((i + 1) < argc)
236 		    {
237 		      gblOps.bpp = atoi (argv[i + 1]);
238 		      i++;
239 		    }
240 		}
241 	      if (strcmp (argv[i], "--fullscreen") == 0)
242 		{
243 		  gblOps.fullsc = TRUE;
244 		}
245 	      if (strcmp (argv[i], "--opengl") == 0)
246 		{
247 		  gblOps.useGL = TRUE;
248 		}
249 	      if (strcmp (argv[i], "--software") == 0)
250 		{
251 		  gblOps.useGL = FALSE;
252 		}
253 	      if (strcmp (argv[i], "--antialias") == 0)
254 		{
255 		  gblOps.aa = 1;
256 		}
257 	      if (strcmp (argv[i], "--no-aa") == 0)
258 		{
259 		  gblOps.aa = 0;
260 		}
261 	      if (strcmp (argv[i], "--help") == 0)
262 		{
263 		  displayHelp ();
264 		  return 1;
265 		}
266 	    }
267 	}
268       else
269 	{			/* Replay */
270 	  if (*replay != NULL)
271 	    free (*replay);
272 	  *replay = malloc ((strlen (argv[i]) + 1) * sizeof (char));
273 	  strcpy (*replay, argv[i]);
274 	}
275     }
276 
277   return FALSE;
278 }
279