use strict;
use warnings;
+use Carp;
use Data::Dumper;
use List::Util qw(sum);
-our $VERSION = '0.10';
+our $VERSION = '0.11';
sub open {
my ($class, $filename) = @_;
For example, using the Jungle tileset:
- $t->col(719)
+ my @rgb = $t->col(719)
is a water minitile, giving a blue color C<[38, 38, 57]>.
my $self = shift;
my ($id) = @_;
- my $minitiles = $self->{tileref}->[$id];
+ my $minitiles = $self->{tileref}->[$id]
+ or croak "tile ref $id does not exist";
my @pix;
for my $tiley (0 .. 3) {
for my $y (0 .. 7) {
=cut
+use Inline with => 'Imager';
+use Inline C => <<'EOS';
+Imager tileimg(SV* self, AV* tiledata) {
+ Imager img = i_img_8_new(4, 4, 3);
+
+ int bit;
+ for (bit = 0; bit < 4 * 4 * 3; ++bit) {
+ SV **bitval = av_fetch(tiledata, bit, 0);
+ if (bitval && SvOK(*bitval))
+ img->idata[bit] = (int)SvIV(*bitval);
+ }
+
+ return img;
+}
+
+SV* colavg(SV* self, Imager img) {
+ int ch;
+ int pixel;
+ int rgb[3];
+
+ for (pixel = 0; pixel < 4 * 4; ++pixel) {
+ for (ch = 0; ch < 3; ++ch) {
+ rgb[ch] += img->idata[pixel*3 + ch];
+ }
+ }
+
+ AV* perlrgb = newAV();
+ for (ch = 0; ch < 3; ++ch) {
+ av_push(perlrgb, newSViv(rgb[ch] >> 4));
+ }
+ return newRV_noinc(perlrgb);
+}
+
+EOS
+
sub tile {
my $self = shift;
my ($id) = @_;
return {
id => $tile,
group => $self->{group}->[$id >> 4],
-# subtype => [ map { $self->{minitype}->[$_] } @$minitiles ],
subtype => [ map { $self->{minitype}->[$_] } $tile*16 .. $tile*16+15 ],
- subcol => [ map { $self->col($_ >> 1) } @$minitiles ],
-# sprite => $self->sprite($tile),
+ sprite => $self->tileimg( [ map { @{ $self->col($_ >> 1) } } @$minitiles ] ),
};
}
my $info = $tile->{group};
$info->{walk} = sum(@{ $tile->{subtype} }) >> 4;
- my @rgb;
- for my $subcol (@{ $tile->{subcol} }) {
- $rgb[$_] += $subcol->[$_] for 0 .. 2;
- }
- $info->{col} = [ map {$_ >> 4} @rgb ];
+ $info->{col} = $self->colavg($tile->{sprite});
return $info;
}
build => 128,
},
subtype => [ 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1 ],
- subcol => [
- [39,40,59], [38,39,57], [40,41,60], [36,37,55],
- [37,38,56], [39,40,60], [38,39,57], [40,41,61],
- [37,38,56], [41,41,61], [40,40,60], [36,36,54],
- [36,36,54], [37,37,55], [35,35,53], [38,38,57]
- ],
+ sprite => Imager->new(4, 4, 3),
=cut