src/Application.cc
changeset 389 e74c1820fbd2
parent 358 37b18b779ffb
child 392 6c4dc68360eb
equal deleted inserted replaced
388:ecb243eebc25 389:e74c1820fbd2
     1 
     1 
     2 #include "Application.hh"
     2 #include "Application.hh"
     3 
     3 
     4 #include <stdexcept>
     4 #include <stdexcept>
       
     5 #include <cstdio>
     5 #include <cassert>
     6 #include <cassert>
       
     7 
       
     8 enum ArgumentCodes {
       
     9     ARG_HELP        = 'h',
       
    10     ARG_PORT        = 'p',
       
    11     ARG_SERVER      = 's',
       
    12     ARG_CLIENT      = 'c',
       
    13     ARG_GRAPHICS    = 'g',
       
    14     ARG_FULLSCREEN  = 'F',
       
    15     ARG_RESOLUTION  = 'R',
       
    16 
       
    17     ARG_LIST_MODES  = 0xff01,
       
    18 };
     6 
    19 
     7 
    20 
     8 /**
    21 /**
     9  * Set the arg_* members
    22  * Set the arg_* members
    10  */
    23  */
    11 void Main::parse_args (int argc, char **argv) {
    24 bool Main::parse_args (int argc, char **argv) {
    12     // set up the options
    25     // set up the options
    13     args.add_option('p', "port", "PORT", "set network port used", true);
    26     args.add_option(ARG_HELP, "help", "",
    14     args.add_option('s', "server", "", "act as a network server", true);
    27             "display argument help and exit");
    15     args.add_option('c', "client", "SERVERHOST", "act as a network client", true);
    28 
    16     args.add_option('g', "graphics", "", "run graphics/local input. Implied with --connect", true);
    29     args.add_option(ARG_PORT, "port", "PORT",
       
    30             "set network port used");
       
    31 
       
    32     args.add_option(ARG_SERVER, "server", "",
       
    33             "act as a network server");
       
    34 
       
    35     args.add_option(ARG_CLIENT, "client", "SERVERHOST",
       
    36             "act as a network client");
       
    37 
       
    38     args.add_option(ARG_GRAPHICS, "graphics", "",        
       
    39             "run graphics/local input. Implied with --connect");
       
    40 
       
    41     args.add_option(ARG_FULLSCREEN, "fullscreen", "",
       
    42             "run graphics in fullscreen mode");
       
    43 
       
    44     args.add_option(ARG_RESOLUTION, "resolution", "WIDTHxHEIGHT",
       
    45             "set graphics resolution");
       
    46     
       
    47     args.add_option(ARG_LIST_MODES, "list-modes", "",
       
    48             "output a list of available display modes and exit");
    17 
    49 
    18     // set defaults
    50     // set defaults
    19     arg_graphics = false;
    51     arg_graphics = false;
    20     arg_port = NETWORK_PORT_STR;
    52     arg_port = NETWORK_PORT_STR;
    21     arg_server = false;
    53     arg_server = false;
    22     arg_connect = "";
    54     arg_connect = "";
       
    55     arg_fullscreen = GRAPHICS_FULLSCREEN;
       
    56     arg_resolution = PixelCoordinate(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT);
    23 
    57 
    24     // extra state
    58     // extra state
    25     bool graphics_default = true;
    59     bool graphics_default = true;
    26     
    60     
    27     try {
    61     try {
    32         throw ArgumentError(e.message);
    66         throw ArgumentError(e.message);
    33     }
    67     }
    34 
    68 
    35     while (args.next()) {
    69     while (args.next()) {
    36         switch (args.get_key()) {
    70         switch (args.get_key()) {
    37             case 'p':
    71             case ARG_HELP:
       
    72                 args.print_help();
       
    73                 return false;
       
    74 
       
    75             case ARG_PORT:
    38                 arg_port = args.get_argument();
    76                 arg_port = args.get_argument();
    39                 break;
    77                 break;
    40 
    78 
    41             case 's':
    79             case ARG_SERVER:
    42                 arg_server = true;
    80                 arg_server = true;
    43                 break;
    81                 break;
    44 
    82 
    45             case 'c':
    83             case ARG_CLIENT:
    46                 arg_connect = args.get_argument();
    84                 arg_connect = args.get_argument();
    47                 break;
    85                 break;
    48 
    86 
    49             case 'g':
    87             case ARG_GRAPHICS:
    50                 arg_graphics = true;
    88                 arg_graphics = true;
    51                 break;
    89                 break;
       
    90 
       
    91             case ARG_FULLSCREEN:
       
    92                 arg_fullscreen = true;
       
    93                 break;
       
    94 
       
    95             case ARG_RESOLUTION:
       
    96                 parse_arg_resolution(args.get_argument());
       
    97                 break;
       
    98 
       
    99             case ARG_LIST_MODES:
       
   100                 dump_display_modes();
       
   101                 return false;
    52 
   102 
    53             case CL_CommandLine::REST_ARG:
   103             case CL_CommandLine::REST_ARG:
    54                 throw ArgumentError(args.get_argument());
   104                 throw ArgumentError(args.get_argument());
    55 
   105 
    56             default:
   106             default:
    64         throw ArgumentError("cannot be both server and client");
   114         throw ArgumentError("cannot be both server and client");
    65 
   115 
    66     // enable graphics by default unless server
   116     // enable graphics by default unless server
    67     if (!arg_server && graphics_default)
   117     if (!arg_server && graphics_default)
    68         arg_graphics = true;
   118         arg_graphics = true;
       
   119     
       
   120     // continue
       
   121     return true;
       
   122 }
       
   123         
       
   124 void Main::parse_arg_resolution (const std::string &val) {
       
   125     unsigned int w, h;
       
   126     
       
   127     // sccanf as unsigned
       
   128     if (sscanf(val.c_str(), "%ux%u", &w, &h) != 2)
       
   129         throw ArgumentError("invalid format for --resolution");
       
   130     
       
   131     // store as PixelCoordinate
       
   132     arg_resolution = PixelCoordinate(w, h);
       
   133 }
       
   134         
       
   135 void Main::dump_display_modes (void) {
       
   136     const std::vector<CL_DisplayMode> &modes = Graphics::getDisplayModes();
       
   137     
       
   138     std::cout << "Available display modes:" << std::endl;
       
   139 
       
   140     for (std::vector<CL_DisplayMode>::const_iterator it = modes.begin(); it != modes.end(); it++)
       
   141         std::cout << "\t" << it->get_resolution().width << "x" << it->get_resolution().height << std::endl;
    69 }
   142 }
    70 
   143 
    71 /**
   144 /**
    72  * IT BEGAN IN AFRIKA
   145  * IT BEGAN IN AFRIKA
    73  */
   146  */
    77     CL_SetupNetwork setup_network;
   150     CL_SetupNetwork setup_network;
    78     CL_SetupDisplay setup_disp;
   151     CL_SetupDisplay setup_disp;
    79     CL_SetupGL setup_gl;
   152     CL_SetupGL setup_gl;
    80 
   153 
    81     try {
   154     try {
    82         // parse arugments
   155         // parse arugments, exit if false
    83         parse_args(argc, argv);
   156         if (parse_args(argc, argv) == false)
       
   157             return 0;
    84 
   158 
    85         // our engine
   159         // our engine
    86         Engine engine;
   160         Engine engine;
    87         
   161         
    88         // setup graphics
   162         // setup graphics
    89         if (arg_graphics)
   163         if (arg_graphics)
    90             engine.setupGraphics();
   164             engine.setupGraphics(arg_resolution, arg_fullscreen);
    91 
   165 
    92         // setup either network server, client or singleplayer
   166         // setup either network server, client or singleplayer
    93         if (arg_server) {
   167         if (arg_server) {
    94             engine.setupNetworkServer(arg_port);
   168             engine.setupNetworkServer(arg_port);
    95 
   169