… | |
… | |
40 | } |
40 | } |
41 | |
41 | |
42 | sub new_from_image { |
42 | sub new_from_image { |
43 | my ($class, $image, %arg) = @_; |
43 | my ($class, $image, %arg) = @_; |
44 | |
44 | |
45 | $class->new (image => $image, %arg) |
45 | $class->new (image => $image, internalformat => undef, %arg) |
46 | } |
46 | } |
47 | |
47 | |
48 | sub new_from_file { |
48 | sub new_from_file { |
49 | my ($class, $path, %arg) = @_; |
49 | my ($class, $path, %arg) = @_; |
50 | |
50 | |
… | |
… | |
65 | # w => $surface->width, |
65 | # w => $surface->width, |
66 | # h => $surface->height, |
66 | # h => $surface->height, |
67 | # ) |
67 | # ) |
68 | #} |
68 | #} |
69 | |
69 | |
70 | sub new_from_layout { |
70 | #sub new_from_layout { |
71 | my ($class, $layout, %arg) = @_; |
71 | # my ($class, $layout, %arg) = @_; |
72 | |
72 | # |
73 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
73 | # my ($w, $h, $data, $format, $internalformat) = $layout->render; |
74 | |
74 | # |
75 | $class->new ( |
75 | # $class->new ( |
76 | w => $w, |
76 | # w => $w, |
77 | h => $h, |
77 | # h => $h, |
78 | data => $data, |
78 | # data => $data, |
79 | format => $format, |
79 | # format => $format, |
80 | internalformat => $format, |
80 | # internalformat => $format, |
81 | type => GL_UNSIGNED_BYTE, |
81 | # type => GL_UNSIGNED_BYTE, |
82 | %arg, |
82 | # %arg, |
83 | ) |
83 | # ) |
84 | } |
84 | #} |
85 | |
85 | |
86 | sub new_from_opengl { |
86 | sub new_from_opengl { |
87 | my ($class, $w, $h, $cb) = @_; |
87 | my ($class, $w, $h, $cb) = @_; |
88 | |
88 | |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
89 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
… | |
… | |
107 | glMatrixMode GL_MODELVIEW; |
107 | glMatrixMode GL_MODELVIEW; |
108 | glLoadIdentity; |
108 | glLoadIdentity; |
109 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
109 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
110 | |
110 | |
111 | } else { |
111 | } else { |
112 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
112 | ($self->{w}, $self->{h}, $data, my $internalformat, $self->{format}, $self->{type}) |
113 | = CFClient::load_image_inline $self->{image}; |
113 | = CFClient::load_image_inline $self->{image}; |
|
|
114 | |
|
|
115 | $self->{internalformat} ||= $internalformat; |
114 | } |
116 | } |
115 | |
117 | |
116 | my ($tw, $th) = @$self{qw(w h)}; |
118 | my ($tw, $th) = @$self{qw(w h)}; |
117 | |
119 | |
118 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
120 | $self->{minified} ||= [CFClient::average $tw, $th, $data] |
… | |
… | |
159 | $self->{type}, |
161 | $self->{type}, |
160 | $data; |
162 | $data; |
161 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
163 | gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
162 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
164 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
163 | } else { |
165 | } else { |
|
|
166 | exists $self->{render_cb} or die; |
164 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
167 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
165 | $self->{internalformat}, |
168 | $self->{internalformat}, |
166 | 0, 0, |
169 | 0, 0, |
167 | $tw, $th, |
170 | $tw, $th, |
168 | 0; |
171 | 0; |
169 | gl_check "copying to texture %dx%d if=%x", |
172 | gl_check "copying to texture %dx%d if=%x", |
170 | $tw, $th, $self->{internalformat}; |
173 | $tw, $th, $self->{internalformat}; |
171 | } |
174 | } |
172 | } |
175 | } |
173 | |
176 | |
|
|
177 | sub shutdown { |
|
|
178 | my ($self) = @_; |
|
|
179 | |
|
|
180 | glDeleteTexture $self->{name} |
|
|
181 | if $self->{name}; |
|
|
182 | } |
|
|
183 | |
174 | sub DESTROY { |
184 | sub DESTROY { |
175 | my ($self) = @_; |
185 | my ($self) = @_; |
176 | |
186 | |
177 | delete $TEXTURES{$self+0}; |
187 | delete $TEXTURES{$self+0}; |
178 | |
188 | |
179 | glDeleteTexture delete $self->{name} |
189 | $self->shutdown; |
180 | if $self->{name}; |
|
|
181 | } |
190 | } |
182 | |
191 | |
183 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
192 | $CFClient::OpenGL::INIT_HOOK{"CFClient::Texture"} = sub { |
|
|
193 | # first mark all existing texture names as in-use, in case the context lost textures |
|
|
194 | glBindTexture GL_TEXTURE_2D, $_->{name} |
|
|
195 | for values %TEXTURES; |
184 | $_->upload |
196 | $_->upload |
185 | for values %TEXTURES; |
197 | for values %TEXTURES; |
186 | }; |
198 | }; |
187 | |
199 | |
|
|
200 | $CFClient::OpenGL::SHUTDOWN_HOOK{"CFClient::Texture"} = sub { |
|
|
201 | $_->shutdown |
|
|
202 | for values %TEXTURES; |
|
|
203 | }; |
|
|
204 | |
188 | 1; |
205 | 1; |
189 | |
206 | |
190 | =back |
207 | =back |
191 | |
208 | |
192 | =head1 AUTHOR |
209 | =head1 AUTHOR |