21
|
1 |
|
|
2 |
#include "Engine.hh"
|
|
3 |
|
|
4 |
#include <stdexcept>
|
|
5 |
#include <cassert>
|
|
6 |
|
|
7 |
#include <ClanLib/core.h>
|
|
8 |
#include <ClanLib/application.h>
|
|
9 |
|
|
10 |
class ArgumentError : public std::exception {
|
24
|
11 |
private:
|
|
12 |
const char *message;
|
|
13 |
|
|
14 |
public:
|
|
15 |
ArgumentError (const std::string &message) : message(message.c_str()) { }
|
21
|
16 |
|
24
|
17 |
virtual const char* what() const throw() {
|
|
18 |
return message;
|
|
19 |
}
|
21
|
20 |
};
|
|
21 |
|
|
22 |
class Main : public CL_ClanApplication {
|
24
|
23 |
private:
|
|
24 |
// arguments
|
|
25 |
CL_CommandLine args;
|
25
|
26 |
|
|
27 |
bool arg_graphics;
|
24
|
28 |
std::string arg_port;
|
25
|
29 |
bool arg_server;
|
24
|
30 |
std::string arg_connect;
|
21
|
31 |
|
24
|
32 |
void parse_args (int argc, char **argv) {
|
|
33 |
// set up the options
|
|
34 |
args.add_option('p', "port", "PORT", "set network port used", true);
|
25
|
35 |
args.add_option('s', "server", "", "act as a network server", true);
|
|
36 |
args.add_option('c', "client", "SERVERHOST", "act as a network client", true);
|
|
37 |
args.add_option('g', "graphics", "", "run graphics/local input. Implied with --connect", true);
|
21
|
38 |
|
24
|
39 |
// set defaults
|
25
|
40 |
arg_graphics = false;
|
24
|
41 |
arg_port = NETWORK_PORT_STR;
|
25
|
42 |
arg_server = false;
|
24
|
43 |
arg_connect = "";
|
|
44 |
|
|
45 |
try {
|
|
46 |
// parse args
|
|
47 |
args.parse_args(argc, argv);
|
25
|
48 |
|
24
|
49 |
} catch (CL_Error &e) {
|
|
50 |
throw ArgumentError(e.message);
|
|
51 |
}
|
21
|
52 |
|
24
|
53 |
while (args.next()) {
|
|
54 |
switch (args.get_key()) {
|
|
55 |
case 'p':
|
|
56 |
arg_port = args.get_argument();
|
|
57 |
break;
|
21
|
58 |
|
25
|
59 |
case 's':
|
|
60 |
arg_server = true;
|
24
|
61 |
break;
|
21
|
62 |
|
24
|
63 |
case 'c':
|
|
64 |
arg_connect = args.get_argument();
|
25
|
65 |
arg_graphics = true;
|
|
66 |
break;
|
|
67 |
|
|
68 |
case 'g':
|
|
69 |
arg_graphics = true;
|
24
|
70 |
break;
|
|
71 |
|
|
72 |
case CL_CommandLine::REST_ARG:
|
|
73 |
throw ArgumentError(args.get_argument());
|
|
74 |
|
|
75 |
default:
|
|
76 |
throw ArgumentError(std::string(1, (char) args.get_key()));
|
|
77 |
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
35
|
81 |
// check for invalid combinations of arugments
|
|
82 |
if (arg_server and !arg_connect.empty())
|
|
83 |
throw ArgumentError("cannot be both server and client");
|
24
|
84 |
}
|
|
85 |
|
|
86 |
public:
|
|
87 |
virtual int main (int argc, char **argv) {
|
25
|
88 |
// initialize the ClanLib components that we use
|
24
|
89 |
CL_SetupCore setup_core;
|
25
|
90 |
CL_SetupNetwork setup_network;
|
|
91 |
CL_SetupDisplay setup_disp;
|
|
92 |
CL_SetupGL setup_gl;
|
|
93 |
|
24
|
94 |
try {
|
|
95 |
// parse arugments
|
|
96 |
parse_args(argc, argv);
|
25
|
97 |
|
|
98 |
// our engine
|
|
99 |
Engine engine;
|
24
|
100 |
|
25
|
101 |
// setup graphics
|
|
102 |
if (arg_graphics)
|
|
103 |
engine.setupGraphics();
|
|
104 |
|
35
|
105 |
// setup either network server, client or singleplayer
|
25
|
106 |
if (arg_server) {
|
|
107 |
engine.setupNetworkServer(arg_port);
|
24
|
108 |
|
|
109 |
} else if (!arg_connect.empty()) {
|
25
|
110 |
engine.setupNetworkClient(arg_connect, arg_port);
|
24
|
111 |
|
35
|
112 |
} else {
|
|
113 |
engine.setupSinglePlayer();
|
|
114 |
}
|
25
|
115 |
|
|
116 |
// run the main loop
|
|
117 |
engine.run();
|
24
|
118 |
|
|
119 |
// succesful return
|
|
120 |
return 0;
|
|
121 |
|
|
122 |
} catch (ArgumentError &e) {
|
|
123 |
std::cerr << e.what() << std::endl;
|
|
124 |
args.print_help();
|
|
125 |
|
|
126 |
// XXX: handle --help
|
|
127 |
return 1;
|
|
128 |
|
|
129 |
} catch (std::exception &e) {
|
|
130 |
std::cerr << e.what() << std::endl;
|
|
131 |
|
|
132 |
return 1;
|
|
133 |
}
|
|
134 |
}
|
21
|
135 |
} app;
|
|
136 |
|