Skip to content

Commit

Permalink
Allow calling SDLx::Surface draw methods with blessed vectors
Browse files Browse the repository at this point in the history
Before this patch, validation of vectors used for coordinates
in SDLx::Surface draw methods checked with

    ref $vector eq 'ARRAY'

However, this meant that it was impossible to use these methods
with eg. an array-based vector class.

To support this, the check is now made with Scalar::Util::reftype,
which more accurately represents what this test is checking.
  • Loading branch information
jjatria committed Jun 3, 2019
1 parent 0f2af3b commit cc5ac70
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/SDLx/Surface.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use vars qw(@ISA @EXPORT @EXPORT_OK);
require Exporter;
require DynaLoader;
use Carp ();
use Scalar::Util ();
use SDL;
use SDL::Rect;
use SDL::Video;
Expand Down Expand Up @@ -219,9 +220,9 @@ sub flip {
sub update {
my ( $surface, $rects ) = @_;

if ( !defined($rects) || ( ref($rects) eq 'ARRAY' && !ref( $rects->[0] ) ) ) {
my @rect;
@rect = @{$rects} if $rects;
if ( !defined($rects) || ( Scalar::Util::reftype $rects eq 'ARRAY' && !ref( $rects->[0] ) ) ) {
my @rect;
@rect = @{$rects} if $rects;
$rect[0] ||= 0;
$rect[1] ||= 0;
$rect[2] ||= $surface->w;
Expand All @@ -239,9 +240,10 @@ sub draw_line {
my ( $self, $start, $end, $color, $antialias ) = @_;

Carp::confess "Error start needs an array ref [x,y]"
unless ref($start) eq 'ARRAY';
unless Scalar::Util::reftype $start eq 'ARRAY';

Carp::confess "Error end needs an array ref [x,y]"
unless ref($end) eq 'ARRAY';
unless Scalar::Util::reftype $end eq 'ARRAY';

unless ( SDL::Config->has('SDL_gfx_primitives') ) {
Carp::cluck("SDL_gfx_primitives support has not been compiled");
Expand Down Expand Up @@ -270,7 +272,9 @@ sub draw_circle {
return;
}

Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
Carp::cluck "Center needs to be an array of format [x,y]"
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;

$color = SDLx::Validate::num_rgba($color);

unless( $antialias )
Expand All @@ -292,7 +296,9 @@ sub draw_circle_filled {
return;
}

Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
Carp::cluck "Center needs to be an array of format [x,y]"
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;

$color = SDLx::Validate::num_rgba($color);

SDL::GFX::Primitives::filled_circle_color( $self, @{$center}, $radius, $color );
Expand Down Expand Up @@ -341,7 +347,9 @@ sub draw_polygon_filled {
sub draw_arc {
my ( $self, $center, $radius, $start, $end, $color ) = @_;

Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
Carp::cluck "Center needs to be an array of format [x,y]"
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;

$color = SDLx::Validate::num_rgba($color);

SDL::GFX::Primitives::arc_color( $self, @$center, $radius, $start, $end, $color );
Expand All @@ -352,7 +360,9 @@ sub draw_arc {
sub draw_ellipse {
my ( $self, $center, $rx, $ry, $color, $antialias ) = @_;

Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
Carp::cluck "Center needs to be an array of format [x,y]"
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;

$color = SDLx::Validate::num_rgba($color);

if ($antialias)
Expand All @@ -370,7 +380,9 @@ sub draw_ellipse {
sub draw_ellipse_filled {
my ( $self, $center, $rx, $ry, $color ) = @_;

Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
Carp::cluck "Center needs to be an array of format [x,y]"
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;

$color = SDLx::Validate::num_rgba($color);

SDL::GFX::Primitives::filled_ellipse_color( $self, @$center, $rx, $ry, $color );
Expand Down

0 comments on commit cc5ac70

Please sign in to comment.