ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.83
Committed: Wed Apr 12 15:35:55 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.82: +4 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2 root 1.25
3 root 1.2 use strict;
4 root 1.25 use utf8;
5 root 1.2
6 root 1.13 use Glib;
7     use Gtk2 -init;
8    
9     use SDL;
10     use SDL::App;
11     use SDL::Event;
12     use SDL::Surface;
13     use SDL::OpenGL;
14     use SDL::OpenGL::Constants;
15    
16 elmex 1.11 use Crossfire;
17 root 1.2 use Crossfire::Protocol;
18    
19 root 1.67 use CFClient;
20 root 1.72 use CFClient::UI;
21 elmex 1.10
22 root 1.63 our $VERSION = '0.1';
23    
24 root 1.75 my $MAX_FPS = 60;
25 root 1.63 my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26    
27 root 1.19 our $FACECACHE;
28    
29 elmex 1.10 our $CFG;
30 root 1.13 our $CONN;
31 root 1.80 our $FAST; # fast, low-quality mode
32 root 1.2
33 root 1.75 our @SDL_MODES;
34 root 1.30 our $WIDTH;
35     our $HEIGHT;
36     our $FULLSCREEN;
37    
38 root 1.71 our $NOW;
39    
40 root 1.69 our $MAPWIDGET;
41 root 1.39 our $FONTSIZE;
42 root 1.57
43 root 1.13 our $SDL_TIMER;
44     our $SDL_APP;
45 root 1.58 our $SDL_EV;
46 root 1.13 our %SDL_CB;
47 root 1.18
48 root 1.30 our $ALT_ENTER_MESSAGE;
49 root 1.51 our $STATUS_LINE;
50 root 1.64 our $DEBUG_STATUS;
51 root 1.30
52 root 1.45 my $last_refresh;
53 root 1.50 my %ANIMATE;
54     my $refresh_handler;
55 root 1.45
56 root 1.82 sub status {
57     $STATUS_LINE->set_text ($_[0]);
58     my ($w, $h) = $STATUS_LINE->size_request;
59     $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h);
60     }
61    
62     sub debug {
63     $DEBUG_STATUS->set_text ($_[0]);
64     my ($w, $h) = $DEBUG_STATUS->size_request;
65     $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
66     }
67    
68 root 1.81 sub config_dialog {
69     my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
70     child => (my $vbox = new CFClient::UI::VBox);
71 root 1.82 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
72 root 1.81 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
73    
74     $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
75     $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
76    
77     $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
78     $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
79    
80     $mode_slider->connect (changed => sub {
81     my ($self, $value) = @_;
82    
83     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
84     $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
85     });
86     $mode_slider->emit (changed => $mode_slider->{range}[0]);
87 root 1.82
88     $table->add (1, 1, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
89 root 1.83 destroy_screen ();
90     ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
91     $FULLSCREEN = $CFG->{fullscreen};
92     init_screen ();
93 root 1.82 });
94 root 1.81
95 root 1.82 $vbox->add (new CFClient::UI::Label align => 0, text => "Server");
96     $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
97     $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host");
98     $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host});
99 root 1.81
100 root 1.82 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port");
101     $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port});
102 root 1.81
103 root 1.82 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
104     $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user});
105 root 1.81
106 root 1.82 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
107     $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
108 root 1.81
109 root 1.82 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size");
110     $table->add (1, 6, new CFClient::UI::Slider
111 root 1.81 req_w => 100,
112     range => [$CFG->{mapsize}, 10, 100 + 1, 1],
113     connect_changed => sub {
114     my ($self, $value) = @_;
115    
116     $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
117     },
118     );
119    
120 root 1.82 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
121     warn "login\n";
122     });
123    
124 root 1.81 $vbox->add (my $hbox = new CFClient::UI::HBox);
125 root 1.82
126 root 1.81 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
127 root 1.82 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
128     status "Configuration Saved";
129 root 1.81 });
130     $CFClient::UI::TOPLEVEL->add ($dialog);
131     }
132 root 1.58
133 root 1.13 sub init_screen {
134     $SDL_APP = new SDL::App
135 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
136     -title => "Crossfire+ Client",
137 root 1.30 -width => $WIDTH,
138     -height => $HEIGHT,
139 root 1.5 -opengl => 1,
140 root 1.51 -red_size => 5,
141     -green_size => 5,
142     -blue_size => 5,
143     -alpha_size => 0,
144 root 1.2 -double_buffer => 1,
145 root 1.30 -fullscreen => $FULLSCREEN,
146 root 1.5 -resizeable => 0;
147 root 1.2
148 root 1.58 $SDL_EV = new SDL::Event;
149     $SDL_EV->set_unicode (1);
150    
151 root 1.76 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub {
152 root 1.62 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
153     while $SDL_EV->poll;
154    
155     1
156     };
157    
158 root 1.45 $last_refresh = SDL::GetTicks;
159    
160 root 1.67 CFClient::gl_init;
161 root 1.30
162 root 1.77 $FONTSIZE = int $HEIGHT / 40;
163 root 1.39
164 root 1.52 #############################################################################
165    
166 root 1.77 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
167 root 1.72 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
168 root 1.52
169 root 1.72 $STATUS_LINE = new CFClient::UI::Label
170 root 1.77 padding => 0,
171     y => $HEIGHT * 49 / 50 - $FONTSIZE;
172 root 1.72 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
173 root 1.51
174 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
175 root 1.77 padding => 0,
176     y => $HEIGHT * 49 / 50,
177     height => $HEIGHT / 50,
178 root 1.68 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
179 root 1.72 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
180 root 1.30
181 root 1.72 $MAPWIDGET = new CFClient::UI::MapWidget;
182     $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
183 root 1.69 $MAPWIDGET->focus_in;
184 root 1.81
185     config_dialog;
186 root 1.2 }
187    
188 root 1.62 sub destroy_screen {
189 root 1.81 $CFClient::UI::TOPLEVEL->{children} = [];
190 root 1.62 remove Glib::Source $SDL_TIMER;
191     undef $SDL_APP;
192     undef $SDL_EV;
193     SDL::Quit;
194     }
195    
196 root 1.13 sub start_game {
197 root 1.30 $WIDTH = $CFG->{width};
198     $HEIGHT = $CFG->{height};
199     $FULLSCREEN = 0;
200    
201 root 1.13 init_screen;
202    
203 root 1.59 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
204 root 1.50
205 root 1.13 $CONN = new conn
206     host => $CFG->{host},
207 root 1.28 port => $CFG->{port},
208     user => $CFG->{user},
209 root 1.37 pass => $CFG->{password},
210 root 1.50 mapw => $mapsize,
211     maph => $mapsize,
212 root 1.37 ;
213 root 1.45
214 root 1.67 CFClient::lowdelay fileno $CONN->{fh};
215 root 1.13 }
216    
217     sub stop_game {
218 root 1.51 remove Glib::Source $refresh_handler if $refresh_handler;
219 root 1.50 undef $refresh_handler;
220 root 1.13
221 root 1.50 undef $CONN;
222 root 1.62 destroy_screen;
223 root 1.13 }
224    
225 root 1.30 sub force_refresh {
226     glViewport 0, 0, $WIDTH, $HEIGHT;
227 root 1.35
228     glMatrixMode GL_PROJECTION;
229     glLoadIdentity;
230 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
231 root 1.35 glMatrixMode GL_MODELVIEW;
232 root 1.73 glLoadIdentity;
233 root 1.35
234 root 1.5 glClear GL_COLOR_BUFFER_BIT;
235 root 1.2
236 root 1.72 $CFClient::UI::TOPLEVEL->draw;
237 root 1.1
238 root 1.2 SDL::GLSwapBuffers;
239 root 1.1 }
240    
241 root 1.64 my $FPS;
242    
243 root 1.30 sub refresh {
244     $refresh_handler ||= add Glib::Idle sub {
245 root 1.58 if ($SDL_APP) {
246 root 1.71 $NOW = SDL::GetTicks;
247 root 1.63
248 root 1.71 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
249     SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
250     $NOW = SDL::GetTicks;
251 root 1.63 }
252    
253 root 1.71 my $interval = ($NOW - $last_refresh) * 0.001;
254     $last_refresh = $NOW;
255 root 1.58
256 root 1.64 if ($interval) {
257     $FPS ||= 1 / $interval;
258 root 1.77 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
259 root 1.66 debug sprintf "%5.02f", $FPS;
260 root 1.64 }
261    
262 root 1.58 force_refresh;
263     $_->animate ($interval) for grep $_, values %ANIMATE;
264    
265     if (%ANIMATE) {
266     1
267     } else {
268     undef $refresh_handler;
269     0
270     }
271 root 1.45 } else {
272     undef $refresh_handler;
273     0
274     }
275 root 1.30 };
276     }
277    
278 root 1.45 sub animation_start {
279     my ($widget) = @_;
280     $ANIMATE{$widget} = $widget;
281     Scalar::Util::weaken $ANIMATE{$widget};
282    
283     refresh;
284     }
285    
286     sub animation_stop {
287     my ($widget) = @_;
288     delete $ANIMATE{$widget};
289     }
290    
291 root 1.13 %SDL_CB = (
292     SDL_QUIT() => sub {
293 root 1.19 main_quit Gtk2;
294 root 1.13 },
295     SDL_VIDEORESIZE() => sub {
296     },
297     SDL_VIDEOEXPOSE() => sub {
298     refresh;
299     },
300     SDL_KEYDOWN() => sub {
301 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
302     # alt-enter
303 root 1.30 $FULLSCREEN = !$FULLSCREEN;
304 root 1.81 destroy_screen;
305 root 1.18 init_screen;
306     } else {
307 root 1.72 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
308 root 1.18 }
309 root 1.13 },
310     SDL_KEYUP() => sub {
311 root 1.72 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
312 root 1.13 },
313     SDL_MOUSEMOTION() => sub {
314 root 1.72 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
315 root 1.13 },
316     SDL_MOUSEBUTTONDOWN() => sub {
317 root 1.72 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
318 root 1.13 },
319     SDL_MOUSEBUTTONUP() => sub {
320 root 1.72 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
321 root 1.13 },
322     SDL_ACTIVEEVENT() => sub {
323 root 1.70 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
324 root 1.13 },
325     );
326 root 1.1
327 root 1.2 @conn::ISA = Crossfire::Protocol::;
328 root 1.1
329 root 1.2 sub conn::map_update {
330 root 1.1 my ($self, $dirty) = @_;
331    
332 root 1.69 $MAPWIDGET->update;
333 root 1.1 }
334    
335 root 1.2 sub conn::map_scroll {
336 root 1.1 my ($self, $dx, $dy) = @_;
337    
338 root 1.45 # refresh;
339 root 1.1 }
340    
341 root 1.2 sub conn::map_clear {
342 root 1.1 my ($self) = @_;
343    
344 root 1.45 # refresh;
345 root 1.1 }
346    
347 root 1.19 sub conn::face_find {
348     my ($self, $face) = @_;
349    
350     $FACECACHE->{"$face->{chksum},$face->{name}"}
351     }
352    
353 root 1.2 sub conn::face_update {
354 root 1.19 my ($self, $face) = @_;
355    
356     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
357 root 1.1
358 root 1.67 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
359 root 1.1 }
360    
361 root 1.33 sub conn::query {
362     my ($self, $flags, $prompt) = @_;
363    
364     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
365     }
366    
367 elmex 1.23 sub gtk_add_cfg_field {
368     my ($tbl, $cfg, $klbl, $key, $value) = @_;
369     my $i = $cfg->{_i}++;
370     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
371     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
372     if ($key eq 'password') {
373     $ent->set_invisible_char ("*");
374     $ent->set (visibility => 0)
375     }
376     $ent->set_text ($value);
377     $ent->signal_connect (changed => sub {
378     my ($ent) = @_;
379     $cfg->{$key} = $ent->get_text;
380 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
381 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
382     $cfg->{$key} = 100;
383 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
384     $cfg->{$key} = 50;
385 elmex 1.47 }
386 elmex 1.23 });
387     }
388    
389     sub run_config_dialog {
390     my (%events) = @_;
391    
392     my $w = Gtk2::Window->new;
393    
394     my @cfg = (
395     [qw/Host host/],
396     [qw/Port port/],
397 elmex 1.47 [qw/Mapsize% mapsize/],
398 elmex 1.23 [qw/Username user/],
399     [qw/Password password/],
400     );
401    
402     my $cfg = {};
403    
404     $w->add (my $vb = Gtk2::VBox->new);
405     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
406     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
407     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
408     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
409     my $i = 0;
410     my $act = 0;
411 root 1.75 for (map { "$_->[0]x$_->[1]" } @SDL_MODES) {
412 elmex 1.23 if ($_ eq $selmode) { $act = $i }
413     $cb->append_text ($_);
414     $i++;
415     }
416     $cb->set_active ($act);
417     $cb->signal_connect (changed => sub {
418     my ($cb) = @_;
419     my $txt = $cb->get_active_text;
420     if ($txt =~ m/(\d+)x(\d+)/) {
421     $::CFG->{width} = $1;
422     $::CFG->{height} = $2;
423     }
424     });
425    
426     $cfg->{_i} = 1;
427     for (@cfg) {
428     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
429     }
430    
431     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
432     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
433     $cb->signal_connect (clicked => sub {
434     for (keys %$cfg) {
435     $::CFG->{$_} = $cfg->{$_}
436     if $_ ne '_i';
437     }
438 root 1.67 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
439 elmex 1.23 });
440     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
441     $cb->signal_connect (clicked => sub {
442     for (keys %$cfg) {
443     $::CFG->{$_} = $cfg->{$_}
444     if $_ ne '_i';
445     }
446     my $cb = $events{login} || sub {};
447     $cb->($::CFG->{user}, $::CFG->{password});
448     });
449     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
450     $cb->signal_connect (clicked => sub {
451 root 1.50 my $cb = $events{logout} || sub {};
452 elmex 1.23 $cb->();
453     });
454     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
455     $cb->signal_connect (clicked => sub { $w->destroy });
456    
457     $w->show_all;
458    
459     $w->signal_connect (destroy => sub { Gtk2->main_quit });
460     }
461    
462    
463 root 1.1 #############################################################################
464    
465 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
466 elmex 1.20
467 root 1.75 @SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
468     @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE };
469    
470 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
471 elmex 1.10
472 root 1.13 $CFG ||= {
473     width => 640,
474     height => 480,
475 root 1.82 fullscreen => 0,
476     sdl_mode => 0,
477 elmex 1.47 mapsize => 100,
478 root 1.13 host => "crossfire.schmorp.de",
479     port => 13327,
480     };
481 elmex 1.12
482 root 1.65 {
483 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
484 root 1.65
485 root 1.67 CFClient::add_font $_ for @fonts;
486     CFClient::set_font $fonts[0];
487 root 1.65 }
488 root 1.40
489 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
490    
491 elmex 1.23 run_config_dialog
492 elmex 1.16 login => sub { start_game },
493     logout => sub { stop_game };
494 root 1.1
495 root 1.13 main Gtk2;
496 root 1.19
497 root 1.68 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";
498 root 1.82