########################################### # SDLMove -- Move images around SDL # Mike Schilli, 2007 (m@perlmeister.com) ########################################### package SDLMove; use strict; use warnings; use SDL; use SDL::App; ########################################### sub new { ########################################### my($class, %options) = @_; my $self = { %options }; bless $self, $class; $self->image($self->{image}); return $self; } ########################################### sub image { ########################################### my($self, $image) = @_; $self->{image} = $image; $self->{drect} = SDL::Rect->new( -width => $image->width, -height => $image->height, -x => $self->{x}, -y => $self->{y}, ); } ########################################### sub move { ########################################### my($self, $direction, $pixels) = @_; my $rect = $self->{drect}; my $app = $self->{app}; if($direction eq "w") { # left $self->{x} -= $pixels if $self->{x} > 0; } elsif($direction eq "e") { # right $self->{x} += $pixels if $self->{x} < $app->width - $rect->width; } elsif($direction eq "n") { # up $self->{y} -= $pixels if $self->{y} > 0; } elsif($direction eq "s") { # down $self->{y} += $pixels if $self->{y} < $app->height - $rect->height; } $self->{old_rect} = SDL::Rect->new( -height => $rect->height, -width => $rect->width, -x => $rect->x, -y => $rect->y, ); $rect->x( $self->{x} ); $rect->y( $self->{y} ); $app->fill($self->{old_rect}, $self->{bg_color}); $self->{image}->blit(0, $self->{app}, $rect); $app->update($self->{old_rect}, $rect); } ########################################### sub wipe { ########################################### my($self) = @_; $self->{app}->fill($self->{drect}, $self->{bg_color}); $self->{app}->update($self->{drect}); } ########################################### sub hit_bottom { ########################################### my($self) = @_; return $self->{y} > $self->{app}->height - $self->{drect}->height; } 1;