… | |
… | |
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($) { |
|
|
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; |
… | |
… | |
117 | sub get($$) { |
123 | sub get($$) { |
118 | my $data; |
124 | my $data; |
119 | |
125 | |
120 | $_[0]->db_get ($_[1], $data) == 0 |
126 | $_[0]->db_get ($_[1], $data) == 0 |
121 | ? $data |
127 | ? $data |
122 | : (); |
128 | : () |
123 | } |
129 | } |
124 | |
130 | |
125 | my %DB_SYNC; |
131 | my %DB_SYNC; |
126 | |
132 | |
127 | sub put($$$) { |
133 | sub put($$$) { |
128 | my ($db, $key, $data) = @_; |
134 | my ($db, $key, $data) = @_; |
129 | |
135 | |
130 | $db->db_put ($key => $data); |
|
|
131 | |
|
|
132 | $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); |
136 | $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); |
133 | |
137 | |
|
|
138 | $db->db_put ($key => $data) |
134 | } |
139 | } |
135 | |
140 | |
136 | package CFClient::Texture; |
141 | package CFClient::Texture; |
137 | |
142 | |
138 | use strict; |
143 | use strict; |
139 | |
144 | |
140 | use Scalar::Util; |
145 | use Scalar::Util; |
141 | |
146 | |
142 | use SDL::OpenGL; |
147 | use CFClient::OpenGL; |
143 | |
148 | |
144 | my %TEXTURES; |
149 | my %TEXTURES; |
145 | |
150 | |
146 | sub new { |
151 | sub new { |
147 | my ($class, %data) = @_; |
152 | my ($class, %data) = @_; |
… | |
… | |
165 | |
170 | |
166 | $class->new (image => $image, %arg) |
171 | $class->new (image => $image, %arg) |
167 | } |
172 | } |
168 | |
173 | |
169 | sub new_from_file { |
174 | sub new_from_file { |
170 | my ($class, $path) = @_; |
175 | my ($class, $path, %arg) = @_; |
171 | |
176 | |
172 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
173 | or die "$path: $!"; |
178 | or die "$path: $!"; |
174 | |
179 | |
175 | local $/; |
180 | local $/; |
176 | $class->new_from_image (<$fh>) |
181 | $class->new_from_image (<$fh>, %arg) |
177 | } |
182 | } |
178 | |
183 | |
179 | #sub new_from_surface { |
184 | #sub new_from_surface { |
180 | # my ($class, $surface) = @_; |
185 | # my ($class, $surface) = @_; |
181 | # |
186 | # |
… | |
… | |
187 | # h => $surface->height, |
192 | # h => $surface->height, |
188 | # ) |
193 | # ) |
189 | #} |
194 | #} |
190 | |
195 | |
191 | sub new_from_layout { |
196 | sub new_from_layout { |
192 | my ($class, $layout) = @_; |
197 | my ($class, $layout, %arg) = @_; |
193 | |
198 | |
194 | my ($w, $h, $data) = $layout->render; |
199 | my ($w, $h, $data) = $layout->render; |
195 | |
200 | |
196 | $class->new ( |
201 | $class->new ( |
197 | w => $w, |
202 | w => $w, |
198 | h => $h, |
203 | h => $h, |
199 | data => $data, |
204 | data => $data, |
200 | internalformat => GL_ALPHA4, |
|
|
201 | format => GL_ALPHA, |
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
202 | type => GL_UNSIGNED_BYTE, |
207 | type => GL_UNSIGNED_BYTE, |
|
|
208 | %arg, |
203 | ) |
209 | ) |
204 | } |
210 | } |
205 | |
211 | |
206 | sub new_from_opengl { |
212 | sub new_from_opengl { |
207 | my ($class, $w, $h, $cb) = @_; |
213 | my ($class, $w, $h, $cb) = @_; |
… | |
… | |
250 | unless ($GL_NPOT) { |
256 | unless ($GL_NPOT) { |
251 | # TODO: does not work for zero-sized textures |
257 | # TODO: does not work for zero-sized textures |
252 | $tw = topot $tw; |
258 | $tw = topot $tw; |
253 | $th = topot $th; |
259 | $th = topot $th; |
254 | |
260 | |
255 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
256 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
257 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
258 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
259 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
260 | } |
266 | } |
261 | } |
267 | } |
262 | |
268 | |
263 | $self->{s} = $self->{w} / $tw; |
269 | $self->{s} = $self->{w} / $tw; |
264 | $self->{t} = $self->{h} / $th; |
270 | $self->{t} = $self->{h} / $th; |
265 | |
271 | |
266 | $self->{name} ||= (glGenTextures 1)->[0]; |
272 | $self->{name} ||= glGenTexture; |
267 | |
273 | |
268 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
274 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
269 | |
275 | |
270 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
271 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
272 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
273 | 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 | } |
274 | |
292 | |
|
|
293 | glGetError; |
|
|
294 | |
275 | if (defined $data) { |
295 | if (defined $data) { |
276 | glTexImage2D GL_TEXTURE_2D, 0, |
296 | glTexImage2D GL_TEXTURE_2D, 0, |
277 | $self->{internalformat}, |
297 | $self->{internalformat}, |
278 | $tw, $th, # need to pad texture first |
298 | $tw, $th, # need to pad texture first |
279 | 0, |
299 | 0, |
280 | $self->{format}, |
300 | $self->{format}, |
281 | $self->{type}, |
301 | $self->{type}, |
282 | $data; |
302 | $data; |
283 | 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 | } |
284 | } else { |
307 | } else { |
285 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
308 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
286 | $self->{internalformat}, |
309 | $self->{internalformat}, |
287 | 0, 0, |
310 | 0, 0, |
288 | $tw, $th, |
311 | $tw, $th, |
289 | 0; |
312 | 0; |
290 | 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 | } |
291 | } |
317 | } |
292 | } |
318 | } |
293 | |
319 | |
294 | sub DESTROY { |
320 | sub DESTROY { |
295 | my ($self) = @_; |
321 | my ($self) = @_; |
296 | |
322 | |
297 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
298 | |
324 | |
299 | glDeleteTextures delete $self->{name} |
325 | glDeleteTexture delete $self->{name} |
300 | if $self->{name}; |
326 | if $self->{name}; |
301 | } |
327 | } |
302 | |
328 | |
303 | sub restore_state{ |
329 | sub restore_state{ |
304 | $_->upload |
330 | $_->upload |