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.33 by root, Sat Apr 15 01:13:45 2006 UTC vs.
Revision 1.44 by root, Mon Apr 24 08:22:20 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}
121 170
122 $class->new (image => $image, %arg) 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,
157 format => GL_ALPHA, 205 format => GL_ALPHA,
206 internalformat => 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) = @_;
206 unless ($GL_NPOT) { 256 unless ($GL_NPOT) {
207 # TODO: does not work for zero-sized textures 257 # TODO: does not work for zero-sized textures
208 $tw = topot $tw; 258 $tw = topot $tw;
209 $th = topot $th; 259 $th = topot $th;
210 260
211 if ($tw != $self->{w} || $th != $self->{h} && defined $data) { 261 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) {
212 my $bpp = (length $data) / ($self->{w} * $self->{h}); 262 my $bpp = (length $data) / ($self->{w} * $self->{h});
213 $data = pack "(a" . ($tw * $bpp) . ")*", 263 $data = pack "(a" . ($tw * $bpp) . ")*",
214 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 264 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
215 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 265 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
216 } 266 }
217 } 267 }
218 268
219 $self->{s} = $self->{w} / $tw; 269 $self->{s} = $self->{w} / $tw;
220 $self->{t} = $self->{h} / $th; 270 $self->{t} = $self->{h} / $th;
221 271
272 glGetError;
273
222 $self->{name} ||= (glGenTextures 1)->[0]; 274 $self->{name} ||= glGenTexture;
223 275
224 glBindTexture GL_TEXTURE_2D, $self->{name}; 276 glBindTexture GL_TEXTURE_2D, $self->{name};
225 277
226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
227 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
228 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 278 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
229 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 }
230 289
231 if (defined $data) { 290 if (defined $data) {
232 glTexImage2D GL_TEXTURE_2D, 0, 291 glTexImage2D GL_TEXTURE_2D, 0,
233 $self->{internalformat}, 292 $self->{internalformat},
234 $tw, $th, # need to pad texture first 293 $tw, $th, # need to pad texture first
235 0, 294 0,
236 $self->{format}, 295 $self->{format},
237 $self->{type}, 296 $self->{type},
238 $data; 297 $data;
239 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 }
240 } else { 302 } else {
241 glCopyTexImage2D GL_TEXTURE_2D, 0, 303 glCopyTexImage2D GL_TEXTURE_2D, 0,
242 $self->{internalformat}, 304 $self->{internalformat},
243 0, 0, 305 0, 0,
244 $tw, $th, 306 $tw, $th,
245 0; 307 0;
246 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 }
247 } 312 }
248} 313}
249 314
250sub DESTROY { 315sub DESTROY {
251 my ($self) = @_; 316 my ($self) = @_;
252 317
253 return unless exists $self->{name}; 318 delete $TEXTURES{$self+0};
254 319
255 glDeleteTextures delete $self->{name}; 320 glDeleteTexture delete $self->{name}
321 if $self->{name};
256} 322}
257 323
258sub restore_state{ 324sub restore_state{
259 $_->upload 325 $_->upload
260 for grep $_, @textures; 326 for values %TEXTURES;
261}; 327};
262 328
2631; 3291;
264 330
265=back 331=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines