ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.86 by root, Wed Apr 12 18:09:22 2006 UTC vs.
Revision 1.87 by root, Wed Apr 12 20:06:37 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8; 4use utf8;
5 5
6use Glib; 6use Time::HiRes 'time';
7use Gtk2 -init; 7use Event;
8 8
9use SDL; 9use SDL;
10use SDL::App; 10use SDL::App;
11use SDL::Event; 11use SDL::Event;
12use SDL::Surface; 12use SDL::Surface;
20 20
21our $VERSION = '0.1'; 21our $VERSION = '0.1';
22 22
23my $MAX_FPS = 60; 23my $MAX_FPS = 60;
24my $MIN_FPS = 5; # unused 24my $MIN_FPS = 5; # unused
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26 25
27our $FACECACHE; 26our $FACECACHE;
27
28our $LAST_REFRESH;
29our $NOW;
28 30
29our $CFG; 31our $CFG;
30our $CONN; 32our $CONN;
31our $FAST; # fast, low-quality mode, possibly useful for software-rendering 33our $FAST; # fast, low-quality mode, possibly useful for software-rendering
32 34
33our @SDL_MODES; 35our @SDL_MODES;
34our $WIDTH; 36our $WIDTH;
35our $HEIGHT; 37our $HEIGHT;
36our $FULLSCREEN; 38our $FULLSCREEN;
37 39
38our $NOW;
39
40our $MAPWIDGET; 40our $MAPWIDGET;
41our $FONTSIZE; 41our $FONTSIZE;
42 42
43our $SDL_TIMER;
44our $SDL_ACTIVE; 43our $SDL_ACTIVE;
45our $SDL_EV; 44our $SDL_EV;
46our %SDL_CB; 45our %SDL_CB;
47 46
48our $ALT_ENTER_MESSAGE; 47our $ALT_ENTER_MESSAGE;
49our $STATUS_LINE; 48our $STATUS_LINE;
50our $DEBUG_STATUS; 49our $DEBUG_STATUS;
51
52my $last_refresh;
53my %ANIMATE;
54my $refresh_handler;
55 50
56sub status { 51sub status {
57 $STATUS_LINE->set_text ($_[0]); 52 $STATUS_LINE->set_text ($_[0]);
58 my ($w, $h) = $STATUS_LINE->size_request; 53 my ($w, $h) = $STATUS_LINE->size_request;
59 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); 54 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h);
159 154
160sub init_screen { 155sub init_screen {
161 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; 156 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
162 $FULLSCREEN = $CFG->{fullscreen}; 157 $FULLSCREEN = $CFG->{fullscreen};
163 158
164 SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO
165 and die "SDL::Init failed!\n";
166
167 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; 159 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
168 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; 160 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
169 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; 161 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
170 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; 162 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0;
171 163
188 $SDL_EV = new SDL::Event; 180 $SDL_EV = new SDL::Event;
189 $SDL_EV->set_unicode (1); 181 $SDL_EV->set_unicode (1);
190 182
191 $SDL_ACTIVE = 1; 183 $SDL_ACTIVE = 1;
192 184
193 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub { 185 $LAST_REFRESH = time - 0.01;
194 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
195 while $SDL_EV->poll;
196
197 1
198 };
199
200 $last_refresh = SDL::GetTicks;
201 186
202 CFClient::gl_init; 187 CFClient::gl_init;
203 188
204 $FONTSIZE = int $HEIGHT / 40; 189 $FONTSIZE = int $HEIGHT / 40;
205 190
227 config_dialog; 212 config_dialog;
228} 213}
229 214
230sub destroy_screen { 215sub destroy_screen {
231 $CFClient::UI::TOPLEVEL->{children} = []; 216 $CFClient::UI::TOPLEVEL->{children} = [];
232 remove Glib::Source $SDL_TIMER;
233 remove Glib::Source $refresh_handler if $refresh_handler;
234 undef $refresh_handler;
235 undef $SDL_ACTIVE; 217 undef $SDL_ACTIVE;
236 undef $SDL_EV; 218 undef $SDL_EV;
237 SDL::Quit; 219 SDL::Quit;
238} 220}
239 221
222my %animate_object;
223my $animate_timer;
224
225my $want_refresh;
226my $can_refresh;
227
228my $fps = 9;
229
240sub force_refresh { 230sub force_refresh {
231 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
232 debug sprintf "%3.2f", $fps;
233
234 $want_refresh = 0;
235 $can_refresh = 0;
236
241 glViewport 0, 0, $WIDTH, $HEIGHT; 237 glViewport 0, 0, $WIDTH, $HEIGHT;
242 238
243 glMatrixMode GL_PROJECTION; 239 glMatrixMode GL_PROJECTION;
244 glLoadIdentity; 240 glLoadIdentity;
245 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; 241 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
249 glClear GL_COLOR_BUFFER_BIT; 245 glClear GL_COLOR_BUFFER_BIT;
250 246
251 $CFClient::UI::TOPLEVEL->draw; 247 $CFClient::UI::TOPLEVEL->draw;
252 248
253 SDL::GLSwapBuffers; 249 SDL::GLSwapBuffers;
254}
255 250
256my $FPS; 251 $LAST_REFRESH = $NOW;
252}
253
254my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
255 $NOW = time;
256
257 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
258 while $SDL_EV->poll;
259
260 if (%animate_object) {
261 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
262 $want_refresh++;
263 }
264
265 if ($want_refresh) {
266 force_refresh;
267 } else {
268 $can_refresh = 1;
269 }
270});
257 271
258sub refresh { 272sub refresh {
259 $refresh_handler ||= add Glib::Idle sub { 273 $want_refresh++;
260 if ($SDL_ACTIVE) {
261 $NOW = SDL::GetTicks;
262
263 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
264 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
265 $NOW = SDL::GetTicks;
266 }
267
268 my $interval = ($NOW - $last_refresh) * 0.001;
269 $last_refresh = $NOW;
270
271 if ($interval) {
272 $FPS ||= 1 / $interval;
273 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
274 debug sprintf "%5.02f", $FPS;
275 }
276
277 force_refresh;
278 $_->animate ($interval) for grep $_, values %ANIMATE;
279
280 if (%ANIMATE) {
281 1
282 } else {
283 undef $refresh_handler;
284 0
285 }
286 } else {
287 undef $refresh_handler;
288 0
289 }
290 };
291} 274}
292 275
293sub animation_start { 276sub animation_start {
294 my ($widget) = @_; 277 my ($widget) = @_;
295 $ANIMATE{$widget} = $widget; 278 $animate_object{$widget} = $widget;
296 Scalar::Util::weaken $ANIMATE{$widget};
297
298 refresh;
299} 279}
300 280
301sub animation_stop { 281sub animation_stop {
302 my ($widget) = @_; 282 my ($widget) = @_;
303 delete $ANIMATE{$widget}; 283 delete $animate_object{$widget};
284}
285
286@conn::ISA = Crossfire::Protocol::;
287
288sub conn::map_update {
289 my ($self, $dirty) = @_;
290
291 $MAPWIDGET->update;
292}
293
294sub conn::map_scroll {
295 my ($self, $dx, $dy) = @_;
296
297# refresh;
298}
299
300sub conn::map_clear {
301 my ($self) = @_;
302
303# refresh;
304}
305
306sub conn::face_find {
307 my ($self, $face) = @_;
308
309 $FACECACHE->{"$face->{chksum},$face->{name}"}
310}
311
312sub conn::face_update {
313 my ($self, $face) = @_;
314
315 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
316
317 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
318}
319
320sub conn::query {
321 my ($self, $flags, $prompt) = @_;
322
323 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
304} 324}
305 325
306%SDL_CB = ( 326%SDL_CB = (
307 SDL_QUIT() => sub { 327 SDL_QUIT() => sub {
308 main_quit Gtk2; 328 Event::unloop -1;
309 }, 329 },
310 SDL_VIDEORESIZE() => sub { 330 SDL_VIDEORESIZE() => sub {
311 }, 331 },
312 SDL_VIDEOEXPOSE() => sub { 332 SDL_VIDEOEXPOSE() => sub {
313 refresh; 333 refresh;
337 SDL_ACTIVEEVENT() => sub { 357 SDL_ACTIVEEVENT() => sub {
338# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 358# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
339 }, 359 },
340); 360);
341 361
342@conn::ISA = Crossfire::Protocol::;
343
344sub conn::map_update {
345 my ($self, $dirty) = @_;
346
347 $MAPWIDGET->update;
348}
349
350sub conn::map_scroll {
351 my ($self, $dx, $dy) = @_;
352
353# refresh;
354}
355
356sub conn::map_clear {
357 my ($self) = @_;
358
359# refresh;
360}
361
362sub conn::face_find {
363 my ($self, $face) = @_;
364
365 $FACECACHE->{"$face->{chksum},$face->{name}"}
366}
367
368sub conn::face_update {
369 my ($self, $face) = @_;
370
371 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
372
373 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
374}
375
376sub conn::query {
377 my ($self, $flags, $prompt) = @_;
378
379 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
380}
381
382sub gtk_add_cfg_field {
383 my ($tbl, $cfg, $klbl, $key, $value) = @_;
384 my $i = $cfg->{_i}++;
385 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
386 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
387 if ($key eq 'password') {
388 $ent->set_invisible_char ("*");
389 $ent->set (visibility => 0)
390 }
391 $ent->set_text ($value);
392 $ent->signal_connect (changed => sub {
393 my ($ent) = @_;
394 $cfg->{$key} = $ent->get_text;
395 # TODO: Mapsize should be a slider in the game gui
396 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
397 $cfg->{$key} = 100;
398 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
399 $cfg->{$key} = 50;
400 }
401 });
402}
403
404sub run_config_dialog {
405 my (%events) = @_;
406
407 my $w = Gtk2::Window->new;
408
409 my @cfg = (
410 [qw/Host host/],
411 [qw/Port port/],
412 [qw/Mapsize% mapsize/],
413 [qw/Username user/],
414 [qw/Password password/],
415 );
416
417 my $cfg = {};
418
419 $w->add (my $vb = Gtk2::VBox->new);
420 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
421 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
422 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
423 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
424 my $i = 0;
425 my $act = 0;
426 for (map { "$_->[0]x$_->[1]" } @SDL_MODES) {
427 if ($_ eq $selmode) { $act = $i }
428 $cb->append_text ($_);
429 $i++;
430 }
431 $cb->set_active ($act);
432 $cb->signal_connect (changed => sub {
433 my ($cb) = @_;
434 my $txt = $cb->get_active_text;
435 if ($txt =~ m/(\d+)x(\d+)/) {
436 $::CFG->{width} = $1;
437 $::CFG->{height} = $2;
438 }
439 });
440
441 $cfg->{_i} = 1;
442 for (@cfg) {
443 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
444 }
445
446 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
447 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
448 $cb->signal_connect (clicked => sub {
449 for (keys %$cfg) {
450 $::CFG->{$_} = $cfg->{$_}
451 if $_ ne '_i';
452 }
453 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
454 });
455 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
456 $cb->signal_connect (clicked => sub {
457 for (keys %$cfg) {
458 $::CFG->{$_} = $cfg->{$_}
459 if $_ ne '_i';
460 }
461 my $cb = $events{login} || sub {};
462 $cb->($::CFG->{user}, $::CFG->{password});
463 });
464 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
465 $cb->signal_connect (clicked => sub {
466 my $cb = $events{logout} || sub {};
467 $cb->();
468 });
469 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
470 $cb->signal_connect (clicked => sub { $w->destroy });
471
472 $w->show_all;
473
474 $w->signal_connect (destroy => sub { Gtk2->main_quit });
475}
476
477
478############################################################################# 362#############################################################################
479
480SDL::Init SDL_INIT_EVERYTHING;
481
482@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
483 @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE };
484 363
485CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 364CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
486 365
487$CFG ||= { 366$CFG ||= {
488 width => 640, 367 width => 640,
492 mapsize => 100, 371 mapsize => 100,
493 host => "crossfire.schmorp.de", 372 host => "crossfire.schmorp.de",
494 port => 13327, 373 port => 13327,
495}; 374};
496 375
376SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO
377 and die "SDL::Init failed!\n";
378
379@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
380 @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL };
381
382init_screen;
383
497{ 384{
498 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); 385 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
499 386
500 CFClient::add_font $_ for @fonts; 387 CFClient::add_font $_ for @fonts;
501 CFClient::set_font $fonts[0]; 388 CFClient::set_font $fonts[0];
502} 389}
503 390
504$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 391$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
505 392
506init_screen; 393Event::loop;
507
508main Gtk2;
509 394
510Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 395Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";
511 396

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines