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.36 by root, Sat Apr 15 23:24:59 2006 UTC vs.
Revision 1.43 by root, Fri Apr 21 07:19:56 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;
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($) {
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
113package CFClient::Database; 119package CFClient::Database;
117sub get($$) { 123sub 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
125my %DB_SYNC; 131my %DB_SYNC;
126 132
127sub put($$$) { 133sub 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
136package CFClient::Texture; 141package CFClient::Texture;
137 142
138use strict; 143use strict;
139 144
140use Scalar::Util; 145use Scalar::Util;
141 146
142use SDL::OpenGL; 147use CFClient::OpenGL;
143 148
144my %TEXTURES; 149my %TEXTURES;
145 150
146sub new { 151sub 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
169sub new_from_file { 174sub 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
191sub new_from_layout { 196sub 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, 205 internalformat => GL_ALPHA4,
201 format => GL_ALPHA, 206 format => GL_ALPHA,
202 type => GL_UNSIGNED_BYTE, 207 type => GL_UNSIGNED_BYTE,
208 %arg,
203 ) 209 )
204} 210}
205 211
206sub new_from_opengl { 212sub 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
272 glGetError;
273
266 $self->{name} ||= (glGenTextures 1)->[0]; 274 $self->{name} ||= glGenTexture;
267 275
268 glBindTexture GL_TEXTURE_2D, $self->{name}; 276 glBindTexture GL_TEXTURE_2D, $self->{name};
269 277
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; 278 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
273 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 }
274 289
275 if (defined $data) { 290 if (defined $data) {
276 glTexImage2D GL_TEXTURE_2D, 0, 291 glTexImage2D GL_TEXTURE_2D, 0,
277 $self->{internalformat}, 292 $self->{internalformat},
278 $tw, $th, # need to pad texture first 293 $tw, $th, # need to pad texture first
279 0, 294 0,
280 $self->{format}, 295 $self->{format},
281 $self->{type}, 296 $self->{type},
282 $data; 297 $data;
283 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 }
284 } else { 302 } else {
285 glCopyTexImage2D GL_TEXTURE_2D, 0, 303 glCopyTexImage2D GL_TEXTURE_2D, 0,
286 $self->{internalformat}, 304 $self->{internalformat},
287 0, 0, 305 0, 0,
288 $tw, $th, 306 $tw, $th,
289 0; 307 0;
290 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 }
291 } 312 }
292} 313}
293 314
294sub DESTROY { 315sub DESTROY {
295 my ($self) = @_; 316 my ($self) = @_;
296 317
297 delete $TEXTURES{$self+0}; 318 delete $TEXTURES{$self+0};
298 319
299 glDeleteTextures delete $self->{name} 320 glDeleteTexture delete $self->{name}
300 if $self->{name}; 321 if $self->{name};
301} 322}
302 323
303sub restore_state{ 324sub restore_state{
304 $_->upload 325 $_->upload

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines