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