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