Simplyify SDL::Rect to be just an XS wrapper, add a few methods to SDL::Game::Rect
[sdlgit/SDL_perl.git] / lib / SDL / Rect.pm
index d31055e..3879a02 100644 (file)
-#!/usr/bin/env perl
-#
-# Rect.pm
-#
-# Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
-#
-# ------------------------------------------------------------------------------
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-# 
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-# 
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-#
-# ------------------------------------------------------------------------------
-#
-# Please feel free to send questions, suggestions or improvements to:
-#
-#      David J. Goehrig
-#      dgoehrig@cpan.org
-#
-
 package SDL::Rect;
-
 use strict;
 use warnings;
-use SDL;
-
-sub new {
-       my $proto = shift;
-       my $class = ref($proto) || $proto;
-       my %options = @_;
-
-       verify (%options, qw/ -x -y -top -left -width -height -w -h / ) if $SDL::DEBUG;
-
-       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);
-       unless ($$self) {
-           require Carp;
-           Carp::croak SDL::GetError();
-       }
-       bless $self,$class;
-       return $self;
-}
-
-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,@_);
-}
+require Exporter;
+require DynaLoader;
+our @ISA = qw(Exporter DynaLoader);
+bootstrap SDL::Rect;
 
 1;
 
-__END__;
+__END__
+
+=pod
 
 =head1 NAME
 
-SDL::Rect - raw object for storing rectangular coordinates
+SDL::Rect - Defines a rectangular area
 
 =head1 SYNOPSIS
 
-  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)
+  my $rect = SDL::Rect->new( 0, 0, 0, 0 );
+  $rect->x(1);
+  $rect->y(2);
+  $rect->w(3);
+  $rect->h(4);
+  my $x = $rect->x; # 1
+  my $y = $rect->y; # 2
+  my $w = $rect->w; # 3
+  my $h = $rect->h; # 4
 
 =head1 DESCRIPTION
 
-C<SDL::Rect::new> creates a SDL_Rect structure which is
-used for specifying regions of pixels for filling, blitting, and updating.
-These objects make it easy to cut and backfill.
+An C<SDL_Rect> defines a rectangular area of pixels.
 
-By default, x, y, height and width are all set to 0.
+=head1 METHODS
 
-=head2 METHODS 
+=head2 new ( $x, $y, $w, $h )
 
-The four fields of a rectangle can be set simply
-by passing a value to the applicable method.  These are:
+The constructor creates a new rectangle with the specified x, y, w, h
+values:
 
-=head3 x
+    my $rect = SDL::Rect->new( 0, 0, 0, 0 );
 
-=head3 left
+=head2 x
 
-sets and fetches the x (lefmost) position of the rectangle.
+If passed a value, this method sets the x component of the rectangle;
+if not, it returns the x component of the rectangle:
 
-=head3 y
+  my $x = $rect->x; # 255
+  $rect->x(128);
 
-=head3 top
+=head2 y
 
-sets and fetches the y (topmost) position.
+If passed a value, this method sets the y component of the rectangle;
+if not, it returns the y component of the rectangle:
 
-=head3 w
+  my $y = $rect->y; # 255
+  $rect->y(128);
 
-=head3 width
+=head2 w
 
-sets and fetches the width of the rectangle (in pixels).
+If passed a value, this method sets the w component of the rectangle;
+if not, it returns the w component of the rectangle:
 
-=head3 h
+  my $w = $rect->w; # 255
+  $rect->w(128);
 
-=head3 height 
+=head2 h
 
-sets and fetches the height of the rectangle (in pixels).
+If passed a value, this method sets the h component of the rectangle;
+if not, it returns the h component of the rectangle:
 
-=head1 AUTHOR
-
-David J. Goehrig
+  my $h = $rect->h; # 255
+  $rect->h(128);
 
 =head1 SEE ALSO
 
-perl(1) SDL::Surface(3)
+L<SDL::Surface>
+
+=cut