… | |
… | |
110 | width => $surface->width, |
110 | width => $surface->width, |
111 | height => $surface->height, |
111 | height => $surface->height, |
112 | ) |
112 | ) |
113 | } |
113 | } |
114 | |
114 | |
|
|
115 | sub new_from_ttf { |
|
|
116 | my ($class, $ttf, $text) = @_; |
|
|
117 | |
|
|
118 | utf8::encode $text; |
|
|
119 | |
|
|
120 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
|
|
121 | (new SDL::Color -r => 255, -g => 255, -b => 255); |
|
|
122 | |
|
|
123 | $class->new_from_surface (bless \$surface, SDL::Surface::) |
|
|
124 | } |
|
|
125 | |
|
|
126 | sub new_from_opengl { |
|
|
127 | my ($class, $w, $h, $cb) = @_; |
|
|
128 | |
|
|
129 | $class->_new (width => $w, height => $h, render => $cb) |
|
|
130 | } |
|
|
131 | |
115 | sub upload { |
132 | sub upload { |
116 | my ($self) = @_; |
133 | my ($self) = @_; |
117 | |
134 | |
118 | return unless $SDL::App::USING_OPENGL; |
135 | return unless $SDL::App::USING_OPENGL; |
119 | |
136 | |
120 | my ($data, $width, $height); |
137 | my $data; |
121 | |
138 | |
122 | if (exists $self->{data}) { |
139 | if (exists $self->{data}) { |
123 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
140 | $data = $self->{data}; |
|
|
141 | } elsif (exists $self->{rendercb}) { |
|
|
142 | glViewport 0, 0, $self->{width}, $self->{height}; |
|
|
143 | glClear GL_COLOR_BUFFER_BIT; |
|
|
144 | |
|
|
145 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
124 | } else { |
146 | } else { |
125 | my $pb = new Gtk2::Gdk::PixbufLoader; |
147 | my $pb = new Gtk2::Gdk::PixbufLoader; |
126 | $pb->write ($self->{image}); |
148 | $pb->write ($self->{image}); |
127 | $pb->close; |
149 | $pb->close; |
128 | |
150 | |
129 | $pb = $pb->get_pixbuf; |
151 | $pb = $pb->get_pixbuf; |
130 | $pb = $pb->add_alpha (0, 0, 0, 0); |
152 | $pb = $pb->add_alpha (0, 0, 0, 0); |
131 | |
153 | |
132 | ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); |
154 | $self->{width} = $pb->get_width; |
|
|
155 | $self->{height} = $pb->get_height; |
|
|
156 | |
|
|
157 | $data = $pb->get_pixels; |
133 | } |
158 | } |
134 | |
159 | |
135 | ($self->{name}) = @{glGenTextures 1}; |
160 | ($self->{name}) = @{glGenTextures 1}; |
136 | |
161 | |
137 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
162 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
… | |
… | |
139 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
164 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
140 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
165 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
141 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
166 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
142 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
167 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
143 | |
168 | |
|
|
169 | if (defined $data) { |
144 | glTexImage2D GL_TEXTURE_2D, 0, |
170 | glTexImage2D GL_TEXTURE_2D, 0, |
145 | GL_RGBA8, |
171 | GL_RGBA8, |
146 | $width, $height, |
172 | $self->{width}, $self->{height}, |
147 | 0, |
173 | 0, |
148 | GL_RGBA, |
174 | GL_RGBA, |
149 | GL_UNSIGNED_BYTE, |
175 | GL_UNSIGNED_BYTE, |
150 | $data; |
176 | $data; |
|
|
177 | } else { |
|
|
178 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
179 | GL_RGBA8, |
|
|
180 | 0, 0, |
|
|
181 | $self->{width}, $self->{height}, |
|
|
182 | 0; |
|
|
183 | } |
151 | } |
184 | } |
152 | |
185 | |
153 | sub DESTROY { |
186 | sub DESTROY { |
154 | my ($self) = @_; |
187 | my ($self) = @_; |
155 | |
188 | |