Image-Tileset

 view release on metacpan or  search on metacpan

Tileset.html  view on Meta::CPAN

      <tile>water-2</tile>
    </layout>
  &lt;/tileset&gt;</pre>
<p>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:</p>
<pre>
  $ts-&gt;spec( [
    {
      type =&gt; 'tiles',
      size =&gt; '32x32',
      x    =&gt; 0,
      y    =&gt; 0,
      tile =&gt; [
        { x =&gt; 0, y =&gt; 0, id =&gt; 'grass'   },
        { x =&gt; 1, y =&gt; 0, id =&gt; 'sand'    },
        { x =&gt; 2, y =&gt; 0, id =&gt; 'dirt'    },
        { x =&gt; 0, y =&gt; 1, id =&gt; 'water-1' },
        { x =&gt; 1, y =&gt; 1, id =&gt; 'water-2' },
        { x =&gt; 2, y =&gt; 1, id =&gt; 'water-3' },
      },
    },
    {
      type =&gt; 'fixed',
      tile =&gt; [
        { x1 =&gt; 96, y1 =&gt; 0, x2 =&gt; 128, y2 =&gt; 96, id =&gt; 'avatar' },
      ],
    },
    {
      type  =&gt; 'animation',
      id    =&gt; 'water',
      speed =&gt; 200,
      tile  =&gt; [ 'water-1', 'water-2', 'water-3', 'water-2' ],
    },
  ]);</pre>
<p>See the examples in the <code>demo/</code> folder for more information.</p>
<p>
</p>
<hr />
<h1><a name="methods">METHODS</a></h1>
<p>
</p>
<h2><a name="new__hash_options_">new (hash options)</a></h2>
<p>Create a new <code>Image::Tileset</code> object. Options include:</p>
<pre>
  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.</pre>
<p>If you provide <code>xml</code>, the XML will be parsed and refined immediately. If you
provide <code>spec</code>, it will be refined immediately. If you provide <code>image</code>, the
image will be loaded immediately.</p>
<p>
</p>
<h2><a name="void_error___">void error ()</a></h2>
<p>Print the last error message given. Example:</p>
<pre>
  $tileset-&gt;image(&quot;myfile.png&quot;) or die $tileset-&gt;error;</pre>
<p>
</p>
<h2><a name="bool_image__string_filepath_">bool image (string filepath)</a></h2>
<p>Load an image file with <code>Image::Magick</code>. The object is just kept in memory for
when you actually want to get a tile from it.</p>
<p>Returns 1 on success, undef on error.</p>
<p>
</p>
<h2><a name="bool_data__bin_data_">bool data (bin data)</a></h2>
<p>If your program already has the image's binary data in memory, it can send it
directly to this function. It will create an <code>Image::Magick</code> object based off
the binary data directly, instead of needing to read a file from disk.</p>
<p>Returns 1 on success, undef on error.</p>
<p>
</p>
<h2><a name="void_clear___">void clear ()</a></h2>
<p>Clear the internal <code>Image::Magick</code> object, unloading the tileset.</p>
<p>
</p>
<h2><a name="bool_xml__string_xmldata___string_specfile_">bool xml (string xmldata | string specfile)</a></h2>
<p>Load a specification file from XML. Pass either XML data or the path to a
file name.</p>
<p>If the data sent to this command begins with a left chevron, &lt;, or contains
newlines, it is assumed to be XML data; otherwise the filesystem is queried.</p>
<p>Returns 1 on success, undef on error.</p>
<p>
</p>
<h2><a name="bool_refine__array_spec_">bool refine (array spec)</a></h2>
<p>Refines the specification data. The spec describes how the image is cut up;
<code>refine()</code> goes through that and stores the exact pixel coordinates of every
tile named in the spec, for quick extraction when the tile is wanted.</p>
<p>This method is called automatically when an XML spec file is parsed. If you
pass in a <code>spec</code> during the call to <code>new()</code>, 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 <code>refine()</code> directly with your new spec.</p>
<p>
</p>
<h2><a name="data_tiles___">data tiles ()</a></h2>
<p>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:</p>
<pre>
  {
    'tile-id' =&gt; [
      x1, y1,
      x2, y2
    ],
    ...
  };</pre>
<p>
</p>
<h2><a name="data_animations___">data animations ()</a></h2>
<p>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:</p>
<pre>
  {
    'animation-id' =&gt; {
      speed =&gt; '...',
      tiles =&gt; [
        'tile-id',
        ...
      ],
    },
  };</pre>
<p>
</p>
<h2><a name="bin_tile__string_id___hash_options__">bin tile (string id[, hash options])</a></h2>
<p>Get the binary data of one of the tiles, named <code>id</code>, from the original
tileset.</p>
<p>You can optionally pass in a hash of named options. The following options are
supported:</p>
<pre>
  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.</pre>
<p>The options <code>scale</code> and <code>size</code> are mutually exclusive.</p>
<p>Examples:</p>
<pre>
  # The tiles are 32x32, but lets scale it 2X so we get back a 64x64 tile
  my $tile = $ts-&gt;tile(&quot;grass&quot;, scale =&gt; 2);</pre>
<pre>
  # Get it at 1/2 its original size, or 16x16
  my $tile = $ts-&gt;tile(&quot;grass&quot;, scale =&gt; 0.5);</pre>
<pre>
  # Get it at 24x24 pixels
  my $tile = $ts-&gt;tile(&quot;grass&quot;, size =&gt; &quot;24x24&quot;);</pre>
<p>Returns undef on error.</p>
<p>
</p>
<h2><a name="data_animation__string_id_">data animation (string id)</a></h2>
<p>Get the animation information about a specific animation ID.</p>
<p>Returns data in the format:</p>
<pre>
  {
    speed =&gt; '...',
    tiles =&gt; [ ... ],
  };</pre>
<p>Returns undef on error.</p>
<p>
</p>
<h2><a name="imagemagick_slice__string_id_">ImageMagick slice (string id)</a></h2>
<p>Returns an <code>Image::Magick</code> object that contains the sliced tile from the
original tileset. This is mostly for internal use only.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="/Image/Magick.html">Image::Magick</a>, which powers this module's graphics handling.</p>
<p><a href="/XML/Simple.html">XML::Simple</a>, which powers this module's XML parsing.</p>
<p>
</p>
<hr />
<h1><a name="changes">CHANGES</a></h1>
<pre>
  0.01  Fri Jan 15 2010
  - Initial release.</pre>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>The tileset graphics included for demonstration purposes are from RPG Maker
2003 and are copyright (C) Enterbrain.</p>
<p>Code written by Noah Petherbridge, <a href="http://www.kirsle.net/">http://www.kirsle.net/</a></p>
<p>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.</p>

</body>

</html>



( run in 0.523 second using v1.01-cache-2.11-cpan-39bf76dae61 )