Merge branch 'experimental'
Kartik Thakore [Sun, 30 Aug 2009 08:49:04 +0000 (04:49 -0400)]
lib/SDL/Rect.pm
t/rectpm.t

index d1d201b..d31055e 100644 (file)
@@ -32,7 +32,6 @@ package SDL::Rect;
 
 use strict;
 use warnings;
-use Carp;
 use SDL;
 
 sub new {
@@ -40,15 +39,18 @@ sub new {
        my $class = ref($proto) || $proto;
        my %options = @_;
 
-       verify (%options, qw/ -x -y -width -height -w -h / ) if $SDL::DEBUG;
+       verify (%options, qw/ -x -y -top -left -width -height -w -h / ) if $SDL::DEBUG;
 
-       my $x = $options{-x}            || 0;
-       my $y = $options{-y}            || 0;
+       my $x = $options{-x}            || $options{-left}  || 0;
+       my $y = $options{-y}            || $options{-top}   || 0;
        my $w = $options{-width}        || $options{-w}         || 0;
        my $h = $options{-height}       || $options{-h}         || 0;
        
        my $self = \SDL::NewRect($x,$y,$w,$h);
-       croak SDL::GetError() unless $$self;
+       unless ($$self) {
+           require Carp;
+           Carp::croak SDL::GetError();
+       }
        bless $self,$class;
        return $self;
 }
@@ -57,21 +59,47 @@ sub DESTROY {
        SDL::FreeRect(${$_[0]});
 }
 
+# TODO: mangle with the symbol table to create an alias
+# to sub x. We could call x from inside the sub but that
+# would be another call and rects are a time-critical object.
+sub left {
+       my $self = shift;
+       SDL::RectX($$self,@_);
+}
+
 sub x {
        my $self = shift;
        SDL::RectX($$self,@_);
 }
 
+### TODO: see 'left' above (this is an 'alias' to sub y)
+sub top {
+       my $self = shift;
+       SDL::RectY($$self,@_);
+}
+
 sub y {
        my $self = shift;
        SDL::RectY($$self,@_);
 }
 
+### TODO: see 'left' above (this is an 'alias' to sub width)
+sub w {
+       my $self = shift;
+       SDL::RectW($$self,@_);
+}
+
 sub width {
        my $self = shift;
        SDL::RectW($$self,@_);
 }
 
+### TODO: see 'left' above (this is an 'alias' to sub height)
+sub h {
+       my $self = shift;
+       SDL::RectH($$self,@_);
+}
+
 sub height {
        my $self = shift;
        SDL::RectH($$self,@_);
@@ -81,48 +109,53 @@ sub height {
 
 __END__;
 
-=pod
-
-
 =head1 NAME
 
-SDL::Rect - a SDL perl extension
+SDL::Rect - raw object for storing rectangular coordinates
 
 =head1 SYNOPSIS
 
-  $rect = new SDL::Rect ( -height => 4, -width => 40 );
+  my $rect = SDL::Rect->new( -height => 4, -width => 40 );
+  
+  $rect->x(12);  # same as $rect->left(12)
+  $rect->y(9);   # same as $rect->top(9)
 
 =head1 DESCRIPTION
 
 C<SDL::Rect::new> creates a SDL_Rect structure which is
-used for specifying regions for filling, blitting, and updating.
+used for specifying regions of pixels for filling, blitting, and updating.
 These objects make it easy to cut and backfill.
-By default, x, y, h, w are 0.
+
+By default, x, y, height and width are all set to 0.
 
 =head2 METHODS 
 
 The four fields of a rectangle can be set simply
 by passing a value to the applicable method.  These are:
 
-=over 4
+=head3 x
+
+=head3 left
+
+sets and fetches the x (lefmost) position of the rectangle.
 
-=item *
+=head3 y
 
-C<SDL::Rect::x>        sets and fetches the x position.
+=head3 top
 
-=item *
+sets and fetches the y (topmost) position.
 
-C<SDL::Rect::y>        sets and fetches the y position.
+=head3 w
 
-=item *
+=head3 width
 
-C<SDL::Rect::width> sets and fetched the width.
+sets and fetches the width of the rectangle (in pixels).
 
-=item *
+=head3 h
 
-C<SDL::Rect::height> sets and fetched the height.
+=head3 height 
 
-=back
+sets and fetches the height of the rectangle (in pixels).
 
 =head1 AUTHOR
 
@@ -131,7 +164,3 @@ David J. Goehrig
 =head1 SEE ALSO
 
 perl(1) SDL::Surface(3)
-
-
-=cut
-
index 422f64a..58a183e 100644 (file)
@@ -39,7 +39,7 @@ use strict;
 
 use Test::More;
 
-plan ( tests => 15 );
+plan ( tests => 35 );
 
 use_ok( 'SDL::Rect' ); 
   
@@ -48,7 +48,12 @@ can_ok ('SDL::Rect', qw/
        x 
        y 
        width 
-       height /);
+       height
+       w
+       h
+       top
+       left
+        /);
 
 my $rect = SDL::Rect->new();
 
@@ -56,18 +61,41 @@ my $rect = SDL::Rect->new();
 is (ref($rect),'SDL::Rect','new went ok');
 is ($rect->x(), 0, 'x is 0');
 is ($rect->y(), 0, 'y is 0');
-is ($rect->width(), 0, 'w is 0');
-is ($rect->height(), 0, 'h is 0');
+is ($rect->top(), 0, 'top is 0');
+is ($rect->left(), 0, 'left is 0');
+is ($rect->width(), 0, 'width is 0');
+is ($rect->height(), 0, 'height is 0');
+is ($rect->w(), 0, 'w is 0');
+is ($rect->h(), 0, 'h is 0');
 
-# set and get at the same time
+# set and get at the same time (and testing method aliases)
+is ($rect->left(15), 15, 'left is now 15');
+is ($rect->x, 15, 'x and left point to the same place');
 is ($rect->x(12), 12, 'x is now 12');
-is ($rect->y(123), 123, 'y is now 12');
+is ($rect->left, 12, 'left is an alias to x');
+
+is ($rect->top(132), 132, 'top is now 132');
+is ($rect->y, 132, 'y and top point to the same place');
+is ($rect->y(123), 123, 'y is now 123');
+is ($rect->top, 123, 'top is an alias to y');
+
+is ($rect->w(54), 54, 'w is now 54');
+is ($rect->width, 54, 'w and width point to the same place');
 is ($rect->width(45), 45, 'w is now 45');
+is ($rect->w, 45, 'w is an alias to width');
+
+is ($rect->h(76), 76, 'h is now 76');
+is ($rect->height, 76, 'h and height point to the same place');
 is ($rect->height(67), 67, 'h is now 67');
+is ($rect->h, 67, 'h is an alias to height');
 
 # get alone
 is ($rect->x(), 12, 'x is 12');
-is ($rect->y(), 123, 'y is 12');
-is ($rect->width(), 45, 'w is 45');
-is ($rect->height(), 67, 'h is 67');
+is ($rect->left(), 12, 'left is 12');
+is ($rect->y(), 123, 'y is 123');
+is ($rect->top(), 123, 'top is 123');
+is ($rect->width(), 45, 'width is 45');
+is ($rect->w(), 45, 'w is 45');
+is ($rect->height(), 67, 'height is 67');
+is ($rect->h(), 67, 'h is 67');