2 // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
17 #include "supertux/command_line_arguments.hpp"
19 #include <boost/format.hpp>
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/main.hpp"
26 #include "util/gettext.hpp"
29 CommandLineArguments::CommandLineArguments() :
31 m_log_level(LOG_WARNING),
35 fullscreen_refresh_rate(),
45 enable_script_debugger(),
51 CommandLineArguments::~CommandLineArguments()
56 CommandLineArguments::print_datadir()
58 // Print the datadir searchpath to stdout, one path per
59 // line. Then exit. Intended for use by the supertux-editor.
62 sp = PHYSFS_getSearchPath();
64 for (sp_index = 0; sp[sp_index]; sp_index++)
65 std::cout << sp[sp_index] << std::endl;
70 CommandLineArguments::print_help(const char* arg0)
72 std::cerr << boost::format(_(
73 "Usage: %s [OPTIONS] [LEVELFILE]\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"
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"
91 " --disable-sound Disable sound effects\n"
92 " --disable-music Disable music\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"
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"
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"
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"
119 CommandLineArguments::print_version()
121 std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
125 CommandLineArguments::parse_args(int argc, char** argv)
127 for(int i = 1; i < argc; ++i)
129 std::string arg = argv[i];
131 if (arg == "--version" || arg == "-v")
133 m_action = PRINT_VERSION;
136 else if (arg == "--help" || arg == "-h")
138 m_action = PRINT_HELP;
140 else if (arg == "--print-datadir")
142 m_action = PRINT_DATADIR;
144 else if (arg == "--debug")
146 m_log_level = LOG_DEBUG;
148 else if (arg == "--verbose")
150 if (m_log_level < LOG_INFO)
152 m_log_level = LOG_INFO;
155 else if (arg == "--datadir")
159 throw std::runtime_error("Need to specify a directory for --datadir");
166 else if (arg == "--userdir")
170 throw std::runtime_error("Need to specify a directory for --userdir");
177 else if (arg == "--fullscreen" || arg == "-f")
179 use_fullscreen = true;
181 else if (arg == "--default" || arg == "-d")
183 use_fullscreen = false;
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
190 else if (arg == "--window" || arg == "-w")
192 use_fullscreen = false;
194 else if (arg == "--geometry" || arg == "-g")
199 throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
204 if (sscanf(argv[i], "%dx%d", &width, &height) != 2)
206 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
210 window_size = Size(width, height);
211 fullscreen_size = Size(width, height);
212 fullscreen_refresh_rate = 0;
216 else if (arg == "--aspect" || arg == "-a")
221 throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
225 int aspect_width = 0;
226 int aspect_height = 0;
227 if (strcmp(argv[i], "auto") == 0)
232 else if (sscanf(argv[i], "%d:%d", &aspect_width, &aspect_height) != 2)
234 throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
238 float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
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),
245 aspect_size = Size(600,
246 static_cast<int>(600 * 1/aspect_ratio + 0.5));
251 else if (arg == "--renderer")
256 throw std::runtime_error("Need to specify a renderer for renderer argument");
260 video = VideoSystem::get_video_system(argv[i]);
263 else if (arg == "--show-fps")
267 else if (arg == "--no-show-fps")
271 else if (arg == "--console")
273 console_enabled = true;
275 else if (arg == "--noconsole")
277 console_enabled = false;
279 else if (arg == "--disable-sound" || arg == "--disable-sfx")
281 sound_enabled = false;
283 else if (arg == "--disable-music")
285 music_enabled = false;
287 else if (arg == "--play-demo")
291 throw std::runtime_error("Need to specify a demo filename");
295 start_demo = argv[++i];
298 else if (arg == "--record-demo")
302 throw std::runtime_error("Need to specify a demo filename");
306 record_demo = argv[++i];
309 else if (arg == "--debug-scripts" || arg == "-s")
311 enable_script_debugger = true;
313 else if (arg[0] != '-')
319 throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
325 CommandLineArguments::merge_into(Config& config)
327 #define merge_option(x) if (x) { config.x = *x; }
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);
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);