#!/usr/bin/perl -w
use strict;

use SWF qw(:ALL);

SWF::setScale(1.0);

my $string = "ming!";

my $f = new SWF::Font("../common/test.fdb");

my $m = new SWF::Movie();
$m->setRate(24.0);
$m->setDimension(2400, 1600);
$m->setBackground(0xff, 0xff, 0xff);

# functions with huge numbers of arbitrary
# arguments are always a good idea!  Really!

sub text {
  my ($r, $g, $b, $a,  $rot, $x, $y, $scale,  $string) = @_;

  my $t = new SWF::Text();
  $t->setFont($f);
  $t->setColor($r, $g, $b, $a);
  $t->setHeight(960);
  $t->moveTo(-($t->getWidth($string))/2, 220); #$t->getAscent()/2);
  $t->addString($string);

  # we can add properties just like a normal php var,
  # as long as the names aren't already used.
  # e.g., we can't set $i->scale, because that's a function
  ## ah, but the Perl SWF objects aren't hashrefs, so we can't
  ## instead, we create our own hashref and stick the props in there

  my $i = $m->add($t);
  my $vals = {};
  $vals->{'x'} = $x;
  $vals->{'y'} = $y;
  $vals->{'rot'} = $rot;
  $vals->{'s'} = $scale;
  $i->rotateTo($rot);
  $i->scale($scale, $scale);

  # but the changes are local to the function, so we have to
  # return the changed object.  kinda weird..

  return ($i,$vals);
}

sub step {
  my ($i,$vals) = @_;

  my $oldrot = $vals->{'rot'};
  $vals->{'rot'} = 19*$vals->{'rot'}/20;
  $vals->{'x'} = (19*$vals->{'x'} + 1200)/20;
  $vals->{'y'} = (19*$vals->{'y'} + 800)/20;
  $vals->{'s'} = (19*$vals->{'s'} + 1.0)/20;
  
  $i->rotateTo($vals->{'rot'});
  $i->scaleTo($vals->{'s'}, $vals->{'s'});
  $i->moveTo($vals->{'x'}, $vals->{'y'});
  
  return ($i,$vals);
}

# see?  it sure paid off in legibility:

my ($i1,$vals1) = text(0xff, 0x33, 0x33, 0xff, 900, 1200, 800, 0.03, $string);
my ($i2,$vals2) = text(0x00, 0x33, 0xff, 0x7f, -560, 1200, 800, 0.04, $string);
my ($i3,$vals3) = text(0xff, 0xff, 0xff, 0x9f, 180, 1200, 800, 0.001, $string);

for(my $i=1; $i<=100; ++$i)
{
  ($i1,$vals1) = step($i1,$vals1);
  ($i2,$vals2) = step($i2,$vals2);
  ($i3,$vals3) = step($i3,$vals3);
  
  $m->nextFrame();
}

$m->save("test.swf");
