From cc5ac7033dc7e1ad7b738d2678ed5eda76323f46 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Joaqu=C3=ADn=20Atria?= <jjatria@gmail.com>
Date: Mon, 3 Jun 2019 22:22:45 +0100
Subject: [PATCH] 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.
---
 lib/SDLx/Surface.pm | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/lib/SDLx/Surface.pm b/lib/SDLx/Surface.pm
index b12c98cc..1f0d0cbe 100644
--- a/lib/SDLx/Surface.pm
+++ b/lib/SDLx/Surface.pm
@@ -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;
@@ -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;
@@ -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");
@@ -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 )
@@ -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 );
@@ -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 );
@@ -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)
@@ -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 );