From: Leon Brocard Date: Wed, 14 Oct 2009 11:22:09 +0000 (+0100) Subject: Simplyify SDL::Rect to be just an XS wrapper, add a few methods to SDL::Game::Rect X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=05eb21df0d3c315f4fbd29b3d7b76016ddbc5e60;p=sdlgit%2FSDL_perl.git Simplyify SDL::Rect to be just an XS wrapper, add a few methods to SDL::Game::Rect --- diff --git a/lib/SDL/Game/Rect.pm b/lib/SDL/Game/Rect.pm index 92e0948..3166b8f 100644 --- a/lib/SDL/Game/Rect.pm +++ b/lib/SDL/Game/Rect.pm @@ -25,6 +25,27 @@ sub new { ############################# ## extra accessors ############################# + +sub left { + my $self = shift; + $self->x(@_); +} + +sub top { + my $self = shift; + $self->y(@_); +} + +sub width { + my $self = shift; + $self->w(@_); +} + +sub height { + my $self = shift; + $self->h(@_); +} + sub bottom { my ($self, $val) = (@_); if (defined $val) { diff --git a/lib/SDL/Rect.pm b/lib/SDL/Rect.pm index ea1c144..3879a02 100644 --- a/lib/SDL/Rect.pm +++ b/lib/SDL/Rect.pm @@ -1,70 +1,80 @@ -=pod -=cut package SDL::Rect; use strict; use warnings; require Exporter; require DynaLoader; - our @ISA = qw(Exporter DynaLoader); -# Items to export into callers namespace by default. Note: do not export -# names by default without a very good reason. Use EXPORT_OK instead. -# Do not simply export all your public functions/methods/constants. bootstrap SDL::Rect; -=head1 Perl binding to C stuct Rect -=cut - -# Preloaded methods go here. - -# 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; - return RectX($self,@_); -} - -sub x { - my $self = shift; - return RectX($self,@_); -} - -### TODO: see 'left' above (this is an 'alias' to sub y) -sub top { - my $self = shift; - return RectY($self,@_); -} - -sub y { - my $self = shift; - return RectY($self,@_); -} - -### TODO: see 'left' above (this is an 'alias' to sub width) -sub w { - my $self = shift; - return RectW($self,@_); -} - -sub width { - my $self = shift; - return RectW($self,@_); -} - -### TODO: see 'left' above (this is an 'alias' to sub height) -sub h { - my $self = shift; - return RectH($self,@_); -} - -sub height { - my $self = shift; - return RectH($self,@_); -} - - -# Autoload methods go after __END__, and are processed by the autosplit program. - 1; + __END__ + +=pod + +=head1 NAME + +SDL::Rect - Defines a rectangular area + +=head1 SYNOPSIS + + 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 + +An C defines a rectangular area of pixels. + +=head1 METHODS + +=head2 new ( $x, $y, $w, $h ) + +The constructor creates a new rectangle with the specified x, y, w, h +values: + + my $rect = SDL::Rect->new( 0, 0, 0, 0 ); + +=head2 x + +If passed a value, this method sets the x component of the rectangle; +if not, it returns the x component of the rectangle: + + my $x = $rect->x; # 255 + $rect->x(128); + +=head2 y + +If passed a value, this method sets the y component of the rectangle; +if not, it returns the y component of the rectangle: + + my $y = $rect->y; # 255 + $rect->y(128); + +=head2 w + +If passed a value, this method sets the w component of the rectangle; +if not, it returns the w component of the rectangle: + + my $w = $rect->w; # 255 + $rect->w(128); + +=head2 h + +If passed a value, this method sets the h component of the rectangle; +if not, it returns the h component of the rectangle: + + my $h = $rect->h; # 255 + $rect->h(128); + +=head1 SEE ALSO + +L + +=cut diff --git a/src/Rect.xs b/src/Rect.xs index cde0b7a..e4b40dc 100644 --- a/src/Rect.xs +++ b/src/Rect.xs @@ -8,9 +8,18 @@ #include +MODULE = SDL::Rect PACKAGE = SDL::Rect PREFIX = rect_ +=for documentation -MODULE = SDL::Rect PACKAGE = SDL::Rect PREFIX = rect_ +SDL_Rect -- Defines a rectangular area + + typedef struct{ + Sint16 x, y; + Uint16 w, h; + } SDL_Rect; + +=cut SDL_Rect * rect_new (CLASS, x, y, w, h) @@ -28,22 +37,8 @@ rect_new (CLASS, x, y, w, h) OUTPUT: RETVAL -void -SetRect(rect, x, y, w, h) - SDL_Rect *rect - Sint16 x - Sint16 y - Uint16 w - Uint16 h - CODE: - rect->x = x; - rect->y = y; - rect->w = w; - rect->h = h; - - Sint16 -rect_RectX ( rect, ... ) +rect_x ( rect, ... ) SDL_Rect *rect CODE: if (items > 1 ) rect->x = SvIV(ST(1)); @@ -52,7 +47,7 @@ rect_RectX ( rect, ... ) RETVAL Sint16 -rect_RectY ( rect, ... ) +rect_y ( rect, ... ) SDL_Rect *rect CODE: if (items > 1 ) rect->y = SvIV(ST(1)); @@ -61,7 +56,7 @@ rect_RectY ( rect, ... ) RETVAL Uint16 -rect_RectW ( rect, ... ) +rect_w ( rect, ... ) SDL_Rect *rect CODE: if (items > 1 ) rect->w = SvIV(ST(1)); @@ -70,7 +65,7 @@ rect_RectW ( rect, ... ) RETVAL Uint16 -rect_RectH ( rect, ... ) +rect_h ( rect, ... ) SDL_Rect *rect CODE: if (items > 1 ) rect->h = SvIV(ST(1)); @@ -83,7 +78,4 @@ void rect_DESTROY(self) SDL_Rect *self CODE: - safefree( (char *)self ); - - diff --git a/t/extendingrect.t b/t/extendingrect.t index 3a10047..b0e652e 100644 --- a/t/extendingrect.t +++ b/t/extendingrect.t @@ -24,7 +24,7 @@ my $rect = MyRect->new(0,0,0,0); isa_ok($rect, 'SDL::Rect'); isa_ok($rect, 'MyRect'); -can_ok($rect, qw(x y top left w h width height)); +can_ok($rect, qw(x y w h)); can_ok($rect, qw(new foo)); $rect->x(10); diff --git a/t/rectpm.t b/t/rectpm.t index 8e4dcc0..3c44559 100644 --- a/t/rectpm.t +++ b/t/rectpm.t @@ -1,101 +1,23 @@ -#!/usr/bin/perl -w -# -# Copyright (C) 2003 Tels -# Copyright (C) 2004 David J. Goehrig -# -# Copyright (C) 2005 David J. Goehrig -# -# ------------------------------------------------------------------------------ -# -# 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 -# -# -# basic testing of SDL::Rect - -BEGIN { - unshift @INC, 'blib/lib','blib/arch'; -} - +#!perl use strict; - -use Test::More; - -plan ( tests => 35 ); - -use_ok( 'SDL::Rect' ); - -can_ok ('SDL::Rect', qw/ - new - x - y - width - height - w - h - top - left - /); - -my $rect = SDL::Rect->new(0,0,0,0); - -# creating with defaults -isa_ok ($rect,'SDL::Rect'); -is ($rect->x(), 0, 'x is 0'); -is ($rect->y(), 0, 'y 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 (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->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->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'); +use warnings; +use Test::More tests => 10; +use_ok('SDL::Rect'); + +my $rect = SDL::Rect->new( 0, 0, 0, 0 ); +isa_ok( $rect, 'SDL::Rect' ); +is( $rect->x(), 0, 'x is 0' ); +is( $rect->y(), 0, 'y is 0' ); +is( $rect->w(), 0, 'w is 0' ); +is( $rect->h(), 0, 'h is 0' ); + +$rect->x(1); +$rect->y(2); +$rect->w(3); +$rect->h(4); + +is( $rect->x(), 1, 'x is now 1' ); +is( $rect->y(), 2, 'y is now 2' ); +is( $rect->w(), 3, 'w is now 3' ); +is( $rect->h(), 4, 'h is now 4' );