--- deliantra/Deliantra-Client/bin/pclient 2006/04/07 20:15:01 1.23 +++ deliantra/Deliantra-Client/bin/pclient 2006/04/12 17:11:02 1.85 @@ -1,5 +1,7 @@ #!/opt/bin/perl + use strict; +use utf8; use Glib; use Gtk2 -init; @@ -12,89 +14,284 @@ use SDL::OpenGL::Constants; use Crossfire; -use Crossfire::Client; use Crossfire::Protocol; -use Crossfire::Client::Util; -use Crossfire::Client::Widget; - -our $FACECACHE; +use CFClient; +use CFClient::UI; our $VERSION = '0.1'; +my $MAX_FPS = 60; +my $MIN_FPS = 5; # unused +my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame + +our $FACECACHE; + our $CFG; our $CONN; +our $FAST; # fast, low-quality mode, possibly useful for software-rendering + +our @SDL_MODES; +our $WIDTH; +our $HEIGHT; +our $FULLSCREEN; + +our $NOW; + +our $MAPWIDGET; +our $FONTSIZE; our $SDL_TIMER; our $SDL_APP; -our $SDL_EV = new SDL::Event; +our $SDL_EV; our %SDL_CB; -our @GL_INIT; # hooks called on every gl init +our $ALT_ENTER_MESSAGE; +our $STATUS_LINE; +our $DEBUG_STATUS; + +my $last_refresh; +my %ANIMATE; +my $refresh_handler; + +sub status { + $STATUS_LINE->set_text ($_[0]); + my ($w, $h) = $STATUS_LINE->size_request; + $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); +} + +sub debug { + $DEBUG_STATUS->set_text ($_[0]); + my ($w, $h) = $DEBUG_STATUS->size_request; + $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); +} + +sub start_game { + status "logging in..."; + + my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; + + $CONN = new conn + host => $CFG->{host}, + port => $CFG->{port}, + user => $CFG->{user}, + pass => $CFG->{password}, + mapw => $mapsize, + maph => $mapsize, + ; + + status "login successful"; + + CFClient::lowdelay fileno $CONN->{fh}; +} + +sub stop_game { + undef $CONN; +} + +sub config_dialog { + my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, + child => (my $vbox = new CFClient::UI::VBox); + $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); + $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); + + $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); + $table->add (1, 0, my $hbox = new CFClient::UI::HBox); + + $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); + $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8); + + $mode_slider->connect (changed => sub { + my ($self, $value) = @_; + + $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; + $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); + }); + $mode_slider->emit (changed => $mode_slider->{range}[0]); + + $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen"); + $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub { + my ($self, $value) = @_; + $CFG->{fullscreen} = $value; + }); + + $table->add (1, 2, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { + destroy_screen (); + init_screen (); + }); + + $vbox->add (new CFClient::UI::Label align => 0, text => "Server"); + $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); + $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host"); + $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host}); + + $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port"); + $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port}); + + $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); + $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user}); + + $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); + $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); + + $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size"); + $table->add (1, 6, new CFClient::UI::Slider + req_w => 100, + range => [$CFG->{mapsize}, 10, 100 + 1, 1], + connect_changed => sub { + my ($self, $value) = @_; + + $CFG->{mapsize} = $self->{range}[0] = $value = int $value; + }, + ); + + $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { + start_game; + }); + + $vbox->add (my $hbox = new CFClient::UI::HBox); + + $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub { + CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; + status "Configuration Saved"; + }); + $CFClient::UI::TOPLEVEL->add ($dialog); +} sub init_screen { + ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; + $FULLSCREEN = $CFG->{fullscreen}; + $SDL_APP = new SDL::App -flags => SDL_ANYFORMAT | SDL_HWSURFACE, -title => "Crossfire+ Client", - -width => $CFG->{width}, - -height => $CFG->{height}, + -width => $WIDTH, + -height => $HEIGHT, -opengl => 1, - -red_size => 8, - -green_size => 8, - -blue_size => 8, + -red_size => 5, + -green_size => 5, + -blue_size => 5, + -alpha_size => 0, -double_buffer => 1, - -fullscreen => $CFG->{fullscreen}, + -fullscreen => $FULLSCREEN, -resizeable => 0; - glClearColor 0, 0, 0, 0; + $SDL_EV = new SDL::Event; + $SDL_EV->set_unicode (1); - glEnable GL_TEXTURE_2D; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - glShadeModel GL_FLAT; - glDisable GL_DEPTH_TEST; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - glEnable GL_BLEND; + $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub { + ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() + while $SDL_EV->poll; - glMatrixMode GL_PROJECTION; - glLoadIdentity; - glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100; + 1 + }; - glMatrixMode GL_MODELVIEW; + $last_refresh = SDL::GetTicks; - $_->() for @GL_INIT; -} + CFClient::gl_init; -sub start_game { - $SDL_TIMER = add Glib::Timeout 1000/20, sub { - while ($SDL_EV->poll) { - ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); - } + $FONTSIZE = int $HEIGHT / 40; - 1 - }; + ############################################################################# - init_screen; + $DEBUG_STATUS = new CFClient::UI::Label padding => 0; + $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); + + $STATUS_LINE = new CFClient::UI::Label + padding => 0, + y => $HEIGHT * 49 / 50 - $FONTSIZE; + $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); - $CONN = new conn - host => $CFG->{host}, - port => $CFG->{port}; + $ALT_ENTER_MESSAGE = new CFClient::UI::Label + padding => 0, + y => $HEIGHT * 49 / 50, + height => $HEIGHT / 50, + text => "Use Alt-Enter to toggle fullscreen mode"; + $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); + + $MAPWIDGET = new CFClient::UI::MapWidget; + $CFClient::UI::TOPLEVEL->add ($MAPWIDGET); + $MAPWIDGET->focus_in; + + config_dialog; } -sub stop_game { +sub destroy_screen { + $CFClient::UI::TOPLEVEL->{children} = []; remove Glib::Source $SDL_TIMER; - + remove Glib::Source $refresh_handler if $refresh_handler; + undef $refresh_handler; undef $SDL_APP; + undef $SDL_EV; SDL::Quit; } -sub refresh { +sub force_refresh { + glViewport 0, 0, $WIDTH, $HEIGHT; + + glMatrixMode GL_PROJECTION; + glLoadIdentity; + glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; + glMatrixMode GL_MODELVIEW; + glLoadIdentity; + glClear GL_COLOR_BUFFER_BIT; - $_->draw for values %Crossfire::Client::Widget::ACTIVE_WIDGETS; + $CFClient::UI::TOPLEVEL->draw; SDL::GLSwapBuffers; } +my $FPS; + +sub refresh { + $refresh_handler ||= add Glib::Idle sub { + if ($SDL_APP) { + $NOW = SDL::GetTicks; + + if ($NOW - $last_refresh < $TICKS_PER_FRAME) { + SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); + $NOW = SDL::GetTicks; + } + + my $interval = ($NOW - $last_refresh) * 0.001; + $last_refresh = $NOW; + + if ($interval) { + $FPS ||= 1 / $interval; + $FPS = $FPS * 0.9 + (1 / $interval) * 0.1; + debug sprintf "%5.02f", $FPS; + } + + force_refresh; + $_->animate ($interval) for grep $_, values %ANIMATE; + + if (%ANIMATE) { + 1 + } else { + undef $refresh_handler; + 0 + } + } else { + undef $refresh_handler; + 0 + } + }; +} + +sub animation_start { + my ($widget) = @_; + $ANIMATE{$widget} = $widget; + Scalar::Util::weaken $ANIMATE{$widget}; + + refresh; +} + +sub animation_stop { + my ($widget) = @_; + delete $ANIMATE{$widget}; +} + %SDL_CB = ( SDL_QUIT() => sub { main_quit Gtk2; @@ -107,26 +304,27 @@ SDL_KEYDOWN() => sub { if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { # alt-enter - $CFG->{fullscreen} = !$CFG->{fullscreen}; + $FULLSCREEN = !$FULLSCREEN; + destroy_screen; init_screen; } else { - Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); + CFClient::UI::feed_sdl_key_down_event ($SDL_EV); } }, SDL_KEYUP() => sub { - Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); + CFClient::UI::feed_sdl_key_up_event ($SDL_EV); }, SDL_MOUSEMOTION() => sub { - warn "sdl motion\n";#d# + CFClient::UI::feed_sdl_motion_event ($SDL_EV); }, SDL_MOUSEBUTTONDOWN() => sub { - Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); + CFClient::UI::feed_sdl_button_down_event ($SDL_EV); }, SDL_MOUSEBUTTONUP() => sub { - Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); + CFClient::UI::feed_sdl_button_up_event ($SDL_EV); }, SDL_ACTIVEEVENT() => sub { - warn "active\n";#d# +# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# }, ); @@ -135,19 +333,19 @@ sub conn::map_update { my ($self, $dirty) = @_; - refresh; + $MAPWIDGET->update; } sub conn::map_scroll { my ($self, $dx, $dy) = @_; - refresh; +# refresh; } sub conn::map_clear { my ($self) = @_; - refresh; +# refresh; } sub conn::face_find { @@ -161,7 +359,13 @@ $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; - $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; + $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; +} + +sub conn::query { + my ($self, $flags, $prompt) = @_; + + warn "<<<>>\n";#d# } sub gtk_add_cfg_field { @@ -177,6 +381,12 @@ $ent->signal_connect (changed => sub { my ($ent) = @_; $cfg->{$key} = $ent->get_text; + # TODO: Mapsize should be a slider in the game gui + if ($key eq 'mapsize' and $cfg->{$key} > 100) { + $cfg->{$key} = 100; + } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { + $cfg->{$key} = 50; + } }); } @@ -188,15 +398,13 @@ my @cfg = ( [qw/Host host/], [qw/Port port/], + [qw/Mapsize% mapsize/], [qw/Username user/], [qw/Password password/], ); my $cfg = {}; - my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); - my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; - $w->add (my $vb = Gtk2::VBox->new); $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; @@ -204,7 +412,7 @@ $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); my $i = 0; my $act = 0; - for (map { "$_->[0]x$_->[1]" } reverse @modes) { + for (map { "$_->[0]x$_->[1]" } @SDL_MODES) { if ($_ eq $selmode) { $act = $i } $cb->append_text ($_); $i++; @@ -231,7 +439,7 @@ $::CFG->{$_} = $cfg->{$_} if $_ ne '_i'; } - Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; + CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; }); $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); $cb->signal_connect (clicked => sub { @@ -244,7 +452,7 @@ }); $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); $cb->signal_connect (clicked => sub { - my $cb = $events{login} || sub {}; + my $cb = $events{logout} || sub {}; $cb->(); }); $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); @@ -258,31 +466,35 @@ ############################################################################# -SDL::Init(SDL_INIT_EVERYTHING()); - -my $mapwidget = Crossfire::Client::Widget::MapWidget->new; - -find_rcfile "uifont.ttf"; +SDL::Init SDL_INIT_EVERYTHING; -$mapwidget->activate; -$mapwidget->focus_in; +@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], + @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE }; -Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; - -$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; +CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; $CFG ||= { width => 640, height => 480, fullscreen => 0, + sdl_mode => 0, + mapsize => 100, host => "crossfire.schmorp.de", port => 13327, }; -run_config_dialog - login => sub { start_game }, - logout => sub { stop_game }; +{ + my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); + + CFClient::add_font $_ for @fonts; + CFClient::set_font $fonts[0]; +} + +$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; + +init_screen; main Gtk2; Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; +