… | |
… | |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
22 | } |
22 | } |
23 | |
23 | |
|
|
24 | package Crossfire::Client::Texture; |
|
|
25 | |
|
|
26 | use Scalar::Util; |
|
|
27 | |
|
|
28 | use SDL::OpenGL; |
|
|
29 | |
|
|
30 | my @textures; |
|
|
31 | |
|
|
32 | sub new_from_file { |
|
|
33 | my ($class, $path) = @_; |
|
|
34 | |
|
|
35 | open my $fh, "<:raw", $path |
|
|
36 | or die "$path: $!"; |
|
|
37 | |
|
|
38 | local $/; |
|
|
39 | $class->new_from_data (<$fh>) |
|
|
40 | } |
|
|
41 | |
|
|
42 | sub new_from_image { |
|
|
43 | my ($class, $image) = @_; |
|
|
44 | |
|
|
45 | my $self = bless { |
|
|
46 | image => $data, |
|
|
47 | }; |
|
|
48 | |
|
|
49 | push @textures, $self; |
|
|
50 | Scalar::Util::weaken $textures[-1]; |
|
|
51 | |
|
|
52 | $self->upload; |
|
|
53 | |
|
|
54 | $self |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub upload { |
|
|
58 | my ($self) = @_; |
|
|
59 | |
|
|
60 | return unless $SDL::App::USING_OPENGL; |
|
|
61 | |
|
|
62 | my ($data, $width, $height); |
|
|
63 | |
|
|
64 | if (exists $self->{data}) { |
|
|
65 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
|
|
66 | } else { |
|
|
67 | my $pb = new Gtk2::Gdk::PixbufLoader; |
|
|
68 | $pb->write ($self->{data}); |
|
|
69 | $pb->close; |
|
|
70 | |
|
|
71 | $pb = $pb->get_pixbuf; |
|
|
72 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
73 | |
|
|
74 | ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); |
|
|
75 | } |
|
|
76 | |
|
|
77 | ($self->{name}) = @{glGenTextures 1}; |
|
|
78 | |
|
|
79 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
|
|
80 | |
|
|
81 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
82 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
|
|
83 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
|
|
84 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
85 | |
|
|
86 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
87 | GL_RGBA8, |
|
|
88 | $width, $height, |
|
|
89 | 0, |
|
|
90 | GL_RGBA, |
|
|
91 | GL_UNSIGNED_BYTE, |
|
|
92 | $data; |
|
|
93 | } |
|
|
94 | |
|
|
95 | sub DESTROY { |
|
|
96 | my ($self) = @_; |
|
|
97 | |
|
|
98 | return unless exists $self->{name}; |
|
|
99 | |
|
|
100 | glDeleteTextures delete $self->{name}; |
|
|
101 | } |
|
|
102 | |
|
|
103 | push @::GLINIT, sub { |
|
|
104 | $_->upload |
|
|
105 | for grep $_, @textures; |
|
|
106 | }; |
|
|
107 | |
24 | 1; |
108 | 1; |
25 | |
109 | |
26 | =back |
110 | =back |
27 | |
111 | |
28 | =head1 AUTHOR |
112 | =head1 AUTHOR |