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.30 by root, Wed Apr 12 21:35:10 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 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;
46} 49}
47 50
48sub find_rcfile($) { 51sub find_rcfile($) {
49 my $path; 52 my $path;
50 53
51 for (@INC) { 54 for (grep !ref, @INC) {
52 $path = "$_/CFClient/resources/$_[0]"; 55 $path = "$_/CFClient/resources/$_[0]";
53 return $path if -r $path; 56 return $path if -r $path;
54 } 57 }
55 58
56 die "FATAL: can't find required file $_[0]\n"; 59 die "FATAL: can't find required file $_[0]\n";
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,
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) = @_;
164 214
165 $class->new (w => $w, h => $h, render_cb => $cb) 215 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
166} 216}
167 217
168sub topot { 218sub topot {
169 (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]
170} 220}
179 if (exists $self->{data}) { 229 if (exists $self->{data}) {
180 $data = $self->{data}; 230 $data = $self->{data};
181 231
182 } elsif (exists $self->{render_cb}) { 232 } elsif (exists $self->{render_cb}) {
183 glViewport 0, 0, $self->{w}, $self->{h}; 233 glViewport 0, 0, $self->{w}, $self->{h};
184 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
185 glMatrixMode GL_PROJECTION; 234 glMatrixMode GL_PROJECTION;
186 glLoadIdentity; 235 glLoadIdentity;
236 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 glMatrixMode GL_MODELVIEW; 237 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity; 238 glLoadIdentity;
189 glClear GL_COLOR_BUFFER_BIT;
190 $self->{render_cb}->($self, $self->{w}, $self->{h}); 239 $self->{render_cb}->($self, $self->{w}, $self->{h});
191 240
192 } else { 241 } else {
193 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) 242 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
194 = CFClient::load_image_inline $self->{image}; 243 = CFClient::load_image_inline $self->{image};
195 } 244 }
196 245
197 my ($tw, $th) = @$self{qw(w h)}; 246 my ($tw, $th) = @$self{qw(w h)};
198 247
199 unless ($tw && $th) { 248 unless ($tw > 0 && $th > 0) {
200 $tw = $th = 1; 249 $tw = $th = 1;
201 $data = "\x00" x 64; 250 $data = "\x00" x 64;
202 } 251 }
252
253 $self->{minified} = [CFClient::average $tw, $th, $data]
254 if $self->{minify};
203 255
204 unless ($GL_NPOT) { 256 unless ($GL_NPOT) {
205 # TODO: does not work for zero-sized textures 257 # TODO: does not work for zero-sized textures
206 $tw = topot $tw; 258 $tw = topot $tw;
207 $th = topot $th; 259 $th = topot $th;
208 260
209 if ($tw != $self->{w} || $th != $self->{h} && defined $data) { 261 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) {
210 my $bpp = (length $data) / ($self->{w} * $self->{h}); 262 my $bpp = (length $data) / ($self->{w} * $self->{h});
211 $data = pack "(a" . ($tw * $bpp) . ")*", 263 $data = pack "(a" . ($tw * $bpp) . ")*",
212 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 264 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
213 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 265 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
214 } 266 }
215 } 267 }
216 268
217 $self->{s} = $self->{w} / $tw; 269 $self->{s} = $self->{w} / $tw;
218 $self->{t} = $self->{h} / $th; 270 $self->{t} = $self->{h} / $th;
219 271
220 $self->{name} ||= (glGenTextures 1)->[0]; 272 $self->{name} ||= glGenTexture;
221 273
222 glBindTexture GL_TEXTURE_2D, $self->{name}; 274 glBindTexture GL_TEXTURE_2D, $self->{name};
223 275
224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 276 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
227 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 }
228 292
293 glGetError;
294
229 if (defined $data) { 295 if (defined $data) {
230 glTexImage2D GL_TEXTURE_2D, 0, 296 glTexImage2D GL_TEXTURE_2D, 0,
231 $self->{internalformat}, 297 $self->{internalformat},
232 $tw, $th, # need to pad texture first 298 $tw, $th, # need to pad texture first
233 0, 299 0,
234 $self->{format}, 300 $self->{format},
235 $self->{type}, 301 $self->{type},
236 $data; 302 $data;
237 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 }
238 } else { 307 } else {
239 glCopyTexImage2D GL_TEXTURE_2D, 0, 308 glCopyTexImage2D GL_TEXTURE_2D, 0,
240 $self->{internalformat}, 309 $self->{internalformat},
241 0, 0, 310 0, 0,
242 $tw, $th, 311 $tw, $th,
243 0; 312 0;
244 glGetError and die; 313 if (my $error = glGetError) {
314 Carp::cluck sprintf "texture upload error: %x %dx%d i=%x",
315 $error, $tw, $th, $self->{internalformat};
316 }
245 } 317 }
246} 318}
247 319
248sub DESTROY { 320sub DESTROY {
249 my ($self) = @_; 321 my ($self) = @_;
250 322
251 return unless exists $self->{name}; 323 delete $TEXTURES{$self+0};
252 324
253 glDeleteTextures delete $self->{name}; 325 glDeleteTexture delete $self->{name}
326 if $self->{name};
254} 327}
255 328
256sub restore_state{ 329sub restore_state{
257 $_->upload 330 $_->upload
258 for grep $_, @textures; 331 for values %TEXTURES;
259}; 332};
260 333
2611; 3341;
262 335
263=back 336=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines