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