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