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),
33 fullscreen_refresh_rate(),
43 enable_script_debugger(),
49 CommandLineArguments::~CommandLineArguments()
54 CommandLineArguments::print_datadir()
56 // Print the datadir searchpath to stdout, one path per
57 // line. Then exit. Intended for use by the supertux-editor.
60 sp = PHYSFS_getSearchPath();
62 for (sp_index = 0; sp[sp_index]; sp_index++)
63 std::cout << sp[sp_index] << std::endl;
68 CommandLineArguments::print_help(const char* argv0)
70 std::string default_user_data_dir =
71 std::string(PHYSFS_getUserDir()) + WRITEDIR_NAME;
73 std::cerr << boost::format(_(
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"
98 "Environment variables:\n"
99 " SUPERTUX2_USER_DIR Directory for user data (savegames, etc.);\n"
103 % argv0 % default_user_data_dir
108 CommandLineArguments::print_version()
110 std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
114 CommandLineArguments::parse_args(int argc, char** argv)
116 for(int i = 1; i < argc; ++i)
118 std::string arg = argv[i];
120 if (arg == "--version" || arg == "-v")
122 m_action = PRINT_VERSION;
125 else if (arg == "--help" || arg == "-h")
127 m_action = PRINT_HELP;
129 else if (arg == "--print-datadir")
131 m_action = PRINT_DATADIR;
133 else if (arg == "--debug")
135 m_log_level = LOG_DEBUG;
137 else if (arg == "--verbose")
139 if (m_log_level < LOG_INFO)
141 m_log_level = LOG_INFO;
144 else if (arg == "--fullscreen" || arg == "-f")
146 use_fullscreen = true;
148 else if (arg == "--default" || arg == "-d")
150 use_fullscreen = false;
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
157 else if (arg == "--window" || arg == "-w")
159 use_fullscreen = false;
161 else if (arg == "--geometry" || arg == "-g")
166 throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
171 if (sscanf(argv[i], "%dx%d", &width, &height) != 2)
173 throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
177 window_size = Size(width, height);
178 fullscreen_size = Size(width, height);
179 fullscreen_refresh_rate = 0;
183 else if (arg == "--aspect" || arg == "-a")
188 throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
192 int aspect_width = 0;
193 int aspect_height = 0;
194 if (strcmp(argv[i], "auto") == 0)
199 else if (sscanf(argv[i], "%d:%d", &aspect_width, &aspect_height) != 2)
201 throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
205 float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
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),
212 aspect_size = Size(600,
213 static_cast<int>(600 * 1/aspect_ratio + 0.5));
218 else if (arg == "--renderer")
223 throw std::runtime_error("Need to specify a renderer for renderer argument");
227 video = VideoSystem::get_video_system(argv[i]);
230 else if (arg == "--show-fps")
234 else if (arg == "--no-show-fps")
238 else if (arg == "--console")
240 console_enabled = true;
242 else if (arg == "--noconsole")
244 console_enabled = false;
246 else if (arg == "--disable-sound" || arg == "--disable-sfx")
248 sound_enabled = false;
250 else if (arg == "--disable-music")
252 music_enabled = false;
254 else if (arg == "--play-demo")
258 throw std::runtime_error("Need to specify a demo filename");
262 start_demo = argv[++i];
265 else if (arg == "--record-demo")
269 throw std::runtime_error("Need to specify a demo filename");
273 record_demo = argv[++i];
276 else if (arg == "--debug-scripts" || arg == "-s")
278 enable_script_debugger = true;
280 else if (arg[0] != '-')
286 throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
292 CommandLineArguments::merge_into(Config& config)
294 #define merge_option(x) if (x) { config.x = *x; }
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);
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);