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