Hello, I'm starting to use SDL perl and have been learning how to use it
by reading the documentation, tutorials, and looking at existing SDL
applications.  I've been looking at code that uses SDL::Surface, and I see
that a commonly used operation on surfaces is to create SDL::Rect objects
for blitting, for example, the tutorial at
http://sdl.perl.org/tutorials/images.html uses this:

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

Finding this kind of awkward, I added an as_rect function to SDL::Surface:

sub as_rect {
        my $self = shift;
        return SDL::Rect->new(
                -height => $self->height(),
                -width  => $self->width(),
                @_
        );
}

This simplifies the first code to:

my $frame_rect = $frame->as_rect;

I added the @_ so you could pass it x and y coords like this:

my $dest_rect  = $frame->as_rect( -x => 0, -y => 20 );

SDL::Surface already depends on SDL::Rect, so a change like this shouldn't
be too harmful.  Rather than grabbing multiple data items out of the
Surface and sticking them into a Rect, it seems somehow proper for the
Surface to produce the Rect directly.

Another common thing I see is that applications are caching the source
Rect, probably to avoid the overhead of creating it frequently.  I think
it would simplify these applications if a Surface maintained one Rect of
its extents itself and provided a method like extents() to get it.   This
could simplify the images tutorial like this:

my $dest_rect  = $frame->as_rect;

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

or for clarity:

my $dest_rect = $frame->as_rect( -x => 0, -y => 0 );

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

- Joshua Neal

Reply via email to