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

# Content
1 #!/opt/bin/perl
2
3 use strict;
4 use utf8;
5
6 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 use Crossfire;
17 use Crossfire::Client;
18 use Crossfire::Protocol;
19
20 use Crossfire::Client::Widget;
21
22 our $FACECACHE;
23
24 our $VERSION = '0.1';
25
26 our %GL_EXT;
27
28 our $CFG;
29 our $CONN;
30
31 our $WIDTH;
32 our $HEIGHT;
33 our $FULLSCREEN;
34
35 our $FONTSIZE;
36
37 our $SDL_TIMER;
38 our $SDL_APP;
39 our $SDL_EV = new SDL::Event;
40 our %SDL_CB;
41
42 our @GL_INIT; # hooks called on every gl init
43
44 our $ALT_ENTER_MESSAGE;
45 our $STATUS_LINE;
46
47 our $TOPLEVEL;
48
49 our $tw; # Test widget #d#
50
51 my $last_refresh;
52 my %ANIMATE;
53 my $refresh_handler;
54
55 sub init_screen {
56 $SDL_APP = new SDL::App
57 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
58 -title => "Crossfire+ Client",
59 -width => $WIDTH,
60 -height => $HEIGHT,
61 -opengl => 1,
62 -red_size => 5,
63 -green_size => 5,
64 -blue_size => 5,
65 -alpha_size => 0,
66 -double_buffer => 1,
67 -fullscreen => $FULLSCREEN,
68 -resizeable => 0;
69
70 $last_refresh = SDL::GetTicks;
71
72 %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 $FONTSIZE = int $HEIGHT / 50;
78
79 #############################################################################
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 $STATUS_LINE = new Crossfire::Client::Widget::Label
94 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
95 "";
96 $TOPLEVEL->add ($STATUS_LINE);
97
98 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
99 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
100 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
101 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
102
103 # Test code #d#
104 unless ($tw) { # haha...
105 $tw = new Crossfire::Client::Widget::Animator;
106 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 $tw->move ($WIDTH - 200, 0);
123 $tw->moveto (0, 0);
124 $TOPLEVEL->add ($tw);
125
126 # $f->move ($WIDTH - 200, 0);
127 # $TOPLEVEL->add ($f);
128 }
129 }
130
131 sub start_game {
132 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
133 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
134 while $SDL_EV->poll;
135
136 1
137 };
138
139 $WIDTH = $CFG->{width};
140 $HEIGHT = $CFG->{height};
141 $FULLSCREEN = 0;
142
143 init_screen;
144
145 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32;
146
147 $CONN = new conn
148 host => $CFG->{host},
149 port => $CFG->{port},
150 user => $CFG->{user},
151 pass => $CFG->{password},
152 mapw => $mapsize,
153 maph => $mapsize,
154 ;
155
156 Crossfire::Client::lowdelay fileno $CONN->{fh};
157 }
158
159 sub stop_game {
160 remove Glib::Source $SDL_TIMER;
161 remove Glib::Source $refresh_handler if $refresh_handler;
162 undef $refresh_handler;
163
164 undef $SDL_APP;
165 undef $CONN;
166 SDL::Quit;
167 }
168
169
170 sub force_refresh {
171 glViewport 0, 0, $WIDTH, $HEIGHT;
172
173 glMatrixMode GL_PROJECTION;
174 glLoadIdentity;
175 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000;
176 glMatrixMode GL_MODELVIEW;
177
178 glClear GL_COLOR_BUFFER_BIT;
179
180 $TOPLEVEL->draw;
181
182 SDL::GLSwapBuffers;
183 }
184
185 sub refresh {
186 $refresh_handler ||= add Glib::Idle sub {
187 return unless $SDL_APP;
188
189 my $next_refresh = SDL::GetTicks;
190 my $interval = ($next_refresh - $last_refresh) * 0.001;
191 $last_refresh = $next_refresh;
192
193 force_refresh;
194 $_->animate ($interval) for grep $_, values %ANIMATE;
195
196 if (%ANIMATE) {
197 1
198 } else {
199 undef $refresh_handler;
200 0
201 }
202 };
203 }
204
205 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 %SDL_CB = (
219 SDL_QUIT() => sub {
220 main_quit Gtk2;
221 },
222 SDL_VIDEORESIZE() => sub {
223 },
224 SDL_VIDEOEXPOSE() => sub {
225 refresh;
226 },
227 SDL_KEYDOWN() => sub {
228 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
229 # alt-enter
230 $FULLSCREEN = !$FULLSCREEN;
231 init_screen;
232 } else {
233 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
234 }
235 },
236 SDL_KEYUP() => sub {
237 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
238 },
239 SDL_MOUSEMOTION() => sub {
240 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 },
245 SDL_MOUSEBUTTONDOWN() => sub {
246 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
247 },
248 SDL_MOUSEBUTTONUP() => sub {
249 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
250 },
251 SDL_ACTIVEEVENT() => sub {
252 warn "active\n";#d#
253 },
254 );
255
256 @conn::ISA = Crossfire::Protocol::;
257
258 sub conn::map_update {
259 my ($self, $dirty) = @_;
260
261 refresh;
262 }
263
264 sub conn::map_scroll {
265 my ($self, $dx, $dy) = @_;
266
267 # refresh;
268 }
269
270 sub conn::map_clear {
271 my ($self) = @_;
272
273 # refresh;
274 }
275
276 sub conn::face_find {
277 my ($self, $face) = @_;
278
279 $FACECACHE->{"$face->{chksum},$face->{name}"}
280 }
281
282 sub conn::face_update {
283 my ($self, $face) = @_;
284
285 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
286
287 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
288 }
289
290 sub conn::query {
291 my ($self, $flags, $prompt) = @_;
292
293 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
294 }
295
296 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 # TODO: Mapsize should be a slider in the game gui
310 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
311 $cfg->{$key} = 100;
312 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
313 $cfg->{$key} = 50;
314 }
315 });
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 [qw/Mapsize% mapsize/],
327 [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 my $cb = $events{logout} || sub {};
384 $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 #############################################################################
396
397 SDL::Init SDL_INIT_EVERYTHING;
398
399 $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
400
401 my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
402
403 $TOPLEVEL->add ($mapwidget);
404 $mapwidget->focus_in;
405
406 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
407
408 $CFG ||= {
409 width => 640,
410 height => 480,
411 mapsize => 100,
412 fullscreen => 0,
413 host => "crossfire.schmorp.de",
414 port => 13327,
415 };
416
417 Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
418
419 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
420
421 run_config_dialog
422 login => sub { start_game },
423 logout => sub { stop_game };
424
425 main Gtk2;
426
427 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";