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 |
|
15 |
use Crossfire; |
16 |
|
17 |
sub init_tilecache($) { |
18 |
my ($cacheprefix) = @_; |
19 |
|
20 |
eval { |
21 |
-M "$cacheprefix.pst" < -M "$Crossfire::LIB/crossfire.0" |
22 |
&& ($Crossfire::TILE = new_from_file Gtk2::Gdk::Pixbuf "$cacheprefix.png") |
23 |
&& (*Crossfire::FACE = Storable::retrieve "$cacheprefix.pst") |
24 |
} or do { |
25 |
my $tile = read_pak "$Crossfire::LIB/crossfire.0"; |
26 |
|
27 |
my %cache; |
28 |
|
29 |
my $idx = 0; |
30 |
|
31 |
for my $name (sort keys %$tile) { |
32 |
my $file = "/tmp/map2png.$$~"; |
33 |
|
34 |
open my $fh, ">:raw", $file |
35 |
or die "$file: $!"; |
36 |
print $fh $tile->{$name}; |
37 |
close $fh; |
38 |
|
39 |
my $tile = $cache{$name} = {}; |
40 |
|
41 |
my $pb = $tile->{$name} = new_from_file Gtk2::Gdk::Pixbuf $file; |
42 |
|
43 |
unlink $file; |
44 |
|
45 |
$tile->{pb} = $pb; |
46 |
$tile->{idx} = $idx; |
47 |
$tile->{w} = int $pb->get_width / TILESIZE; |
48 |
$tile->{h} = int $pb->get_height / TILESIZE; |
49 |
|
50 |
$idx += $tile->{w} * $tile->{h}; |
51 |
} |
52 |
|
53 |
my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64; |
54 |
|
55 |
while (my ($name, $tile) = each %cache) { |
56 |
my $tpb = delete $tile->{pb}; |
57 |
my $ofs = $tile->{idx}; |
58 |
|
59 |
for my $x (0 .. $tile->{w} - 1) { |
60 |
for my $y (0 .. $tile->{h} - 1) { |
61 |
my $idx = $ofs + $x + $y * $tile->{w}; |
62 |
$tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE, |
63 |
$pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64); |
64 |
} |
65 |
} |
66 |
} |
67 |
|
68 |
$pb->save ("$cacheprefix.png", "png"); |
69 |
Storable::nstore \%cache, "$cacheprefix.pst"; |
70 |
|
71 |
$Crossfire::TILE = $pb; |
72 |
*Crossfire::FACE = \%cache; |
73 |
} |
74 |
} |
75 |
|
76 |
init_tilecache "$Crossfire::CACHEDIR/tilecache"; |
77 |
|
78 |
=head1 AUTHOR |
79 |
|
80 |
Marc Lehmann <schmorp@schmorp.de> |
81 |
http://home.schmorp.de/ |
82 |
|
83 |
Robin Redeker <elmex@ta-sa.org> |
84 |
http://www.ta-sa.org/ |
85 |
|
86 |
=cut |
87 |
|
88 |
1 |