ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.80
Committed: Wed Apr 12 02:00:07 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.79: +1 -10 lines
Log Message:
bugfixes

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