ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.53
Committed: Sun Apr 9 22:17:40 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.52: +4 -1 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 root 1.53 my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y);
241     my $widget = $TOPLEVEL->find_widget ($x, $y);
242    
243     warn "mouse $x, $y = $widget\n";
244 root 1.13 },
245     SDL_MOUSEBUTTONDOWN() => sub {
246 root 1.22 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
247 root 1.13 },
248     SDL_MOUSEBUTTONUP() => sub {
249 root 1.22 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
250 root 1.13 },
251     SDL_ACTIVEEVENT() => sub {
252     warn "active\n";#d#
253     },
254     );
255 root 1.1
256 root 1.2 @conn::ISA = Crossfire::Protocol::;
257 root 1.1
258 root 1.2 sub conn::map_update {
259 root 1.1 my ($self, $dirty) = @_;
260    
261 root 1.2 refresh;
262 root 1.1 }
263    
264 root 1.2 sub conn::map_scroll {
265 root 1.1 my ($self, $dx, $dy) = @_;
266    
267 root 1.45 # refresh;
268 root 1.1 }
269    
270 root 1.2 sub conn::map_clear {
271 root 1.1 my ($self) = @_;
272    
273 root 1.45 # refresh;
274 root 1.1 }
275    
276 root 1.19 sub conn::face_find {
277     my ($self, $face) = @_;
278    
279     $FACECACHE->{"$face->{chksum},$face->{name}"}
280     }
281    
282 root 1.2 sub conn::face_update {
283 root 1.19 my ($self, $face) = @_;
284    
285     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
286 root 1.1
287 root 1.18 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
288 root 1.1 }
289    
290 root 1.33 sub conn::query {
291     my ($self, $flags, $prompt) = @_;
292    
293     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
294     }
295    
296 elmex 1.23 sub gtk_add_cfg_field {
297     my ($tbl, $cfg, $klbl, $key, $value) = @_;
298     my $i = $cfg->{_i}++;
299     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
300     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
301     if ($key eq 'password') {
302     $ent->set_invisible_char ("*");
303     $ent->set (visibility => 0)
304     }
305     $ent->set_text ($value);
306     $ent->signal_connect (changed => sub {
307     my ($ent) = @_;
308     $cfg->{$key} = $ent->get_text;
309 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
310 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
311     $cfg->{$key} = 100;
312 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
313     $cfg->{$key} = 50;
314 elmex 1.47 }
315 elmex 1.23 });
316     }
317    
318     sub run_config_dialog {
319     my (%events) = @_;
320    
321     my $w = Gtk2::Window->new;
322    
323     my @cfg = (
324     [qw/Host host/],
325     [qw/Port port/],
326 elmex 1.47 [qw/Mapsize% mapsize/],
327 elmex 1.23 [qw/Username user/],
328     [qw/Password password/],
329     );
330    
331     my $cfg = {};
332    
333     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
334     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
335    
336     $w->add (my $vb = Gtk2::VBox->new);
337     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
338     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
339     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
340     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
341     my $i = 0;
342     my $act = 0;
343     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
344     if ($_ eq $selmode) { $act = $i }
345     $cb->append_text ($_);
346     $i++;
347     }
348     $cb->set_active ($act);
349     $cb->signal_connect (changed => sub {
350     my ($cb) = @_;
351     my $txt = $cb->get_active_text;
352     if ($txt =~ m/(\d+)x(\d+)/) {
353     $::CFG->{width} = $1;
354     $::CFG->{height} = $2;
355     }
356     });
357    
358     $cfg->{_i} = 1;
359     for (@cfg) {
360     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
361     }
362    
363     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
364     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
365     $cb->signal_connect (clicked => sub {
366     for (keys %$cfg) {
367     $::CFG->{$_} = $cfg->{$_}
368     if $_ ne '_i';
369     }
370     Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
371     });
372     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
373     $cb->signal_connect (clicked => sub {
374     for (keys %$cfg) {
375     $::CFG->{$_} = $cfg->{$_}
376     if $_ ne '_i';
377     }
378     my $cb = $events{login} || sub {};
379     $cb->($::CFG->{user}, $::CFG->{password});
380     });
381     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
382     $cb->signal_connect (clicked => sub {
383 root 1.50 my $cb = $events{logout} || sub {};
384 elmex 1.23 $cb->();
385     });
386     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
387     $cb->signal_connect (clicked => sub { $w->destroy });
388    
389     $w->show_all;
390    
391     $w->signal_connect (destroy => sub { Gtk2->main_quit });
392     }
393    
394    
395 root 1.1 #############################################################################
396    
397 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
398 elmex 1.20
399 elmex 1.41 $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
400    
401 elmex 1.20 my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
402    
403 elmex 1.41 $TOPLEVEL->add ($mapwidget);
404 elmex 1.16 $mapwidget->focus_in;
405    
406 root 1.22 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
407 elmex 1.10
408 root 1.13 $CFG ||= {
409     width => 640,
410     height => 480,
411 elmex 1.47 mapsize => 100,
412 root 1.13 fullscreen => 0,
413     host => "crossfire.schmorp.de",
414     port => 13327,
415     };
416 elmex 1.12
417 root 1.42 Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
418 root 1.40
419 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
420    
421 elmex 1.23 run_config_dialog
422 elmex 1.16 login => sub { start_game },
423     logout => sub { stop_game };
424 root 1.1
425 root 1.13 main Gtk2;
426 root 1.19
427     Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";