src/Application.cc
author terom
Sun, 07 Dec 2008 22:15:21 +0000
changeset 267 2cb6f1421e45
parent 185 25becd2cb026
child 358 37b18b779ffb
permissions -rw-r--r--
enable graphics by default unless --server
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#include "Engine.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
#include "Error.hh"
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
#include <stdexcept>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
#include <ClanLib/core.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
#include <ClanLib/application.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
class ArgumentError : public Error {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
        ArgumentError (const std::string &message) : Error(message) { }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
};
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
class Main : public CL_ClanApplication {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
    private:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
        // arguments
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    19
        CL_CommandLine args;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
        bool arg_graphics;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
        std::string arg_port;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
        bool arg_server;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
        std::string arg_connect;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    25
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    26
        void parse_args (int argc, char **argv) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
            // set up the options
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
            args.add_option('p', "port", "PORT", "set network port used", true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
            args.add_option('s', "server", "", "act as a network server", true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    30
            args.add_option('c', "client", "SERVERHOST", "act as a network client", true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    31
            args.add_option('g', "graphics", "", "run graphics/local input. Implied with --connect", true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    32
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
            // set defaults
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    34
            arg_graphics = false;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
            arg_port = NETWORK_PORT_STR;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    36
            arg_server = false;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    37
            arg_connect = "";
267
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    38
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    39
            // extra state
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    40
            bool graphics_default = true;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    41
            
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    42
            try {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    43
                // parse args
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    44
                args.parse_args(argc, argv);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    46
            } catch (CL_Error &e) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    47
                throw ArgumentError(e.message);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    49
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
            while (args.next()) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
                switch (args.get_key()) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    52
                    case 'p':
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    53
                        arg_port = args.get_argument();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
                        break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    55
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    56
                    case 's':
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    57
                        arg_server = true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    58
                        break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    59
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    60
                    case 'c':
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
                        arg_connect = args.get_argument();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
                        break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    63
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    64
                    case 'g':
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    65
                        arg_graphics = true;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    66
                        break;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
                    case CL_CommandLine::REST_ARG:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
                        throw ArgumentError(args.get_argument());
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    70
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
                    default:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    72
                        throw ArgumentError(std::string(1, (char) args.get_key()));
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    73
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
            }
267
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    76
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
            // check for invalid combinations of arugments
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    78
            if (arg_server and !arg_connect.empty())
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
                throw ArgumentError("cannot be both server and client");
267
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    80
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    81
            // enable graphics by default unless server
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    82
            if (!arg_server && graphics_default)
2cb6f1421e45 enable graphics by default unless --server
terom
parents: 185
diff changeset
    83
                arg_graphics = true;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    84
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    85
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
    public:
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    87
        virtual int main (int argc, char **argv) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    88
            // initialize the ClanLib components that we use
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    89
            CL_SetupCore setup_core;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    90
            CL_SetupNetwork setup_network;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    91
            CL_SetupDisplay setup_disp;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    92
            CL_SetupGL setup_gl;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    93
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
            try {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
                // parse arugments
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    96
                parse_args(argc, argv);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    97
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
                // our engine
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
                Engine engine;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   100
                
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   101
                // setup graphics
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   102
                if (arg_graphics)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   103
                    engine.setupGraphics();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   105
                // setup either network server, client or singleplayer
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
                if (arg_server) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
                    engine.setupNetworkServer(arg_port);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
                } else if (!arg_connect.empty()) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
                    engine.setupNetworkClient(arg_connect, arg_port);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
                
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   112
                } else {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
                    engine.setupSinglePlayer();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   114
                }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   115
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   116
                // run the main loop
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
                engine.run();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   118
                
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   119
                // succesful return
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   120
                return 0;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   121
            
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   122
            } catch (ArgumentError &e) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   123
                std::cerr << e.what() << std::endl;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   124
                args.print_help();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   125
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
                // XXX: handle --help
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
                return 1;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   128
            } catch (CL_Error &e) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   129
                std::cerr << "main: CL_Error:" << e.message << std::endl;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   130
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   131
                return 1;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   132
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   133
            } catch (std::exception &e) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   134
                std::cerr << "FATAL [uncaught_exception] " << e.what() << std::endl;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   135
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   136
                return 1;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   137
            }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   138
        }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   139
} app;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   140