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"
21 #include <boost/format.hpp>
23 #include "supertux/gameconfig.hpp"
24 #include "supertux/main.hpp"
25 #include "util/gettext.hpp"
28 CommandLineArguments::CommandLineArguments() :
30 m_log_level(LOG_WARNING),
32 fullscreen_refresh_rate(),
42 enable_script_debugger(),
48 CommandLineArguments::~CommandLineArguments()
53 CommandLineArguments::print_datadir()
55 // Print the datadir searchpath to stdout, one path per
56 // line. Then exit. Intended for use by the supertux-editor.
59 sp = PHYSFS_getSearchPath();
61 for (sp_index = 0; sp[sp_index]; sp_index++)
62 std::cout << sp[sp_index] << std::endl;
67 CommandLineArguments::print_help(const char* argv0)
69 std::string default_user_data_dir =
70 std::string(PHYSFS_getUserDir()) + WRITEDIR_NAME;
72 std::cerr << boost::format(_(
74 "Usage: %s [OPTIONS] [LEVELFILE]\n\n"
75 "CommandLineArguments:\n"
76 " --verbose Print verbose messages\n"
77 " --debug Print extra verbose messages\n"
78 " -f, --fullscreen Run in fullscreen mode\n"
79 " -w, --window Run in window mode\n"
80 " -g, --geometry WIDTHxHEIGHT Run SuperTux in given resolution\n"
81 " -a, --aspect WIDTH:HEIGHT Run SuperTux with given aspect ratio\n"
82 " -d, --default Reset video settings to default values\n"
83 " --renderer RENDERER Use sdl, opengl, or auto to render\n"
84 " --disable-sound Disable sound effects\n"
85 " --disable-music Disable music\n"
86 " -h, --help Show this help message and quit\n"
87 " -v, --version Show SuperTux version and quit\n"
88 " --console Enable ingame scripting console\n"
89 " --noconsole Disable ingame scripting console\n"
90 " --show-fps Display framerate in levels\n"
91 " --no-show-fps Do not display framerate in levels\n"
92 " --record-demo FILE LEVEL Record a demo to FILE\n"
93 " --play-demo FILE LEVEL Play a recorded demo\n"
94 " -s, --debug-scripts Enable script debugger.\n"
95 " --print-datadir Print supertux's primary data directory.\n"
97 "Environment variables:\n"
98 " SUPERTUX2_USER_DIR Directory for user data (savegames, etc.);\n"
102 % argv0 % default_user_data_dir
107 CommandLineArguments::print_version()
109 std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
113 CommandLineArguments::parse_args(int argc, char** argv)
115 for(int i = 1; i < argc; ++i)
117 std::string arg = argv[i];
119 if (arg == "--version" || arg == "-v")
121 m_action = PRINT_VERSION;
124 else if (arg == "--help" || arg == "-h")
126 m_action = PRINT_HELP;
128 else if (arg == "--print-datadir")
130 m_action = PRINT_DATADIR;
132 else if (arg == "--debug")
134 m_log_level = LOG_DEBUG;
136 else if (arg == "--verbose")
138 if (m_log_level < LOG_INFO)
140 m_log_level = LOG_INFO;
143 else if (arg == "--fullscreen" || arg == "-f")
145 use_fullscreen = true;
147 else if (arg == "--default" || arg == "-d")
149 use_fullscreen = false;
151 window_size = Size(800, 600);
152 fullscreen_size = Size(800, 600);
153 fullscreen_refresh_rate = 0;
154 aspect_size = Size(0, 0); // auto detect
156 else if (arg == "--window" || arg == "-w")
158 use_fullscreen = false;
160 else if (arg == "--geometry" || arg == "-g")
165 throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
170 if (sscanf(argv[i], "%dx%d", &width, &height) != 2)
172 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
176 window_size = Size(width, height);
177 fullscreen_size = Size(width, height);
178 fullscreen_refresh_rate = 0;
182 else if (arg == "--aspect" || arg == "-a")
187 throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
191 int aspect_width = 0;
192 int aspect_height = 0;
193 if (strcmp(argv[i], "auto") == 0)
198 else if (sscanf(argv[i], "%d:%d", &aspect_width, &aspect_height) != 2)
200 throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
204 float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
206 // use aspect ratio to calculate logical resolution
207 if (aspect_ratio > 1) {
208 aspect_size = Size(static_cast<int>(600 * aspect_ratio + 0.5),
211 aspect_size = Size(600,
212 static_cast<int>(600 * 1/aspect_ratio + 0.5));
217 else if (arg == "--renderer")
222 throw std::runtime_error("Need to specify a renderer for renderer argument");
226 video = VideoSystem::get_video_system(argv[i]);
229 else if (arg == "--show-fps")
233 else if (arg == "--no-show-fps")
237 else if (arg == "--console")
239 console_enabled = true;
241 else if (arg == "--noconsole")
243 console_enabled = false;
245 else if (arg == "--disable-sound" || arg == "--disable-sfx")
247 sound_enabled = false;
249 else if (arg == "--disable-music")
251 music_enabled = false;
253 else if (arg == "--play-demo")
257 throw std::runtime_error("Need to specify a demo filename");
261 start_demo = argv[++i];
264 else if (arg == "--record-demo")
268 throw std::runtime_error("Need to specify a demo filename");
272 record_demo = argv[++i];
275 else if (arg == "--debug-scripts" || arg == "-s")
277 enable_script_debugger = true;
279 else if (arg[0] != '-')
285 throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
291 CommandLineArguments::merge_into(Config& config)
293 #define merge_option(x) if (x) { config.x = *x; }
295 merge_option(fullscreen_size);
296 merge_option(fullscreen_refresh_rate);
297 merge_option(window_size);
298 merge_option(aspect_size);
299 merge_option(use_fullscreen);
301 merge_option(show_fps);
302 merge_option(sound_enabled);
303 merge_option(music_enabled);
304 merge_option(console_enabled);
305 merge_option(start_level);
306 merge_option(enable_script_debugger);
307 merge_option(start_demo);
308 merge_option(record_demo);