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 |
print $fh $tile->{$name}; |
35 |
|
|
close $fh; |
36 |
root |
1.10 |
my $pb = new_from_file Gtk2::Gdk::Pixbuf $filename; |
37 |
root |
1.5 |
unlink $filename; |
38 |
root |
1.1 |
|
39 |
root |
1.11 |
my $tile = $cache{$name} = { |
40 |
|
|
pb => $pb, |
41 |
|
|
idx => $idx, |
42 |
|
|
w => int $pb->get_width / TILESIZE, |
43 |
|
|
h => int $pb->get_height / TILESIZE, |
44 |
|
|
}; |
45 |
|
|
|
46 |
root |
1.1 |
|
47 |
|
|
$idx += $tile->{w} * $tile->{h}; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
my $pb = new Gtk2::Gdk::Pixbuf "rgb", 1, 8, 64 * TILESIZE, TILESIZE * int +($idx + 63) / 64; |
51 |
|
|
|
52 |
|
|
while (my ($name, $tile) = each %cache) { |
53 |
|
|
my $tpb = delete $tile->{pb}; |
54 |
|
|
my $ofs = $tile->{idx}; |
55 |
|
|
|
56 |
|
|
for my $x (0 .. $tile->{w} - 1) { |
57 |
|
|
for my $y (0 .. $tile->{h} - 1) { |
58 |
|
|
my $idx = $ofs + $x + $y * $tile->{w}; |
59 |
|
|
$tpb->copy_area ($x * TILESIZE, $y * TILESIZE, TILESIZE, TILESIZE, |
60 |
|
|
$pb, ($idx % 64) * TILESIZE, TILESIZE * int $idx / 64); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
$pb->save ("$cacheprefix.png", "png"); |
66 |
root |
1.7 |
Crossfire::save_ref \%cache, "$cacheprefix.pst"; |
67 |
root |
1.1 |
|
68 |
root |
1.3 |
$Crossfire::TILE = $pb; |
69 |
|
|
*Crossfire::FACE = \%cache; |
70 |
root |
1.1 |
} |
71 |
|
|
} |
72 |
|
|
|
73 |
root |
1.8 |
init_tilecache "$Crossfire::VARDIR/tilecache"; |
74 |
root |
1.3 |
|
75 |
root |
1.1 |
=head1 AUTHOR |
76 |
|
|
|
77 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
78 |
|
|
http://home.schmorp.de/ |
79 |
|
|
|
80 |
|
|
Robin Redeker <elmex@ta-sa.org> |
81 |
|
|
http://www.ta-sa.org/ |
82 |
|
|
|
83 |
|
|
=cut |
84 |
|
|
|
85 |
|
|
1 |