Fixed mixup between Win32's "supertux2" and Linux/MacOSX ".supertux2" directory name
[supertux.git] / src / supertux / command_line_arguments.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/command_line_arguments.hpp"
18
19 #include <boost/format.hpp>
20 #include <iostream>
21 #include <physfs.h>
22 #include <stdexcept>
23
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/main.hpp"
26 #include "util/gettext.hpp"
27 #include "version.h"
28
29 CommandLineArguments::CommandLineArguments() :
30   m_action(NO_ACTION),
31   m_log_level(LOG_WARNING),
32   datadir(),
33   userdir(),
34   fullscreen_size(),
35   fullscreen_refresh_rate(),
36   window_size(),
37   aspect_size(),
38   use_fullscreen(),
39   video(),
40   show_fps(),
41   sound_enabled(),
42   music_enabled(),
43   console_enabled(),
44   start_level(),
45   enable_script_debugger(),
46   start_demo(),
47   record_demo()
48 {
49 }
50
51 CommandLineArguments::~CommandLineArguments()
52 {
53 }
54
55 void
56 CommandLineArguments::print_datadir()
57 {
58   // Print the datadir searchpath to stdout, one path per
59   // line. Then exit. Intended for use by the supertux-editor.
60   char **sp;
61   size_t sp_index;
62   sp = PHYSFS_getSearchPath();
63   if (sp)
64     for (sp_index = 0; sp[sp_index]; sp_index++)
65       std::cout << sp[sp_index] << std::endl;
66   PHYSFS_freeList(sp);
67 }
68
69 void
70 CommandLineArguments::print_help(const char* arg0)
71 {
72   std::cerr << boost::format(_(
73                  "Usage: %s [OPTIONS] [LEVELFILE]\n"
74                  "\n"
75                  "General Options:\n"
76                  "  -h, --help                   Show this help message and quit\n"
77                  "  -v, --version                Show SuperTux version and quit\n"
78                  "  --verbose                    Print verbose messages\n"
79                  "  --debug                      Print extra verbose messages\n"
80                  "  --print-datadir              Print supertux's primary data directory.\n"
81                  "\n"
82                  "Video Options:\n"
83                  "  -f, --fullscreen             Run in fullscreen mode\n"
84                  "  -w, --window                 Run in window mode\n"
85                  "  -g, --geometry WIDTHxHEIGHT  Run SuperTux in given resolution\n"
86                  "  -a, --aspect WIDTH:HEIGHT    Run SuperTux with given aspect ratio\n"
87                  "  -d, --default                Reset video settings to default values\n"
88                  "  --renderer RENDERER          Use sdl, opengl, or auto to render\n"
89                  "\n"
90                  "Audio Options:\n"
91                  "  --disable-sound              Disable sound effects\n"
92                  "  --disable-music              Disable music\n"
93                  "\n"
94                  "Game Options:\n"
95                  "  --console                    Enable ingame scripting console\n"
96                  "  --noconsole                  Disable ingame scripting console\n"
97                  "  --show-fps                   Display framerate in levels\n"
98                  "  --no-show-fps                Do not display framerate in levels\n"
99                  "  -s, --debug-scripts          Enable script debugger.\n"
100                  "\n"
101                  "Demo Recording Options:\n"
102                  "  --record-demo FILE LEVEL     Record a demo to FILE\n"
103                  "  --play-demo FILE LEVEL       Play a recorded demo\n"
104                  "\n"
105                  "Directory Options:\n"
106                  "  --datadir DIR                Set the directory for the games datafiles\n"
107                  "  --userdir DIR                Set the directory for user data (savegames, etc.)\n"
108                  "\n"
109                  "Environment variables:\n"
110                  "  SUPERTUX2_USER_DIR           Directory for user data (savegames, etc.)\n"
111                  "  SUPERTUX2_DATA_DIR           Directory for the games datafiles\n"
112                  "\n"
113                  ))
114             % arg0
115             << std::flush;
116 }
117
118 void
119 CommandLineArguments::print_version()
120 {
121   std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
122 }
123
124 void
125 CommandLineArguments::parse_args(int argc, char** argv)
126 {
127   for(int i = 1; i < argc; ++i)
128   {
129     std::string arg = argv[i];
130
131     if (arg == "--version" || arg == "-v")
132     {
133       m_action = PRINT_VERSION;
134
135     }
136     else if (arg == "--help" || arg == "-h")
137     {
138       m_action = PRINT_HELP;
139     }
140     else if (arg == "--print-datadir")
141     {
142       m_action = PRINT_DATADIR;
143     }
144     else if (arg == "--debug")
145     {
146       m_log_level = LOG_DEBUG;
147     }
148     else if (arg == "--verbose")
149     {
150       if (m_log_level < LOG_INFO)
151       {
152         m_log_level = LOG_INFO;
153       }
154     }
155     else if (arg == "--datadir")
156     {
157       if (i+1 >= argc)
158       {
159         throw std::runtime_error("Need to specify a directory for --datadir");
160       }
161       else
162       {
163         datadir = argv[++i];
164       }
165     }
166     else if (arg == "--userdir")
167     {
168       if (i+1 >= argc)
169       {
170         throw std::runtime_error("Need to specify a directory for --userdir");
171       }
172       else
173       {
174         userdir = argv[++i];
175       }
176     }
177     else if (arg == "--fullscreen" || arg == "-f")
178     {
179       use_fullscreen = true;
180     }
181     else if (arg == "--default" || arg == "-d")
182     {
183       use_fullscreen = false;
184
185       window_size = Size(800, 600);
186       fullscreen_size = Size(800, 600);
187       fullscreen_refresh_rate = 0;
188       aspect_size = Size(0, 0);  // auto detect
189     }
190     else if (arg == "--window" || arg == "-w")
191     {
192       use_fullscreen = false;
193     }
194     else if (arg == "--geometry" || arg == "-g")
195     {
196       i += 1;
197       if (i >= argc)
198       {
199         throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
200       }
201       else
202       {
203         int width, height;
204         if (sscanf(argv[i], "%dx%d", &width, &height) != 2)
205         {
206           throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
207         }
208         else
209         {
210           window_size     = Size(width, height);
211           fullscreen_size = Size(width, height);
212           fullscreen_refresh_rate = 0;
213         }
214       }
215     }
216     else if (arg == "--aspect" || arg == "-a")
217     {
218       i += 1;
219       if (i >= argc)
220       {
221         throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
222       }
223       else
224       {
225         int aspect_width  = 0;
226         int aspect_height = 0;
227         if (strcmp(argv[i], "auto") == 0)
228         {
229           aspect_width  = 0;
230           aspect_height = 0;
231         }
232         else if (sscanf(argv[i], "%d:%d", &aspect_width, &aspect_height) != 2)
233         {
234           throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
235         }
236         else
237         {
238           float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
239
240           // use aspect ratio to calculate logical resolution
241           if (aspect_ratio > 1) {
242             aspect_size = Size(static_cast<int>(600 * aspect_ratio + 0.5),
243                                          600);
244           } else {
245             aspect_size = Size(600,
246                                          static_cast<int>(600 * 1/aspect_ratio + 0.5));
247           }
248         }
249       }
250     }
251     else if (arg == "--renderer")
252     {
253       i += 1;
254       if (i >= argc)
255       {
256         throw std::runtime_error("Need to specify a renderer for renderer argument");
257       }
258       else
259       {
260         video = VideoSystem::get_video_system(argv[i]);
261       }
262     }
263     else if (arg == "--show-fps")
264     {
265       show_fps = true;
266     }
267     else if (arg == "--no-show-fps")
268     {
269       show_fps = false;
270     }
271     else if (arg == "--console")
272     {
273       console_enabled = true;
274     }
275     else if (arg == "--noconsole")
276     {
277       console_enabled = false;
278     }
279     else if (arg == "--disable-sound" || arg == "--disable-sfx")
280     {
281       sound_enabled = false;
282     }
283     else if (arg == "--disable-music")
284     {
285       music_enabled = false;
286     }
287     else if (arg == "--play-demo")
288     {
289       if (i+1 >= argc)
290       {
291         throw std::runtime_error("Need to specify a demo filename");
292       }
293       else
294       {
295         start_demo = argv[++i];
296       }
297     }
298     else if (arg == "--record-demo")
299     {
300       if (i+1 >= argc)
301       {
302         throw std::runtime_error("Need to specify a demo filename");
303       }
304       else
305       {
306         record_demo = argv[++i];
307       }
308     }
309     else if (arg == "--debug-scripts" || arg == "-s")
310     {
311       enable_script_debugger = true;
312     }
313     else if (arg[0] != '-')
314     {
315       start_level = arg;
316     }
317     else
318     {
319       throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
320     }
321   }
322 }
323
324 void
325 CommandLineArguments::merge_into(Config& config)
326 {
327 #define merge_option(x) if (x) { config.x = *x; }
328
329   merge_option(fullscreen_size);
330   merge_option(fullscreen_refresh_rate);
331   merge_option(window_size);
332   merge_option(aspect_size);
333   merge_option(use_fullscreen);
334   merge_option(video);
335   merge_option(show_fps);
336   merge_option(sound_enabled);
337   merge_option(music_enabled);
338   merge_option(console_enabled);
339   merge_option(start_level);
340   merge_option(enable_script_debugger);
341   merge_option(start_demo);
342   merge_option(record_demo);
343
344 #undef merge_option
345 }
346
347 /* EOF */