source: golgotha/src/golg/net/startup.cc

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 9.8 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "net/startup.hh"
10#include "loaders/load.hh"
11#include "device/device.hh"
12#include "device/event.hh"
13#include "gui/text_input.hh"
14#include "gui/text.hh"
15#include "gui/button.hh"
16#include "resources.hh"
17#include "network/login.hh"
18#include "mess_id.hh"
19#include "network/net_find.hh"
20#include "network/net_prot.hh"
21#include "time/timedev.hh"
22#include "net/client.hh"
23#include "net/server.hh"
24#include "app/app.hh"
25
26i4_event_reaction_class *g1_net_window_class::create_orec(int mess_id)
27{
28  i4_object_message_event_class *om=new i4_object_message_event_class(this, mess_id);
29  return new i4_event_reaction_class(this, om);
30}
31
32g1_net_window_class::g1_net_window_class(w16 w, w16 h,
33                                         i4_graphical_style_class *style,
34                                         i4_net_protocol *protocol,
35                                         char *bg_res,
36                                         int poll_delay,
37                                         int poll_id)
38  : i4_parent_window_class(w,h),
39    style(style),
40    protocol(protocol),
41    poll_delay(poll_delay),
42    poll_id(poll_id)
43{
44  bg=i4_load_image(i4gets(bg_res));
45
46  i4_object_message_event_class poll(this, poll_id);
47  poll_event_id=i4_time_dev.request_event(this, &poll, poll_delay);
48}
49   
50void g1_net_window_class::receive_event(i4_event *ev)
51{
52  if (ev->type()==i4_event::OBJECT_MESSAGE)
53  {
54    CAST_PTR(oev, i4_object_message_event_class, ev);
55    if (oev->object==this)
56    {
57      if (oev->sub_type==poll_id)
58      {
59        poll();
60
61        i4_object_message_event_class poll(this, poll_id);
62        poll_event_id=i4_time_dev.request_event(this, &poll, poll_delay);
63      }
64      else object_message(oev->sub_type);
65    }
66    else i4_parent_window_class::receive_event(ev);
67  }
68  else i4_parent_window_class::receive_event(ev);
69}
70
71
72void g1_net_window_class::parent_draw(i4_draw_context_class &context)
73{
74  if (bg)
75  {
76    local_image->clear(0, context);
77    bg->put_image(local_image, 0, 0, context);
78  }
79}
80
81
82g1_net_window_class::~g1_net_window_class()
83{
84  i4_time_dev.cancel_event(poll_event_id);
85
86  if (bg)
87    delete bg;
88}
89
90g1_startup_window::g1_startup_window(w16 w, w16 h,
91                                     i4_graphical_style_class *style,
92                                     i4_net_protocol *protocol)
93  : g1_net_window_class(w,h, style, protocol, "net_bg_image", 500, POLL)
94{
95  hostname=new i4_text_input_class(style, i4_string_man.get(0), 190, 200, this);
96  add_child(g1_resources.net_hostname_x, g1_resources.net_hostname_y, hostname);
97
98  username=new i4_text_input_class(style, *g1_resources.username, 90, 10, this);
99  add_child(g1_resources.net_username_x, g1_resources.net_username_y, username);
100
101  i4_text_window_class *start_game_text=new i4_text_window_class(i4gets("start_server"), style);
102  i4_button_class *new_game;
103  new_game=new i4_button_class(0,start_game_text, style, create_orec(START_SERVER));
104  add_child(g1_resources.net_start_x, g1_resources.net_start_y, new_game);
105
106  i4_text_window_class *quit_text=new i4_text_window_class(i4gets("net_main_menu"), style);
107  add_child(new_game->x(), new_game->y()+new_game->height()+1,
108            new i4_button_class(0, quit_text, style, create_orec(QUIT_NET_GAME)));
109
110  i4_net_protocol *p=protocol;
111  if (p)
112    find=p->create_finder_socket(g1_resources.net_find_port, g1_resources.net_find_port);
113  else find=0;
114
115  t_buts=0;
116  buts=0;
117}
118
119void g1_startup_window::free_buts()
120{
121  for (int i=0; i<t_buts; i++)
122  {
123    remove_child(buts[i]);
124    delete buts[i];
125  }
126   
127  if (buts)
128    i4_free(buts);
129  buts=0;
130  t_buts=0;
131}
132
133void g1_startup_window::grab_uname()
134{
135  delete g1_resources.username;
136  i4_query_text_input_class q;
137  i4_kernel.send_event(username, &q);
138  g1_resources.username=q.copy_of_data;
139  q.copy_of_data=0;
140}
141
142void g1_startup_window::object_message(int id)
143{
144  switch (id)
145  {
146    case START_SERVER :
147    {
148      grab_uname();
149
150      g1_server=new g1_server_class(g1_resources.net_udp_port, protocol);
151
152      i4_user_message_event_class u(G1_SERVER_MENU);
153      i4_kernel.send_event(i4_current_app, &u);
154    } break;
155
156    case QUIT_NET_GAME :
157    {
158      grab_uname();
159
160      i4_user_message_event_class u(G1_MAIN_MENU);
161      i4_kernel.send_event(i4_current_app, &u);
162    } break;
163  }
164
165  if (id>=LAST)
166  {
167    grab_uname();
168
169    i4_finder_socket::server s;
170    find->get_server(id-LAST, s);
171
172    s.addr->set_port(g1_resources.net_udp_port);
173    g1_client=new g1_client_class(s.addr, g1_resources.net_udp_port, protocol);
174
175    i4_user_message_event_class u(G1_CLIENT_JOINED_MENU);
176    i4_kernel.send_event(i4_current_app, &u);   
177  }
178}
179
180void g1_startup_window::poll()
181{
182  if (find)
183    if (find->poll())
184    {
185      free_buts();
186
187      t_buts=find->total_servers();
188      buts=(i4_button_class **)i4_malloc(sizeof(i4_button_class *)*t_buts,"but array");
189 
190      int y=g1_resources.net_found_y, x=g1_resources.net_found_x1;
191
192      for (int i=0; i<t_buts; i++)
193      {
194        i4_finder_socket::server s;
195        find->get_server(i, s);
196
197        i4_text_window_class *t=new i4_text_window_class(*s.notification_string, style);
198        buts[i]=new i4_button_class(0, t, style, create_orec(LAST + i));
199 
200        add_child(x,y, buts[i]);
201        y+=buts[i]->height()+1;
202      }
203    }
204}
205
206g1_startup_window::~g1_startup_window()
207{
208  if (find)
209    delete find;
210
211  free_buts();
212}
213
214g1_server_start_window::g1_server_start_window(w16 w, w16 h,
215                                               i4_graphical_style_class *style,
216                                               i4_net_protocol *protocol)
217  : g1_net_window_class(w,h, style, protocol, "server_bg_image", 100, POLL)
218{
219  memset(names,0,sizeof(names));
220
221  i4_text_window_class *start_game_text=new i4_text_window_class(i4gets("start_net_game"),
222                                                                 style);
223
224  i4_object_message_event_class *sng_e=new i4_object_message_event_class(this, START_NET_GAME);
225  i4_event_reaction_class *sng_r=new i4_event_reaction_class(this, sng_e);
226  i4_button_class *new_game=new i4_button_class(0, start_game_text, style, sng_r);
227  add_child(480,20, new_game);
228
229  i4_text_window_class *quit_text=new i4_text_window_class(i4gets("net_main_menu"), style);
230  i4_object_message_event_class *q_e=new i4_object_message_event_class(this, QUIT_NET_GAME);
231  i4_event_reaction_class *q_r=new i4_event_reaction_class(this, q_e);
232  i4_button_class *q=new i4_button_class(0, quit_text, style, q_r);
233  add_child(new_game->x(), new_game->y()+new_game->height()+1, q);
234
235  if (protocol)
236    note=protocol->create_notifier_socket(g1_resources.net_find_port, *g1_resources.username);
237  else note=0;
238}
239
240
241void g1_server_start_window::object_message(int id)
242{
243  switch (id)
244  {
245    case START_NET_GAME :
246    {
247      i4_user_message_event_class u(G1_START_NEW_GAME);
248      i4_kernel.send_event(i4_current_app, &u);
249    } break;
250
251    case QUIT_NET_GAME :
252    {
253      if (g1_server)
254      {
255        delete g1_server;
256        g1_server=0;
257      }
258
259      i4_user_message_event_class u(G1_MAIN_MENU);
260      i4_kernel.send_event(i4_current_app, &u);
261    } break;
262  }
263}
264
265void g1_server_start_window::poll()
266{
267  if (note)
268    note->poll();
269
270  if (g1_server)
271  {
272    g1_server->poll();
273    if (g1_server->list_changed)
274    {
275      g1_server->list_changed=i4_F;
276
277      int i;
278      for (i=0; i<G1_MAX_PLAYERS; i++)
279        if (names[i])
280        {
281          remove_child(names[i]);
282          names[i]=0;
283        }
284
285      i4_text_window_class *t;
286      int x=i4getn("net_joined_x"), y=i4getn("net_joined_y");
287
288      t=new i4_text_window_class(*g1_resources.username, style);
289      add_child(x, y, t);
290      y+=t->height()+1;
291      names[0]=t;
292
293      for (i=1; i<G1_MAX_PLAYERS; i++)
294      {
295        if (g1_server->clients[i].addr)
296        {
297          t=new i4_text_window_class(*g1_server->clients[i].username, style);
298          add_child(x,y,t);
299          y+=t->height()+1;
300          names[i]=t;
301        }
302      }
303    }
304  }
305}
306
307g1_server_start_window::~g1_server_start_window()
308{
309  if (note)
310    delete note;
311}
312
313
314
315g1_client_wait_window::g1_client_wait_window(w16 w, w16 h,
316                                             i4_graphical_style_class *style,
317                                             i4_net_protocol *protocol)
318  : g1_net_window_class(w,h, style, protocol, "client_wait_image", 100, POLL)
319{
320  i4_text_window_class *quit_text=new i4_text_window_class(i4gets("net_cancel"), style);
321  i4_object_message_event_class *q_e=new i4_object_message_event_class(this, QUIT_NET_GAME);
322  i4_event_reaction_class *q_r=new i4_event_reaction_class(this, q_e);
323  i4_button_class *q=new i4_button_class(0, quit_text, style, q_r);
324  add_child(i4getn("net_cancel_x"), i4getn("net_cancel_y"), q);
325 
326}
327
328void g1_client_wait_window::object_message(int id)
329{
330  if (id==QUIT_NET_GAME)
331  {
332    i4_user_message_event_class u(G1_MAIN_MENU);
333    i4_kernel.send_event(i4_current_app, &u);
334  }
335}
336
337void g1_client_wait_window::poll()
338{
339  if (!g1_client || !g1_client->poll())
340  {   
341    i4_user_message_event_class u(G1_MAIN_MENU);
342    i4_kernel.send_event(i4_current_app, &u);
343  }
344}
345
346g1_client_wait_window::~g1_client_wait_window()
347{
348  if (g1_client)
349  {
350    delete g1_client;
351    g1_client=0;
352  }
353}
Note: See TracBrowser for help on using the repository browser.