tags: Tutorials

=head1 NAME

SDL::Tutorial::Images

=head2 CATEGORY

Tutorials

=head1 SYNOPSIS

	# to read this tutorial
	$ perldoc SDL::Tutorial::Images

	# to create a demo animation program based on this tutorial
	$ perl -MSDL::Tutorial::Images=sdl_images.pl -e 1

=head1 ANIMATING IMAGES

Since you're already familiar with the concepts behind animation, it's time to
learn how to work with images.  As usual, the important point is that computer animation is just I<simulating> motion by painting several slightly different frames to the screen every second.

There are two ways to vary an image on screen.  One is to change its
coordinates so it's at a slightly different position.  This is very easy to do;
it's just like animating a rectangle.  The other way is to change the image
itself so it's slightly different.  This is a little more difficult, as you'll
need to draw the alternate image beforehand somehow.

=head2 Loading Images

As usual, start with an L<SDL::App> object representing the image window.  Then
preload the image file.  This is easy; just pass the C<name> parameter to the
L<SDL::Surface> constructor:

S< >

	use SDL::Surface;

	my $frame = SDL::Surface->new( -name => 'frame1.png' );

S< >

B<Note:> you'll need to have compiled SDL Perl (and probably SDL) to support
JPEG and PNG files for this to work.

That's it; now you have an SDL::Surface object containing the image.  You can
use the C<height()>, C<width()>, and C<bpp()> methods to retrieve its height,
width, and bits per pixel, if you need them.

=head2 Displaying Images

Drawing an image onto the screen requires blitting it from one surface to
another.  (Remember, "blitting" means copying bits in memory.)  The C<blit()>
method of SDL::Surface objects comes in handy.  Its arguments are a little odd,
though.  Assuming C<$app> is the SDL::App object, as usual:

S< >

	use SDL::Rect;

	my $frame_rect = SDL::Rect->new(
		-height => $frame->height(),
		-width  => $frame->width(),
		-x      => 0,
		-y      => 0,
	);

	my $dest_rect  = SDL::Rect->new(
		-height => $frame->height(),
		-width  => $frame->width(),
		-x      => 0,
		-y      => 0,
	);

	$frame->blit( $frame_rect, $app, $dest_rect );
	$app->update( $dest_rect );

S< >

Here we have two L<SDL::Rect> objects which represent rectangular regions of a
Surface.  C<$frame_rect> represents the entire area of C<$frame>, while
C<$dest_rect> represents the area of the main window in which to blit the
frame.  This may be clearer with more descriptive variable names:

S< >

	$source_surface->blit(
		$area_of_source_to_blit,
		$destination_surface,
		$destination_area
	);

S< >

As usual, call C<update()> on C<$app> to see the change.

Requiring the source and destination Rect objects may seem tedious in this
simple example, but it's highly useful for copying only part of surface to part
of another.  For example, animating this image is a matter of changing the C<x>
and C<y> coordinates of C<$dest_rect>:

S< >

	for my $x ( 1 .. 100 )
	{
		$dest_rect->x( $x );
		$frame->blit( $frame_rect, $app, $dest_rect );
		$app->update( $dest_rect );
	}

S< >

Of course, you'll have to redraw all or part of the screen to avoid artifacts,
as discussed in the previous tutorial.

=head2 Multi-Image Animation

That covers moving a single image around the screen.  What if you want
something more?  For example, what if you want to animate a stick figure
walking?

You'll need several frames, just as in a flip-book.  Each frame should be slightly different than the one before it.  It's probably handy to encapsulate all of this in a C<Walker> class:

S< >

	package Walker;

	use SDL::Surface;

	sub new
	{
		my ($class, @images) = @_;
		my $self = [ map { SDL::Surface->new( -name => $_ ) } @images ];

		bless $self, $class;
	}

	sub next_frame
	{
		my $self  = shift;
		my $frame = shift @$self;

		push @$self, $frame;
		return $frame;
	}

S< >

To use this class, instantiate an object:

S< >

	my $walker = Walker->new( 'frame1.png', 'frame2.png', 'frame3.png' );

S< >

Then call C<next_frame()> within the loop:

S< >

	for my $x ( 1 .. 100 )
	{
		my $frame = $walker->next_frame();

		$dest_rect->x( $x );
		$frame->blit( $frame_rect, $app, $dest_rect );
		$app->update( $dest_rect );
	}

S< >

Again, the rest of the frame drawing is missing from this example so as not to
distract from this technique.  You'll probably want to abstract the undrawing
and redrawing into a separate subroutine so you don't have to worry about it
every time.

It'd be easy to make C<next_frame()> much smarter, selecting an image
appropriate to the direction of travel, using a bored animation when the
character is no longer moving, or adding other characteristics to the
character.  As you can see, the hard part of this technique is generating the
images beforehand.  That can add up to a tremendous amount of art and that's
one reason for the popularity of 3D models... but that's another tutorial much
further down the road.

More importantly, it's time to discuss how to make these animations run more
smoothly.  More on that next time.

=head1 SEE ALSO

=over 4

=item L<SDL::Tutorial>

basic SDL tutorial

=item L<SDL::Tutorial::Animation>

non-image animation

=back

=head1 AUTHOR

chromatic, E<lt>chromatic@wgz.orgE<gt>

Written for and maintained by the Perl SDL project, L<http://sdl.perl.org/>.

=head1 BUGS

No known bugs.

=head1 COPYRIGHT

Copyright (c) 2004, chromatic.  All rights reserved.  This module is
distributed under the same terms as Perl itself, in the hope that it is useful
but certainly under no guarantee.
