--- deliantra/Deliantra-Client/bin/pclient 2006/04/06 21:15:11 1.6
+++ deliantra/Deliantra-Client/bin/pclient 2006/04/09 18:28:23 1.47
@@ -1,13 +1,10 @@
#!/opt/bin/perl
use strict;
+use utf8;
-use Crossfire::Client;
-use Crossfire::Protocol;
-
-package Crossfire::Client; # uh, yeah
-
-use strict;
+use Glib;
+use Gtk2 -init;
use SDL;
use SDL::App;
@@ -16,16 +13,44 @@
use SDL::OpenGL;
use SDL::OpenGL::Constants;
-my $conn;
-my $app;
+use Crossfire;
+use Crossfire::Client;
+use Crossfire::Protocol;
+
+use Crossfire::Client::Widget;
+
+our $FACECACHE;
+
+our $VERSION = '0.1';
+
+our %GL_EXT;
+
+our $CFG;
+our $CONN;
-my $WIDTH = 640;
-my $HEIGHT = 480;
+our $WIDTH;
+our $HEIGHT;
+our $FULLSCREEN;
-sub glinit {
- # nuke all gl context data
-
- $app = new SDL::App
+our $FONTSIZE;
+
+our $SDL_TIMER;
+our $SDL_APP;
+our $SDL_EV = new SDL::Event;
+our %SDL_CB;
+
+our @GL_INIT; # hooks called on every gl init
+
+our $ALT_ENTER_MESSAGE;
+
+our $TOPLEVEL;
+
+our $tw; # Test widget #d#
+
+my $last_refresh;
+
+sub init_screen {
+ $SDL_APP = new SDL::App
-flags => SDL_ANYFORMAT | SDL_HWSURFACE,
-title => "Crossfire+ Client",
-width => $WIDTH,
@@ -35,97 +60,180 @@
-green_size => 8,
-blue_size => 8,
-double_buffer => 1,
+ -fullscreen => $FULLSCREEN,
-resizeable => 0;
- glEnable GL_TEXTURE_2D;
- glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL;
- glShadeModel GL_FLAT;
- glDisable GL_DEPTH_TEST;
- glMatrixMode GL_PROJECTION;
+ $last_refresh = SDL::GetTicks;
- glLoadIdentity;
- glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1;
+ %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
- # re-bind all textures
-}
+ $GL_EXT{GL_ARB_texture_non_power_of_two}
+ or warn "WARNING: non-power-of-two opengl extension required";
-sub refresh {
- glClearColor 0.5, 0.5, 0.7, 0;
- glClear GL_COLOR_BUFFER_BIT;
+ $FONTSIZE = int $HEIGHT / 50;
- my $map = $conn->{map};
+ $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);
+
+ # 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;
+ $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);
- for my $x (0 .. $conn->{mapw} - 1) {
- for my $y (0 .. $conn->{maph} - 1) {
+# $f->move ($WIDTH - 200, 0);
+# $TOPLEVEL->add ($f);
+ }
- my $cell = $map->[$x][$y]
- or next;
+ glClearColor 0, 0, 0, 0;
- for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
- my $tex = $conn->{face}[$num]{texture} || 0;
-
- glBindTexture GL_TEXTURE_2D, $tex;
-
- glColor 1,0.2,0.7;
- glBegin GL_QUADS;
- glTexCoord 0, 0; glVertex $x, $y;
- glTexCoord 0, 1; glVertex $x, $y + 0.9;
- glTexCoord 1, 1; glVertex $x + 0.9, $y + 0.9;
- glTexCoord 1, 0; glVertex $x + 0.9, $y;
- glEnd;
- }
- }
- }
+ glEnable GL_TEXTURE_2D;
+ glEnable GL_COLOR_MATERIAL;
+ glShadeModel GL_FLAT;
+ glDisable GL_DEPTH_TEST;
+ glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
- SDL::GLSwapBuffers;
+ $_->() for @GL_INIT;
}
-my $ev = new SDL::Event;
-my %ev_cb;
+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;
+
+ $CONN = new conn
+ host => $CFG->{host},
+ port => $CFG->{port},
+ user => $CFG->{user},
+ pass => $CFG->{password},
+ mapw => 50,
+ maph => 37,
+ ;
-sub event(&$) {
- $ev_cb{$_[0]->()} = $_[1];
+ Crossfire::Client::lowdelay fileno $CONN->{fh};
}
-sub does(&) { shift }
+sub stop_game {
+ remove Glib::Source $SDL_TIMER;
-event {SDL_QUIT} does {
- exit;
-};
+ undef $SDL_APP;
+ SDL::Quit;
+}
-event {SDL_VIDEORESIZE} does {
- print "resize\n";
-};
-event {SDL_VIDEOEXPOSE} does {
- refresh;
-};
+sub force_refresh {
+ glViewport 0, 0, $WIDTH, $HEIGHT;
-event {SDL_KEYDOWN} does {
- print "keypress\n";
-};
+ glMatrixMode GL_PROJECTION;
+ glLoadIdentity;
+ glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000;
+ glMatrixMode GL_MODELVIEW;
-event {SDL_KEYUP} does {
- print "keyup\n";#d#
-};
+ glClear GL_COLOR_BUFFER_BIT;
-event {SDL_MOUSEMOTION} does {
- print "motion\n";
-};
+ $TOPLEVEL->draw;
-event {SDL_MOUSEBUTTONDOWN} does {
- print "button\n";
-};
+ SDL::GLSwapBuffers;
+}
-event {SDL_MOUSEBUTTONUP} does {
- print "buttup\n";
-};
+my %ANIMATE;
-event {SDL_ACTIVEEVENT} does {
- print "active\n";
-};
+my $refresh_handler;
-package Crossfire::Client;
+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;
+
+ force_refresh;
+ $_->animate ($interval) for grep $_, values %ANIMATE;
+
+ if (%ANIMATE) {
+ 1
+ } 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;
+ },
+ 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;
+ init_screen;
+ } else {
+ Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
+ }
+ },
+ SDL_KEYUP() => sub {
+ Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
+ },
+ SDL_MOUSEMOTION() => sub {
+ warn "sdl motion\n";#d#
+ },
+ SDL_MOUSEBUTTONDOWN() => sub {
+ Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
+ },
+ SDL_MOUSEBUTTONUP() => sub {
+ Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
+ },
+ SDL_ACTIVEEVENT() => sub {
+ warn "active\n";#d#
+ },
+);
@conn::ISA = Crossfire::Protocol::;
@@ -138,69 +246,165 @@
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) = @_;
+
+ $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
- warn "up face $self,$num,$face\n";#d#
- #TODO
- open my $fh, ">:raw", "/tmp/x~";
- syswrite $fh, $face->{image};
- close $fh;
-
- my $surface = new SDL::Surface -name => "/tmp/x~";
-
- unlink "/tmp/x~";
-
- my ($tex) = @{glGenTextures 1};
- glGetError();
-
- $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;
-
- $surface->rgba;
-
- glTexImage2D GL_TEXTURE_2D, 0,
- 4, # components
- $surface->width, $surface->height,
- 0,
- GL_RGBA,
- GL_UNSIGNED_BYTE,
- $surface->pixels;
- my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr;
+ $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
}
+sub conn::query {
+ my ($self, $flags, $prompt) = @_;
+
+ 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;
+ 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');
+ }
+ });
+}
+
+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';
+ }
+ Crossfire::Client::write_cfg "$Crossfire::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{login} || 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 });
+}
+
+
#############################################################################
-use Event;
+SDL::Init SDL_INIT_EVERYTHING;
-glinit;
+$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
-$conn = new conn
- host => "cf.schmorp.de",
- port => 13327;
+my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
-Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
- while ($ev->poll) {
- ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
- }
-});
+$TOPLEVEL->add ($mapwidget);
+$mapwidget->focus_in;
+
+Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
+
+$CFG ||= {
+ width => 640,
+ height => 480,
+ mapsize => 100,
+ fullscreen => 0,
+ host => "crossfire.schmorp.de",
+ port => 13327,
+};
+
+Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
+
+$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
-Event::loop;
+run_config_dialog
+ login => sub { start_game },
+ logout => sub { stop_game };
+main Gtk2;
+Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";