ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/bin/kgsueme
(Generate patch)

Comparing kgsueme/bin/kgsueme (file contents):
Revision 1.16 by pcg, Fri May 30 07:19:16 2003 UTC vs.
Revision 1.17 by pcg, Fri May 30 09:15:37 2003 UTC

27sub load_img { 27sub load_img {
28 new_from_file Gtk::Gdk::Pixbuf "$IMGDIR/$_[0]" 28 new_from_file Gtk::Gdk::Pixbuf "$IMGDIR/$_[0]"
29# load_image Gtk::Gdk::ImlibImage "$IMGDIR/$_[0]" 29# load_image Gtk::Gdk::ImlibImage "$IMGDIR/$_[0]"
30 or die "$IMGDIR/$_[0]: $!"; 30 or die "$IMGDIR/$_[0]: $!";
31} 31}
32
33my @fontchars = ('A' .. 'Z', 0 .. 9);
32 34
33our @black_img = load_img "b-01.png"; 35our @black_img = load_img "b-01.png";
34our @white_img = map +(load_img "w-0$_.png"), 1,2,3,4,5; 36our @white_img = map +(load_img "w-0$_.png"), 1,2,3,4,5;
35our @triangle_img = map +(load_img "triangle-$_.png"), qw(b w); 37our @triangle_img = map +(load_img "triangle-$_.png"), qw(b w);
36our @square_img = map +(load_img "square-$_.png"), qw(b w); 38our @square_img = map +(load_img "square-$_.png"), qw(b w);
37our @circle_img = map +(load_img "circle-$_.png"), qw(b w); 39our @circle_img = map +(load_img "circle-$_.png"), qw(b w);
38our $board_img = load_img "woodgrain-01.jpg"; 40our $board_img = load_img "woodgrain-01.jpg";
41
42our @font = (
43 [map +(load_img "font/$_-black.png"), @fontchars],
44 [map +(load_img "font/$_-white.png"), @fontchars],
45 );
46our %fontmap;
47@fontmap{@fontchars} = (0..25 + 10);
48@fontmap{'a' .. 'z'} = (0..25);
39 49
40{ 50{
41 #my $audioserver = new Audio::Play(0); 51 #my $audioserver = new Audio::Play(0);
42 my %sound; 52 my %sound;
43 $SIG{CHLD} = 'IGNORE'; 53 $SIG{CHLD} = 'IGNORE';
166 $main->{status}->push($context_id{$type} ||= $main->{status}->get_context_id($type), $text) if $text; 176 $main->{status}->push($context_id{$type} ||= $main->{status}->get_context_id($type), $text) if $text;
167 } 177 }
168} 178}
169 179
170if (0) { 180if (0) {
181 use KGS::Constants;
182
171 my $board = new game size => 5; 183 my $board = new game size => 5;
184 $board->{board} = new KGS::Game::Board;
185 $board->{board}{board}[0][0] = MARK_B;
186 $board->{board}{board}[1][1] = MARK_GRAY_B | MARK_SMALL_W;
187 $board->{board}{board}[2][2] = MARK_W | MARK_TRIANGLE;
172 $board->{window}->show_all; 188 $board->{window}->show_all;
173} 189}
174 190
175main Gtk; 191main Gtk;
176 192
589 delete $self->{stack}; 605 delete $self->{stack};
590 $self->repaint_board; 606 $self->repaint_board;
591 1; 607 1;
592} 608}
593 609
594sub expose_event { 610sub INTERP_NEAREST (){ 1 }
595 my ($widget, $self, $event) = @_;
596
597 $self->{pixmap} or return;
598
599 my ($ox, $oy, $s) = @{$self->{offsets}};
600
601 my ($x, $y, $w, $h) =
602 @{Gtk::Gdk::Rectangle->intersect(
603 $event->{area},
604 [$ox, $oy, $s, $s]
605 )};
606
607 $self->{canvas}->window->draw_pixmap (
608 $self->{canvas}->style->white_gc,
609 $self->{pixmap},
610 $x - $ox, $y - $oy, $x, $y, $w, $h,
611 );
612 1;
613}
614
615# create new, _transparent_ pixbuf
616sub new_pixbuf {
617 my ($w, $h) = @_;
618
619 $pixbuf;
620}
621
622sub INTERP_TILES (){ 1 } 611sub INTERP_TILES (){ 1 }
623sub INTERP_BILINEAR (){ 2 } 612sub INTERP_BILINEAR (){ 2 }
624sub INTERP_HYPER (){ 3 } 613sub INTERP_HYPER (){ 3 }
625 614
626sub new_pixbuf { 615sub new_pixbuf {
627 my ($w, $h, $clear) = @_; 616 my ($w, $h, $alpha, $clear) = @_;
628 617
629 my $pixbuf = new Gtk::Gdk::Pixbuf 'rgb', 1, 8, $w, $h; 618 my $pixbuf = new Gtk::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
630 619
631 if ($clear) { # damn, need to clear it ourselves 620 if ($clear) { # damn, need to clear it ourselves
632 my $row = "\x00\x00\x00\x00" x $w; 621 my $row = "\x00\x00\x00\x00" x $w;
633 $pixbuf->put_pixels ($row, $_, 0) for 0 .. $h - 1; 622 $pixbuf->put_pixels ($row, $_, 0) for 0 .. $h - 1;
634 } 623 }
637} 626}
638 627
639sub scale_pixbuf { 628sub scale_pixbuf {
640 my ($src, $w, $h, $mode) = @_; 629 my ($src, $w, $h, $mode) = @_;
641 630
642 my $dst = new_pixbuf $w, $h; 631 my $dst = new_pixbuf $w, $h, 1;
643 632
644 $src->scale( 633 $src->scale(
645 $dst, 0, 0, $w, $h, 0, 0, 634 $dst, 0, 0, $w, $h, 0, 0,
646 $w / $src->get_width, $h / $src->get_height, 635 $w / $src->get_width, $h / $src->get_height,
647 $mode, 636 $mode,
648 ); 637 );
649 638
650 $dst; 639 $dst;
651} 640}
652 641
653sub label_font {
654 my ($size) = @_;
655
656 $size = int $size;
657 $size = 34 if $size > 34;
658
659 # I am soo incapable
660 for (8, 10, 11, 12, 14, 17, 18, 20, 24, 25, 34) {
661 next unless $size <= $_;
662 my $font = Gtk::Gdk::Font->fontset_load ("-*-helvetica-bold-r-*--$_-*");
663 return $font if $font;
664 }
665
666 return Gtk::Gdk::Font->fontset_load ("-*-helvetica-bold-r-*--8-*");
667}
668
669sub center_text {
670 my ($drawable, $font, $gc, $x, $y, $t) = @_;
671 my $w = $font->string_width ($t);
672 my $h = $font->string_height($t) - $font->descent;
673 $drawable->draw_text ($font, $gc, $x - $w*0.5, $y + $h * 0.5, $t, length $t);
674}
675
676# create a stack of stones 642# create a stack of stones
677sub create_stack { 643sub create_stack {
678 my ($self, $gc, $mark, $size, $rand) = @_; 644 my ($self, $mark, $size, $rand) = @_;
679 645
680 my $c = \$self->{stack}{$mark}; 646 my $c = \$self->{stack}{$mark};
681 unless ($$c) { 647 unless ($$c) {
682 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) { 648 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
683 my $base = 649 my $base =
684 $mark & (MARK_B | MARK_GRAY_B | MARK_W | MARK_GRAY_W) 650 $mark & (MARK_B | MARK_GRAY_B | MARK_W | MARK_GRAY_W)
685 ? scale_pixbuf $stone, $size, $size, INTERP_HYPER 651 ? scale_pixbuf $stone, $size, $size, INTERP_HYPER
686 : new_pixbuf $size, $size, 1; 652 : new_pixbuf $size, $size, 1, 1;
687 653
688 if ($mark & (MARK_GRAY_B | MARK_GRAY_W)) { 654 if ($mark & (MARK_GRAY_B | MARK_GRAY_W)) {
689 # make transparent by stippling :( 655 # make transparent by stippling :(
690 # fix this to use compositing if/when we have full compositing support 656 # fix this to use compositing if/when we have full compositing support
691 # in kgsueme 657 # in kgsueme
722 INTERP_HYPER, 255 688 INTERP_HYPER, 255
723 ); 689 );
724 } 690 }
725 } 691 }
726 692
727 push @$$c, [$base->render_pixmap_and_mask (128)]; 693 #push @$$c, [$base->render_pixmap_and_mask (128)];
694 push @$$c, $base;
728 } 695 }
729 } 696 }
730 697
731 @{$$c->[$rand % @$$c]}; 698 $$c->[$rand % @$$c];
699}
700
701sub pixbuf_text {
702 my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
703
704 my $c = $::font[$colour][$::fontmap{substr $text, 0, 1}];
705
706 if ($c) {
707 my ($w, $h) = ($c->get_width, $c->get_height);
708 my $s = $height / $h;
709
710 $x -= $w * $s *0.5;
711 $y -= $height * 0.5;
712
713 $c->composite ($pixbuf,
714 $x, $y, $w*$s+1, $height+1, $x, $y, $s, $s,
715 INTERP_HYPER, 128);
716
717 } else {
718 warn "unable to render character '$text'";
719 }
720}
721
722my $black_pb;
723
724sub pixbuf_rect {
725 my ($pb, $x1, $y1, $x2, $y2) = @_;
726 # we fake lines by... an unspeakable method :/
727 unless ($black_pb) {
728 $black_pb = new_pixbuf 1, 1, 0, 0;
729 $black_pb->put_pixels ("\x44\x11\x11", 0, 0);
730 }
731
732 $black_pb->scale ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, 10000, 10000, INTERP_NEAREST);
732} 733}
733 734
734sub repaint_board { 735sub repaint_board {
735 my ($self) = @_; 736 my ($self) = @_;
736 my $canvas = $self->{canvas}; 737 my $canvas = $self->{canvas};
743 744
744 my $s = $w > $h ? $h : $w; 745 my $s = $w > $h ? $h : $w;
745 746
746 $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s]; 747 $self->{offsets} = [int (($w - $s) / 2), int (($h - $s) / 2), $s];
747 748
748 my $pixmap = $self->{pixmap} = new Gtk::Gdk::Pixmap $self->{canvas}->window, $s, $s; 749 my $pixbuf;
749 750
750 { 751 {
751 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height); 752 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
752 753
753 my $bg = $s < $bw && $s < $bh ? $::board_img : scale_pixbuf $::board_img, $s, $s, INTERP_TILES; 754 if ($s < $bw && $s < $bh) {
754 $bg->render_to_drawable( 755 $pixbuf = new_pixbuf $s, $s, 0, 0;
755 $pixmap, $self->{canvas}->style->white_gc, 756 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
756 0, 0, 0, 0, $s, $s, 757 } else {
757 0, 0, 0 758 $pixbuf = scale_pixbuf $::board_img, $s, $s, INTERP_TILES;
758 ); 759 }
759 } 760 }
760 761
761 my $gc = Gtk::Gdk::GC->new ($pixmap); 762 $self->{pixbuf} = $pixbuf;
762
763 $gc->rgb_gc_set_foreground($line_colour);
764 $gc->set_line_attributes (int ($s / 300) + 1, 'solid', 'projecting', 'miter');
765 763
766 my $size = $self->{size}; 764 my $size = $self->{size};
767 my $border = int ($s / $size); 765 my $border = int ($s / $size);
768 my $s2 = $s - $border * 2; 766 my $s2 = $s - $border * 2;
769 my $edge = int ($s2 / $size) | 1; 767 my $edge = int ($s2 / $size) | 1;
770 my $ofs = int ($edge / 2); 768 my $ofs = int ($edge / 2);
771 769
772 my $font = label_font $ofs;
773
774 my @k = map int ($s2 * $_ / $size - $ofs + $border + 0.5), 0 .. $size; 770 my @k = map int ($s2 * $_ / $size - $ofs + $border + 0.5), 0 .. $size;
771
772 my $linew = int ($s / 300);
775 773
776 my $a = "A"; 774 my $a = "A";
777 for my $i (1 .. $size) { 775 for my $i (1 .. $size) {
778 $pixmap->draw_line ($gc, $k[$i], $k[1], $k[$i], $k[$size]); 776 pixbuf_rect $pixbuf, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew;
779 $pixmap->draw_line ($gc, $k[1], $k[$i], $k[$size], $k[$i]); 777 pixbuf_rect $pixbuf, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew;
780 778
781 center_text $pixmap, $font, $gc, $k[$i], ($ofs +$border) / 2, $a; 779 pixbuf_text $pixbuf, 0, $k[$i], ($ofs +$border) / 2, $ofs, $a;
782 center_text $pixmap, $font, $gc, $k[$i], $s2 + $border + $ofs / 2, $a; 780 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border + $ofs / 2, $ofs, $a;
783 center_text $pixmap, $font, $gc, ($ofs + $border) / 2, $k[$i], $size - $i + 1; 781 pixbuf_text $pixbuf, 0, ($ofs + $border) / 2, $k[$i], $ofs, $size - $i + 1;
784 center_text $pixmap, $font, $gc, $s2 + $border + $ofs / 2, $k[$i], $size - $i + 1; 782 pixbuf_text $pixbuf, 0, $s2 + $border + $ofs / 2, $k[$i], $ofs, $size - $i + 1;
785 783
786 $a++; 784 $a++;
787 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK... 785 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
788 } 786 }
789 787
790 # hoshi-points(!)#d# 788 # hoshi-points(!)#d#
791 # caching of empty board gfx(!)#d# 789 # caching of empty board gfx(!)#d#
792 790
793 for my $x (1 .. $size) { 791 for my $x (1 .. $size) {
794 for my $y (1 .. $size) { 792 for my $y (1 .. $size) {
795 my $yk = $s2 * $x / $size - $ofs + $border;
796 my $mark = $self->{board}{board}[$x-1][$y-1]; 793 my $mark = $self->{board}{board}[$x-1][$y-1];
797 794
798 if ($mark) { 795 if ($mark) {
799 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs); 796 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
797 my $pb = $self->create_stack($mark, $edge, $x * 17 + $y * 11 );
798
799 $pb->composite ($pixbuf, $dx, $dy, $edge, $edge, $dx, $dy, 1, 1, INTERP_TILES, 192);
800
800 my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 ); 801 #my ($pm, $bm) = $self->create_stack($gc, $mark, $edge, $x * 17 + $y * 11 );
801 802
802 $gc->set_clip_mask ($bm); 803 #$gc->set_clip_mask ($bm);
803 $gc->set_clip_origin ($dx, $dy); 804 #$gc->set_clip_origin ($dx, $dy);
804 $pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge); 805 #$pixmap->draw_pixmap ($gc, $pm, 0, 0, $dx, $dy, $edge, $edge);
805 } 806 }
806 } 807 }
807 } 808 }
809}
810
811sub expose_event {
812 my ($widget, $self, $event) = @_;
813
814 $self->{pixbuf} or return;
815
816 my ($ox, $oy, $s) = @{$self->{offsets}};
817
818 my ($x, $y, $w, $h) =
819 @{Gtk::Gdk::Rectangle->intersect(
820 $event->{area},
821 [$ox, $oy, $s, $s]
822 )};
823
824 if (defined $x) {
825 $self->{pixbuf}->render_to_drawable ($self->{canvas}->window, $self->{canvas}->style->white_gc,
826 $x - $ox, $y - $oy, $x, $y, $w, $h);
827 #$self->{canvas}->window->draw_pixmap (
828 # $self->{canvas}->style->white_gc,
829 # $self->{pixmap},
830 # $x - $ox, $y - $oy, $x, $y, $w, $h,
831 #);
832 }
833 1;
808} 834}
809 835
810sub event_update_tree { 836sub event_update_tree {
811 my ($self) = @_; 837 my ($self) = @_;
812 838

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines