Noah Petherbridge > Image-Tileset > Image::Tileset

Download:
Image-Tileset-0.01.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.01   Source  

NAME ^

Image::Tileset - A tileset loader.

SYNOPSIS ^

  use Image::Tileset;

  my $ts = new Image::Tileset (
    image => "my-tileset.png",
    xml   => "my-tileset.xml",
  );

  open (OUT, ">grass.png");
  binmode OUT;
  print OUT $ts->tile("grass");
  close (OUT);

DESCRIPTION ^

Image::Tileset is a simple tileset image loader. The preferred usage is to have an XML description file alongside the tileset image that describes how the tiles are to be sliced up.

The module supports "simple" tilesets (where all tiles have a uniform width and height, though they don't need to begin at the top left corner of the image) as well as "fixed" tilesets (where you need to specify the exact pixel coords of every tile).

It also supports the management of animations for your tiles (but not the means to display them; this is left up to your front-end code. There is a demo that uses Perl/Tk to give you an idea how to do this).

SPECIFICATION FILE ^

Tileset images are paired with a "specification file," which describes how the image is to be sliced up into the different tiles.

The spec file is usually an XML document, and it's read with XML::Simple. If you wish, you can also send the spec data as a Perl data structure, skipping the XML part.

An example XML file is as follows, and shows all the capabilities of the spec file markup:

  <?xml version="1.0" encoding="utf-8"?>
  <tileset>
    <!--
      The simplest form: the uniform tile set. In this case, all the tiles are
      32x32 pixels large and the first tile is in the top left corner of the
      image, at pixel coordinate 0,0
    -->
    <layout type="tiles" size="32x32" x="0" y="0">
      <!--
        Within a "tiles" layout, X and Y refer to the "tile coordinate", not
        the "pixel coordinate". So, the top left tile is 0,0 and the one to
        the right of it is 1,0 (even though its pixel coordinate would be 32,0).
        The module takes care of this all for you!)

        Each tile needs a unique ID, called the "tile id".
      -->
      <tile x="0" y="0" id="grass" />
      <tile x="1" y="0" id="sand"  />
      <tile x="2" y="0" id="dirt"  />

      <!--
        We have three "water" tiles that we intend to animate later, but
        each one still needs its own unique ID!
      -->
      <tile x="0" y="1" id="water-1" />
      <tile x="1" y="1" id="water-2" />
      <tile x="2" y="1" id="water-3" />
    </layout>

    <!--
      In addition to simple grid-based tiles, you can also specify pixel
      coordinates directly. Use the "fixed" layout for this.
    -->
    <layout type="fixed">
      <!--
        In fixed layout, you need to specify 4 pixel coordinates for where
        the tile appears in the image: x1,y1,x2,y2.
      -->
      <tile x1="96" y1="0" x2="128" y2="96" id="avatar" />
    </layout>

    <!--
      For animations, you need to give the animation a unique ID and then
      tell it which tiles (by their IDs) go into the animation. The "speed"
      attribute controls how fast the animation plays by setting the delay
      (in milliseconds) until the next tile should be shown.
    -->
    <layout type="animation" id="water" speed="200">
      <tile>water-1</tile>
      <tile>water-2</tile>
      <tile>water-3</tile>
      <tile>water-2</tile>
    </layout>
  </tileset>

Your application can also provide spec data as a Perl structure instead of as XML. Here is an example of the above XML as a Perl structure:

  $ts->spec( [
    {
      type => 'tiles',
      size => '32x32',
      x    => 0,
      y    => 0,
      tile => [
        { x => 0, y => 0, id => 'grass'   },
        { x => 1, y => 0, id => 'sand'    },
        { x => 2, y => 0, id => 'dirt'    },
        { x => 0, y => 1, id => 'water-1' },
        { x => 1, y => 1, id => 'water-2' },
        { x => 2, y => 1, id => 'water-3' },
      },
    },
    {
      type => 'fixed',
      tile => [
        { x1 => 96, y1 => 0, x2 => 128, y2 => 96, id => 'avatar' },
      ],
    },
    {
      type  => 'animation',
      id    => 'water',
      speed => 200,
      tile  => [ 'water-1', 'water-2', 'water-3', 'water-2' ],
    },
  ]);

See the examples in the demo/ folder for more information.

METHODS ^

new (hash options)

Create a new Image::Tileset object. Options include:

  bool   debug:  Debug mode (prints stuff to the terminal on STDERR)
  string xml:    Path to an XML spec file that describes the image.
  hash   spec:   Spec data in Perl data structure form (skip XML file).
  string image:  Path to the image file.

If you provide xml, the XML will be parsed and refined immediately. If you provide spec, it will be refined immediately. If you provide image, the image will be loaded immediately.

void error ()

Print the last error message given. Example:

  $tileset->image("myfile.png") or die $tileset->error;

bool image (string filepath)

Load an image file with Image::Magick. The object is just kept in memory for when you actually want to get a tile from it.

Returns 1 on success, undef on error.

bool data (bin data)

If your program already has the image's binary data in memory, it can send it directly to this function. It will create an Image::Magick object based off the binary data directly, instead of needing to read a file from disk.

Returns 1 on success, undef on error.

void clear ()

Clear the internal Image::Magick object, unloading the tileset.

bool xml (string xmldata | string specfile)

Load a specification file from XML. Pass either XML data or the path to a file name.

If the data sent to this command begins with a left chevron, <, or contains newlines, it is assumed to be XML data; otherwise the filesystem is queried.

Returns 1 on success, undef on error.

bool refine (array spec)

Refines the specification data. The spec describes how the image is cut up; refine() goes through that and stores the exact pixel coordinates of every tile named in the spec, for quick extraction when the tile is wanted.

This method is called automatically when an XML spec file is parsed. If you pass in a spec during the call to new(), this method will be called automatically for your spec. If you want to load a spec directly after you've created the object, you can call refine() directly with your new spec.

data tiles ()

Return the tile coordinate information. In array context, returns a list of the tile ID's. In scalar context, returns a hash reference in the following format:

  {
    'tile-id' => [
      x1, y1,
      x2, y2
    ],
    ...
  };

data animations ()

Return the animation information. In array context, returns a list of the animation ID's. In scalar context, returns a hash reference in the following format:

  {
    'animation-id' => {
      speed => '...',
      tiles => [
        'tile-id',
        ...
      ],
    },
  };

bin tile (string id[, hash options])

Get the binary data of one of the tiles, named id, from the original tileset.

You can optionally pass in a hash of named options. The following options are supported:

  int scale:   Scale the tile before returning its data. This is a number to
               scale it by, for example '2' returns it at 200% its original size,
               while '0.5' returns it at 50% its original size.
  str size:    Resize the tile to this exact size before returning it, for
               example '64x64'.
  bool magick: If true, returns the Image::Magick object instead of the binary
               data. If you want to make additional modifications to the image
               (i.e. edit its colors, apply special effects), use the 'magick'
               option and then apply the effects yourself.

The options scale and size are mutually exclusive.

Examples:

  # The tiles are 32x32, but lets scale it 2X so we get back a 64x64 tile
  my $tile = $ts->tile("grass", scale => 2);

  # Get it at 1/2 its original size, or 16x16
  my $tile = $ts->tile("grass", scale => 0.5);

  # Get it at 24x24 pixels
  my $tile = $ts->tile("grass", size => "24x24");

Returns undef on error.

data animation (string id)

Get the animation information about a specific animation ID.

Returns data in the format:

  {
    speed => '...',
    tiles => [ ... ],
  };

Returns undef on error.

ImageMagick slice (string id)

Returns an Image::Magick object that contains the sliced tile from the original tileset. This is mostly for internal use only.

SEE ALSO ^

Image::Magick, which powers this module's graphics handling.

XML::Simple, which powers this module's XML parsing.

CHANGES ^

  0.01  Fri Jan 15 2010
  - Initial release.

COPYRIGHT ^

The tileset graphics included for demonstration purposes are from RPG Maker 2003 and are copyright (C) Enterbrain.

Code written by Noah Petherbridge, http://www.kirsle.net/

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: