ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.38 by root, Mon Apr 17 06:50:26 2006 UTC vs.
Revision 1.48 by root, Tue Apr 25 11:18:48 2006 UTC

19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 21 XSLoader::load "CFClient", $VERSION;
22} 22}
23 23
24use Carp ();
24use AnyEvent; 25use AnyEvent;
25use BerkeleyDB; 26use BerkeleyDB;
26use SDL::OpenGL; 27use CFClient::OpenGL;
27 28
28our %GL_EXT; 29our %GL_EXT;
29our $GL_VERSION; 30our $GL_VERSION;
30 31
31our $GL_NPOT; 32our $GL_NPOT;
48} 49}
49 50
50sub find_rcfile($) { 51sub 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";
93mkdir "$Crossfire::VARDIR/pclient", 0777; 94mkdir "$Crossfire::VARDIR/pclient", 0777;
94 95
95our $DB_ENV = new BerkeleyDB::Env 96our $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
104sub db_table($) { 105sub db_table($) {
105 my ($table) = @_; 106 my ($table) = @_;
106 107
141 142
142use strict; 143use strict;
143 144
144use Scalar::Util; 145use Scalar::Util;
145 146
146use SDL::OpenGL; 147use CFClient::OpenGL;
147 148
148my %TEXTURES; 149my %TEXTURES;
149 150
150sub new { 151sub 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
173sub new_from_file { 174sub 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
195sub new_from_layout { 196sub 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
210sub new_from_opengl { 212sub new_from_opengl {
211 my ($class, $w, $h, $cb) = @_; 213 my ($class, $w, $h, $cb) = @_;
212 214
213 $class->new (w => $w, h => $h, render_cb => $cb) 215 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
214} 216}
215 217
216sub topot { 218sub topot {
217 (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]
218} 220}
241 = CFClient::load_image_inline $self->{image}; 243 = CFClient::load_image_inline $self->{image};
242 } 244 }
243 245
244 my ($tw, $th) = @$self{qw(w h)}; 246 my ($tw, $th) = @$self{qw(w h)};
245 247
246 unless ($tw && $th) { 248 unless ($tw > 0 && $th > 0) {
247 $tw = $th = 1; 249 $tw = $th = 1;
248 $data = "\x00" x 64; 250 $data = "\x00" x 64;
249 } 251 }
250 252
251 $self->{minified} = [CFClient::average $tw, $th, $data] 253 $self->{minified} = [CFClient::average $tw, $th, $data]
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
270 $self->{name} ||= (glGenTextures 1)->[0]; 272 $self->{name} ||= glGenTexture;
271 273
272 glBindTexture GL_TEXTURE_2D, $self->{name}; 274 glBindTexture GL_TEXTURE_2D, $self->{name};
273 275
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; 276 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
277 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 }
278 292
293 glGetError;
294
279 if (defined $data) { 295 if (defined $data) {
280 glTexImage2D GL_TEXTURE_2D, 0, 296 glTexImage2D GL_TEXTURE_2D, 0,
281 $self->{internalformat}, 297 $self->{internalformat},
282 $tw, $th, # need to pad texture first 298 $tw, $th, # need to pad texture first
283 0, 299 0,
284 $self->{format}, 300 $self->{format},
285 $self->{type}, 301 $self->{type},
286 $data; 302 $data;
287 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 }
288 } else { 307 } else {
289 glCopyTexImage2D GL_TEXTURE_2D, 0, 308 glCopyTexImage2D GL_TEXTURE_2D, 0,
290 $self->{internalformat}, 309 $self->{internalformat},
291 0, 0, 310 0, 0,
292 $tw, $th, 311 $tw, $th,
293 0; 312 0;
294 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 }
295 } 317 }
296} 318}
297 319
298sub DESTROY { 320sub DESTROY {
299 my ($self) = @_; 321 my ($self) = @_;
300 322
301 delete $TEXTURES{$self+0}; 323 delete $TEXTURES{$self+0};
302 324
303 glDeleteTextures delete $self->{name} 325 glDeleteTexture delete $self->{name}
304 if $self->{name}; 326 if $self->{name};
305} 327}
306 328
307sub restore_state{ 329sub restore_state{
308 $_->upload 330 $_->upload

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines