… | |
… | |
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 { |
|
|
33 | my ($class, %data) = @_; |
|
|
34 | |
|
|
35 | my $self = bless \%data, $class; |
|
|
36 | |
|
|
37 | push @textures, $self; |
|
|
38 | Scalar::Util::weaken $textures[-1]; |
|
|
39 | |
|
|
40 | $self->upload; |
|
|
41 | |
|
|
42 | $self |
|
|
43 | } |
|
|
44 | |
|
|
45 | sub new_from_image { |
|
|
46 | my ($class, $image) = @_; |
|
|
47 | |
|
|
48 | $class->_new (image => $image) |
|
|
49 | } |
|
|
50 | |
|
|
51 | sub new_from_file { |
|
|
52 | my ($class, $path) = @_; |
|
|
53 | |
|
|
54 | open my $fh, "<:raw", $path |
|
|
55 | or die "$path: $!"; |
|
|
56 | |
|
|
57 | local $/; |
|
|
58 | $class->new_from_image (<$fh>) |
|
|
59 | } |
|
|
60 | |
|
|
61 | sub new_from_surface { |
|
|
62 | my ($class, $surface) = @_; |
|
|
63 | |
|
|
64 | $surface->rgba; |
|
|
65 | |
|
|
66 | $class->_new ( |
|
|
67 | data => $surface->pixels, |
|
|
68 | width => $surface->width, |
|
|
69 | height => $surface->height, |
|
|
70 | ) |
|
|
71 | } |
|
|
72 | |
|
|
73 | sub upload { |
|
|
74 | my ($self) = @_; |
|
|
75 | |
|
|
76 | return unless $SDL::App::USING_OPENGL; |
|
|
77 | |
|
|
78 | my ($data, $width, $height); |
|
|
79 | |
|
|
80 | if (exists $self->{data}) { |
|
|
81 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
|
|
82 | } else { |
|
|
83 | my $pb = new Gtk2::Gdk::PixbufLoader; |
|
|
84 | $pb->write ($self->{image}); |
|
|
85 | $pb->close; |
|
|
86 | |
|
|
87 | $pb = $pb->get_pixbuf; |
|
|
88 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
89 | |
|
|
90 | ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); |
|
|
91 | } |
|
|
92 | |
|
|
93 | ($self->{name}) = @{glGenTextures 1}; |
|
|
94 | |
|
|
95 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
|
|
96 | |
|
|
97 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
98 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
|
|
99 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
|
|
100 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
101 | |
|
|
102 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
103 | GL_RGBA8, |
|
|
104 | $width, $height, |
|
|
105 | 0, |
|
|
106 | GL_RGBA, |
|
|
107 | GL_UNSIGNED_BYTE, |
|
|
108 | $data; |
|
|
109 | } |
|
|
110 | |
|
|
111 | sub DESTROY { |
|
|
112 | my ($self) = @_; |
|
|
113 | |
|
|
114 | return unless exists $self->{name}; |
|
|
115 | |
|
|
116 | glDeleteTextures delete $self->{name}; |
|
|
117 | } |
|
|
118 | |
|
|
119 | push @::GLINIT, sub { |
|
|
120 | $_->upload |
|
|
121 | for grep $_, @textures; |
|
|
122 | }; |
|
|
123 | |
24 | 1; |
124 | 1; |
25 | |
125 | |
26 | =back |
126 | =back |
27 | |
127 | |
28 | =head1 AUTHOR |
128 | =head1 AUTHOR |