author | terom |
Tue, 09 Dec 2008 04:41:48 +0000 | |
changeset 368 | fe49a4b12575 |
parent 358 | 37b18b779ffb |
child 389 | e74c1820fbd2 |
permissions | -rw-r--r-- |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
1 |
|
358 | 2 |
#include "Application.hh" |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
3 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
4 |
#include <stdexcept> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
5 |
#include <cassert> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
6 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
7 |
|
358 | 8 |
/** |
9 |
* Set the arg_* members |
|
10 |
*/ |
|
11 |
void Main::parse_args (int argc, char **argv) { |
|
12 |
// set up the options |
|
13 |
args.add_option('p', "port", "PORT", "set network port used", true); |
|
14 |
args.add_option('s', "server", "", "act as a network server", true); |
|
15 |
args.add_option('c', "client", "SERVERHOST", "act as a network client", true); |
|
16 |
args.add_option('g', "graphics", "", "run graphics/local input. Implied with --connect", true); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
17 |
|
358 | 18 |
// set defaults |
19 |
arg_graphics = false; |
|
20 |
arg_port = NETWORK_PORT_STR; |
|
21 |
arg_server = false; |
|
22 |
arg_connect = ""; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
23 |
|
358 | 24 |
// extra state |
25 |
bool graphics_default = true; |
|
26 |
||
27 |
try { |
|
28 |
// parse args |
|
29 |
args.parse_args(argc, argv); |
|
267 | 30 |
|
358 | 31 |
} catch (CL_Error &e) { |
32 |
throw ArgumentError(e.message); |
|
33 |
} |
|
267 | 34 |
|
358 | 35 |
while (args.next()) { |
36 |
switch (args.get_key()) { |
|
37 |
case 'p': |
|
38 |
arg_port = args.get_argument(); |
|
39 |
break; |
|
40 |
||
41 |
case 's': |
|
42 |
arg_server = true; |
|
43 |
break; |
|
44 |
||
45 |
case 'c': |
|
46 |
arg_connect = args.get_argument(); |
|
47 |
break; |
|
48 |
||
49 |
case 'g': |
|
267 | 50 |
arg_graphics = true; |
358 | 51 |
break; |
52 |
||
53 |
case CL_CommandLine::REST_ARG: |
|
54 |
throw ArgumentError(args.get_argument()); |
|
55 |
||
56 |
default: |
|
57 |
throw ArgumentError(std::string(1, (char) args.get_key())); |
|
58 |
||
59 |
} |
|
60 |
} |
|
61 |
||
62 |
// check for invalid combinations of arugments |
|
63 |
if (arg_server and !arg_connect.empty()) |
|
64 |
throw ArgumentError("cannot be both server and client"); |
|
65 |
||
66 |
// enable graphics by default unless server |
|
67 |
if (!arg_server && graphics_default) |
|
68 |
arg_graphics = true; |
|
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* IT BEGAN IN AFRIKA |
|
73 |
*/ |
|
74 |
int Main::main (int argc, char **argv) { |
|
75 |
// initialize the ClanLib components that we use |
|
76 |
CL_SetupCore setup_core; |
|
77 |
CL_SetupNetwork setup_network; |
|
78 |
CL_SetupDisplay setup_disp; |
|
79 |
CL_SetupGL setup_gl; |
|
80 |
||
81 |
try { |
|
82 |
// parse arugments |
|
83 |
parse_args(argc, argv); |
|
84 |
||
85 |
// our engine |
|
86 |
Engine engine; |
|
87 |
||
88 |
// setup graphics |
|
89 |
if (arg_graphics) |
|
90 |
engine.setupGraphics(); |
|
91 |
||
92 |
// setup either network server, client or singleplayer |
|
93 |
if (arg_server) { |
|
94 |
engine.setupNetworkServer(arg_port); |
|
95 |
||
96 |
} else if (!arg_connect.empty()) { |
|
97 |
engine.setupNetworkClient(arg_connect, arg_port); |
|
98 |
||
99 |
} else { |
|
100 |
engine.setupSinglePlayer(); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
101 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
102 |
|
358 | 103 |
// run the main loop |
104 |
engine.run(); |
|
105 |
||
106 |
// succesful return |
|
107 |
return 0; |
|
108 |
||
109 |
} catch (ArgumentError &e) { |
|
110 |
std::cerr << e.what() << std::endl; |
|
111 |
args.print_help(); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
112 |
|
358 | 113 |
// XXX: handle --help |
114 |
return 1; |
|
115 |
} catch (CL_Error &e) { |
|
116 |
std::cerr << "main: CL_Error:" << e.message << std::endl; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |
|
358 | 118 |
return 1; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
119 |
|
358 | 120 |
} catch (std::exception &e) { |
121 |
std::cerr << "FATAL [uncaught_exception] " << e.what() << std::endl; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
122 |
|
358 | 123 |
return 1; |
124 |
} |
|
125 |
} |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
126 |