ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/dclient/src/frontend/client/main.cpp
Revision: 1.4
Committed: Tue Oct 19 09:45:10 2010 UTC (16 years ago) by sf-pippijn
Branch: MAIN
Changes since 1.3: +3 -1 lines
Log Message:
- use wchar_t for single characters (glyphs and line drawing)
- merge ncurses and ndk wrappers
- simplify colour caching

File Contents

# User Rev Content
1 sf-pippijn 1.1 #include "autoconf.h"
2    
3     #include "dclient/asio_service_manager.h"
4     #include "dclient/client.h"
5     #include "dclient/input_handler.h"
6     #include "dclient/query_handler.h"
7     #include "dclient/renderer.h"
8     #include "dclient/widgets.h"
9 sf-pippijn 1.2 #include "dclient/widgets/inventory.h"
10 sf-pippijn 1.1
11     #include "deliantra/asio_communicator.h"
12     #include "deliantra/command_parser.h"
13     #include "deliantra/handshake_handler.h"
14 sf-pippijn 1.2 #include "deliantra/inventory_manager.h"
15 sf-pippijn 1.1 #include "deliantra/message_logger.h"
16    
17     #include "deliantra/data/item.h"
18    
19     #include "adt/bind.h"
20    
21     #include <ndk++/application.hh>
22    
23     #include <boost/asio/posix/stream_descriptor.hpp>
24 sf-pippijn 1.3 #include <boost/asio/deadline_timer.hpp>
25 sf-pippijn 1.1
26 sf-pippijn 1.3 #include <csignal>
27 sf-pippijn 1.1 #include <cstring>
28     #include <iomanip>
29     #include <memory>
30    
31     template<typename Handler>
32     struct fd_listener
33     {
34     fd_listener (boost::asio::io_service &io_service, int fd, Handler &handler)
35     : in (io_service, fd)
36     , handler (handler)
37     , reading (false)
38     {
39     read_more ();
40     }
41    
42     private:
43     void read_more ()
44     {
45     if (!reading)
46     {
47     reading = true;
48     in.async_read_some (boost::asio::null_buffers (), bind (&fd_listener::handle_read, this));
49     }
50     }
51    
52     void handle_read (boost::system::error_code const &error, size_t length)
53     {
54     reading = false;
55     handler.handle_read (error, length);
56     read_more ();
57     }
58    
59     boost::asio::posix::stream_descriptor in;
60     Handler &handler;
61     bool reading;
62     };
63    
64     static void
65     start_game ( std::string const &server
66     , std::string const &port
67     , std::string const &user
68     , std::string const &pass
69     )
70     {
71     // ncurses
72 sf-pippijn 1.4 setlocale (LC_ALL, "");
73     std::auto_ptr<ndk::application> app (ndk::app = new ndk::application);
74     app->init_palette ();
75 sf-pippijn 1.1
76     // asio
77     boost::asio::io_service io_service;
78    
79     // deliantra
80     asio_communicator comm (io_service, server, port);
81     command_parser cmdp;
82     handshake_handler hh (comm);
83     message_logger logger;
84 sf-pippijn 1.2 inventory_manager invman;
85 sf-pippijn 1.1
86     // dclient
87     client cli (comm, cmdp);
88     widgets ui;
89     renderer rend (ui);
90     asio_service_manager svs (io_service);
91     input_handler input (comm, svs, ui);
92     fd_listener<input_handler> in (io_service, STDIN_FILENO, input);
93     query_handler qh (comm, ui, user, pass);
94    
95     // link them all together
96 sf-pippijn 1.2 invman.set_inventory_view (*ui.inventory);
97     invman.set_container_view (*ui.container);
98 sf-pippijn 1.1 comm.set_packet_handler (cli);
99     cmdp.add_command_handler (hh);
100     cmdp.add_command_handler (qh);
101 sf-pippijn 1.2 cmdp.add_command_handler (invman);
102 sf-pippijn 1.1 cmdp.add_command_handler (rend);
103     cmdp.add_command_handler (logger);
104    
105     // run the event loop
106     io_service.run ();
107     }
108    
109    
110     int
111     main (int argc, char *argv[])
112     try
113     {
114     if (argc == 2 && !strcmp (argv[1], "--version"))
115     {
116     printf ("%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
117     return EXIT_SUCCESS;
118     }
119    
120     if (argc < 3 || !strcmp (argv[1], "--help"))
121     {
122     printf ("Usage: %s <host> <port> [user] [password]\n", PACKAGE);
123     return EXIT_SUCCESS;
124     }
125    
126     std::string server, port, user, pass;
127    
128     server = argv[1];
129     port = argv[2];
130     if (argc == 5)
131     {
132     user = argv[3];
133     pass = argv[4];
134     }
135    
136     start_game (server, port, user, pass);
137    
138     return EXIT_SUCCESS;
139     }
140     catch (std::exception const &e)
141     {
142 sf-pippijn 1.3 LOG ("exception") << e.what () << "\n";
143 sf-pippijn 1.1 printf ("exception caught: %s\n", e.what ());
144 sf-pippijn 1.3 return EXIT_FAILURE;
145 sf-pippijn 1.1 }
146     catch (int i)
147     {
148 sf-pippijn 1.3 LOG ("exception") << '0' << std::oct << std::setfill ('0') << std::setw (3) << i << "\n";
149 sf-pippijn 1.1 printf ("exception caught: 0%03o\n", i);
150 sf-pippijn 1.3 return EXIT_FAILURE;
151 sf-pippijn 1.1 }
152     catch (char const *s)
153     {
154 sf-pippijn 1.3 LOG ("exception") << s << "\n";
155 sf-pippijn 1.1 printf ("exception caught: %s\n", s);
156 sf-pippijn 1.3 return EXIT_FAILURE;
157 sf-pippijn 1.1 }
158     catch (...)
159     {
160 sf-pippijn 1.3 LOG ("exception");
161 sf-pippijn 1.1 puts ("exception caught");
162 sf-pippijn 1.3 return EXIT_FAILURE;
163 sf-pippijn 1.1 }