--- deliantra/Deliantra-Client/bin/pclient 2006/04/09 18:28:23 1.47 +++ deliantra/Deliantra-Client/bin/pclient 2006/04/11 14:04:27 1.69 @@ -14,16 +14,17 @@ use SDL::OpenGL::Constants; use Crossfire; -use Crossfire::Client; use Crossfire::Protocol; -use Crossfire::Client::Widget; - -our $FACECACHE; +use CFClient; +use CFClient::Widget; our $VERSION = '0.1'; -our %GL_EXT; +my $MAX_FPS = 30; +my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame + +our $FACECACHE; our $CFG; our $CONN; @@ -32,22 +33,23 @@ our $HEIGHT; our $FULLSCREEN; +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 $TOPLEVEL; - -our $tw; # Test widget #d# +our $STATUS_LINE; +our $DEBUG_STATUS; my $last_refresh; +my %ANIMATE; +my $refresh_handler; + +our ($tw, $te); # Test widget #d# sub init_screen { $SDL_APP = new SDL::App @@ -56,130 +58,172 @@ -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 => $FULLSCREEN, -resizeable => 0; - $last_refresh = SDL::GetTicks; + $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 + }; - %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; + $last_refresh = SDL::GetTicks; - $GL_EXT{GL_ARB_texture_non_power_of_two} - or warn "WARNING: non-power-of-two opengl extension required"; + CFClient::gl_init; $FONTSIZE = int $HEIGHT / 50; - $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label - 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, - "Use Alt-Enter to toggle fullscreen mode"; - $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); - $TOPLEVEL->add ($ALT_ENTER_MESSAGE); + ############################################################################# + + glClearColor 0.45, 0.45, 0.45, 1; + + glEnable GL_TEXTURE_2D; + glEnable GL_COLOR_MATERIAL; + glShadeModel GL_FLAT; + glDisable GL_DEPTH_TEST; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + + ############################################################################# + + $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); + + $MAPWIDGET = new CFClient::Widget::MapWidget; + $CFClient::Widget::TOPLEVEL->add ($MAPWIDGET); + $MAPWIDGET->focus_in; # Test code #d# unless ($tw) { # haha... - $tw = new Crossfire::Client::Widget::Animator; - my $lbl1 = new Crossfire::Client::Widget::Label - 0, 0, 10, $FONTSIZE, "This is a\nTEST!\nOf a themed\nFrame!"; - my $lbl2 = new Crossfire::Client::Widget::Label - 0, 0, 10, $FONTSIZE, "LBL2"; - - my $vb = new Crossfire::Client::Widget::VBox; - my $f = new Crossfire::Client::Widget::FancyFrame; - my $f2 = new Crossfire::Client::Widget::FancyFrame; + $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 => 600, 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->w (400); - $tw->h (300); - $tw->move ($WIDTH - 200, 0); $tw->moveto (0, 0); - $TOPLEVEL->add ($tw); + $CFClient::Widget::TOPLEVEL->add ($tw); # $f->move ($WIDTH - 200, 0); -# $TOPLEVEL->add ($f); +# $CFClient::Widget::TOPLEVEL->add ($f); } +} - glClearColor 0, 0, 0, 0; - - glEnable GL_TEXTURE_2D; - glEnable GL_COLOR_MATERIAL; - glShadeModel GL_FLAT; - glDisable GL_DEPTH_TEST; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - - $_->() for @GL_INIT; +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/100, sub { - ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() - while $SDL_EV->poll; - - 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}, user => $CFG->{user}, pass => $CFG->{password}, - mapw => 50, - maph => 37, + mapw => $mapsize, + maph => $mapsize, ; - Crossfire::Client::lowdelay fileno $CONN->{fh}; + 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 force_refresh { glViewport 0, 0, $WIDTH, $HEIGHT; glMatrixMode GL_PROJECTION; glLoadIdentity; - glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; + glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; glMatrixMode GL_MODELVIEW; glClear GL_COLOR_BUFFER_BIT; - $TOPLEVEL->draw; + $CFClient::Widget::TOPLEVEL->draw; SDL::GLSwapBuffers; } -my %ANIMATE; +sub debug { + $DEBUG_STATUS->set_text ($_[0]); + $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); + $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); +} -my $refresh_handler; +my $FPS; sub refresh { $refresh_handler ||= add Glib::Idle sub { - my $next_refresh = SDL::GetTicks; - my $interval = ($next_refresh - $last_refresh) * 0.001; - $last_refresh = $next_refresh; + if ($SDL_APP) { + my $next_refresh = SDL::GetTicks; - force_refresh; - $_->animate ($interval) for grep $_, values %ANIMATE; + 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; + } - if (%ANIMATE) { - 1 + force_refresh; + $_->animate ($interval) for grep $_, values %ANIMATE; + + if (%ANIMATE) { + 1 + } else { + undef $refresh_handler; + 0 + } } else { undef $refresh_handler; 0 @@ -215,23 +259,23 @@ $FULLSCREEN = !$FULLSCREEN; init_screen; } else { - Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); + CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); } }, SDL_KEYUP() => sub { - Crossfire::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 { - Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); + CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); }, SDL_MOUSEBUTTONUP() => sub { - Crossfire::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# }, ); @@ -240,7 +284,7 @@ sub conn::map_update { my ($self, $dirty) = @_; - refresh; + $MAPWIDGET->update; } sub conn::map_scroll { @@ -266,7 +310,7 @@ $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 { @@ -288,12 +332,11 @@ $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; - $ent->set_text ('100'); - } elsif ($key eq 'mapsize' and $cfg->{$key} < 0) { - $cfg->{$key} = 0; - $ent->set_text ('0'); + } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { + $cfg->{$key} = 50; } }); } @@ -350,7 +393,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 { @@ -363,7 +406,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); @@ -379,14 +422,7 @@ SDL::Init SDL_INIT_EVERYTHING; -$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; - -my $mapwidget = Crossfire::Client::Widget::MapWidget->new; - -$TOPLEVEL->add ($mapwidget); -$mapwidget->focus_in; - -Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; +CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; $CFG ||= { width => 640, @@ -397,7 +433,12 @@ port => 13327, }; -Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; +{ + 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" } || {};