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.66 by root, Thu May 25 21:18:47 2006 UTC vs.
Revision 1.70 by root, Fri May 26 18:28:23 2006 UTC

24use utf8; 24use utf8;
25 25
26use Carp (); 26use Carp ();
27use AnyEvent (); 27use AnyEvent ();
28use BerkeleyDB; 28use BerkeleyDB;
29
30use CFClient::OpenGL;
31
32our %GL_EXT;
33our $GL_VERSION;
34
35our $GL_NPOT;
36our $GL_DEBUG = 1;
37
38sub gl_init {
39 $GL_VERSION = gl_version * 1;
40 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
41
42 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
43 $GL_NPOT = 0 if gl_vendor =~ /ATI Technologies/; # ATI doesn't get it right...
44
45 glDisable GL_COLOR_MATERIAL;
46 glShadeModel GL_FLAT;
47 glDisable GL_DITHER;
48 glDisable GL_DEPTH_TEST;
49 glDepthMask 0;
50 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
51
52 CFClient::Texture::restore_state ();
53}
54
55sub gl_check {
56 return unless $GL_DEBUG;
57
58 if (my $error = glGetError) {
59 my ($format, @args) = @_;
60 Carp::cluck sprintf "opengl error %x while $format", $error, @args;
61 }
62}
63 29
64sub find_rcfile($) { 30sub find_rcfile($) {
65 my $path; 31 my $path;
66 32
67 for (grep !ref, @INC) { 33 for (grep !ref, @INC) {
171 . $_[1]->content->present ($_[0]) 137 . $_[1]->content->present ($_[0])
172} 138}
173 139
174sub view_verbatim { 140sub view_verbatim {
175 (join "", 141 (join "",
176 map +("\t" x ($indent / 2)) . "$_\n", 142 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
177 split /\n/, CFClient::UI::Label::escape ($_[1])) 143 split /\n/, CFClient::UI::Label::escape ($_[1]))
178 . "\n" 144 . "\n"
179} 145}
180 146
181sub view_textblock { 147sub view_textblock {
182 ("\t" x ($indent / 2)) . "$_[1]\n\n" 148 ("\t" x ($indent / 2)) . "$_[1]\n\n"
183} 149}
184 150
185sub view_head1 { 151sub view_head1 {
186 "<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 152 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
187 . $_[1]->content->present ($_[0]) 153 . $_[1]->content->present ($_[0])
188}; 154};
189 155
190sub view_head2 { 156sub view_head2 {
191 "<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n" 157 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
158 . $_[1]->content->present ($_[0])
159};
160
161sub view_head3 {
162 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
192 . $_[1]->content->present ($_[0]) 163 . $_[1]->content->present ($_[0])
193}; 164};
194 165
195sub view_over { 166sub view_over {
196 local $indent = $indent + $_[1]->indent; 167 local $indent = $indent + $_[1]->indent;
304 . "</small>\n"; 275 . "</small>\n";
305 276
306 $self->{face_widget} ||= new CFClient::UI::Face 277 $self->{face_widget} ||= new CFClient::UI::Face
307 can_events => 1, 278 can_events => 1,
308 can_hover => 1, 279 can_hover => 1,
280 anim => $self->{anim},
309 animspeed => $self->{animspeed}, # TODO# must be set at creation time 281 animspeed => $self->{animspeed}, # TODO# must be set at creation time
310 connect_button_down => $button_cb, 282 connect_button_down => $button_cb,
311 ; 283 ;
312 $self->{face_widget}{face} = $self->{face}; 284 $self->{face_widget}{face} = $self->{face};
313 $self->{face_widget}{anim} = $self->{anim}; 285 $self->{face_widget}{anim} = $self->{anim};
321 293
322 $self->{desc_widget} ||= new CFClient::UI::Label 294 $self->{desc_widget} ||= new CFClient::UI::Label
323 can_events => 1, 295 can_events => 1,
324 can_hover => 1, 296 can_hover => 1,
325 ellipsise => 2, 297 ellipsise => 2,
326 xalign => 0, 298 align => -1,
327 connect_button_down => $button_cb, 299 connect_button_down => $button_cb,
328 ; 300 ;
329 my $desc = CFClient::Item::desc_string $self; 301 my $desc = CFClient::Item::desc_string $self;
330 $self->{desc_widget}->set_text ($desc); 302 $self->{desc_widget}->set_text ($desc);
331 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std"); 303 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
332 304
333 $self->{weight_widget} ||= new CFClient::UI::Label 305 $self->{weight_widget} ||= new CFClient::UI::Label
334 can_events => 1, 306 can_events => 1,
335 can_hover => 1, 307 can_hover => 1,
336 ellipsise => 0, 308 ellipsise => 0,
337 xalign => 0.5, 309 align => 0,
338 connect_button_down => $button_cb, 310 connect_button_down => $button_cb,
339 ; 311 ;
340 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self); 312 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self);
341 313
342 $self->{weight_widget}->set_tooltip ( 314 $self->{weight_widget}->set_tooltip (
345 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ") 317 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
346 . "\n\n$tooltip_std" 318 . "\n\n$tooltip_std"
347 ); 319 );
348} 320}
349 321
350package CFClient::Texture;
351
352use strict;
353
354use Scalar::Util;
355
356use CFClient::OpenGL;
357
358my %TEXTURES;
359
360sub new {
361 my ($class, %data) = @_;
362
363 my $self = bless {
364 internalformat => GL_RGBA,
365 format => GL_RGBA,
366 type => GL_UNSIGNED_BYTE,
367 %data,
368 }, $class;
369
370 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
371
372 $self->upload;
373
374 $self
375}
376
377sub new_from_image {
378 my ($class, $image, %arg) = @_;
379
380 $class->new (image => $image, %arg)
381}
382
383sub new_from_file {
384 my ($class, $path, %arg) = @_;
385
386 open my $fh, "<:raw", $path
387 or die "$path: $!";
388
389 local $/;
390 $class->new_from_image (<$fh>, %arg)
391}
392
393#sub new_from_surface {
394# my ($class, $surface) = @_;
395#
396# $surface->rgba;
397#
398# $class->new (
399# data => $surface->pixels,
400# w => $surface->width,
401# h => $surface->height,
402# )
403#}
404
405sub new_from_layout {
406 my ($class, $layout, %arg) = @_;
407
408 my ($w, $h, $data, $format, $internalformat) = $layout->render;
409
410 $class->new (
411 w => $w,
412 h => $h,
413 data => $data,
414 format => $format,
415 internalformat => $format,
416 type => GL_UNSIGNED_BYTE,
417 %arg,
418 )
419}
420
421sub new_from_opengl {
422 my ($class, $w, $h, $cb) = @_;
423
424 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
425}
426
427sub topot {
428 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
429}
430
431sub upload {
432 my ($self) = @_;
433
434 return unless $GL_VERSION;
435
436 my $data;
437
438 if (exists $self->{data}) {
439 $data = $self->{data};
440
441 } elsif (exists $self->{render_cb}) {
442 glViewport 0, 0, $self->{w}, $self->{h};
443 glMatrixMode GL_PROJECTION;
444 glLoadIdentity;
445 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
446 glMatrixMode GL_MODELVIEW;
447 glLoadIdentity;
448 $self->{render_cb}->($self, $self->{w}, $self->{h});
449
450 } else {
451 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
452 = CFClient::load_image_inline $self->{image};
453 }
454
455 my ($tw, $th) = @$self{qw(w h)};
456
457 unless ($tw > 0 && $th > 0) {
458 $tw = $th = 1;
459 $data = "\x00" x 64;
460 }
461
462 $self->{minified} = [CFClient::average $tw, $th, $data]
463 if $self->{minify};
464
465 unless ($GL_NPOT) {
466 # TODO: does not work for zero-sized textures
467 $tw = topot $tw;
468 $th = topot $th;
469
470 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) {
471 my $bpp = (length $data) / ($self->{w} * $self->{h});
472 $data = pack "(a" . ($tw * $bpp) . ")*",
473 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
474 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
475 }
476 }
477
478 $self->{s} = $self->{w} / $tw;
479 $self->{t} = $self->{h} / $th;
480
481 $self->{name} ||= glGenTexture;
482
483 glBindTexture GL_TEXTURE_2D, $self->{name};
484
485 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
486 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
487
488 if ($::FAST) {
489 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
490 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
491 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
492 # alternatively check for 0x8191
493 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
494 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
495 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
496 } else {
497 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
498 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
499 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
500 }
501
502 glGetError;
503
504 if (defined $data) {
505 glTexImage2D GL_TEXTURE_2D, 0,
506 $self->{internalformat},
507 $tw, $th,
508 0,
509 $self->{format},
510 $self->{type},
511 $data;
512 CFClient::gl_check "uploading texture %dx%d if=%x f=%x t=%x",
513 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
514 } else {
515 glCopyTexImage2D GL_TEXTURE_2D, 0,
516 $self->{internalformat},
517 0, 0,
518 $tw, $th,
519 0;
520 CFClient::gl_check "copying to texture %dx%d if=%x",
521 $tw, $th, $self->{internalformat};
522 }
523
524 glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side
525}
526
527sub DESTROY {
528 my ($self) = @_;
529
530 delete $TEXTURES{$self+0};
531
532 glDeleteTexture delete $self->{name}
533 if $self->{name};
534}
535
536sub restore_state {
537 $_->upload
538 for values %TEXTURES;
539}
540
5411; 3221;
542 323
543=back 324=back
544 325
545=head1 AUTHOR 326=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines