Example #1
0
void
PingusMain::parse_args(int argc, char** argv)
{
  CommandLine argp;
  argp.add_usage(_("[OPTIONS]... [FILE]"));
  argp.add_doc(_("Pingus is a puzzle game where you need to guide a bunch of little penguins around the world."));

  argp.add_group(_("General Options:"));
  argp.add_option('h', "help", "", 
                  _("Displays this help"));
  argp.add_option('V', "version", "", 
                  _("Print version number and exit"));
  argp.add_option('v', "verbose", "",
                  _("Enable info level log output"));
  argp.add_option('D', "debug", "", 
                  _("Enable debug level log output"));
  argp.add_option('Q', "quiet", "", 
                  _("Disable all log output"));   

  argp.add_group(_("Display Options:"));
  argp.add_option('w', "window", "",
                  _("Start in Window Mode"));
  argp.add_option('f', "fullscreen", "",
                  _("Start in Fullscreen"));
  argp.add_option('r', "renderer", "RENDERER",
                  _("Use the given renderer (default: sdl)"));
  argp.add_option('g', "geometry", "{width}x{height}",  
                  _("Set the window resolution for pingus (default: 800x600)"));
  argp.add_option('R', "fullscreen-resolution", "{width}x{height}",  
                  _("Set the resolution used in fullscreen mode (default: 800x600)"));
  argp.add_option(346, "software-cursor", "",
                  _("Enable software cursor"));

  argp.add_group(_("Game Options:"));
  argp.add_option(337, "no-auto-scrolling", "",
                  _("Disable automatic scrolling"));
  argp.add_option(338, "drag-drop-scrolling", "",
                  _("Enable drag'n drop scrolling"));

  argp.add_group(_("Sound Options:"));
  argp.add_option('s', "disable-sound", "", 
                  _("Disable sound"));
  argp.add_option('m', "disable-music", "", 
                  _("Disable music"));

  argp.add_group("Language Options:");
  argp.add_option('l', "language", "LANG",
                  _("Select language to use with Pingus"));
  argp.add_option(365, "list-languages", "",
                  _("List all available languages"));

  argp.add_group("Editor Options:");
  argp.add_option('e', "editor", "",
                  _("Loads the level editor"));

  argp.add_group(_("Directory Options:"));
  argp.add_option('d', "datadir", _("DIR"),
                  _("Load game datafiles from DIR"));
  argp.add_option('u', "userdir", _("DIR"),
                  _("Load config files and store savegames in DIR"));
  argp.add_option('a', "addon", _("DIR"),
                  _("Load game modifications from DIR"));
  argp.add_option(342, "no-cfg-file", "",
                  _("Don't read ~/.pingus/config"));
  argp.add_option('c', "config", _("FILE"),
                  _("Read config options from FILE"));
  argp.add_option(360, "controller", "FILE",
                  _("Uses the controller given in FILE"));

  argp.add_group(_("Debug Options:"));
  argp.add_option(334, "developer-mode",  "",  
                  _("Enables some special features for developers"));
  argp.add_option('t', "speed", "SPEED",
                  _("Set the game speed (0=fastest, >0=slower)"));
  argp.add_option('k', "fps", "FPS",
                  _("Set the desired game framerate (frames per second)"));
  argp.add_option(344, "tile-size", "INT",
                  _("Set the size of the map tiles (default: 32)"));

  argp.parse_args(argc, argv);
  argp.set_help_indent(20);

  while (argp.next())
  {
    switch (argp.get_key()) 
    {          
      case 'r': // --renderer
        if (argp.get_argument() == "help")
        {
          std::cout << "Available renderers: " << std::endl;
          std::cout << "   delta: Software rendering with dirty-rectangles" << std::endl;
          std::cout << "     sdl: Software rendering" << std::endl;
          std::cout << "  opengl: Hardware accelerated graphics" << std::endl;
          std::cout << "    null: No rendering at all, for debugging" << std::endl;
          exit(EXIT_SUCCESS);
        }
        else
        {
          cmd_options.framebuffer_type.set(framebuffer_type_from_string(argp.get_argument()));

          //FIXME:
          //std::cout << "Unknown renderer: " << argp.get_argument()
          //<< " use '--renderer help' to get a list of available renderer" << std::endl;
          //exit(EXIT_FAILURE);
        }
        break;

      case 'e': // -e, --editor
        cmd_options.editor.set(true);
        break;

      case 't': // -t, --set-speed
        cmd_options.speed.set(StringUtil::to<int>(argp.get_argument()));  
        break;

      case 'k': // -k, --set-fps
        cmd_options.desiredfps.set(StringUtil::to<float>(argp.get_argument()));
        break;

      case 's': // -s, --disable-sound
        cmd_options.disable_sound.set(true);
        break;

      case 'm': // -m, --disable-music
        cmd_options.disable_music.set(true);
        break;
            
      case 'g':
      {
        Size size; 
        if (sscanf(argp.get_argument().c_str(), "%dx%d", &size.width, &size.height) != 2)
        {
          std::cout << "Resolution std::string is wrong, it should be like: \n"
                    << "\"640x480\" or \"800x600\"" << std::endl;
          exit(EXIT_FAILURE);
        }
        cmd_options.geometry.set(size);
      }
      break;

      case 'R':
      {
        Size size; 
        if (sscanf(argp.get_argument().c_str(), "%dx%d", &size.width, &size.height) != 2)
        {
          std::cout << "Resolution std::string is wrong, it should be like: \n"
                    << "\"640x480\" or \"800x600\"" << std::endl;
          exit(EXIT_FAILURE);
        }
        cmd_options.fullscreen_resolution.set(size);
      }
      break;

      case 'd': // -d, --datadir
        cmd_options.datadir.set(argp.get_argument());
        break;

      case 'a': // -a, --addon
        g_path_manager.add_overlay_path(argp.get_argument());
        break;

      case 'u': // -u, --userdir
        cmd_options.userdir.set(argp.get_argument());
        break;

      case 'V':
        std::cout <<
          "Pingus 0.7.6\n"
          "Copyright (C) 1998-2011 Ingo Ruhnke <*****@*****.**>\n"
          "See the file AUTHORS for a complete list of contributors.\n"
          "Pingus comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n"
          "welcome to redistribute it under certain conditions; see the file COPYING for details."
                  << std::endl;
        exit(EXIT_SUCCESS);
        break;
          
      case 'f': // --fullscreen
        cmd_options.fullscreen.set(true);
        break;
          
      case 'w': // --window
        cmd_options.fullscreen.set(false);
        break;

      case 334: // --developer-mode
        cmd_options.developer_mode.set(true);
        globals::developer_mode = true;
        break;

      case 337:
        cmd_options.auto_scrolling.set(false);
        break;

      case 338:
        cmd_options.drag_drop_scrolling.set(true);
        break;

      case 342: // --no-cfg-file
        cmd_options.no_config_file.set(true); 
        break;

      case 344:
        cmd_options.tile_size.set(StringUtil::to<int>(argp.get_argument()));
        break;

      case 346:
        cmd_options.software_cursor.set(true);
        break;

      case 'c':
        cmd_options.merge(Options::from_file(Pathname(argp.get_argument(), Pathname::SYSTEM_PATH)));
        break;

      case 'D':
        g_logger.set_log_level(Logger::kDebug);
        break;

      case 'v':
        g_logger.set_log_level(Logger::kInfo);
        break;

      case 'Q':
        g_logger.set_log_level(Logger::kNone);
        break;

      case 360:
        cmd_options.controller.set(argp.get_argument());
        break;

      case 'l': // language
        cmd_options.language.set(argp.get_argument());
        break;

      case 365: // list-languages
        cmd_options.list_languages.set(true);
        break;

      case 'h':
        argp.print_help();
        exit(EXIT_SUCCESS);
        break;

      case CommandLine::REST_ARG:
        if (!cmd_options.rest.is_set())
        {
          cmd_options.rest.set(argp.get_argument());
        }
        else
        {
          std::cout << "Wrong argument: '" << argp.get_argument() << "'" << std::endl;
          std::cout << "You can only give one file argument," << std::endl;
          exit(EXIT_FAILURE);
        }
        break;

      default:
        std::cout << "Error: Got " << argp.get_key() << " " << argp.get_argument() << std::endl;
        break;
    }
  }
}
Example #2
0
int main(int argc, char** argv)
{
  try {
    bool fullscreen = false;
    int screen_width  = 800;
    int screen_height = 600;
    int canvas_width  = 2048;
    int canvas_height = 2048;
    std::string hostname;
    std::string port     = "4711";
    int rest_arg_count = 0;
    std::string stylus;

    CommandLine argp;

    argp.add_usage("[OPTIONS] HOSTNAME PORT");
    argp.add_group("Display:");
    argp.add_option('g', "geometry",  "WIDTHxHEIGHT", "Set the windows size to WIDTHxHEIGHT");
    argp.add_option('c', "canvas",    "WIDTHxHEIGHT", "Set the size of the paintable canvas to WIDTHxHEIGHT");
    argp.add_option('f', "fullscreen", "",            "Start the application in fullscreen mode");
    argp.add_option('w', "window",     "",            "Start the application in window mode");
    argp.add_option('v', "version",    "",            "Display the netBrush version");
    argp.add_option('i', "input",      "NAME",        "Use XInput device NAME for drawing (tablet support)");
    argp.add_option('h', "help",       "",            "Show this help text");

    argp.parse_args(argc, argv);

    while(argp.next())
      {
        switch(argp.get_key())
          {
          case 'g':
            {
              if (sscanf(argp.get_argument().c_str(), "%dx%d",
                         &screen_width, &screen_height) == 2)
                {
                  std::cout << "Geometry: " << screen_width << "x" << screen_height << std::endl;
                }
              else
                {
                  throw std::runtime_error("Geometry option '-g' requires argument of type {WIDTH}x{HEIGHT}");
                }
            }
            break;

          case 'i':
            {
              stylus = argp.get_argument();
            }
            break;

          case 'c':
            {
              if (sscanf(argp.get_argument().c_str(), "%dx%d",
                         &canvas_width, &canvas_height) == 2)
                {
                  std::cout << "Geometry: " << canvas_width << "x" << canvas_height << std::endl;
                }
              else
                {
                  throw std::runtime_error("Canvas option '-c' requires argument of type {WIDTH}x{HEIGHT}");
                }
            }
            break;

          case 'f':
            fullscreen = true;
            break;

          case 'w':
            fullscreen = false;
            break;

          case 'h':
            argp.print_help();
            return 0;
            break;

          case 'v':
            std::cout << "netBrush 0.1.0" << std::endl;
            break;

          case CommandLine::REST_ARG:
            //std::cout << "Rest: " << argp.get_argument() << std::endl;
            if (rest_arg_count == 0)
              {
                hostname = argp.get_argument();
                rest_arg_count += 1;
              }
            else if (rest_arg_count == 1)
              {
                port = argp.get_argument();
                rest_arg_count += 1;
              }
            else
              {
                std::cout << "Invalide argument " << argp.get_argument() << std::endl;
                exit(EXIT_FAILURE);
              }
            break;
          }
      }

    if(SDL_Init(SDL_INIT_VIDEO)== -1) {
      printf("SDL_Init: %s\n", SDL_GetError());
      exit(1);
    }
    atexit(SDL_Quit);

    if(SDLNet_Init()==-1) {
      printf("SDLNet_Init: %s\n", SDLNet_GetError());
      exit(2);
    }
    atexit(SDLNet_Quit);

    Uint32 flags = SDL_HWSURFACE;
    if (fullscreen)
      flags |= SDL_FULLSCREEN;
    screen = SDL_SetVideoMode(screen_width, screen_height, 32, flags); 
    if (screen == 0)
      printf("SDL_SetVideoMode: %s\n", SDL_GetError());
    SDL_WM_SetCaption("netBrush", "netBrush");

    if (!stylus.empty()) // enable tablet support
      {
        SDL_VERSION(&syswm.version); // this is important!
        if (SDL_GetWMInfo(&syswm) == -1)
          {
            std::cout << "Couldn't get WM info " << std::endl;
          }

        syswm.info.x11.lock_func();
        xinput = new InputDevice_XInput(syswm.info.x11.display, syswm.info.x11.window, stylus);
        syswm.info.x11.unlock_func();

        SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
      }

    // 18 is scrollbar
    screen_buffer = new ScreenBuffer(Rect(38, 2, screen->w - 128 - 18 - 2 - 2, screen->h - 16 - 4 - 38)); 
    draw_ctx      = new DrawingContext(canvas_width, canvas_height);
    stroke_buffer = new StrokeBuffer(canvas_width, canvas_height);
    
    //std::cout << "# clear screen" << std::endl;

    // clear screen
    draw_ctx->clear();

    //std::cout << "# clear screen done" << std::endl;

    client_draw_param = new DrawingParameter();
    stroke_buffer->set_param(client_draw_param);
  
    server = new ServerConnection();
    if (!hostname.empty() && !port.empty())
      {
        std::cout << "Connecting to: " << hostname << ":" << atoi(port.c_str()) << std::endl;
        server->connect(hostname.c_str(), atoi(port.c_str()));
        std::ostringstream title_line;
        title_line << "netBrush - online: " << hostname << ":" << atoi(port.c_str());
        SDL_WM_SetCaption(title_line.str().c_str(), "netBrush");
      }
    else
      {
        std::cout << "# use '" << argv[0] << " HOSTNAME PORT' to connect a networking session" << std::endl;
        SDL_WM_SetCaption("netBrush - offline mode", "netBrush");
      }
  
    widget_manager = new WidgetManager();
    controller     = new Controller();

    widget_manager->add(navigation = new Navigation(Rect(Point(screen->w - 128 - 2, screen->h - 128 - 2),
                                                         Size(128, 128))));
    {
      SDL_Rect color_rect;
      color_rect.x = 768;
      color_rect.y = 100;

      color_rect.w = 128;
      color_rect.h = 128;

      //widget_manager->add(new ColorSelector(&color_rect));
    }

    widget_manager->add(screen_buffer);

    widget_manager->add(vertical_scrollbar = 
                        new Scrollbar(0, canvas_height, screen_buffer->get_rect().get_height(), Scrollbar::VERTICAL,
                                      Rect(screen->w - 128 - 16 - 2 - 2, 2,
                                           screen->w - 128 - 2 - 2, screen->h - 16 - 4 - 38)));

    widget_manager->add(horizontal_scrollbar = 
                        new Scrollbar(0, canvas_width, screen_buffer->get_rect().get_width(), Scrollbar::HORIZONTAL,
                                      Rect(38, screen->h - 16 - 2 - 38,
                                           screen->w - 128 - 18 - 2 - 2, screen->h - 2 - 38)));

    // Main Loop
    while(true)
      {
        process_events();
        server->update();
        widget_manager->update();
        SDL_Delay(10);
      }
  } catch(std::exception& err) {
    std::cout << "Exception: " << err.what() << std::endl;
  }
  
  return 0;
}
Example #3
0
int main(int argc, char** argv)
{
  try {
    std::string port;
    CommandLine argp;

    argp.add_usage("[OPTIONS] PORT");
    argp.add_group("General:");
    argp.add_option('v', "version", "", "Print the netBrush server version");
    argp.add_option('h', "help", "", "Print this help");

    argp.parse_args(argc, argv);
    while(argp.next())
      {
        switch(argp.get_key())
          {
          case 'h':
            argp.print_help();
            return 0;
            break;

          case 'v':
            std::cout << "netBrush Server 0.1.0" << std::endl;
            return 0;
            break;

          case CommandLine::REST_ARG:
            if (!port.empty())
              {
                std::cout << "Invalid argument: " << argp.get_argument() << std::endl;
                return 1;
              }
            else
              {
                port = argp.get_argument();
              }
            break;
          }
      }

    if (port.empty())
      {
        argp.print_help();
        return 1;
      }

    if(SDL_Init(0)==-1) {
      printf("SDL_Init: %s\n", SDL_GetError());
      exit(1);
    }

    if(SDLNet_Init()==-1) {
      printf("SDLNet_Init: %s\n", SDLNet_GetError());
      exit(2);
    }

    atexit(SDL_Quit);
    atexit(SDLNet_Quit);

    std::ostringstream filename;
    filename << "sessions/session-" << time(NULL) << ".nbr";

    std::cout << "# writing log to " << filename.str() << std::endl;
    outfile = new std::ofstream(filename.str().c_str());

    if (argc == 2)
      {
        std::cout << "# listening on: " << port << std::endl;
        connect(atoi(port.c_str()));
      }
    else
      {
        std::cout << "Usage: " << argv[0] << " PORT" << std::endl;
      }

    outfile->close();
    delete outfile;
  } catch (std::exception& err) {
    std::cout << "Exception: " << err.what() << std::endl;
  }
  return 0;
}