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