--- deliantra/Deliantra-Client/bin/pclient 2006/04/07 17:18:22 1.15 +++ deliantra/Deliantra-Client/bin/pclient 2006/04/11 13:38:22 1.68 @@ -1,6 +1,7 @@ #!/opt/bin/perl use strict; +use utf8; use Glib; use Gtk2 -init; @@ -13,112 +14,234 @@ use SDL::OpenGL::Constants; use Crossfire; -use Crossfire::Client; use Crossfire::Protocol; -use Client::Util; -use Client::Widget; +use CFClient; +use CFClient::Widget; our $VERSION = '0.1'; +my $MAX_FPS = 30; +my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame + +our $FACECACHE; + our $CFG; our $CONN; +our $WIDTH; +our $HEIGHT; +our $FULLSCREEN; + +our $FONTSIZE; + our $SDL_TIMER; our $SDL_APP; -our $SDL_EV = new SDL::Event; +our $SDL_EV; our %SDL_CB; +our $ALT_ENTER_MESSAGE; +our $STATUS_LINE; +our $DEBUG_STATUS; + +my $last_refresh; +my %ANIMATE; +my $refresh_handler; + +our ($tw, $te); # Test widget #d# + sub init_screen { - # nuke all gl context data - $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; + $SDL_EV = new SDL::Event; + $SDL_EV->set_unicode (1); + + $SDL_TIMER = add Glib::Timeout 1000/50, sub { + ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() + while $SDL_EV->poll; + + 1 + }; + + $last_refresh = SDL::GetTicks; + + CFClient::gl_init; + + $FONTSIZE = int $HEIGHT / 50; + + ############################################################################# + + glClearColor 0.45, 0.45, 0.45, 1; + glEnable GL_TEXTURE_2D; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; + glEnable GL_COLOR_MATERIAL; glShadeModel GL_FLAT; glDisable GL_DEPTH_TEST; glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - glEnable GL_BLEND; - glMatrixMode GL_PROJECTION; - glLoadIdentity; - glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1; + ############################################################################# + + $DEBUG_STATUS = new CFClient::Widget::Label; + $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS); + + $STATUS_LINE = new CFClient::Widget::Label + y => $HEIGHT * 59 / 60 - $FONTSIZE; + $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE); + + $ALT_ENTER_MESSAGE = new CFClient::Widget::Label + y => $HEIGHT * 59 / 60, + height => $HEIGHT / 60, + text => "Use Alt-Enter to toggle fullscreen mode"; + $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); + + # Test code #d# + unless ($tw) { # haha... + $te = new CFClient::Widget::FancyFrame x => 300, z => 1; + $te->add (new CFClient::Widget::Entry); + $CFClient::Widget::TOPLEVEL->add ($te); + + $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 400, h => 300; + my $lbl1 = new CFClient::Widget::Label text => "This is a\nTEST!\nOf a themed\nFrame!"; + my $lbl2 = new CFClient::Widget::Label text => "LBL2"; + my $vb = new CFClient::Widget::VBox; + my $f = new CFClient::Widget::FancyFrame; + my $f2 = new CFClient::Widget::FancyFrame; + $f->add ($lbl1); + $f2->add ($lbl2); + $vb->add ($f); + $vb->add ($f2, 1); + + $tw->add ($vb); + $tw->moveto (0, 0); + $CFClient::Widget::TOPLEVEL->add ($tw); + +# $f->move ($WIDTH - 200, 0); +# $CFClient::Widget::TOPLEVEL->add ($f); + } +} - # re-bind all textures +sub destroy_screen { + remove Glib::Source $SDL_TIMER; + undef $SDL_APP; + undef $SDL_EV; + SDL::Quit; } 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 })->(); - } - - 1 - }; + $WIDTH = $CFG->{width}; + $HEIGHT = $CFG->{height}; + $FULLSCREEN = 0; init_screen; + 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}; + port => $CFG->{port}, + user => $CFG->{user}, + pass => $CFG->{password}, + mapw => $mapsize, + maph => $mapsize, + ; + + CFClient::lowdelay fileno $CONN->{fh}; } sub stop_game { - remove Glib::Source $SDL_TIMER; + remove Glib::Source $refresh_handler if $refresh_handler; + undef $refresh_handler; - undef $SDL_APP; - SDL::Quit; + undef $CONN; + destroy_screen; } -sub refresh { - glClearColor 0, 0, 0, 0; +sub force_refresh { + glViewport 0, 0, $WIDTH, $HEIGHT; + + glMatrixMode GL_PROJECTION; + glLoadIdentity; + glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; + glMatrixMode GL_MODELVIEW; + glClear GL_COLOR_BUFFER_BIT; - my $map = $CONN->{map}; + $CFClient::Widget::TOPLEVEL->draw; + + SDL::GLSwapBuffers; +} + +sub debug { + $DEBUG_STATUS->set_text ($_[0]); + $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); + $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); +} + +my $FPS; - for my $x (0 .. $CONN->{mapw} - 1) { - for my $y (0 .. $CONN->{maph} - 1) { +sub refresh { + $refresh_handler ||= add Glib::Idle sub { + if ($SDL_APP) { + my $next_refresh = SDL::GetTicks; + + if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { + SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); + $next_refresh = SDL::GetTicks; + } + + my $interval = ($next_refresh - $last_refresh) * 0.001; + $last_refresh = $next_refresh; + + if ($interval) { + $FPS ||= 1 / $interval; + $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; + debug sprintf "%5.02f", $FPS; + } - my $cell = $map->[$x][$y] - or next; + force_refresh; + $_->animate ($interval) for grep $_, values %ANIMATE; - my $darkness = $cell->[3] * (1 / 255); - glColor $darkness, $darkness, $darkness; - - for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { - my $tex = $CONN->{face}[$num]{texture} || 0; - - glBindTexture GL_TEXTURE_2D, $tex; - - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex $x, $y; - glTexCoord 0, 1; glVertex $x, $y + 1; - glTexCoord 1, 1; glVertex $x + 1, $y + 1; - glTexCoord 1, 0; glVertex $x + 1, $y; - glEnd; + if (%ANIMATE) { + 1 + } else { + undef $refresh_handler; + 0 } + } else { + undef $refresh_handler; + 0 } - } + }; +} - SDL::GLSwapBuffers; +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 { - warn "sdl quit\n";#d# - exit; + main_quit Gtk2; }, SDL_VIDEORESIZE() => sub { }, @@ -126,22 +249,28 @@ refresh; }, SDL_KEYDOWN() => sub { -# Client::Widget::feed_sdl_key_down_event ($SDL_EV); + if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { + # alt-enter + $FULLSCREEN = !$FULLSCREEN; + init_screen; + } else { + CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); + } }, SDL_KEYUP() => sub { -# Client::Widget::feed_sdl_key_up_event ($SDL_EV); + CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); }, SDL_MOUSEMOTION() => sub { - warn "sdl motion\n";#d# + CFClient::Widget::feed_sdl_motion_event ($SDL_EV); }, SDL_MOUSEBUTTONDOWN() => sub { -# Client::Widget::feed_sdl_button_down_event ($SDL_EV); + CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); }, SDL_MOUSEBUTTONUP() => sub { -# Client::Widget::feed_sdl_button_up_event ($SDL_EV); + CFClient::Widget::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# }, ); @@ -156,62 +285,167 @@ sub conn::map_scroll { my ($self, $dx, $dy) = @_; - refresh; +# refresh; } sub conn::map_clear { my ($self) = @_; - refresh; +# refresh; +} + +sub conn::face_find { + my ($self, $face) = @_; + + $FACECACHE->{"$face->{chksum},$face->{name}"} } sub conn::face_update { - my ($self, $num, $face) = @_; + my ($self, $face) = @_; - my $pb = new Gtk2::Gdk::PixbufLoader; - $pb->write ($face->{image}); - $pb->close; + $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; - $pb = $pb->get_pixbuf; - $pb = $pb->add_alpha (0, 0, 0, 0); + $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; +} - glGetError(); - my ($tex) = @{glGenTextures 1}; +sub conn::query { + my ($self, $flags, $prompt) = @_; - $face->{texture} = $tex; - - glBindTexture GL_TEXTURE_2D, $tex; - my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; - - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; - - glTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, - $pb->get_width, $pb->get_height, - 0, - GL_RGBA, - GL_UNSIGNED_BYTE, - $pb->get_pixels; - my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; + warn "<<<>>\n";#d# +} + +sub gtk_add_cfg_field { + my ($tbl, $cfg, $klbl, $key, $value) = @_; + my $i = $cfg->{_i}++; + $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); + $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); + if ($key eq 'password') { + $ent->set_invisible_char ("*"); + $ent->set (visibility => 0) + } + $ent->set_text ($value); + $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; + } + }); +} + +sub run_config_dialog { + my (%events) = @_; + + my $w = Gtk2::Window->new; + + 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}; + $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); + $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) { + if ($_ eq $selmode) { $act = $i } + $cb->append_text ($_); + $i++; + } + $cb->set_active ($act); + $cb->signal_connect (changed => sub { + my ($cb) = @_; + my $txt = $cb->get_active_text; + if ($txt =~ m/(\d+)x(\d+)/) { + $::CFG->{width} = $1; + $::CFG->{height} = $2; + } + }); + + $cfg->{_i} = 1; + for (@cfg) { + gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); + } + + $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); + $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); + $cb->signal_connect (clicked => sub { + for (keys %$cfg) { + $::CFG->{$_} = $cfg->{$_} + if $_ ne '_i'; + } + CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; + }); + $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); + $cb->signal_connect (clicked => sub { + for (keys %$cfg) { + $::CFG->{$_} = $cfg->{$_} + if $_ ne '_i'; + } + my $cb = $events{login} || sub {}; + $cb->($::CFG->{user}, $::CFG->{password}); + }); + $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); + $cb->signal_connect (clicked => sub { + my $cb = $events{logout} || sub {}; + $cb->(); + }); + $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); + $cb->signal_connect (clicked => sub { $w->destroy }); + + $w->show_all; + + $w->signal_connect (destroy => sub { Gtk2->main_quit }); } + ############################################################################# -Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; +SDL::Init SDL_INIT_EVERYTHING; + +my $mapwidget = CFClient::Widget::MapWidget->new; + +$CFClient::Widget::TOPLEVEL->add ($mapwidget); +$mapwidget->focus_in; + +CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; $CFG ||= { width => 640, height => 480, + mapsize => 100, fullscreen => 0, host => "crossfire.schmorp.de", port => 13327, }; -Client::Util::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" } || {}; + +run_config_dialog + login => sub { start_game }, + logout => sub { stop_game }; main Gtk2; + +Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";