--- deliantra/Deliantra-Client/bin/pclient 2006/04/06 21:15:11 1.6 +++ deliantra/Deliantra-Client/bin/pclient 2006/04/07 18:49:53 1.18 @@ -1,13 +1,8 @@ #!/opt/bin/perl - use strict; -use Crossfire::Client; -use Crossfire::Protocol; - -package Crossfire::Client; # uh, yeah - -use strict; +use Glib; +use Gtk2 -init; use SDL; use SDL::App; @@ -16,116 +11,122 @@ use SDL::OpenGL; use SDL::OpenGL::Constants; -my $conn; -my $app; +use Crossfire; +use Crossfire::Client; +use Crossfire::Protocol; + +use Client::Util; +use Client::Widget; + +our $VERSION = '0.1'; + +our $CFG; +our $CONN; + +our $SDL_TIMER; +our $SDL_APP; +our $SDL_EV = new SDL::Event; +our %SDL_CB; -my $WIDTH = 640; -my $HEIGHT = 480; +our @GL_INIT; # hooks called on every gl init -sub glinit { - # nuke all gl context data - - $app = new SDL::App +sub init_screen { + $SDL_APP = new SDL::App -flags => SDL_ANYFORMAT | SDL_HWSURFACE, -title => "Crossfire+ Client", - -width => $WIDTH, - -height => $HEIGHT, + -width => $CFG->{width}, + -height => $CFG->{height}, -opengl => 1, -red_size => 8, -green_size => 8, -blue_size => 8, -double_buffer => 1, + -fullscreen => $CFG->{fullscreen}, -resizeable => 0; glEnable GL_TEXTURE_2D; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL; + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; glShadeModel GL_FLAT; glDisable GL_DEPTH_TEST; - glMatrixMode GL_PROJECTION; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + glEnable GL_BLEND; + glMatrixMode GL_PROJECTION; glLoadIdentity; - glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; + glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1; - # re-bind all textures + $_->() for @GL_INIT; } -sub refresh { - glClearColor 0.5, 0.5, 0.7, 0; - glClear GL_COLOR_BUFFER_BIT; - - my $map = $conn->{map}; - - for my $x (0 .. $conn->{mapw} - 1) { - for my $y (0 .. $conn->{maph} - 1) { - - my $cell = $map->[$x][$y] - or next; - - 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; - } +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 })->(); } - } - SDL::GLSwapBuffers; -} + 1 + }; -my $ev = new SDL::Event; -my %ev_cb; + init_screen; -sub event(&$) { - $ev_cb{$_[0]->()} = $_[1]; + $CONN = new conn + host => $CFG->{host}, + port => $CFG->{port}; } -sub does(&) { shift } - -event {SDL_QUIT} does { - exit; -}; - -event {SDL_VIDEORESIZE} does { - print "resize\n"; -}; - -event {SDL_VIDEOEXPOSE} does { - refresh; -}; +sub stop_game { + remove Glib::Source $SDL_TIMER; -event {SDL_KEYDOWN} does { - print "keypress\n"; -}; - -event {SDL_KEYUP} does { - print "keyup\n";#d# -}; - -event {SDL_MOUSEMOTION} does { - print "motion\n"; -}; + undef $SDL_APP; + SDL::Quit; +} -event {SDL_MOUSEBUTTONDOWN} does { - print "button\n"; -}; +sub refresh { + glClearColor 0, 0, 0, 0; + glClear GL_COLOR_BUFFER_BIT; -event {SDL_MOUSEBUTTONUP} does { - print "buttup\n"; -}; + for (values %Client::Widget::ACTIVE_WIDGETS) { + $_->draw + } -event {SDL_ACTIVEEVENT} does { - print "active\n"; -}; + SDL::GLSwapBuffers; +} -package Crossfire::Client; +%SDL_CB = ( + SDL_QUIT() => sub { + warn "sdl quit\n";#d# + exit; + }, + 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 + $CFG->{fullscreen} = !$CFG->{fullscreen}; + init_screen; + } else { + Client::Widget::feed_sdl_key_down_event ($SDL_EV); + } + }, + SDL_KEYUP() => sub { + Client::Widget::feed_sdl_key_up_event ($SDL_EV); + }, + SDL_MOUSEMOTION() => sub { + warn "sdl motion\n";#d# + }, + SDL_MOUSEBUTTONDOWN() => sub { + Client::Widget::feed_sdl_button_down_event ($SDL_EV); + }, + SDL_MOUSEBUTTONUP() => sub { + Client::Widget::feed_sdl_button_up_event ($SDL_EV); + }, + SDL_ACTIVEEVENT() => sub { + warn "active\n";#d# + }, +); @conn::ISA = Crossfire::Protocol::; @@ -150,57 +151,28 @@ sub conn::face_update { my ($self, $num, $face) = @_; - 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}; } ############################################################################# -use Event; +my $mapwidget = Client::MapWidget->new; -glinit; +$mapwidget->activate; +$mapwidget->focus_in; -$conn = new conn - host => "cf.schmorp.de", - port => 13327; +Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; -Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { - while ($ev->poll) { - ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); - } -}); - -Event::loop; +$CFG ||= { + width => 640, + height => 480, + fullscreen => 0, + host => "crossfire.schmorp.de", + port => 13327, +}; +Client::Util::run_config_dialog + login => sub { start_game }, + logout => sub { stop_game }; +main Gtk2;