… | |
… | |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "CFClient", $VERSION; |
21 | XSLoader::load "CFClient", $VERSION; |
22 | } |
22 | } |
23 | |
23 | |
|
|
24 | use Carp (); |
24 | use AnyEvent; |
25 | use AnyEvent; |
25 | use BerkeleyDB; |
26 | use BerkeleyDB; |
26 | use SDL::OpenGL; |
27 | use CFClient::OpenGL; |
27 | |
28 | |
28 | our %GL_EXT; |
29 | our %GL_EXT; |
29 | our $GL_VERSION; |
30 | our $GL_VERSION; |
30 | |
31 | |
31 | our $GL_NPOT; |
32 | our $GL_NPOT; |
… | |
… | |
93 | mkdir "$Crossfire::VARDIR/pclient", 0777; |
94 | mkdir "$Crossfire::VARDIR/pclient", 0777; |
94 | |
95 | |
95 | our $DB_ENV = new BerkeleyDB::Env |
96 | our $DB_ENV = new BerkeleyDB::Env |
96 | -Home => "$Crossfire::VARDIR/pclient", |
97 | -Home => "$Crossfire::VARDIR/pclient", |
97 | -Cachesize => 1_000_000, |
98 | -Cachesize => 1_000_000, |
98 | -ErrFile => "/proc/self/fd/2", |
99 | -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt", |
99 | -ErrPrefix => "DATABASE", |
100 | # -ErrPrefix => "DATABASE", |
100 | -Verbose => 1, |
101 | -Verbose => 1, |
101 | -Flags => DB_CREATE | DB_JOINENV | DB_RECOVER_FATAL | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN, |
102 | -Flags => DB_CREATE | DB_RECOVER_FATAL | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN, |
102 | or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error"; |
103 | or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error"; |
103 | |
104 | |
104 | sub db_table($) { |
105 | sub db_table($) { |
105 | my ($table) = @_; |
106 | my ($table) = @_; |
106 | |
107 | |
… | |
… | |
141 | |
142 | |
142 | use strict; |
143 | use strict; |
143 | |
144 | |
144 | use Scalar::Util; |
145 | use Scalar::Util; |
145 | |
146 | |
146 | use SDL::OpenGL; |
147 | use CFClient::OpenGL; |
147 | |
148 | |
148 | my %TEXTURES; |
149 | my %TEXTURES; |
149 | |
150 | |
150 | sub new { |
151 | sub new { |
151 | my ($class, %data) = @_; |
152 | my ($class, %data) = @_; |
… | |
… | |
169 | |
170 | |
170 | $class->new (image => $image, %arg) |
171 | $class->new (image => $image, %arg) |
171 | } |
172 | } |
172 | |
173 | |
173 | sub new_from_file { |
174 | sub new_from_file { |
174 | my ($class, $path) = @_; |
175 | my ($class, $path, %arg) = @_; |
175 | |
176 | |
176 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
177 | or die "$path: $!"; |
178 | or die "$path: $!"; |
178 | |
179 | |
179 | local $/; |
180 | local $/; |
180 | $class->new_from_image (<$fh>) |
181 | $class->new_from_image (<$fh>, %arg) |
181 | } |
182 | } |
182 | |
183 | |
183 | #sub new_from_surface { |
184 | #sub new_from_surface { |
184 | # my ($class, $surface) = @_; |
185 | # my ($class, $surface) = @_; |
185 | # |
186 | # |
… | |
… | |
191 | # h => $surface->height, |
192 | # h => $surface->height, |
192 | # ) |
193 | # ) |
193 | #} |
194 | #} |
194 | |
195 | |
195 | sub new_from_layout { |
196 | sub new_from_layout { |
196 | my ($class, $layout) = @_; |
197 | my ($class, $layout, %arg) = @_; |
197 | |
198 | |
198 | my ($w, $h, $data) = $layout->render; |
199 | my ($w, $h, $data) = $layout->render; |
199 | |
200 | |
200 | $class->new ( |
201 | $class->new ( |
201 | w => $w, |
202 | w => $w, |
202 | h => $h, |
203 | h => $h, |
203 | data => $data, |
204 | data => $data, |
204 | internalformat => GL_ALPHA4, |
|
|
205 | format => GL_ALPHA, |
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
206 | type => GL_UNSIGNED_BYTE, |
207 | type => GL_UNSIGNED_BYTE, |
|
|
208 | %arg, |
207 | ) |
209 | ) |
208 | } |
210 | } |
209 | |
211 | |
210 | sub new_from_opengl { |
212 | sub new_from_opengl { |
211 | my ($class, $w, $h, $cb) = @_; |
213 | my ($class, $w, $h, $cb) = @_; |
… | |
… | |
254 | unless ($GL_NPOT) { |
256 | unless ($GL_NPOT) { |
255 | # TODO: does not work for zero-sized textures |
257 | # TODO: does not work for zero-sized textures |
256 | $tw = topot $tw; |
258 | $tw = topot $tw; |
257 | $th = topot $th; |
259 | $th = topot $th; |
258 | |
260 | |
259 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
260 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
261 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
262 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
263 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
264 | } |
266 | } |
265 | } |
267 | } |
266 | |
268 | |
267 | $self->{s} = $self->{w} / $tw; |
269 | $self->{s} = $self->{w} / $tw; |
268 | $self->{t} = $self->{h} / $th; |
270 | $self->{t} = $self->{h} / $th; |
269 | |
271 | |
|
|
272 | glGetError; |
|
|
273 | |
270 | $self->{name} ||= (glGenTextures 1)->[0]; |
274 | $self->{name} ||= glGenTexture; |
271 | |
275 | |
272 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
276 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
273 | |
277 | |
274 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
275 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
278 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
279 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
280 | |
|
|
281 | if ($::FAST) { |
|
|
282 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
283 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
284 | } else { |
|
|
285 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
286 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
287 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $self->{mipmap} ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR; |
|
|
288 | } |
278 | |
289 | |
279 | if (defined $data) { |
290 | if (defined $data) { |
280 | glTexImage2D GL_TEXTURE_2D, 0, |
291 | glTexImage2D GL_TEXTURE_2D, 0, |
281 | $self->{internalformat}, |
292 | $self->{internalformat}, |
282 | $tw, $th, # need to pad texture first |
293 | $tw, $th, # need to pad texture first |
283 | 0, |
294 | 0, |
284 | $self->{format}, |
295 | $self->{format}, |
285 | $self->{type}, |
296 | $self->{type}, |
286 | $data; |
297 | $data; |
287 | glGetError and die; |
298 | if (my $error = glGetError) { |
|
|
299 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x", |
|
|
300 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
301 | } |
288 | } else { |
302 | } else { |
289 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
303 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
290 | $self->{internalformat}, |
304 | $self->{internalformat}, |
291 | 0, 0, |
305 | 0, 0, |
292 | $tw, $th, |
306 | $tw, $th, |
293 | 0; |
307 | 0; |
294 | glGetError and die "glCopyTexImage2D $tw,$th"; |
308 | if (my $error = glGetError) { |
|
|
309 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x", |
|
|
310 | $error, $tw, $th, $self->{internalformat}; |
|
|
311 | } |
295 | } |
312 | } |
296 | } |
313 | } |
297 | |
314 | |
298 | sub DESTROY { |
315 | sub DESTROY { |
299 | my ($self) = @_; |
316 | my ($self) = @_; |
300 | |
317 | |
301 | delete $TEXTURES{$self+0}; |
318 | delete $TEXTURES{$self+0}; |
302 | |
319 | |
303 | glDeleteTextures delete $self->{name} |
320 | glDeleteTexture delete $self->{name} |
304 | if $self->{name}; |
321 | if $self->{name}; |
305 | } |
322 | } |
306 | |
323 | |
307 | sub restore_state{ |
324 | sub restore_state{ |
308 | $_->upload |
325 | $_->upload |