Separate KeyboardConfig out into a separate class that can be stored in the global...
[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   fullscreen_size(),
33   fullscreen_refresh_rate(),
34   window_size(),
35   aspect_size(),
36   use_fullscreen(),
37   video(),
38   show_fps(),
39   sound_enabled(),
40   music_enabled(),
41   console_enabled(),
42   start_level(),
43   enable_script_debugger(),
44   start_demo(),
45   record_demo()
46 {
47 }
48
49 CommandLineArguments::~CommandLineArguments()
50 {
51 }
52
53 void
54 CommandLineArguments::print_datadir()
55 {
56   // Print the datadir searchpath to stdout, one path per
57   // line. Then exit. Intended for use by the supertux-editor.
58   char **sp;
59   size_t sp_index;
60   sp = PHYSFS_getSearchPath();
61   if (sp)
62     for (sp_index = 0; sp[sp_index]; sp_index++)
63       std::cout << sp[sp_index] << std::endl;
64   PHYSFS_freeList(sp);
65 }
66
67 void
68 CommandLineArguments::print_help(const char* argv0)
69 {
70   std::string default_user_data_dir =
71       std::string(PHYSFS_getUserDir()) + WRITEDIR_NAME;
72
73   std::cerr << boost::format(_(
74                  "\n"
75                  "Usage: %s [OPTIONS] [LEVELFILE]\n\n"
76                  "CommandLineArguments:\n"
77                  "  --verbose                    Print verbose messages\n"
78                  "  --debug                      Print extra verbose messages\n"
79                  "  -f, --fullscreen             Run in fullscreen mode\n"
80                  "  -w, --window                 Run in window mode\n"
81                  "  -g, --geometry WIDTHxHEIGHT  Run SuperTux in given resolution\n"
82                  "  -a, --aspect WIDTH:HEIGHT    Run SuperTux with given aspect ratio\n"
83                  "  -d, --default                Reset video settings to default values\n"
84                  "  --renderer RENDERER          Use sdl, opengl, or auto to render\n"
85                  "  --disable-sound              Disable sound effects\n"
86                  "  --disable-music              Disable music\n"
87                  "  -h, --help                   Show this help message and quit\n"
88                  "  -v, --version                Show SuperTux version and quit\n"
89                  "  --console                    Enable ingame scripting console\n"
90                  "  --noconsole                  Disable ingame scripting console\n"
91                  "  --show-fps                   Display framerate in levels\n"
92                  "  --no-show-fps                Do not display framerate in levels\n"
93                  "  --record-demo FILE LEVEL     Record a demo to FILE\n"
94                  "  --play-demo FILE LEVEL       Play a recorded demo\n"
95                  "  -s, --debug-scripts          Enable script debugger.\n"
96                  "  --print-datadir              Print supertux's primary data directory.\n"
97                  "\n"
98                  "Environment variables:\n"
99                  "  SUPERTUX2_USER_DIR           Directory for user data (savegames, etc.);\n"
100                  "                               default %s\n"
101                  "\n"
102                  ))
103             % argv0 % default_user_data_dir
104             << std::flush;
105 }
106
107 void
108 CommandLineArguments::print_version()
109 {
110   std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
111 }
112
113 void
114 CommandLineArguments::parse_args(int argc, char** argv)
115 {
116   for(int i = 1; i < argc; ++i)
117   {
118     std::string arg = argv[i];
119
120     if (arg == "--version" || arg == "-v")
121     {
122       m_action = PRINT_VERSION;
123
124     }
125     else if (arg == "--help" || arg == "-h")
126     {
127       m_action = PRINT_HELP;
128     }
129     else if (arg == "--print-datadir")
130     {
131       m_action = PRINT_DATADIR;
132     }
133     else if (arg == "--debug")
134     {
135       m_log_level = LOG_DEBUG;
136     }
137     else if (arg == "--verbose")
138     {
139       if (m_log_level < LOG_INFO)
140       {
141         m_log_level = LOG_INFO;
142       }
143     }
144     else if (arg == "--fullscreen" || arg == "-f")
145     {
146       use_fullscreen = true;
147     }
148     else if (arg == "--default" || arg == "-d")
149     {
150       use_fullscreen = false;
151
152       window_size = Size(800, 600);
153       fullscreen_size = Size(800, 600);
154       fullscreen_refresh_rate = 0;
155       aspect_size = Size(0, 0);  // auto detect
156     }
157     else if (arg == "--window" || arg == "-w")
158     {
159       use_fullscreen = false;
160     }
161     else if (arg == "--geometry" || arg == "-g")
162     {
163       i += 1;
164       if (i >= argc)
165       {
166         throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
167       }
168       else
169       {
170         int width, height;
171         if (sscanf(argv[i], "%dx%d", &width, &height) != 2)
172         {
173           throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
174         }
175         else
176         {
177           window_size     = Size(width, height);
178           fullscreen_size = Size(width, height);
179           fullscreen_refresh_rate = 0;
180         }
181       }
182     }
183     else if (arg == "--aspect" || arg == "-a")
184     {
185       i += 1;
186       if (i >= argc)
187       {
188         throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
189       }
190       else
191       {
192         int aspect_width  = 0;
193         int aspect_height = 0;
194         if (strcmp(argv[i], "auto") == 0)
195         {
196           aspect_width  = 0;
197           aspect_height = 0;
198         }
199         else if (sscanf(argv[i], "%d:%d", &aspect_width, &aspect_height) != 2)
200         {
201           throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
202         }
203         else
204         {
205           float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
206
207           // use aspect ratio to calculate logical resolution
208           if (aspect_ratio > 1) {
209             aspect_size = Size(static_cast<int>(600 * aspect_ratio + 0.5),
210                                          600);
211           } else {
212             aspect_size = Size(600,
213                                          static_cast<int>(600 * 1/aspect_ratio + 0.5));
214           }
215         }
216       }
217     }
218     else if (arg == "--renderer")
219     {
220       i += 1;
221       if (i >= argc)
222       {
223         throw std::runtime_error("Need to specify a renderer for renderer argument");
224       }
225       else
226       {
227         video = VideoSystem::get_video_system(argv[i]);
228       }
229     }
230     else if (arg == "--show-fps")
231     {
232       show_fps = true;
233     }
234     else if (arg == "--no-show-fps")
235     {
236       show_fps = false;
237     }
238     else if (arg == "--console")
239     {
240       console_enabled = true;
241     }
242     else if (arg == "--noconsole")
243     {
244       console_enabled = false;
245     }
246     else if (arg == "--disable-sound" || arg == "--disable-sfx")
247     {
248       sound_enabled = false;
249     }
250     else if (arg == "--disable-music")
251     {
252       music_enabled = false;
253     }
254     else if (arg == "--play-demo")
255     {
256       if (i+1 >= argc)
257       {
258         throw std::runtime_error("Need to specify a demo filename");
259       }
260       else
261       {
262         start_demo = argv[++i];
263       }
264     }
265     else if (arg == "--record-demo")
266     {
267       if (i+1 >= argc)
268       {
269         throw std::runtime_error("Need to specify a demo filename");
270       }
271       else
272       {
273         record_demo = argv[++i];
274       }
275     }
276     else if (arg == "--debug-scripts" || arg == "-s")
277     {
278       enable_script_debugger = true;
279     }
280     else if (arg[0] != '-')
281     {
282       start_level = arg;
283     }
284     else
285     {
286       throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
287     }
288   }
289 }
290
291 void
292 CommandLineArguments::merge_into(Config& config)
293 {
294 #define merge_option(x) if (x) { config.x = *x; }
295
296   merge_option(fullscreen_size);
297   merge_option(fullscreen_refresh_rate);
298   merge_option(window_size);
299   merge_option(aspect_size);
300   merge_option(use_fullscreen);
301   merge_option(video);
302   merge_option(show_fps);
303   merge_option(sound_enabled);
304   merge_option(music_enabled);
305   merge_option(console_enabled);
306   merge_option(start_level);
307   merge_option(enable_script_debugger);
308   merge_option(start_demo);
309   merge_option(record_demo);
310
311 #undef merge_option
312 }
313
314 /* EOF */