--- deliantra/Deliantra-Client/DC.pm 2006/04/08 22:23:57 1.13 +++ deliantra/Deliantra-Client/DC.pm 2006/04/11 17:02:35 1.23 @@ -1,10 +1,10 @@ =head1 NAME -Crossfire::Client - undocumented utility garbage for our crossfire client +CFClient - undocumented utility garbage for our crossfire client =head1 SYNOPSIS - use Crossfire::Client; + use CFClient; =head1 DESCRIPTION @@ -12,20 +12,34 @@ =cut -package Crossfire::Client; +package CFClient; BEGIN { $VERSION = '0.1'; use XSLoader; - XSLoader::load "Crossfire::Client", $VERSION; + XSLoader::load "CFClient", $VERSION; +} + +our %GL_EXT; +our $GL_VERSION; + +our $GL_NPOT; + +sub gl_init { + $GL_VERSION = gl_version * 1; + %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; + + $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; + + CFClient::Texture::restore_state (); } sub find_rcfile($) { my $path; for (@INC) { - $path = "$_/Crossfire/resources/$_[0]"; + $path = "$_/CFClient/resources/$_[0]"; return $path if -r $path; } @@ -64,7 +78,7 @@ close CFG; } -package Crossfire::Client::Texture; +package CFClient::Texture; use Scalar::Util; @@ -72,10 +86,14 @@ my @textures; -sub _new { +sub new { my ($class, %data) = @_; - my $self = bless \%data, $class; + my $self = bless { + internalformat => GL_RGBA, + format => GL_RGBA, + %data, + }, $class; push @textures, $self; Scalar::Util::weaken $textures[-1]; @@ -88,7 +106,7 @@ sub new_from_image { my ($class, $image) = @_; - $class->_new (image => $image) + $class->new (image => $image) } sub new_from_file { @@ -101,33 +119,40 @@ $class->new_from_image (<$fh>) } -sub new_from_surface { - my ($class, $surface) = @_; - - $surface->rgba; - - $class->_new ( - data => $surface->pixels, - width => $surface->width, - height => $surface->height, +#sub new_from_surface { +# my ($class, $surface) = @_; +# +# $surface->rgba; +# +# $class->new ( +# data => $surface->pixels, +# width => $surface->width, +# height => $surface->height, +# ) +#} + +sub new_from_layout { + my ($class, $layout) = @_; + + my ($w, $h, $data) = $layout->render; + + $class->new ( + width => $w, + height => $h, + data => $data, + internalformat => GL_ALPHA4, + format => GL_ALPHA, ) } -sub new_from_ttf { - my ($class, $ttf, $text) = @_; - - utf8::encode $text; - - my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, - (new SDL::Color -r => 0, -g => 0, -b => 0); - - $class->new_from_surface (bless \$surface, SDL::Surface::) -} - sub new_from_opengl { my ($class, $w, $h, $cb) = @_; - $class->_new (width => $w, height => $h, rendercb => $cb) + $class->new (width => $w, height => $h, rendercb => $cb) +} + +sub topot { + (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] } sub upload { @@ -143,7 +168,7 @@ glViewport 0, 0, $self->{width}, $self->{height}; glMatrixMode GL_PROJECTION; glLoadIdentity; - glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; + glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; glMatrixMode GL_MODELVIEW; glPushmatrix; glLoadIdentity; @@ -164,38 +189,49 @@ $data = $pb->get_pixels; } - ($self->{name}) = @{glGenTextures 1}; + my ($tw, $th) = @$self{qw(width height)}; + + unless ($GL_NPOT && $tw && $th) { + $tw = topot $tw; + $th = topot $th; + + if (defined $data) { + my $bpp = (length $data) / ($self->{width} * $self->{height}); + $data = pack "(a" . ($tw * $bpp) . ")*", + unpack "(a" . ($self->{width} * $bpp) . ")*", $data; + $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height}); + } + } + + $self->{s} = $self->{width} / $tw; + $self->{t} = $self->{height} / $th; + + $self->{name} ||= (glGenTextures 1)->[0]; glBindTexture GL_TEXTURE_2D, $self->{name}; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; + 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; + glGetError; if (defined $data) { glTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, - $self->{width}, $self->{height}, + $self->{internalformat}, + $tw, $th, # need to pad texture first 0, - GL_RGBA, + $self->{format}, GL_UNSIGNED_BYTE, $data; + glGetError and die; } else { - warn "I{DTEDRAW TEX: $self->{width} $self->{height} \n"; - warn "ERR: ". (glGetError). "\n"; - my ($bd, $br) = (glGet (GL_DRAW_BUFFER), glGet (GL_READ_BUFFER)); - warn "BUF:[$bd $br]\n"; - glCopyTexImage2D GL_TEXTURE_2D, 0, - GL_RGBA8, + $self->{internalformat}, 0, 0, - $self->{width}, $self->{height}, + $tw, $th, 0; glPopmatrix; - warn "ERR: ". (glGetError). "\n"; - SDL::GLSwapBuffers; - sleep 1; } } @@ -207,7 +243,7 @@ glDeleteTextures delete $self->{name}; } -push @::GLINIT, sub { +sub restore_state{ $_->upload for grep $_, @textures; };