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.31 by root, Fri Apr 14 14:55:27 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 SDL::OpenGL; 24use Carp ();
25use AnyEvent;
26use BerkeleyDB;
27use CFClient::OpenGL;
25 28
26our %GL_EXT; 29our %GL_EXT;
27our $GL_VERSION; 30our $GL_VERSION;
28 31
29our $GL_NPOT; 32our $GL_NPOT;
86 } 89 }
87 90
88 close CFG; 91 close CFG;
89} 92}
90 93
94mkdir "$Crossfire::VARDIR/pclient", 0777;
95
96our $DB_ENV = new BerkeleyDB::Env
97 -Home => "$Crossfire::VARDIR/pclient",
98 -Cachesize => 1_000_000,
99 -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt",
100# -ErrPrefix => "DATABASE",
101 -Verbose => 1,
102 -Flags => DB_CREATE | DB_RECOVER_FATAL | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN,
103 or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error";
104
105sub db_table($) {
106 my ($table) = @_;
107
108 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
109
110 new CFClient::Database
111 -Env => $DB_ENV,
112 -Filename => $table,
113# -Filename => "database",
114# -Subname => $table,
115 -Flags => DB_CREATE | DB_UPGRADE,
116 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error";
117}
118
119package CFClient::Database;
120
121our @ISA = BerkeleyDB::Btree::;
122
123sub get($$) {
124 my $data;
125
126 $_[0]->db_get ($_[1], $data) == 0
127 ? $data
128 : ()
129}
130
131my %DB_SYNC;
132
133sub put($$$) {
134 my ($db, $key, $data) = @_;
135
136 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
137
138 $db->db_put ($key => $data)
139}
140
91package CFClient::Texture; 141package CFClient::Texture;
92 142
93use strict; 143use strict;
94 144
95use Scalar::Util; 145use Scalar::Util;
96 146
97use SDL::OpenGL; 147use CFClient::OpenGL;
98 148
99my @textures; 149my %TEXTURES;
100 150
101sub new { 151sub new {
102 my ($class, %data) = @_; 152 my ($class, %data) = @_;
103 153
104 my $self = bless { 154 my $self = bless {
106 format => GL_RGBA, 156 format => GL_RGBA,
107 type => GL_UNSIGNED_BYTE, 157 type => GL_UNSIGNED_BYTE,
108 %data, 158 %data,
109 }, $class; 159 }, $class;
110 160
111 push @textures, $self; 161 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
112 Scalar::Util::weaken $textures[-1];
113 162
114 $self->upload; 163 $self->upload;
115 164
116 $self 165 $self
117} 166}
118 167
119sub new_from_image { 168sub new_from_image {
120 my ($class, $image) = @_; 169 my ($class, $image, %arg) = @_;
121 170
122 $class->new (image => $image) 171 $class->new (image => $image, %arg)
123} 172}
124 173
125sub new_from_file { 174sub new_from_file {
126 my ($class, $path) = @_; 175 my ($class, $path, %arg) = @_;
127 176
128 open my $fh, "<:raw", $path 177 open my $fh, "<:raw", $path
129 or die "$path: $!"; 178 or die "$path: $!";
130 179
131 local $/; 180 local $/;
132 $class->new_from_image (<$fh>) 181 $class->new_from_image (<$fh>, %arg)
133} 182}
134 183
135#sub new_from_surface { 184#sub new_from_surface {
136# my ($class, $surface) = @_; 185# my ($class, $surface) = @_;
137# 186#
143# h => $surface->height, 192# h => $surface->height,
144# ) 193# )
145#} 194#}
146 195
147sub new_from_layout { 196sub new_from_layout {
148 my ($class, $layout) = @_; 197 my ($class, $layout, %arg) = @_;
149 198
150 my ($w, $h, $data) = $layout->render; 199 my ($w, $h, $data) = $layout->render;
151 200
152 $class->new ( 201 $class->new (
153 w => $w, 202 w => $w,
154 h => $h, 203 h => $h,
155 data => $data, 204 data => $data,
156 internalformat => GL_ALPHA4, 205 internalformat => GL_ALPHA4,
157 format => GL_ALPHA, 206 format => GL_ALPHA,
158 type => GL_UNSIGNED_BYTE, 207 type => GL_UNSIGNED_BYTE,
208 %arg,
159 ) 209 )
160} 210}
161 211
162sub new_from_opengl { 212sub new_from_opengl {
163 my ($class, $w, $h, $cb) = @_; 213 my ($class, $w, $h, $cb) = @_;
198 unless ($tw && $th) { 248 unless ($tw && $th) {
199 $tw = $th = 1; 249 $tw = $th = 1;
200 $data = "\x00" x 64; 250 $data = "\x00" x 64;
201 } 251 }
202 252
253 $self->{minified} = [CFClient::average $tw, $th, $data]
254 if $self->{minify};
255
203 unless ($GL_NPOT) { 256 unless ($GL_NPOT) {
204 # TODO: does not work for zero-sized textures 257 # TODO: does not work for zero-sized textures
205 $tw = topot $tw; 258 $tw = topot $tw;
206 $th = topot $th; 259 $th = topot $th;
207 260
208 if ($tw != $self->{w} || $th != $self->{h} && defined $data) { 261 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) {
209 my $bpp = (length $data) / ($self->{w} * $self->{h}); 262 my $bpp = (length $data) / ($self->{w} * $self->{h});
210 $data = pack "(a" . ($tw * $bpp) . ")*", 263 $data = pack "(a" . ($tw * $bpp) . ")*",
211 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 264 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
212 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 265 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
213 } 266 }
214 } 267 }
215 268
216 $self->{s} = $self->{w} / $tw; 269 $self->{s} = $self->{w} / $tw;
217 $self->{t} = $self->{h} / $th; 270 $self->{t} = $self->{h} / $th;
218 271
272 glGetError;
273
219 $self->{name} ||= (glGenTextures 1)->[0]; 274 $self->{name} ||= glGenTexture;
220 275
221 glBindTexture GL_TEXTURE_2D, $self->{name}; 276 glBindTexture GL_TEXTURE_2D, $self->{name};
222 277
223 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 278 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
226 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 }
227 289
228 if (defined $data) { 290 if (defined $data) {
229 glTexImage2D GL_TEXTURE_2D, 0, 291 glTexImage2D GL_TEXTURE_2D, 0,
230 $self->{internalformat}, 292 $self->{internalformat},
231 $tw, $th, # need to pad texture first 293 $tw, $th, # need to pad texture first
232 0, 294 0,
233 $self->{format}, 295 $self->{format},
234 $self->{type}, 296 $self->{type},
235 $data; 297 $data;
236 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 }
237 } else { 302 } else {
238 glCopyTexImage2D GL_TEXTURE_2D, 0, 303 glCopyTexImage2D GL_TEXTURE_2D, 0,
239 $self->{internalformat}, 304 $self->{internalformat},
240 0, 0, 305 0, 0,
241 $tw, $th, 306 $tw, $th,
242 0; 307 0;
243 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 }
244 } 312 }
245} 313}
246 314
247sub DESTROY { 315sub DESTROY {
248 my ($self) = @_; 316 my ($self) = @_;
249 317
250 return unless exists $self->{name}; 318 delete $TEXTURES{$self+0};
251 319
252 glDeleteTextures delete $self->{name}; 320 glDeleteTexture delete $self->{name}
321 if $self->{name};
253} 322}
254 323
255sub restore_state{ 324sub restore_state{
256 $_->upload 325 $_->upload
257 for grep $_, @textures; 326 for values %TEXTURES;
258}; 327};
259 328
2601; 3291;
261 330
262=back 331=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines