ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.81
Committed: Wed Apr 12 02:05:46 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.80: +60 -60 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.81 sub config_dialog {
57     my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
58     child => (my $vbox = new CFClient::UI::VBox);
59     $vbox->add (new CFClient::UI::Label align => 0, text => "Setup");
60     $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
61    
62     $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
63     $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
64    
65     $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
66     $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
67    
68     $mode_slider->connect (changed => sub {
69     my ($self, $value) = @_;
70    
71     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
72     $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
73     });
74     $mode_slider->emit (changed => $mode_slider->{range}[0]);
75    
76     $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Host");
77     $table->add (1, 1, my $host = new CFClient::UI::Entry text => $CFG->{host});
78    
79     $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Port");
80     $table->add (1, 2, my $port = new CFClient::UI::Entry text => $CFG->{port});
81    
82     $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Username");
83     $table->add (1, 3, my $user = new CFClient::UI::Entry text => $CFG->{user});
84    
85     $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Password");
86     $table->add (1, 4, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
87    
88     $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Map Size");
89     $table->add (1, 5, new CFClient::UI::Slider
90     req_w => 100,
91     range => [$CFG->{mapsize}, 10, 100 + 1, 1],
92     connect_changed => sub {
93     my ($self, $value) = @_;
94    
95     $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
96     },
97     );
98    
99     $vbox->add (my $hbox = new CFClient::UI::HBox);
100     $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
101     warn "save\n";
102     });
103     $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
104     warn "login\n";
105     });
106     $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
107     warn "apply\n";
108     });
109    
110     $CFClient::UI::TOPLEVEL->add ($dialog);
111     }
112 root 1.58
113 root 1.13 sub init_screen {
114     $SDL_APP = new SDL::App
115 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
116     -title => "Crossfire+ Client",
117 root 1.30 -width => $WIDTH,
118     -height => $HEIGHT,
119 root 1.5 -opengl => 1,
120 root 1.51 -red_size => 5,
121     -green_size => 5,
122     -blue_size => 5,
123     -alpha_size => 0,
124 root 1.2 -double_buffer => 1,
125 root 1.30 -fullscreen => $FULLSCREEN,
126 root 1.5 -resizeable => 0;
127 root 1.2
128 root 1.58 $SDL_EV = new SDL::Event;
129     $SDL_EV->set_unicode (1);
130    
131 root 1.76 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub {
132 root 1.62 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
133     while $SDL_EV->poll;
134    
135     1
136     };
137    
138 root 1.45 $last_refresh = SDL::GetTicks;
139    
140 root 1.67 CFClient::gl_init;
141 root 1.30
142 root 1.77 $FONTSIZE = int $HEIGHT / 40;
143 root 1.39
144 root 1.52 #############################################################################
145    
146 root 1.77 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
147 root 1.72 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
148 root 1.52
149 root 1.72 $STATUS_LINE = new CFClient::UI::Label
150 root 1.77 padding => 0,
151     y => $HEIGHT * 49 / 50 - $FONTSIZE;
152 root 1.72 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
153 root 1.51
154 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
155 root 1.77 padding => 0,
156     y => $HEIGHT * 49 / 50,
157     height => $HEIGHT / 50,
158 root 1.68 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
159 root 1.72 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
160 root 1.30
161 root 1.72 $MAPWIDGET = new CFClient::UI::MapWidget;
162     $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
163 root 1.69 $MAPWIDGET->focus_in;
164 root 1.81
165     config_dialog;
166 root 1.2 }
167    
168 root 1.62 sub destroy_screen {
169 root 1.81 $CFClient::UI::TOPLEVEL->{children} = [];
170 root 1.62 remove Glib::Source $SDL_TIMER;
171     undef $SDL_APP;
172     undef $SDL_EV;
173     SDL::Quit;
174     }
175    
176 root 1.13 sub start_game {
177 root 1.30 $WIDTH = $CFG->{width};
178     $HEIGHT = $CFG->{height};
179     $FULLSCREEN = 0;
180    
181 root 1.13 init_screen;
182    
183 root 1.59 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
184 root 1.50
185 root 1.13 $CONN = new conn
186     host => $CFG->{host},
187 root 1.28 port => $CFG->{port},
188     user => $CFG->{user},
189 root 1.37 pass => $CFG->{password},
190 root 1.50 mapw => $mapsize,
191     maph => $mapsize,
192 root 1.37 ;
193 root 1.45
194 root 1.67 CFClient::lowdelay fileno $CONN->{fh};
195 root 1.13 }
196    
197     sub stop_game {
198 root 1.51 remove Glib::Source $refresh_handler if $refresh_handler;
199 root 1.50 undef $refresh_handler;
200 root 1.13
201 root 1.50 undef $CONN;
202 root 1.62 destroy_screen;
203 root 1.13 }
204    
205 root 1.30 sub force_refresh {
206     glViewport 0, 0, $WIDTH, $HEIGHT;
207 root 1.35
208     glMatrixMode GL_PROJECTION;
209     glLoadIdentity;
210 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
211 root 1.35 glMatrixMode GL_MODELVIEW;
212 root 1.73 glLoadIdentity;
213 root 1.35
214 root 1.5 glClear GL_COLOR_BUFFER_BIT;
215 root 1.2
216 root 1.72 $CFClient::UI::TOPLEVEL->draw;
217 root 1.1
218 root 1.2 SDL::GLSwapBuffers;
219 root 1.1 }
220    
221 root 1.64 sub debug {
222     $DEBUG_STATUS->set_text ($_[0]);
223 root 1.75 my ($w, $h) = $DEBUG_STATUS->size_request;
224     $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
225 root 1.64 }
226    
227     my $FPS;
228    
229 root 1.30 sub refresh {
230     $refresh_handler ||= add Glib::Idle sub {
231 root 1.58 if ($SDL_APP) {
232 root 1.71 $NOW = SDL::GetTicks;
233 root 1.63
234 root 1.71 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
235     SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
236     $NOW = SDL::GetTicks;
237 root 1.63 }
238    
239 root 1.71 my $interval = ($NOW - $last_refresh) * 0.001;
240     $last_refresh = $NOW;
241 root 1.58
242 root 1.64 if ($interval) {
243     $FPS ||= 1 / $interval;
244 root 1.77 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
245 root 1.66 debug sprintf "%5.02f", $FPS;
246 root 1.64 }
247    
248 root 1.58 force_refresh;
249     $_->animate ($interval) for grep $_, values %ANIMATE;
250    
251     if (%ANIMATE) {
252     1
253     } else {
254     undef $refresh_handler;
255     0
256     }
257 root 1.45 } else {
258     undef $refresh_handler;
259     0
260     }
261 root 1.30 };
262     }
263    
264 root 1.45 sub animation_start {
265     my ($widget) = @_;
266     $ANIMATE{$widget} = $widget;
267     Scalar::Util::weaken $ANIMATE{$widget};
268    
269     refresh;
270     }
271    
272     sub animation_stop {
273     my ($widget) = @_;
274     delete $ANIMATE{$widget};
275     }
276    
277 root 1.13 %SDL_CB = (
278     SDL_QUIT() => sub {
279 root 1.19 main_quit Gtk2;
280 root 1.13 },
281     SDL_VIDEORESIZE() => sub {
282     },
283     SDL_VIDEOEXPOSE() => sub {
284     refresh;
285     },
286     SDL_KEYDOWN() => sub {
287 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
288     # alt-enter
289 root 1.30 $FULLSCREEN = !$FULLSCREEN;
290 root 1.81 destroy_screen;
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";