#include "Application.hh"
#include <stdexcept>
#include <cassert>
/**
* Set the arg_* members
*/
void Main::parse_args (int argc, char **argv) {
// set up the options
args.add_option('p', "port", "PORT", "set network port used", true);
args.add_option('s', "server", "", "act as a network server", true);
args.add_option('c', "client", "SERVERHOST", "act as a network client", true);
args.add_option('g', "graphics", "", "run graphics/local input. Implied with --connect", true);
// set defaults
arg_graphics = false;
arg_port = NETWORK_PORT_STR;
arg_server = false;
arg_connect = "";
// extra state
bool graphics_default = true;
try {
// parse args
args.parse_args(argc, argv);
} catch (CL_Error &e) {
throw ArgumentError(e.message);
}
while (args.next()) {
switch (args.get_key()) {
case 'p':
arg_port = args.get_argument();
break;
case 's':
arg_server = true;
break;
case 'c':
arg_connect = args.get_argument();
break;
case 'g':
arg_graphics = true;
break;
case CL_CommandLine::REST_ARG:
throw ArgumentError(args.get_argument());
default:
throw ArgumentError(std::string(1, (char) args.get_key()));
}
}
// check for invalid combinations of arugments
if (arg_server and !arg_connect.empty())
throw ArgumentError("cannot be both server and client");
// enable graphics by default unless server
if (!arg_server && graphics_default)
arg_graphics = true;
}
/**
* IT BEGAN IN AFRIKA
*/
int Main::main (int argc, char **argv) {
// initialize the ClanLib components that we use
CL_SetupCore setup_core;
CL_SetupNetwork setup_network;
CL_SetupDisplay setup_disp;
CL_SetupGL setup_gl;
try {
// parse arugments
parse_args(argc, argv);
// our engine
Engine engine;
// setup graphics
if (arg_graphics)
engine.setupGraphics();
// setup either network server, client or singleplayer
if (arg_server) {
engine.setupNetworkServer(arg_port);
} else if (!arg_connect.empty()) {
engine.setupNetworkClient(arg_connect, arg_port);
} else {
engine.setupSinglePlayer();
}
// run the main loop
engine.run();
// succesful return
return 0;
} catch (ArgumentError &e) {
std::cerr << e.what() << std::endl;
args.print_help();
// XXX: handle --help
return 1;
} catch (CL_Error &e) {
std::cerr << "main: CL_Error:" << e.message << std::endl;
return 1;
} catch (std::exception &e) {
std::cerr << "FATAL [uncaught_exception] " << e.what() << std::endl;
return 1;
}
}