--- deliantra/Deliantra-Client/bin/pclient 2006/04/06 15:30:09 1.1 +++ deliantra/Deliantra-Client/bin/pclient 2006/04/12 21:35:11 1.90 @@ -1,365 +1,433 @@ #!/opt/bin/perl +use strict; +use utf8; + +use Time::HiRes 'time'; +use Event; + use SDL; use SDL::App; use SDL::Event; use SDL::Surface; +use SDL::OpenGL; -my $conn; -my $map_surface; +use Crossfire; +use Crossfire::Protocol; -my $app = new SDL::App - -flags => SDL_HWSURFACE | SDL_ANYFORMAT | SDL_HWACCEL | SDL_ASYNCBLIT, - -title => "Crossfire+ Client", - -width => 640, - -height => 480, - -depth => 24, - -double_buffer => 1, - -resizeable => 1; +use CFClient; +use CFClient::UI; -sub redraw { - $map_surface or return; +our $VERSION = '0.1'; - $map_surface->blit (0, $app, 0); - $app->sync; -} +my $MAX_FPS = 60; +my $MIN_FPS = 5; # unused as of yet -my $ev = new SDL::Event; -my %ev_cb; +our $FACECACHE; -sub event(&$) { - $ev_cb{$_[0]->()} = $_[1]; -} +our $LAST_REFRESH; +our $NOW; -sub does(&) { shift } +our $CFG; +our $CONN; +our $FAST; # fast, low-quality mode, possibly useful for software-rendering -event {SDL_QUIT} does { - exit; -}; +our @SDL_MODES; +our $WIDTH; +our $HEIGHT; +our $FULLSCREEN; -event {SDL_VIDEORESIZE} does { - print "resize\n"; -}; +our $MAPWIDGET; +our $FONTSIZE; -event {SDL_KEYDOWN} does { - print "keypress\n"; -}; +our $SDL_ACTIVE; +our $SDL_EV; +our %SDL_CB; -event {SDL_KEYUP} does { - print "keyup\n";#d# -}; +our $ALT_ENTER_MESSAGE; +our $STATUS_LINE; +our $DEBUG_STATUS; -event {SDL_MOUSEMOTION} does { - print "motion\n"; -}; - -event {SDL_MOUSEBUTTONDOWN} does { - print "button\n"; -}; - -event {SDL_MOUSEBUTTONUP} does { - print "buttup\n"; -}; +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); +} -event {SDL_ACTIVEEVENT} does { - print "active\n"; -}; +sub debug { + $DEBUG_STATUS->set_text ($_[0]); + my ($w, $h) = $DEBUG_STATUS->size_request; + $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); +} -package Crossfire::Protocol; +sub start_game { + status "logging in..."; -use AnyEvent; -use IO::Socket::INET; + my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; -sub new { - my $class = shift; - my $self = bless { @_ }, $class; + $CONN = new conn + host => $CFG->{host}, + port => $CFG->{port}, + user => $CFG->{user}, + pass => $CFG->{password}, + mapw => $mapsize, + maph => $mapsize, + ; - $self->{fh} = new IO::Socket::INET PeerHost => $self->{host}, PeerPort => $self->{port} - or die "$self->{host}:$self->{port}: $!"; - $self->{fh}->blocking (0); # stupid nonblock default + status "login successful"; - my $buf; + CFClient::lowdelay fileno $CONN->{fh}; +} - $self->{w} = AnyEvent->io (fh => $self->{fh}, poll => 'r', cb => sub { - if (sysread $self->{fh}, $buf, 16384, length $buf) { - if (2 <= length $buf) { - my $len = unpack "n", $buf; - if ($len + 2 <= length $buf) { - substr $buf, 0, 2, ""; - $self->feed (substr $buf, 0, $len, ""); - } - } - } else { - delete $self->{w}; - close $self->{fh}; - } - }); +sub stop_game { + undef $CONN; +} - $self->send ("version 1023 1027 perlclient"); - $self->send ("setup sound 1 exp 1 map1acmd 1 itemcmd 2 darkness 1 mapsize 63x63 newmapcmd 1 facecache 1 extendedMapInfos 1 extendedTextInfos 1"); - $self->send ("addme"); +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]); - $self -} + $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); + $table->add (1, 0, my $hbox = new CFClient::UI::HBox); -sub feed { - my ($self, $data) = @_; + $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); - $data =~ s/^(\S+)\s// - or return; + $mode_slider->connect (changed => sub { + my ($self, $value) = @_; - my $command = "feed_$1"; + $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]); - $self->$command ($data); -} + $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; + }); -sub feed_version { - my ($self, $version) = @_; -} + $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fast & Ugly"); + $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub { + my ($self, $value) = @_; + $CFG->{fast} = $value; + }); -sub feed_setup { - my ($self, $data) = @_; + $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fog of War"); + $table->add (1, 2, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub { + my ($self, $value) = @_; + $CFG->{fow_intensity} = $value; + }); - $data =~ s/^ +//; + $table->add (1, 4, 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 Setup"); + $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; + }, + ); - $self->{setup} = { split / +/, $data }; + $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { + start_game; + }); - ($self->{mapw}, $self->{maph}) = split /x/, $self->{setup}{mapsize}; + $vbox->add (my $hbox = new CFClient::UI::HBox); - $self->feed_newmap; + $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 feed_query { - my ($self, $data) = @_; - warn "Q<$data>\n"; +sub sdl_init { + SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO + and die "SDL::Init failed!\n"; } -sub feed_stats { - my ($self, $data) = @_; -# warn "S<$data>\n"; -} +sub init_screen { + sdl_init; -sub feed_face1 { - my ($self, $data) = @_; + ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; + $FULLSCREEN = $CFG->{fullscreen}; + $FAST = $CFG->{fast}; - my ($num, $chksum, $name) = unpack "nNa*", $data; + SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; + SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; + SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; + SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; - $self->{face}[$num] = { name => $name, chksum => $chksum }; -} + SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; + SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; + SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; + SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; -sub feed_drawinfo { - my ($self, $data) = @_; -# warn "<$data>\n"; -} + SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1; + SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15; + SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0; -sub feed_delinv { - my ($self, $data) = @_; -} + SDL::SetVideoMode $WIDTH, $HEIGHT, 0, + SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF + | ($FULLSCREEN ? SDL_FULLSCREEN : 0) + or die "SDL::SetVideoMode failed!\n"; -sub feed_item2 { - my ($self, $data) = @_; -} + SDL::WMSetCaption "Crossfire+ Client", "Crossfire+"; -sub feed_map1a { - my ($self, $data) = @_; + $SDL_EV = new SDL::Event; + $SDL_EV->set_unicode (1); - my $map = $self->{map} ||= []; + $SDL_ACTIVE = 1; - my @dirty; - my ($coord, $x, $y, $darkness, $fa, $fb, $fc, $cell); + $LAST_REFRESH = time - 0.01; - while (length $data) { - $coord = unpack "n", substr $data, 0, 2, ""; + CFClient::gl_init; - $x = ($coord >> 10) & 63; - $y = ($coord >> 4) & 63; + $FONTSIZE = int $HEIGHT / 40; - $cell = $map->[$x][$y] ||= []; + ############################################################################# - $cell->[3] = unpack "C", substr $data, 0, 1, "" - if $coord & 8; - $cell->[0] = unpack "n", substr $data, 0, 2, "" - if $coord & 4; - $cell->[1] = unpack "n", substr $data, 0, 2, "" - if $coord & 2; - $cell->[2] = unpack "n", substr $data, 0, 2, "" - if $coord & 1; + $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); - @$cell = () - unless $coord & 15; + $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); - push @dirty, [$x, $y]; - } + $MAPWIDGET = new CFClient::UI::MapWidget; + $CFClient::UI::TOPLEVEL->add ($MAPWIDGET); + $MAPWIDGET->focus_in; - $self->map_update (\@dirty); + config_dialog; } -sub feed_map_scroll { - my ($self, $data) = @_; +sub destroy_screen { + $CFClient::UI::TOPLEVEL->{children} = []; + undef $SDL_ACTIVE; + undef $SDL_EV; + SDL::Quit; +} - my ($dx, $dy) = split / /, $data; +my %animate_object; +my $animate_timer; - my $map = $self->{map} ||= []; +my $want_refresh; +my $can_refresh; - $self->{mapx} += $dx; - $self->{mapy} += $dy; +my $fps = 9; - if ($dx > 0) { - unshift @$_, ([]) x $dx for @$map; - } elsif ($dx < 0) { - splice @$_, 0, -$dx, () for @$map; - } +sub force_refresh { + $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05; + debug sprintf "%3.2f", $fps; - if ($dy > 0) { - unshift @$map, ([]) x $dy; - } elsif ($dy < 0) { - splice @$map, 0, -$dy, (); - } + $want_refresh = 0; + $can_refresh = 0; - $self->map_scroll ($dx, $dy); -} + glViewport 0, 0, $WIDTH, $HEIGHT; -sub feed_newmap { - my ($self) = @_; + glMatrixMode GL_PROJECTION; + glLoadIdentity; + glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; + glMatrixMode GL_MODELVIEW; + glLoadIdentity; - $self->{map} = []; - $self->{mapx} = 0; - $self->{mapy} = 0; + glClearColor +($CFG->{fow_intensity}) x 3, 1; + glClear GL_COLOR_BUFFER_BIT; - $self->map_clear; -} + $CFClient::UI::TOPLEVEL->draw; -sub feed_image { - my ($self, $data) = @_; + SDL::GLSwapBuffers; - my ($face, $len, $data) = unpack "NNa*", $data; + $LAST_REFRESH = $NOW; +} - $self->{face}[$face]{image} = $data; +my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub { + $NOW = time; - $self->face_update ($face, $self->{face}[$face]); -} + ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() + while $SDL_EV->poll; -sub map_clear { } -sub map_update { } -sub map_scroll { } + if (%animate_object) { + $_->animate ($LAST_REFRESH - $NOW) for values %animate_object; + $want_refresh++; + } -sub face_update { } + if ($want_refresh) { + force_refresh; + } else { + $can_refresh = 1; + } +}); -sub send { - my ($self, $data) = @_; +sub refresh { + $want_refresh++; +} - $data = pack "na*", length $data, $data; +sub animation_start { + my ($widget) = @_; + $animate_object{$widget} = $widget; +} - syswrite $self->{fh}, $data; +sub animation_stop { + my ($widget) = @_; + delete $animate_object{$widget}; } -package conn; +@conn::ISA = Crossfire::Protocol::; -@ISA = Crossfire::Protocol::; +sub conn::user_send { + my ($self, $command) = @_; + + $self->send ($command); + status $command; +} -sub map_update { +sub conn::map_update { my ($self, $dirty) = @_; - for (@$dirty) { - my ($x, $y) = @$_; + $MAPWIDGET->update; +} - my $px = $x * 32; - my $py = $y * 32; +sub conn::map_scroll { + my ($self, $dx, $dy) = @_; - my $dst = new SDL::Rect -x => $px, -y => $py, -width => 32, -height => 32; +# refresh; +} - my $cell = $self->{map}[$x][$y]; +sub conn::map_clear { + my ($self) = @_; - if ($cell) { - $cell->[0] or $map_surface->fill ($dst, new SDL::Color -r => 0, -g => 0, -b => 0); # is_floor is opaque, I hope +# refresh; +} - for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { - my $surface = $self->{face}[$num]{surface} ||= do { - $self->send ("askface $num"); +sub conn::face_find { + my ($self, $face) = @_; - # TODO: fog of "war" - my $surface = new SDL::Surface - -flags => SDL::SDL_HWSURFACE | SDL::SDL_ANYFORMAT | SDL::SDL_HWACCEL | SDL::SDL_ASYNCBLIT, - -width => 32, - -height => 32, - -depth => 32; + $FACECACHE->{"$face->{chksum},$face->{name}"} +} - $surface->fill (0, new SDL::Color -r => 128, -g => 128, -b => 128); +sub conn::face_update { + my ($self, $face) = @_; - $surface - }; + $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; - $surface->blit (0, $map_surface, $dst); - } - } else { - $map_surface->fill ($dst, new SDL::Color -r => 0, -g => 0, -b => 0); - } - } - - ::redraw; + $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; } -sub map_scroll { - my ($self, $dx, $dy) = @_; +sub conn::query { + my ($self, $flags, $prompt) = @_; - ::redraw; + warn "<<<>>\n";#d# } -sub map_clear { - my ($self) = @_; +%SDL_CB = ( + SDL_QUIT() => sub { + Event::unloop -1; + }, + SDL_VIDEORESIZE() => sub { + }, + SDL_VIDEOEXPOSE() => sub { + refresh; + }, + SDL_KEYDOWN() => sub { + if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { + # alt-enter + $FULLSCREEN = !$FULLSCREEN; + destroy_screen; + init_screen; + } else { + CFClient::UI::feed_sdl_key_down_event ($SDL_EV); + } + }, + SDL_KEYUP() => sub { + CFClient::UI::feed_sdl_key_up_event ($SDL_EV); + }, + SDL_MOUSEMOTION() => sub { + CFClient::UI::feed_sdl_motion_event ($SDL_EV); + }, + SDL_MOUSEBUTTONDOWN() => sub { + CFClient::UI::feed_sdl_button_down_event ($SDL_EV); + }, + SDL_MOUSEBUTTONUP() => sub { + CFClient::UI::feed_sdl_button_up_event ($SDL_EV); + }, + SDL_ACTIVEEVENT() => sub { +# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# + }, +); - $map_surface = new SDL::Surface - -flags => SDL::HWSURFACE, - -width => $self->{mapw} * 32, - -height => $self->{maph} * 32, - -depth => 32; +############################################################################# + +CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; - SDL::SetClipRect $$map_surface, 0; - $map_surface->fill (0, new SDL::Color -r => 0, -g => 0, -b => 0); +my %DEF_CFG = ( + width => 640, + height => 480, + fast => 0, + fow_intensity => 0.45, + fullscreen => 0, + sdl_mode => 0, + mapsize => 100, + host => "crossfire.schmorp.de", + port => 13327, +); - ::redraw; +while (my ($k, $v) = each %DEF_CFG) { + $CFG->{$k} = $v unless exists $CFG->{$k}; } -sub face_update { - my ($self, $num, $face) = @_; +sdl_init; - warn "up face $self,$num,$face\n";#d# - #TODO - open my $fh, ">:raw", "/tmp/x~"; - syswrite $fh, $face->{image}; - close $fh; +@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], + @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL }; - $face->{surface} = (new SDL::Surface -name => "/tmp/x~")->display_format; +@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; - unlink "/tmp/x~"; +$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; - my @dirty; +init_screen; - for my $x (0..$self->{mapw} - 1) { - for my $y (0..$self->{maph} - 1) { - push @dirty, [$x, $y] - if grep $_ == $num, @{$self->{map}[$x][$y] || []}; - } - } - $self->map_update (\@dirty); -} - -package main; - -############################################################################# +{ + my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); -use Event; + CFClient::add_font $_ for @fonts; + CFClient::set_font $fonts[0]; +} -$conn = new conn - host => "cf.schmorp.de", - port => 13327; - -Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { - while ($ev->poll) { - ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); - } -}); +$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; Event::loop; +Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";