… | |
… | |
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; |
… | |
… | |
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 | my @NAMES; |
|
|
146 | |
150 | |
147 | sub new { |
151 | sub new { |
148 | my ($class, %data) = @_; |
152 | my ($class, %data) = @_; |
149 | |
153 | |
150 | my $self = bless { |
154 | my $self = bless { |
… | |
… | |
166 | |
170 | |
167 | $class->new (image => $image, %arg) |
171 | $class->new (image => $image, %arg) |
168 | } |
172 | } |
169 | |
173 | |
170 | sub new_from_file { |
174 | sub new_from_file { |
171 | my ($class, $path) = @_; |
175 | my ($class, $path, %arg) = @_; |
172 | |
176 | |
173 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
174 | or die "$path: $!"; |
178 | or die "$path: $!"; |
175 | |
179 | |
176 | local $/; |
180 | local $/; |
177 | $class->new_from_image (<$fh>) |
181 | $class->new_from_image (<$fh>, %arg) |
178 | } |
182 | } |
179 | |
183 | |
180 | #sub new_from_surface { |
184 | #sub new_from_surface { |
181 | # my ($class, $surface) = @_; |
185 | # my ($class, $surface) = @_; |
182 | # |
186 | # |
… | |
… | |
188 | # h => $surface->height, |
192 | # h => $surface->height, |
189 | # ) |
193 | # ) |
190 | #} |
194 | #} |
191 | |
195 | |
192 | sub new_from_layout { |
196 | sub new_from_layout { |
193 | my ($class, $layout) = @_; |
197 | my ($class, $layout, %arg) = @_; |
194 | |
198 | |
195 | my ($w, $h, $data) = $layout->render; |
199 | my ($w, $h, $data) = $layout->render; |
196 | |
200 | |
197 | $class->new ( |
201 | $class->new ( |
198 | w => $w, |
202 | w => $w, |
199 | h => $h, |
203 | h => $h, |
200 | data => $data, |
204 | data => $data, |
201 | internalformat => GL_ALPHA4, |
|
|
202 | format => GL_ALPHA, |
205 | format => GL_ALPHA, |
|
|
206 | internalformat => GL_ALPHA, |
203 | type => GL_UNSIGNED_BYTE, |
207 | type => GL_UNSIGNED_BYTE, |
|
|
208 | %arg, |
204 | ) |
209 | ) |
205 | } |
210 | } |
206 | |
211 | |
207 | sub new_from_opengl { |
212 | sub new_from_opengl { |
208 | my ($class, $w, $h, $cb) = @_; |
213 | my ($class, $w, $h, $cb) = @_; |
209 | |
214 | |
210 | $class->new (w => $w, h => $h, render_cb => $cb) |
215 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
211 | } |
216 | } |
212 | |
217 | |
213 | sub topot { |
218 | sub topot { |
214 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
219 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
215 | } |
220 | } |
… | |
… | |
238 | = CFClient::load_image_inline $self->{image}; |
243 | = CFClient::load_image_inline $self->{image}; |
239 | } |
244 | } |
240 | |
245 | |
241 | my ($tw, $th) = @$self{qw(w h)}; |
246 | my ($tw, $th) = @$self{qw(w h)}; |
242 | |
247 | |
243 | unless ($tw && $th) { |
248 | unless ($tw > 0 && $th > 0) { |
244 | $tw = $th = 1; |
249 | $tw = $th = 1; |
245 | $data = "\x00" x 64; |
250 | $data = "\x00" x 64; |
246 | } |
251 | } |
247 | |
252 | |
248 | $self->{minified} = [CFClient::average $tw, $th, $data] |
253 | $self->{minified} = [CFClient::average $tw, $th, $data] |
… | |
… | |
251 | unless ($GL_NPOT) { |
256 | unless ($GL_NPOT) { |
252 | # TODO: does not work for zero-sized textures |
257 | # TODO: does not work for zero-sized textures |
253 | $tw = topot $tw; |
258 | $tw = topot $tw; |
254 | $th = topot $th; |
259 | $th = topot $th; |
255 | |
260 | |
256 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
261 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
257 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
262 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
258 | $data = pack "(a" . ($tw * $bpp) . ")*", |
263 | $data = pack "(a" . ($tw * $bpp) . ")*", |
259 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
264 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
260 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
265 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
261 | } |
266 | } |
262 | } |
267 | } |
263 | |
268 | |
264 | $self->{s} = $self->{w} / $tw; |
269 | $self->{s} = $self->{w} / $tw; |
265 | $self->{t} = $self->{h} / $th; |
270 | $self->{t} = $self->{h} / $th; |
266 | |
271 | |
267 | $self->{name} ||= (pop @NAMES) || (glGenTextures 1)->[0]; |
272 | $self->{name} ||= glGenTexture; |
268 | |
273 | |
269 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
274 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
270 | |
275 | |
271 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
272 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
|
|
273 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
276 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
274 | 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 | } |
275 | |
292 | |
|
|
293 | glGetError; |
|
|
294 | |
276 | if (defined $data) { |
295 | if (defined $data) { |
277 | glTexImage2D GL_TEXTURE_2D, 0, |
296 | glTexImage2D GL_TEXTURE_2D, 0, |
278 | $self->{internalformat}, |
297 | $self->{internalformat}, |
279 | $tw, $th, # need to pad texture first |
298 | $tw, $th, # need to pad texture first |
280 | 0, |
299 | 0, |
281 | $self->{format}, |
300 | $self->{format}, |
282 | $self->{type}, |
301 | $self->{type}, |
283 | $data; |
302 | $data; |
284 | 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 | } |
285 | } else { |
307 | } else { |
286 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
308 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
287 | $self->{internalformat}, |
309 | $self->{internalformat}, |
288 | 0, 0, |
310 | 0, 0, |
289 | $tw, $th, |
311 | $tw, $th, |
290 | 0; |
312 | 0; |
291 | 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 | } |
292 | } |
317 | } |
293 | } |
318 | } |
294 | |
319 | |
295 | sub DESTROY { |
320 | sub DESTROY { |
296 | my ($self) = @_; |
321 | my ($self) = @_; |
297 | |
322 | |
298 | delete $TEXTURES{$self+0}; |
323 | delete $TEXTURES{$self+0}; |
299 | |
324 | |
300 | if (my $name = delete $self->{name}) { |
325 | glDeleteTexture delete $self->{name} |
301 | glDeleteTextures $name; |
326 | if $self->{name}; |
302 | push @NAMES, $name; |
|
|
303 | } |
|
|
304 | |
|
|
305 | } |
327 | } |
306 | |
328 | |
307 | sub restore_state{ |
329 | sub restore_state{ |
308 | $_->upload |
330 | $_->upload |
309 | for grep $_, values %TEXTURES; |
331 | for values %TEXTURES; |
310 | }; |
332 | }; |
311 | |
333 | |
312 | 1; |
334 | 1; |
313 | |
335 | |
314 | =back |
336 | =back |