1 |
=head1 NAME |
2 |
|
3 |
Crossfire::Tilecache - Tile Cache using Gtk2::Gdk::Pixbuf |
4 |
|
5 |
=cut |
6 |
|
7 |
package Crossfire::Tilecache; |
8 |
|
9 |
our $VERSION = '0.1'; |
10 |
|
11 |
use strict; |
12 |
|
13 |
use Gtk2; |
14 |
use File::Temp; |
15 |
|
16 |
use Crossfire; |
17 |
|
18 |
sub init_tilecache($) { |
19 |
my ($cacheprefix) = @_; |
20 |
|
21 |
eval { |
22 |
-M "$cacheprefix.pst" < -M "$Crossfire::LIB/crossfire.0" |
23 |
&& ($Crossfire::TILE = new_from_file Gtk2::Gdk::Pixbuf "$cacheprefix.png") |
24 |
&& (*Crossfire::FACE = Crossfire::load_ref "$cacheprefix.pst") |
25 |
} 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 |
my ($fh, $filename) = File::Temp::tempfile; |
34 |
|
35 |
print $fh $tile->{$name}; |
36 |
close $fh; |
37 |
|
38 |
my $tile = $cache{$name} = {}; |
39 |
|
40 |
my $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $filename; |
41 |
|
42 |
unlink $filename; |
43 |
|
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 |
Crossfire::save_ref \%cache, "$cacheprefix.pst"; |
69 |
|
70 |
$Crossfire::TILE = $pb; |
71 |
*Crossfire::FACE = \%cache; |
72 |
} |
73 |
} |
74 |
|
75 |
init_tilecache "$Crossfire::VARDIR/tilecache"; |
76 |
|
77 |
=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 |