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