src/Application.cc
branchnew_graphics
changeset 417 c503e0c6a740
parent 412 721c60072091
child 418 194bc810a570
equal deleted inserted replaced
416:38cba347a3a9 417:c503e0c6a740
     1 
     1 
     2 #include "Application.hh"
     2 #include "Application.hh"
     3 
     3 #include "Config.hh"
     4 #include <ClanLib/gl.h>
     4 
       
     5 #if GRAPHICS_ENABLED
       
     6     // for dump_display_modes
       
     7     #include "Graphics/Display.hh"
       
     8     
       
     9     // for CL_SetupGL
       
    10     #include <ClanLib/gl.h>
       
    11 #endif
       
    12 
     5 #include <stdexcept>
    13 #include <stdexcept>
     6 #include <sstream>
    14 #include <sstream>
     7 #include <cstdio>
    15 #include <cstdio>
     8 #include <cassert>
    16 #include <cassert>
     9 
    17 
    35 
    43 
    36 /**
    44 /**
    37  * Set the arg_* members
    45  * Set the arg_* members
    38  */
    46  */
    39 bool Main::parse_args (int argc, char **argv) {
    47 bool Main::parse_args (int argc, char **argv) {
       
    48 
    40     // set up the options
    49     // set up the options
       
    50     args.add_usage("[OPTIONS]");
       
    51     args.set_help_indent(30);
       
    52     
       
    53     if (NETWORK_ENABLED) {
       
    54         args.add_group("Network");
       
    55 
       
    56         args.add_option(ARG_PORT, "port", "PORT",
       
    57                 "set network port used");
       
    58 
       
    59         args.add_option(ARG_SERVER, "server", "",
       
    60                 "act as a network server");
       
    61         
       
    62         // require graphics for network-client
       
    63         if (GRAPHICS_ENABLED)
       
    64             args.add_option(ARG_CLIENT, "client", "SERVERHOST",
       
    65                     "act as a network client");
       
    66     }
       
    67 
       
    68     if (GRAPHICS_ENABLED) {
       
    69         args.add_group("Graphics");
       
    70 
       
    71         args.add_option(ARG_GRAPHICS, "graphics", "",        
       
    72                 "run graphics/local input. Implied with --client");
       
    73 
       
    74         args.add_option(ARG_FULLSCREEN, "fullscreen", "",
       
    75                 "run graphics in fullscreen mode");
       
    76 
       
    77         args.add_option(ARG_RESOLUTION, "resolution", "WIDTHxHEIGHT",
       
    78                 "set graphics resolution");
       
    79         
       
    80         args.add_option(ARG_LIST_MODES, "list-modes", "",
       
    81                 "output a list of available display modes and exit");
       
    82     }
       
    83 
       
    84     args.add_group("Game");
       
    85     args.add_option(ARG_TERRAIN_SEED, "terrain-seed", "SEED",
       
    86             "set seed for terrain random generator");
       
    87 
       
    88     args.add_option(ARG_TERRAIN_SIZE, "terrain-size", "WIDTHxHEIGHT",
       
    89             "set terrain size for random generator");
       
    90 
       
    91     
       
    92     args.add_group("General");
    41     args.add_option(ARG_HELP, "help", "",
    93     args.add_option(ARG_HELP, "help", "",
    42             "display argument help and exit");
    94             "display argument help and exit");
    43 
    95 
    44     args.add_option(ARG_PORT, "port", "PORT",
       
    45             "set network port used");
       
    46 
       
    47     args.add_option(ARG_SERVER, "server", "",
       
    48             "act as a network server");
       
    49 
       
    50     args.add_option(ARG_CLIENT, "client", "SERVERHOST",
       
    51             "act as a network client");
       
    52 
       
    53     args.add_option(ARG_GRAPHICS, "graphics", "",        
       
    54             "run graphics/local input. Implied with --connect");
       
    55 
       
    56     args.add_option(ARG_FULLSCREEN, "fullscreen", "",
       
    57             "run graphics in fullscreen mode");
       
    58 
       
    59     args.add_option(ARG_RESOLUTION, "resolution", "WIDTHxHEIGHT",
       
    60             "set graphics resolution");
       
    61     
       
    62     args.add_option(ARG_LIST_MODES, "list-modes", "",
       
    63             "output a list of available display modes and exit");
       
    64 
       
    65     args.add_option(ARG_TERRAIN_SEED, "terrain-seed", "SEED",
       
    66             "set seed for terrain random generator");
       
    67 
       
    68     args.add_option(ARG_TERRAIN_SIZE, "terrain-size", "WIDTHxHEIGHT",
       
    69             "set terrain size for random generator");
       
    70 
       
    71     args.add_option(ARG_VERSION, "version", "",
    96     args.add_option(ARG_VERSION, "version", "",
    72             "output application version and exit");
    97             "output application version plus configuration and exit");
    73 
    98 
       
    99 #if GRAPHICS_ENABLED    
    74     // extra state
   100     // extra state
    75     bool resolution_default = true;
   101     bool resolution_default = true;
       
   102 
       
   103 #endif    
    76     
   104     
    77     try {
   105     try {
    78         // parse args
   106         // parse args
    79         args.parse_args(argc, argv);
   107         args.parse_args(argc, argv);
    80 
   108 
    86         switch (args.get_key()) {
   114         switch (args.get_key()) {
    87             case ARG_HELP:
   115             case ARG_HELP:
    88                 args.print_help();
   116                 args.print_help();
    89                 return false;
   117                 return false;
    90 
   118 
       
   119 #if NETWORK_ENABLED                
    91             case ARG_PORT:
   120             case ARG_PORT:
    92                 net_port = args.get_argument();
   121                 net_port = args.get_argument();
    93                 break;
   122                 break;
    94 
   123 
    95             case ARG_SERVER:
   124             case ARG_SERVER:
    98 
   127 
    99             case ARG_CLIENT:
   128             case ARG_CLIENT:
   100                 net_connect = args.get_argument();
   129                 net_connect = args.get_argument();
   101                 break;
   130                 break;
   102 
   131 
       
   132 #endif                
       
   133 
       
   134 #if GRAPHICS_ENABLED                
   103             case ARG_GRAPHICS:
   135             case ARG_GRAPHICS:
   104                 graphics_enabled = true;
   136                 graphics_enabled = true;
   105                 break;
   137                 break;
   106 
   138 
   107             case ARG_FULLSCREEN:
   139             case ARG_FULLSCREEN:
   123                 break;
   155                 break;
   124 
   156 
   125             case ARG_LIST_MODES:
   157             case ARG_LIST_MODES:
   126                 dump_display_modes();
   158                 dump_display_modes();
   127                 return false;
   159                 return false;
       
   160 #endif
   128 
   161 
   129             case ARG_VERSION:
   162             case ARG_VERSION:
   130                 dump_version();
   163                 dump_version();
   131                 return false;
   164                 return false;
   132 
   165 
   145                 throw ArgumentError(std::string() + "Unknown argument key: " + (char) args.get_key());
   178                 throw ArgumentError(std::string() + "Unknown argument key: " + (char) args.get_key());
   146 
   179 
   147         }
   180         }
   148     }
   181     }
   149 
   182 
       
   183 #if NETWORK_ENABLED    
   150     // check for invalid combinations of arugments
   184     // check for invalid combinations of arugments
   151     if (net_server and !net_connect.empty())
   185     if (net_server and !net_connect.empty())
   152         throw ArgumentError("cannot be both server and client");
   186         throw ArgumentError("cannot be both server and client");
   153 
   187 #endif
       
   188 
       
   189 #if GRAPHICS_ENABLED    
   154     // enable graphics by default unless server
   190     // enable graphics by default unless server
   155     if (!net_server)
   191     if (!net_server)
   156         graphics_enabled = true;
   192         graphics_enabled = true;
   157     
   193 #endif    
       
   194 
   158     // continue
   195     // continue
   159     return true;
   196     return true;
   160 }
   197 }
   161         
   198         
   162 int Main::parse_arg_int (const std::string &arg_val, const char *arg_name) {
   199 int Main::parse_arg_int (const std::string &arg_val, const char *arg_name) {
   178     if (sscanf(arg_val.c_str(), "%ux%u", &w, &h) != 2)
   215     if (sscanf(arg_val.c_str(), "%ux%u", &w, &h) != 2)
   179         throw ArgumentError(std::string() + "invalid format for " + arg_name + ": " + arg_val);
   216         throw ArgumentError(std::string() + "invalid format for " + arg_name + ": " + arg_val);
   180     
   217     
   181     return PixelDimensions(w, h);
   218     return PixelDimensions(w, h);
   182 }
   219 }
   183         
   220 
       
   221 #if GRAPHICS_ENABLED
   184 void Main::dump_display_modes (void) {
   222 void Main::dump_display_modes (void) {
       
   223     // get the list of display modes from graphics
   185     const std::vector<CL_DisplayMode> &modes = graphics::Display::getDisplayModes();
   224     const std::vector<CL_DisplayMode> &modes = graphics::Display::getDisplayModes();
       
   225     PixelCoordinate last_resolution;
   186     
   226     
   187     std::cout << "Available display modes:" << std::endl;
   227     std::cout << "Available display modes:" << std::endl;
   188 
   228     
   189     for (std::vector<CL_DisplayMode>::const_iterator it = modes.begin(); it != modes.end(); it++)
   229     // iterate over the list of available display modes
   190         std::cout << "\t" << it->get_resolution().width << "x" << it->get_resolution().height << std::endl;
   230     for (std::vector<CL_DisplayMode>::const_iterator it = modes.begin(); it != modes.end(); it++) {
   191 }
   231         PixelCoordinate resolution(it->get_resolution().width, it->get_resolution().height);
       
   232         
       
   233         // filter out those that haven't changed
       
   234         if (resolution != last_resolution)
       
   235             std::cout << "\t" << it->get_resolution().width << "x" << it->get_resolution().height << std::endl;
       
   236         
       
   237         // update for next item
       
   238         last_resolution = resolution;
       
   239     }
       
   240 }
       
   241 #endif
   192         
   242         
   193 void Main::dump_version (void) {
   243 void Main::dump_version (void) {
   194     std::cout << PROJECT_LONG_NAME << " version " << PROJECT_VERSION << " built " << PROJECT_BUILD_TIMESTAMP << std::endl;
   244     std::cout 
       
   245         << PROJECT_LONG_NAME << " version " << PROJECT_VERSION << " built [" << PROJECT_BUILD_TIMESTAMP << "]" << std::endl;
       
   246 
       
   247     // then some additional stuff about what's enabled
       
   248     std::cout
       
   249         << std::endl
       
   250         << "Enabled components:" << std::endl
       
   251         << "\tGraphics:\t" << (GRAPHICS_ENABLED ? "Yes" : "No") << std::endl
       
   252         << "\tNetwork:\t" << (NETWORK_ENABLED ? "Yes" : "No") << std::endl;
   195 }
   253 }
   196 
   254 
   197 /**
   255 /**
   198  * IT BEGAN IN AFRIKA
   256  * IT BEGAN IN AFRIKA
   199  */
   257  */
   200 int Main::main (int argc, char **argv) {
   258 int Main::main (int argc, char **argv) {
   201     // initialize the ClanLib components that we use
   259     // initialize the ClanLib components that we use
   202     CL_SetupCore setup_core;
   260     CL_SetupCore setup_core;
       
   261 
       
   262 #if GRAPHICS_ENABLED    
       
   263     // XXX: move to Graphics/Graphics.hh?
   203     CL_SetupDisplay setup_disp;
   264     CL_SetupDisplay setup_disp;
   204     CL_SetupGL setup_gl;
   265     CL_SetupGL setup_gl;
       
   266 #endif
   205 
   267 
   206     try {
   268     try {
   207         // parse arugments, exit if false
   269         // parse arugments, exit if false
   208         if (parse_args(argc, argv) == false)
   270         if (parse_args(argc, argv) == false)
   209             return 0;
   271             return 0;
   224             engine.setupNetworkServer(net_port);
   286             engine.setupNetworkServer(net_port);
   225 
   287 
   226         } else if (!net_connect.empty()) {
   288         } else if (!net_connect.empty()) {
   227             engine.setupNetworkClient(net_connect, net_port);
   289             engine.setupNetworkClient(net_connect, net_port);
   228         
   290         
       
   291         } else if (graphics_enabled) {
       
   292             engine.setupSinglePlayer();
       
   293 
   229         } else {
   294         } else {
   230             engine.setupSinglePlayer();
   295             throw ArgumentError("Nothing to do");
       
   296 
   231         }
   297         }
   232 
   298 
   233         // run the main loop
   299         // run the main loop
   234         engine.run();
   300         engine.run();
   235         
   301         
   236         // succesful return
   302         // succesful return
   237         return 0;
   303         return 0;
   238     
   304     
   239     } catch (ArgumentError &e) {
   305     } catch (ArgumentError &e) {
   240         std::cerr << e.what() << std::endl;
   306         std::cerr 
       
   307             << "Error: " << e.what() << std::endl
       
   308             << std::endl;
       
   309 
   241         args.print_help();
   310         args.print_help();
   242 
   311 
   243         // XXX: handle --help
       
   244         return 1;
   312         return 1;
       
   313 
   245     } catch (CL_Error &e) {
   314     } catch (CL_Error &e) {
   246         std::cerr << "main: CL_Error:" << e.message << std::endl;
   315         std::cerr << "main: CL_Error:" << e.message << std::endl;
   247 
   316 
   248         return 1;
   317         return 1;
   249 
   318