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