Fixed test for hardware or underlying fails
[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 SDL;
36
37 sub new {
38         my $proto = shift;
39         my $class = ref($proto) || $proto;
40         my %options = @_;
41
42         verify (%options, qw/ -x -y -top -left -width -height -w -h / ) if $SDL::DEBUG;
43
44         my $x = $options{-x}            || $options{-left}  || 0;
45         my $y = $options{-y}            || $options{-top}   || 0;
46         my $w = $options{-width}        || $options{-w}         || 0;
47         my $h = $options{-height}       || $options{-h}         || 0;
48         
49         my $self = \SDL::NewRect($x,$y,$w,$h);
50         unless ($$self) {
51             require Carp;
52             Carp::croak SDL::GetError();
53         }
54         bless $self,$class;
55         return $self;
56 }
57
58 sub DESTROY {
59         SDL::FreeRect(${$_[0]});
60 }
61
62 # TODO: mangle with the symbol table to create an alias
63 # to sub x. We could call x from inside the sub but that
64 # would be another call and rects are a time-critical object.
65 sub left {
66         my $self = shift;
67         SDL::RectX($$self,@_);
68 }
69
70 sub x {
71         my $self = shift;
72         SDL::RectX($$self,@_);
73 }
74
75 ### TODO: see 'left' above (this is an 'alias' to sub y)
76 sub top {
77         my $self = shift;
78         SDL::RectY($$self,@_);
79 }
80
81 sub y {
82         my $self = shift;
83         SDL::RectY($$self,@_);
84 }
85
86 ### TODO: see 'left' above (this is an 'alias' to sub width)
87 sub w {
88         my $self = shift;
89         SDL::RectW($$self,@_);
90 }
91
92 sub width {
93         my $self = shift;
94         SDL::RectW($$self,@_);
95 }
96
97 ### TODO: see 'left' above (this is an 'alias' to sub height)
98 sub h {
99         my $self = shift;
100         SDL::RectH($$self,@_);
101 }
102
103 sub height {
104         my $self = shift;
105         SDL::RectH($$self,@_);
106 }
107
108 1;
109
110 __END__;
111
112 =head1 NAME
113
114 SDL::Rect - raw object for storing rectangular coordinates
115
116 =head1 SYNOPSIS
117
118   my $rect = SDL::Rect->new( -height => 4, -width => 40 );
119   
120   $rect->x(12);  # same as $rect->left(12)
121   $rect->y(9);   # same as $rect->top(9)
122
123 =head1 DESCRIPTION
124
125 C<SDL::Rect::new> creates a SDL_Rect structure which is
126 used for specifying regions of pixels for filling, blitting, and updating.
127 These objects make it easy to cut and backfill.
128
129 By default, x, y, height and width are all set to 0.
130
131 =head2 METHODS 
132
133 The four fields of a rectangle can be set simply
134 by passing a value to the applicable method.  These are:
135
136 =head3 x
137
138 =head3 left
139
140 sets and fetches the x (lefmost) position of the rectangle.
141
142 =head3 y
143
144 =head3 top
145
146 sets and fetches the y (topmost) position.
147
148 =head3 w
149
150 =head3 width
151
152 sets and fetches the width of the rectangle (in pixels).
153
154 =head3 h
155
156 =head3 height 
157
158 sets and fetches the height of the rectangle (in pixels).
159
160 =head1 AUTHOR
161
162 David J. Goehrig
163
164 =head1 SEE ALSO
165
166 perl(1) SDL::Surface(3)