implemented 'aliases' left, top, h and w
[sdlgit/SDL_perl.git] / lib / SDL / Rect.pm
1 #!/usr/bin/env perl
2 #
3 # Rect.pm
4 #
5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
6 #
7 # ------------------------------------------------------------------------------
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 #
23 # ------------------------------------------------------------------------------
24 #
25 # Please feel free to send questions, suggestions or improvements to:
26 #
27 #       David J. Goehrig
28 #       dgoehrig@cpan.org
29 #
30
31 package SDL::Rect;
32
33 use strict;
34 use warnings;
35 use Carp;
36 use SDL;
37
38 sub new {
39         my $proto = shift;
40         my $class = ref($proto) || $proto;
41         my %options = @_;
42
43         verify (%options, qw/ -x -y -top -left -width -height -w -h / ) if $SDL::DEBUG;
44
45         my $x = $options{-x}            || $options{-left}  || 0;
46         my $y = $options{-y}            || $options{-top}   || 0;
47         my $w = $options{-width}        || $options{-w}         || 0;
48         my $h = $options{-height}       || $options{-h}         || 0;
49         
50         my $self = \SDL::NewRect($x,$y,$w,$h);
51         croak SDL::GetError() unless $$self;
52         bless $self,$class;
53         return $self;
54 }
55
56 sub DESTROY {
57         SDL::FreeRect(${$_[0]});
58 }
59
60 # TODO: mangle with the symbol table to create an alias
61 # to sub x. We could call x from inside the sub but that
62 # would be another call and rects are a time-critical object.
63 sub left {
64         my $self = shift;
65         SDL::RectX($$self,@_);
66 }
67
68 sub x {
69         my $self = shift;
70         SDL::RectX($$self,@_);
71 }
72
73 ### TODO: see 'left' above
74 sub top {
75         my $self = shift;
76         SDL::RectY($$self,@_);
77 }
78
79 sub y {
80         my $self = shift;
81         SDL::RectY($$self,@_);
82 }
83
84 ### TODO: see 'left' above
85 sub w {
86         my $self = shift;
87         SDL::RectW($$self,@_);
88 }
89
90 sub width {
91         my $self = shift;
92         SDL::RectW($$self,@_);
93 }
94
95 ### TODO: see 'left' above
96 sub h {
97         my $self = shift;
98         SDL::RectH($$self,@_);
99 }
100
101 sub height {
102         my $self = shift;
103         SDL::RectH($$self,@_);
104 }
105
106 1;
107
108 __END__;
109
110 =head1 NAME
111
112 SDL::Rect - raw object for storing rectangular coordinates
113
114 =head1 SYNOPSIS
115
116   my $rect = SDL::Rect->new( -height => 4, -width => 40 );
117   
118   $rect->x(12);  # same as $rect->left(12)
119   $rect->y(9);   # same as $rect->top(9)
120
121 =head1 DESCRIPTION
122
123 C<SDL::Rect::new> creates a SDL_Rect structure which is
124 used for specifying regions of pixels for filling, blitting, and updating.
125 These objects make it easy to cut and backfill.
126
127 By default, x, y, height and width are all set to 0.
128
129 =head2 METHODS 
130
131 The four fields of a rectangle can be set simply
132 by passing a value to the applicable method.  These are:
133
134 =head3 x
135
136 =head3 left
137
138 sets and fetches the x (lefmost) position of the rectangle.
139
140 =head3 y
141
142 =head3 top
143
144 sets and fetches the y (topmost) position.
145
146 =head3 w
147
148 =head3 width
149
150 sets and fetched the width of the rectangle (in pixels).
151
152 =head3 h
153
154 =head3 height 
155
156 sets and fetched the height of the rectangle (in pixels).
157
158 =back
159
160 =head1 AUTHOR
161
162 David J. Goehrig
163
164 =head1 SEE ALSO
165
166 perl(1) SDL::Surface(3)