--- deliantra/Deliantra-Client/DC/Texture.pm 2007/08/28 01:23:47 1.28 +++ deliantra/Deliantra-Client/DC/Texture.pm 2008/09/08 19:53:23 1.36 @@ -1,10 +1,10 @@ =head1 NAME -CFPlus::Texture - tetxure class for CFPlus +DC::Texture - tetxure class for Deliantra-Client =head1 SYNOPSIS - use CFPlus::Texture; + use DC::Texture; =head1 DESCRIPTION @@ -12,12 +12,12 @@ =cut -package CFPlus::Texture; +package DC::Texture; use strict; use List::Util qw(max min); -use CFPlus::OpenGL; +use DC::OpenGL; my %TEXTURES; my ($MAX_W, $MAX_H) = (4096, 4096); # maximum texture size attempted by this module @@ -32,7 +32,7 @@ %data, }, $class; - CFPlus::weaken ($TEXTURES{$self+0} = $self); + DC::weaken ($TEXTURES{$self+0} = $self); $self->upload unless delete $self->{delay}; @@ -49,15 +49,10 @@ $class->new (image => $image, internalformat => undef, %arg) } - -sub new_from_file { +sub new_from_resource { my ($class, $path, %arg) = @_; - open my $fh, "<:raw", $path - or die "$path: $!"; - - local $/; - $class->new_from_image (<$fh>, %arg) + $class->new (resource_path => $path, internalformat => undef, %arg) } #sub new_from_surface { @@ -112,7 +107,7 @@ return if $self->{loading}; unless ($GL_VERSION) { - $self->{was_loaded} = 1; + $self->{want_upload} = -1; return; } @@ -123,6 +118,14 @@ my ($data, $dw, $dh); + if (exists $self->{resource_path}) { + open my $fh, "<:raw", DC::find_rcfile $self->{resource_path}; + local $/; + delete $self->{internalformat}; + $self->{image} = <$fh>; + $self->{delete_image} = 1; + } + if (defined $self->{data}) { $data = $self->{data}; ($dw, $dh) = @$self{qw(w h)}; @@ -132,8 +135,8 @@ } elsif (defined $self->{image}) { ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) - = CFPlus::load_image_inline $self->{image}; - + = DC::load_image_inline $self->{image}; + $self->{internalformat} ||= $internalformat; ($dw, $dh) = @$self{qw(w h)}; @@ -141,13 +144,13 @@ } elsif (defined $self->{tile}) { ++$self->{loading}; - return CFPlus::DB::get tilecache => $self->{tile}, sub { + return DC::DB::get tilecache => $self->{tile}, sub { $self->loading_done ($_[0]); }; } elsif (defined $self->{path}) { ++$self->{loading}; - return CFPlus::DB::read_file $self->{path}, sub { + return DC::DB::read_file $self->{path}, sub { $self->loading_done ($_[0]); }; @@ -159,7 +162,7 @@ defined $data or $self->{render_cb} or die; # some sanity check - $self->{minified} ||= [CFPlus::average $dw, $dh, $data] + $self->{minified} ||= [DC::average $dw, $dh, $data] if $self->{minify}; # against rather broken cards we enforce a maximum texture size @@ -168,8 +171,8 @@ # if only pot-textures are allowed, pot'ify tw/th unless ($GL_NPOT) { - $tw = CFPlus::minpot $tw; - $th = CFPlus::minpot $th; + $tw = DC::minpot $tw; + $th = DC::minpot $th; } # now further decrease texture size until the @@ -234,6 +237,10 @@ } } + if ((my $name = delete $self->{want_upload}) > 0) { + $self->{name} = $name; + } + glBindTexture GL_TEXTURE_2D, $self->{name} ||= glGenTexture; if ($self->{wrap}) { @@ -321,17 +328,24 @@ $self->unload; } -$CFPlus::OpenGL::INIT_HOOK{"CFPlus::Texture"} = sub { - for (values %TEXTURES) { - next unless delete $_->{was_loaded}; - $_->upload; +$DC::OpenGL::INIT_HOOK{"DC::Texture"} = sub { + for my $tex (values %TEXTURES) { + if (my $name = $tex->{want_upload}) { + $tex->upload; + + if ($tex->{loading} && $name > 0) { + # if loading is delayed we still have to allocate the texture name + glBindTexture GL_TEXTURE_2D, $name; + glTexImage2D GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE; + } + } } }; -$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Texture"} = sub { +$DC::OpenGL::SHUTDOWN_HOOK{"DC::Texture"} = sub { for (values %TEXTURES) { next unless $_->{name}; - $_->{was_loaded} = 1; + $_->{want_upload} = $_->{name}; $_->unload; } };