ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra/Tilecache.pm
Revision: 1.8
Committed: Thu Feb 23 13:23:45 2006 UTC (18 years, 3 months ago) by root
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.2 Crossfire::Tilecache - Tile Cache using Gtk2::Gdk::Pixbuf
4 root 1.1
5     =cut
6    
7 root 1.2 package Crossfire::Tilecache;
8 root 1.1
9     our $VERSION = '0.1';
10    
11     use strict;
12    
13     use Gtk2;
14 root 1.4 use File::Temp;
15 root 1.1
16     use Crossfire;
17    
18     sub init_tilecache($) {
19     my ($cacheprefix) = @_;
20    
21     eval {
22     -M "$cacheprefix.pst" < -M "$Crossfire::LIB/crossfire.0"
23 root 1.3 && ($Crossfire::TILE = new_from_file Gtk2::Gdk::Pixbuf "$cacheprefix.png")
24 root 1.7 && (*Crossfire::FACE = Crossfire::load_ref "$cacheprefix.pst")
25 root 1.1 } or do {
26     my $tile = read_pak "$Crossfire::LIB/crossfire.0";
27    
28     my %cache;
29    
30     my $idx = 0;
31    
32     for my $name (sort keys %$tile) {
33 root 1.6 my ($fh, $filename) = File::Temp::tempfile;
34 root 1.1
35     print $fh $tile->{$name};
36     close $fh;
37    
38     my $tile = $cache{$name} = {};
39    
40 root 1.4 my $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $filename;
41 root 1.1
42 root 1.5 unlink $filename;
43 root 1.1
44     $tile->{pb} = $pb;
45     $tile->{idx} = $idx;
46     $tile->{w} = int $pb->get_width / TILESIZE;
47     $tile->{h} = int $pb->get_height / TILESIZE;
48    
49     $idx += $tile->{w} * $tile->{h};
50     }
51    
52     my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64;
53    
54     while (my ($name, $tile) = each %cache) {
55     my $tpb = delete $tile->{pb};
56     my $ofs = $tile->{idx};
57    
58     for my $x (0 .. $tile->{w} - 1) {
59     for my $y (0 .. $tile->{h} - 1) {
60     my $idx = $ofs + $x + $y * $tile->{w};
61     $tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE,
62     $pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64);
63     }
64     }
65     }
66    
67     $pb->save ("$cacheprefix.png", "png");
68 root 1.7 Crossfire::save_ref \%cache, "$cacheprefix.pst";
69 root 1.1
70 root 1.3 $Crossfire::TILE = $pb;
71     *Crossfire::FACE = \%cache;
72 root 1.1 }
73     }
74    
75 root 1.8 init_tilecache "$Crossfire::VARDIR/tilecache";
76 root 1.3
77 root 1.1 =head1 AUTHOR
78    
79     Marc Lehmann <schmorp@schmorp.de>
80     http://home.schmorp.de/
81    
82     Robin Redeker <elmex@ta-sa.org>
83     http://www.ta-sa.org/
84    
85     =cut
86    
87     1