src/Application.cc
changeset 389 e74c1820fbd2
parent 358 37b18b779ffb
child 392 6c4dc68360eb
--- a/src/Application.cc	Tue Jan 13 16:11:37 2009 +0000
+++ b/src/Application.cc	Tue Jan 13 20:17:03 2009 +0200
@@ -2,24 +2,58 @@
 #include "Application.hh"
 
 #include <stdexcept>
+#include <cstdio>
 #include <cassert>
 
+enum ArgumentCodes {
+    ARG_HELP        = 'h',
+    ARG_PORT        = 'p',
+    ARG_SERVER      = 's',
+    ARG_CLIENT      = 'c',
+    ARG_GRAPHICS    = 'g',
+    ARG_FULLSCREEN  = 'F',
+    ARG_RESOLUTION  = 'R',
+
+    ARG_LIST_MODES  = 0xff01,
+};
+
 
 /**
  * Set the arg_* members
  */
-void Main::parse_args (int argc, char **argv) {
+bool 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);
+    args.add_option(ARG_HELP, "help", "",
+            "display argument help and exit");
+
+    args.add_option(ARG_PORT, "port", "PORT",
+            "set network port used");
+
+    args.add_option(ARG_SERVER, "server", "",
+            "act as a network server");
+
+    args.add_option(ARG_CLIENT, "client", "SERVERHOST",
+            "act as a network client");
+
+    args.add_option(ARG_GRAPHICS, "graphics", "",        
+            "run graphics/local input. Implied with --connect");
+
+    args.add_option(ARG_FULLSCREEN, "fullscreen", "",
+            "run graphics in fullscreen mode");
+
+    args.add_option(ARG_RESOLUTION, "resolution", "WIDTHxHEIGHT",
+            "set graphics resolution");
+    
+    args.add_option(ARG_LIST_MODES, "list-modes", "",
+            "output a list of available display modes and exit");
 
     // set defaults
     arg_graphics = false;
     arg_port = NETWORK_PORT_STR;
     arg_server = false;
     arg_connect = "";
+    arg_fullscreen = GRAPHICS_FULLSCREEN;
+    arg_resolution = PixelCoordinate(GRAPHICS_RESOLUTION_WIDTH, GRAPHICS_RESOLUTION_HEIGHT);
 
     // extra state
     bool graphics_default = true;
@@ -34,22 +68,38 @@
 
     while (args.next()) {
         switch (args.get_key()) {
-            case 'p':
+            case ARG_HELP:
+                args.print_help();
+                return false;
+
+            case ARG_PORT:
                 arg_port = args.get_argument();
                 break;
 
-            case 's':
+            case ARG_SERVER:
                 arg_server = true;
                 break;
 
-            case 'c':
+            case ARG_CLIENT:
                 arg_connect = args.get_argument();
                 break;
 
-            case 'g':
+            case ARG_GRAPHICS:
                 arg_graphics = true;
                 break;
 
+            case ARG_FULLSCREEN:
+                arg_fullscreen = true;
+                break;
+
+            case ARG_RESOLUTION:
+                parse_arg_resolution(args.get_argument());
+                break;
+
+            case ARG_LIST_MODES:
+                dump_display_modes();
+                return false;
+
             case CL_CommandLine::REST_ARG:
                 throw ArgumentError(args.get_argument());
 
@@ -66,6 +116,29 @@
     // enable graphics by default unless server
     if (!arg_server && graphics_default)
         arg_graphics = true;
+    
+    // continue
+    return true;
+}
+        
+void Main::parse_arg_resolution (const std::string &val) {
+    unsigned int w, h;
+    
+    // sccanf as unsigned
+    if (sscanf(val.c_str(), "%ux%u", &w, &h) != 2)
+        throw ArgumentError("invalid format for --resolution");
+    
+    // store as PixelCoordinate
+    arg_resolution = PixelCoordinate(w, h);
+}
+        
+void Main::dump_display_modes (void) {
+    const std::vector<CL_DisplayMode> &modes = Graphics::getDisplayModes();
+    
+    std::cout << "Available display modes:" << std::endl;
+
+    for (std::vector<CL_DisplayMode>::const_iterator it = modes.begin(); it != modes.end(); it++)
+        std::cout << "\t" << it->get_resolution().width << "x" << it->get_resolution().height << std::endl;
 }
 
 /**
@@ -79,15 +152,16 @@
     CL_SetupGL setup_gl;
 
     try {
-        // parse arugments
-        parse_args(argc, argv);
+        // parse arugments, exit if false
+        if (parse_args(argc, argv) == false)
+            return 0;
 
         // our engine
         Engine engine;
         
         // setup graphics
         if (arg_graphics)
-            engine.setupGraphics();
+            engine.setupGraphics(arg_resolution, arg_fullscreen);
 
         // setup either network server, client or singleplayer
         if (arg_server) {