ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.65
Committed: Tue Apr 11 12:27:51 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.64: +6 -1 lines
Log Message:
add typemap and bold/?italic fonts

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