… | |
… | |
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; |
… | |
… | |
48 | } |
49 | } |
49 | |
50 | |
50 | sub find_rcfile($) { |
51 | sub find_rcfile($) { |
51 | my $path; |
52 | my $path; |
52 | |
53 | |
53 | for (@INC) { |
54 | for (grep !ref, @INC) { |
54 | $path = "$_/CFClient/resources/$_[0]"; |
55 | $path = "$_/CFClient/resources/$_[0]"; |
55 | return $path if -r $path; |
56 | return $path if -r $path; |
56 | } |
57 | } |
57 | |
58 | |
58 | die "FATAL: can't find required file $_[0]\n"; |
59 | die "FATAL: can't find required file $_[0]\n"; |
… | |
… | |
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($) { |
|
|
106 | my ($table) = @_; |
|
|
107 | |
|
|
108 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
109 | |
105 | new CFClient::Database |
110 | new CFClient::Database |
106 | -Env => $DB_ENV, |
111 | -Env => $DB_ENV, |
|
|
112 | -Filename => $table, |
107 | -Filename => "database", |
113 | # -Filename => "database", |
108 | -Subname => $_[0], |
114 | # -Subname => $table, |
109 | -Flags => DB_CREATE | DB_UPGRADE, |
115 | -Flags => DB_CREATE | DB_UPGRADE, |
110 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"; |
116 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"; |
111 | } |
117 | } |
112 | |
118 | |
113 | package CFClient::Database; |
119 | package CFClient::Database; |
… | |
… | |
136 | |
142 | |
137 | use strict; |
143 | use strict; |
138 | |
144 | |
139 | use Scalar::Util; |
145 | use Scalar::Util; |
140 | |
146 | |
141 | use SDL::OpenGL; |
147 | use CFClient::OpenGL; |
142 | |
148 | |
143 | my %TEXTURES; |
149 | my %TEXTURES; |
144 | |
150 | |
145 | sub new { |
151 | sub new { |
146 | my ($class, %data) = @_; |
152 | my ($class, %data) = @_; |
… | |
… | |
164 | |
170 | |
165 | $class->new (image => $image, %arg) |
171 | $class->new (image => $image, %arg) |
166 | } |
172 | } |
167 | |
173 | |
168 | sub new_from_file { |
174 | sub new_from_file { |
169 | my ($class, $path) = @_; |
175 | my ($class, $path, %arg) = @_; |
170 | |
176 | |
171 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
172 | or die "$path: $!"; |
178 | or die "$path: $!"; |
173 | |
179 | |
174 | local $/; |
180 | local $/; |
175 | $class->new_from_image (<$fh>) |
181 | $class->new_from_image (<$fh>, %arg) |
176 | } |
182 | } |
177 | |
183 | |
178 | #sub new_from_surface { |
184 | #sub new_from_surface { |
179 | # my ($class, $surface) = @_; |
185 | # my ($class, $surface) = @_; |
180 | # |
186 | # |
… | |
… | |
186 | # h => $surface->height, |
192 | # h => $surface->height, |
187 | # ) |
193 | # ) |
188 | #} |
194 | #} |
189 | |
195 | |
190 | sub new_from_layout { |
196 | sub new_from_layout { |
191 | my ($class, $layout) = @_; |
197 | my ($class, $layout, %arg) = @_; |
192 | |
198 | |
193 | my ($w, $h, $data) = $layout->render; |
199 | my ($w, $h, $data) = $layout->render; |
194 | |
200 | |
195 | $class->new ( |
201 | $class->new ( |
196 | w => $w, |
202 | w => $w, |
197 | h => $h, |
203 | h => $h, |
198 | data => $data, |
204 | data => $data, |
199 | internalformat => GL_ALPHA4, |
|
|
200 | format => GL_ALPHA, |
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
201 | type => GL_UNSIGNED_BYTE, |
207 | type => GL_UNSIGNED_BYTE, |
|
|
208 | %arg, |
202 | ) |
209 | ) |
203 | } |
210 | } |
204 | |
211 | |
205 | sub new_from_opengl { |
212 | sub new_from_opengl { |
206 | my ($class, $w, $h, $cb) = @_; |
213 | my ($class, $w, $h, $cb) = @_; |
… | |
… | |
249 | unless ($GL_NPOT) { |
256 | unless ($GL_NPOT) { |
250 | # TODO: does not work for zero-sized textures |
257 | # TODO: does not work for zero-sized textures |
251 | $tw = topot $tw; |
258 | $tw = topot $tw; |
252 | $th = topot $th; |
259 | $th = topot $th; |
253 | |
260 | |
254 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
255 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
256 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
257 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
258 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
259 | } |
266 | } |
260 | } |
267 | } |
261 | |
268 | |
262 | $self->{s} = $self->{w} / $tw; |
269 | $self->{s} = $self->{w} / $tw; |
263 | $self->{t} = $self->{h} / $th; |
270 | $self->{t} = $self->{h} / $th; |
264 | |
271 | |
265 | $self->{name} ||= (glGenTextures 1)->[0]; |
272 | $self->{name} ||= glGenTexture; |
266 | |
273 | |
267 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
274 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
268 | |
275 | |
269 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
270 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
271 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
272 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
277 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
278 | |
|
|
279 | if ($::FAST) { |
|
|
280 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
281 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
282 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
283 | # alternatively check for 0x8191 |
|
|
284 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
285 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
286 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
287 | } else { |
|
|
288 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
289 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
290 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
291 | } |
273 | |
292 | |
|
|
293 | glGetError; |
|
|
294 | |
274 | if (defined $data) { |
295 | if (defined $data) { |
275 | glTexImage2D GL_TEXTURE_2D, 0, |
296 | glTexImage2D GL_TEXTURE_2D, 0, |
276 | $self->{internalformat}, |
297 | $self->{internalformat}, |
277 | $tw, $th, # need to pad texture first |
298 | $tw, $th, # need to pad texture first |
278 | 0, |
299 | 0, |
279 | $self->{format}, |
300 | $self->{format}, |
280 | $self->{type}, |
301 | $self->{type}, |
281 | $data; |
302 | $data; |
282 | glGetError and die; |
303 | if (my $error = glGetError) { |
|
|
304 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x f=%x t=%x", |
|
|
305 | $error, $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
|
|
306 | } |
283 | } else { |
307 | } else { |
284 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
308 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
285 | $self->{internalformat}, |
309 | $self->{internalformat}, |
286 | 0, 0, |
310 | 0, 0, |
287 | $tw, $th, |
311 | $tw, $th, |
288 | 0; |
312 | 0; |
289 | glGetError and die "glCopyTexImage2D $tw,$th"; |
313 | if (my $error = glGetError) { |
|
|
314 | Carp::cluck sprintf "texture upload error: %x %dx%d i=%x", |
|
|
315 | $error, $tw, $th, $self->{internalformat}; |
|
|
316 | } |
290 | } |
317 | } |
291 | } |
318 | } |
292 | |
319 | |
293 | sub DESTROY { |
320 | sub DESTROY { |
294 | my ($self) = @_; |
321 | my ($self) = @_; |
295 | |
322 | |
296 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
297 | |
324 | |
298 | glDeleteTextures delete $self->{name} |
325 | glDeleteTexture delete $self->{name} |
299 | if $self->{name}; |
326 | if $self->{name}; |
300 | } |
327 | } |
301 | |
328 | |
302 | sub restore_state{ |
329 | sub restore_state{ |
303 | $_->upload |
330 | $_->upload |