Skip to content

Commit 3afad9b

Browse files
committed
Allow calling SDLx::Surface draw methods with blessed vectors
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.
1 parent 0f2af3b commit 3afad9b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

lib/SDLx/Surface.pm

+22-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use vars qw(@ISA @EXPORT @EXPORT_OK);
55
require Exporter;
66
require DynaLoader;
77
use Carp ();
8+
use Scalar::Util ();
89
use SDL;
910
use SDL::Rect;
1011
use SDL::Video;
@@ -219,9 +220,9 @@ sub flip {
219220
sub update {
220221
my ( $surface, $rects ) = @_;
221222

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

241242
Carp::confess "Error start needs an array ref [x,y]"
242-
unless ref($start) eq 'ARRAY';
243+
unless Scalar::Util::reftype $start eq 'ARRAY';
244+
243245
Carp::confess "Error end needs an array ref [x,y]"
244-
unless ref($end) eq 'ARRAY';
246+
unless Scalar::Util::reftype $end eq 'ARRAY';
245247

246248
unless ( SDL::Config->has('SDL_gfx_primitives') ) {
247249
Carp::cluck("SDL_gfx_primitives support has not been compiled");
@@ -270,7 +272,9 @@ sub draw_circle {
270272
return;
271273
}
272274

273-
Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
275+
Carp::cluck "Center needs to be an array of format [x,y]"
276+
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;
277+
274278
$color = SDLx::Validate::num_rgba($color);
275279

276280
unless( $antialias )
@@ -292,7 +296,9 @@ sub draw_circle_filled {
292296
return;
293297
}
294298

295-
Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
299+
Carp::cluck "Center needs to be an array of format [x,y]"
300+
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;
301+
296302
$color = SDLx::Validate::num_rgba($color);
297303

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

344-
Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
350+
Carp::cluck "Center needs to be an array of format [x,y]"
351+
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;
352+
345353
$color = SDLx::Validate::num_rgba($color);
346354

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

355-
Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
363+
Carp::cluck "Center needs to be an array of format [x,y]"
364+
unless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;
365+
356366
$color = SDLx::Validate::num_rgba($color);
357367

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

373-
Carp::cluck "Center needs to be an array of format [x,y]" unless ( ref $center eq 'ARRAY' && scalar @$center == 2 );
383+
Carp::cluck "Center needs to be an array of format [x,y]"
384+
iunless Scalar::Util::reftype $center eq 'ARRAY' && scalar @$center == 2;
385+
374386
$color = SDLx::Validate::num_rgba($color);
375387

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

0 commit comments

Comments
 (0)