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.34 by root, Sat Apr 15 21:16:49 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) = @_;
148 153
149 my $self = bless { 154 my $self = bless {
151 format => GL_RGBA, 156 format => GL_RGBA,
152 type => GL_UNSIGNED_BYTE, 157 type => GL_UNSIGNED_BYTE,
153 %data, 158 %data,
154 }, $class; 159 }, $class;
155 160
156 push @textures, $self; 161 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
157 Scalar::Util::weaken $textures[-1];
158 162
159 $self->upload; 163 $self->upload;
160 164
161 $self 165 $self
162} 166}
166 170
167 $class->new (image => $image, %arg) 171 $class->new (image => $image, %arg)
168} 172}
169 173
170sub new_from_file { 174sub 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
192sub new_from_layout { 196sub 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, 205 internalformat => GL_ALPHA4,
202 format => GL_ALPHA, 206 format => GL_ALPHA,
203 type => GL_UNSIGNED_BYTE, 207 type => GL_UNSIGNED_BYTE,
208 %arg,
204 ) 209 )
205} 210}
206 211
207sub new_from_opengl { 212sub new_from_opengl {
208 my ($class, $w, $h, $cb) = @_; 213 my ($class, $w, $h, $cb) = @_;
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
272 glGetError;
273
267 $self->{name} ||= (glGenTextures 1)->[0]; 274 $self->{name} ||= glGenTexture;
268 275
269 glBindTexture GL_TEXTURE_2D, $self->{name}; 276 glBindTexture GL_TEXTURE_2D, $self->{name};
270 277
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; 278 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
274 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 }
275 289
276 if (defined $data) { 290 if (defined $data) {
277 glTexImage2D GL_TEXTURE_2D, 0, 291 glTexImage2D GL_TEXTURE_2D, 0,
278 $self->{internalformat}, 292 $self->{internalformat},
279 $tw, $th, # need to pad texture first 293 $tw, $th, # need to pad texture first
280 0, 294 0,
281 $self->{format}, 295 $self->{format},
282 $self->{type}, 296 $self->{type},
283 $data; 297 $data;
284 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 }
285 } else { 302 } else {
286 glCopyTexImage2D GL_TEXTURE_2D, 0, 303 glCopyTexImage2D GL_TEXTURE_2D, 0,
287 $self->{internalformat}, 304 $self->{internalformat},
288 0, 0, 305 0, 0,
289 $tw, $th, 306 $tw, $th,
290 0; 307 0;
291 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 }
292 } 312 }
293} 313}
294 314
295sub DESTROY { 315sub DESTROY {
296 my ($self) = @_; 316 my ($self) = @_;
297 317
298 return unless exists $self->{name}; 318 delete $TEXTURES{$self+0};
299 319
300 glDeleteTextures delete $self->{name}; 320 glDeleteTexture delete $self->{name}
321 if $self->{name};
301} 322}
302 323
303sub restore_state{ 324sub restore_state{
304 $_->upload 325 $_->upload
305 for grep $_, @textures; 326 for values %TEXTURES;
306}; 327};
307 328
3081; 3291;
309 330
310=back 331=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines