]> git.tdb.fi Git - r2c2.git/blob - options.cpp
b5c36cd5409cca9e43822fda7215e9f1e9018b52
[r2c2.git] / options.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/core/getopt.h>
9 #include <msp/fs/utils.h>
10 #include <msp/strings/lexicalcast.h>
11 #include <msp/strings/regex.h>
12 #include "options.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 Options::Options(int argc, char **argv):
18         screen_w(1280),
19         screen_h(960),
20         fullscreen(false),
21         debug(false),
22         network(false),
23         simulate(false)
24 {
25         string res;
26
27         GetOpt getopt;
28         getopt.add_option('r', "resolution",  res,         GetOpt::REQUIRED_ARG);
29         getopt.add_option('f', "fullscreen",  fullscreen,  GetOpt::NO_ARG);
30         getopt.add_option('g', "debug",       debug,       GetOpt::NO_ARG);
31         getopt.add_option('d', "driver",      driver,      GetOpt::REQUIRED_ARG);
32         getopt.add_option('s', "simulate",    simulate,    GetOpt::NO_ARG);
33         getopt.add_option('n', "network",     network,     GetOpt::NO_ARG);
34         getopt.add_option(     "state",       state_fn,    GetOpt::REQUIRED_ARG);
35         getopt(argc, argv);
36
37         if(!res.empty())
38         {
39                 if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
40                 {
41                         screen_w = lexical_cast<unsigned>(m[1].str);
42                         screen_h = lexical_cast<unsigned>(m[2].str);
43                 }
44                 else
45                         throw UsageError("Invalid resolution");
46         }
47
48         const vector<string> &args = getopt.get_args();
49         if(args.empty())
50                 throw UsageError("No layout given");
51
52         layout_fn = args[0];
53
54         if(state_fn.empty())
55                 state_fn = FS::basepart(layout_fn)+".state";
56 }