ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.46
Committed: Sun Apr 9 17:39:56 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.45: +19 -6 lines
Log Message:
added size_allocate

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