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.27 by root, Wed Apr 12 18:09:21 2006 UTC vs.
Revision 1.51 by root, Sun May 14 20:51:19 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;
33our $GL_DEBUG = 1;
30 34
31sub gl_init { 35sub gl_init {
32 $GL_VERSION = gl_version * 1; 36 $GL_VERSION = gl_version * 1;
33 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; 37 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
34 38
35 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; 39 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
36 40
37 glClearColor 0.45, 0.45, 0.45, 1;
38
39 glEnable GL_TEXTURE_2D;
40 glEnable GL_COLOR_MATERIAL; 41 glDisable GL_COLOR_MATERIAL;
41 glShadeModel GL_FLAT; 42 glShadeModel GL_FLAT;
43 glDisable GL_DITHER;
42 glDisable GL_DEPTH_TEST; 44 glDisable GL_DEPTH_TEST;
43 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
44
45 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; 45 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
46 46
47 CFClient::Texture::restore_state (); 47 CFClient::Texture::restore_state ();
48}
49
50sub gl_check {
51 return unless $GL_DEBUG;
52
53 if (my $error = glGetError) {
54 Carp::cluck sprintf "opengl error %x while %s", $error, sprintf @_;
55 }
48} 56}
49 57
50sub find_rcfile($) { 58sub find_rcfile($) {
51 my $path; 59 my $path;
52 60
53 for (@INC) { 61 for (grep !ref, @INC) {
54 $path = "$_/CFClient/resources/$_[0]"; 62 $path = "$_/CFClient/resources/$_[0]";
55 return $path if -r $path; 63 return $path if -r $path;
56 } 64 }
57 65
58 die "FATAL: can't find required file $_[0]\n"; 66 die "FATAL: can't find required file $_[0]\n";
88 } 96 }
89 97
90 close CFG; 98 close CFG;
91} 99}
92 100
101mkdir "$Crossfire::VARDIR/pclient", 0777;
102
103our $DB_ENV = new BerkeleyDB::Env
104 -Home => "$Crossfire::VARDIR/pclient",
105 -Cachesize => 1_000_000,
106 -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt",
107# -ErrPrefix => "DATABASE",
108 -Verbose => 1,
109 -Flags => DB_CREATE | DB_RECOVER_FATAL | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN,
110 or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error";
111
112sub db_table($) {
113 my ($table) = @_;
114
115 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
116
117 new CFClient::Database
118 -Env => $DB_ENV,
119 -Filename => $table,
120# -Filename => "database",
121# -Subname => $table,
122 -Property => DB_CHKSUM,
123 -Flags => DB_CREATE | DB_UPGRADE,
124 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error";
125}
126
127package CFClient::Database;
128
129our @ISA = BerkeleyDB::Btree::;
130
131sub get($$) {
132 my $data;
133
134 $_[0]->db_get ($_[1], $data) == 0
135 ? $data
136 : ()
137}
138
139my %DB_SYNC;
140
141sub put($$$) {
142 my ($db, $key, $data) = @_;
143
144 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
145
146 $db->db_put ($key => $data)
147}
148
93package CFClient::Texture; 149package CFClient::Texture;
94 150
95use strict; 151use strict;
96 152
97use Scalar::Util; 153use Scalar::Util;
98 154
99use SDL::OpenGL; 155use CFClient::OpenGL;
100 156
101my @textures; 157my %TEXTURES;
102 158
103sub new { 159sub new {
104 my ($class, %data) = @_; 160 my ($class, %data) = @_;
105 161
106 my $self = bless { 162 my $self = bless {
107 internalformat => GL_RGBA, 163 internalformat => GL_RGBA,
108 format => GL_RGBA, 164 format => GL_RGBA,
165 type => GL_UNSIGNED_BYTE,
109 %data, 166 %data,
110 }, $class; 167 }, $class;
111 168
112 push @textures, $self; 169 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
113 Scalar::Util::weaken $textures[-1];
114 170
115 $self->upload; 171 $self->upload;
116 172
117 $self 173 $self
118} 174}
119 175
120sub new_from_image { 176sub new_from_image {
121 my ($class, $image) = @_; 177 my ($class, $image, %arg) = @_;
122 178
123 $class->new (image => $image) 179 $class->new (image => $image, %arg)
124} 180}
125 181
126sub new_from_file { 182sub new_from_file {
127 my ($class, $path) = @_; 183 my ($class, $path, %arg) = @_;
128 184
129 open my $fh, "<:raw", $path 185 open my $fh, "<:raw", $path
130 or die "$path: $!"; 186 or die "$path: $!";
131 187
132 local $/; 188 local $/;
133 $class->new_from_image (<$fh>) 189 $class->new_from_image (<$fh>, %arg)
134} 190}
135 191
136#sub new_from_surface { 192#sub new_from_surface {
137# my ($class, $surface) = @_; 193# my ($class, $surface) = @_;
138# 194#
144# h => $surface->height, 200# h => $surface->height,
145# ) 201# )
146#} 202#}
147 203
148sub new_from_layout { 204sub new_from_layout {
149 my ($class, $layout) = @_; 205 my ($class, $layout, %arg) = @_;
150 206
151 my ($w, $h, $data) = $layout->render; 207 my ($w, $h, $data, $format, $internalformat) = $layout->render;
152 208
153 $class->new ( 209 $class->new (
154 w => $w, 210 w => $w,
155 h => $h, 211 h => $h,
156 data => $data, 212 data => $data,
157 internalformat => GL_ALPHA4,
158 format => GL_ALPHA, 213 format => $format,
214 internalformat => $format,
215 type => GL_UNSIGNED_BYTE,
216 %arg,
159 ) 217 )
160} 218}
161 219
162sub new_from_opengl { 220sub new_from_opengl {
163 my ($class, $w, $h, $cb) = @_; 221 my ($class, $w, $h, $cb) = @_;
164 222
165 $class->new (w => $w, h => $h, render_cb => $cb) 223 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
166} 224}
167 225
168sub topot { 226sub topot {
169 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] 227 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
170} 228}
179 if (exists $self->{data}) { 237 if (exists $self->{data}) {
180 $data = $self->{data}; 238 $data = $self->{data};
181 239
182 } elsif (exists $self->{render_cb}) { 240 } elsif (exists $self->{render_cb}) {
183 glViewport 0, 0, $self->{w}, $self->{h}; 241 glViewport 0, 0, $self->{w}, $self->{h};
184 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
185 glMatrixMode GL_PROJECTION; 242 glMatrixMode GL_PROJECTION;
186 glLoadIdentity; 243 glLoadIdentity;
244 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 glMatrixMode GL_MODELVIEW; 245 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity; 246 glLoadIdentity;
189 glClear GL_COLOR_BUFFER_BIT;
190 $self->{render_cb}->($self, $self->{w}, $self->{h}); 247 $self->{render_cb}->($self, $self->{w}, $self->{h});
191 248
192 } else { 249 } else {
193 my $pb = new Gtk2::Gdk::PixbufLoader; 250 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
194 $pb->write ($self->{image}); 251 = CFClient::load_image_inline $self->{image};
195 $pb->close;
196
197 $pb = $pb->get_pixbuf;
198 $pb = $pb->add_alpha (0, 0, 0, 0);
199
200 $self->{w} = $pb->get_width;
201 $self->{h} = $pb->get_height;
202
203 $data = $pb->get_pixels;
204 } 252 }
205 253
206 my ($tw, $th) = @$self{qw(w h)}; 254 my ($tw, $th) = @$self{qw(w h)};
207 255
208 unless ($tw && $th) { 256 unless ($tw > 0 && $th > 0) {
209 $tw = $th = 1; 257 $tw = $th = 1;
210 $data = "\x00" x 64; 258 $data = "\x00" x 64;
211 } 259 }
260
261 $self->{minified} = [CFClient::average $tw, $th, $data]
262 if $self->{minify};
212 263
213 unless ($GL_NPOT) { 264 unless ($GL_NPOT) {
214 # TODO: does not work for zero-sized textures 265 # TODO: does not work for zero-sized textures
215 $tw = topot $tw; 266 $tw = topot $tw;
216 $th = topot $th; 267 $th = topot $th;
217 268
218 if ($tw != $self->{w} || $th != $self->{h} && defined $data) { 269 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) {
219 my $bpp = (length $data) / ($self->{w} * $self->{h}); 270 my $bpp = (length $data) / ($self->{w} * $self->{h});
220 $data = pack "(a" . ($tw * $bpp) . ")*", 271 $data = pack "(a" . ($tw * $bpp) . ")*",
221 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 272 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
222 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 273 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
223 } 274 }
224 } 275 }
225 276
226 $self->{s} = $self->{w} / $tw; 277 $self->{s} = $self->{w} / $tw;
227 $self->{t} = $self->{h} / $th; 278 $self->{t} = $self->{h} / $th;
228 279
229 $self->{name} ||= (glGenTextures 1)->[0]; 280 $self->{name} ||= glGenTexture;
230 281
231 glBindTexture GL_TEXTURE_2D, $self->{name}; 282 glBindTexture GL_TEXTURE_2D, $self->{name};
232 283
233 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
234 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
235 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 284 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
236 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 285 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
286
287 if ($::FAST) {
288 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
289 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
290 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
291 # alternatively check for 0x8191
292 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
293 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
294 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
295 } else {
296 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
297 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
298 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
299 }
237 300
301 glGetError;
302
238 if (defined $data) { 303 if (defined $data) {
239 glTexImage2D GL_TEXTURE_2D, 0, 304 glTexImage2D GL_TEXTURE_2D, 0,
240 $self->{internalformat}, 305 $self->{internalformat},
241 $tw, $th, # need to pad texture first 306 $tw, $th, # need to pad texture first
242 0, 307 0,
243 $self->{format}, 308 $self->{format},
244 GL_UNSIGNED_BYTE, 309 $self->{type},
245 $data; 310 $data;
246 glGetError and die; 311 CFClient::gl_check "uploading texture %dx%d if=%x f=%x t=%x",
312 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
247 } else { 313 } else {
248 glCopyTexImage2D GL_TEXTURE_2D, 0, 314 glCopyTexImage2D GL_TEXTURE_2D, 0,
249 $self->{internalformat}, 315 $self->{internalformat},
250 0, 0, 316 0, 0,
251 $tw, $th, 317 $tw, $th,
252 0; 318 0;
253 glGetError and die; 319 CFClient::gl_check "copying to texture %dx%d if=%x",
320 $tw, $th, $self->{internalformat};
254 } 321 }
255} 322}
256 323
257sub DESTROY { 324sub DESTROY {
258 my ($self) = @_; 325 my ($self) = @_;
259 326
260 return unless exists $self->{name}; 327 delete $TEXTURES{$self+0};
261 328
262 glDeleteTextures delete $self->{name}; 329 glDeleteTexture delete $self->{name}
330 if $self->{name};
263} 331}
264 332
265sub restore_state{ 333sub restore_state{
266 $_->upload 334 $_->upload
267 for grep $_, @textures; 335 for values %TEXTURES;
268}; 336};
269 337
2701; 3381;
271 339
272=back 340=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines