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