Merge branch 'master' of git.shadowcat.co.uk:SDL-Site
[sdlgit/SDL-Site.git] / pages / SDLx-Rect.html-inc
CommitLineData
30fd24c2 1<div class="pod">
2<!-- INDEX START -->
3<h3 id="TOP">Index</h3>
4
5<ul><li><a href="#NAME">NAME</a></li>
6<li><a href="#CATEGORY">CATEGORY</a></li>
7<li><a href="#SYNOPSIS">SYNOPSIS</a></li>
8<li><a href="#DESCRIPTION">DESCRIPTION</a>
9<ul><li><a href="#ATTRIBUTES">ATTRIBUTES</a></li>
10<li><a href="#METHODS">METHODS </a>
11<ul><li><a href="#new_left_top_width_height">new ($left, $top, $width, $height)</a></li>
12<li><a href="#copy">copy</a></li>
13<li><a href="#duplicate">duplicate</a></li>
14<li><a href="#move_x_y">move(x, y)</a></li>
15<li><a href="#move_ip_x_y">move_ip(x, y)</a></li>
16<li><a href="#inflate_x_y">inflate(x, y)</a></li>
17<li><a href="#inflate_ip_x_y">inflate_ip(x, y)</a></li>
18<li><a href="#clamp_rect">clamp($rect)</a></li>
19<li><a href="#clamp_ip_rect">clamp_ip($rect)</a></li>
20<li><a href="#clip_rect">clip($rect)</a></li>
21<li><a href="#clip_ip_rect">clip_ip($rect)</a></li>
22<li><a href="#union_rect">union($rect)</a></li>
23<li><a href="#union_ip_rect">union_ip($rect)</a></li>
24<li><a href="#unionall_rect1_rect2">unionall( [$rect1, $rect2, ...] )</a></li>
25<li><a href="#unionall_ip_rect1_rect2">unionall_ip( [$rect1, $rect2, ...] )</a></li>
26<li><a href="#fit_rect">fit($rect)</a></li>
27<li><a href="#fit_ip_rect">fit_ip($rect)</a></li>
28<li><a href="#normalize">normalize</a></li>
29<li><a href="#contains_rect">contains($rect)</a></li>
30<li><a href="#collidepoint_x_y">collidepoint(x, y)</a></li>
31<li><a href="#colliderect_rect">colliderect($rect)</a></li>
32<li><a href="#collidelist_rect1_rect2">collidelist( [$rect1, $rect2, ...] )</a></li>
33<li><a href="#collidelistall_rect1_rect2">collidelistall( [$rect1, $rect2, ...] )</a></li>
34<li><a href="#collidehash_key1_gt_rect1_key2_gt_re">collidehash( {key1 =&gt; $rect1, key2 =&gt; $rect2, ...} )</a></li>
35<li><a href="#collidehashall_key1_gt_rect1_key2_gt">collidehashall( {key1 =&gt; $rect1, key2 =&gt; $rect2, ...} )</a></li>
36</ul>
37</li>
38</ul>
39</li>
30fd24c2 40<li><a href="#BUGS">BUGS</a></li>
41<li><a href="#SUPPORT">SUPPORT</a></li>
c7e8d3c6 42<li><a href="#AUTHORS">AUTHORS</a></li>
30fd24c2 43<li><a href="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a></li>
44<li><a href="#COPYRIGHT_amp_LICENSE">COPYRIGHT &amp; LICENSE</a></li>
45<li><a href="#SEE_ALSO">SEE ALSO</a>
46</li>
47</ul><hr />
48<!-- INDEX END -->
49
50<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
51<div id="NAME_CONTENT">
52<p>SDLx::Rect - SDL extension for storing and manipulating rectangular coordinates</p>
53
54</div>
55<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
56<div id="CATEGORY_CONTENT">
57<p>Extension</p>
58
59</div>
60<h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
61<div id="SYNOPSIS_CONTENT">
62<p>SDLx::Rect works as a SDL::Rect in the lower layer (SDL::*) but provides more methods to users.</p>
63<pre> use SDLx::Rect; #instead of SDL::Rect
64
65 my $rect = SDLx::Rect-&gt;new( $x, $y, $w, $h); #same as SDL::Rect
66
67 ...
68
69 SDL::Video::fill_rect( .. , $rect, ...); # use like SDL::Rect
70
71
72
73
74</pre>
75
76</div>
77<h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
78<div id="DESCRIPTION_CONTENT">
79<p><code>SDLx::Rect</code> object are used to store and manipulate rectangular areas. Rect objects are created from a combination of left (or x), top (or y), width (or w) and height (or h) values, just like raw <code>SDL::Rect objects</code>.</p>
80<p>All <code>SDLx::Rect</code> methods that change either position or size of a Rect return <strong>a new copy</strong> of the Rect with the affected changes. The original Rect is <strong>not</strong> modified. If you wish to modify the current Rect object, you can use the equivalent &quot;in-place&quot; methods that do not return but instead affects the original Rect. These &quot;in-place&quot; methods are denoted with the &quot;ip&quot; suffix. Note that changing a Rect's attribute is <i>always</i> an in-place operation.</p>
81
82
83
84
85
86</div>
87<h2 id="ATTRIBUTES">ATTRIBUTES</h2>
88<div id="ATTRIBUTES_CONTENT">
89<p>All Rect attributes are acessors, meaning you can get them by name, and set them by passing a value:</p>
90<pre> $rect-&gt;left(15);
91 $rect-&gt;left; # 15
92
93</pre>
94<p>The Rect object has several attributes which can be used to resize, move and align the Rect.</p>
95
96
97
98
99<dl>
100 <dt>* width, w - gets/sets object's width</dt>
101 <dt>* height, h - gets/sets object's height</dt>
102 <dt>* left, x - moves the object left position to match the given coordinate</dt>
103 <dt>* top, y - moves the object top position to match the given coordinate</dt>
104 <dt>* bottom - moves the object bottom position to match the given coordinate</dt>
105 <dt>* right - moves the object right position to match the given coordinate</dt>
106 <dt>* centerx - moves the object's horizontal center to match the given coordinate</dt>
107 <dt>* centery - moves the object's vertical center to match the given coordinate</dt>
108</dl>
109<p>Some of the attributes above can be fetched or set in pairs:</p>
110<pre> $rect-&gt;topleft(10, 15); # top is now 10, left is now 15
111
112 my ($width, $height) = $rect-&gt;size;
113
114
115
116
117</pre>
118<dl>
119 <dt>* size - gets/sets object's size (width, height)</dt>
120 <dt>* topleft - gets/sets object's top and left positions</dt>
121 <dt>* midleft - gets/sets object's vertical center and left positions</dt>
122 <dt>* bottomleft - gets/sets object's bottom and left positions</dt>
123 <dt>* center - gets/sets object's center (horizontal(x), vertical(y))</dt>
124 <dt>* topright - gets/sets object's top and right positions</dt>
125 <dt>* midright - gets/sets object's vertical center and right positions</dt>
126 <dt>* bottomright - gets/sets object's bottom and right positions</dt>
127 <dt>* midtop - gets/sets object's horizontal center and top positions</dt>
128 <dt>* midbottom - gets/sets object's horizontal center and bottom positions</dt>
129</dl>
130
131
132
133
134
135</div>
136<h2 id="METHODS">METHODS </h2>
137<div id="METHODS_CONTENT">
138<p>Methods denoted as receiving Rect objects can receive either <code>&lt;SDLx::Rect</code>&gt; or raw <code>&lt;SDL::Rect</code>&gt; objects.</p>
139
140</div>
141<h3 id="new_left_top_width_height">new ($left, $top, $width, $height)</h3>
142<div id="new_left_top_width_height_CONTENT">
143<p>Returns a new Rect object with the given coordinates. If any value is omitted (by passing undef), 0 is used instead.</p>
144
145</div>
146<h3 id="copy">copy</h3>
147<div id="copy_CONTENT">
148
149</div>
150<h3 id="duplicate">duplicate</h3>
151<div id="duplicate_CONTENT">
152<p>Returns a new Rect object having the same position and size as the original</p>
153
154</div>
155<h3 id="move_x_y">move(x, y)</h3>
156<div id="move_x_y_CONTENT">
157<p>Returns a new Rect that is moved by the given offset. The x and y arguments can be any integer value, positive or negative.</p>
158
159</div>
160<h3 id="move_ip_x_y">move_ip(x, y)</h3>
161<div id="move_ip_x_y_CONTENT">
162<p>Same as <code>&lt;move</code>&gt; above, but moves the current Rect in place and returns nothing.</p>
163
164</div>
165<h3 id="inflate_x_y">inflate(x, y)</h3>
166<div id="inflate_x_y_CONTENT">
167<p>Grows or shrinks the rectangle. Returns a new Rect with the size changed by the given offset. The rectangle remains centered around its current center. Negative values will return a shrinked rectangle instead.</p>
168
169</div>
170<h3 id="inflate_ip_x_y">inflate_ip(x, y)</h3>
171<div id="inflate_ip_x_y_CONTENT">
172<p>Same as <code>&lt;inflate</code>&gt; above, but grows/shrinks the current Rect in place and returns nothing.</p>
173
174</div>
175<h3 id="clamp_rect">clamp($rect)</h3>
176<div id="clamp_rect_CONTENT">
177<p>Returns a new Rect moved to be completely inside the Rect object passed as an argument. If the current Rect is too large to fit inside the passed Rect, it is centered inside it, but its size is not changed.</p>
178
179</div>
180<h3 id="clamp_ip_rect">clamp_ip($rect)</h3>
181<div id="clamp_ip_rect_CONTENT">
182<p>Same as <code>&lt;clamp</code>&gt; above, but moves the current Rect in place and returns nothing.</p>
183
184</div>
185<h3 id="clip_rect">clip($rect)</h3>
186<div id="clip_rect_CONTENT">
187<p>Returns a new Rect with the intersection between the two Rect objects, that is, returns a new Rect cropped to be completely inside the Rect object passed as an argument. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned, in the original Rect's (x,y) coordinates.</p>
188
189</div>
190<h3 id="clip_ip_rect">clip_ip($rect)</h3>
191<div id="clip_ip_rect_CONTENT">
192<p>Same as <code>&lt;clip</code>&gt; above, but crops the current Rect in place and returns nothing. As the original method, the Rect becomes zero-sized if the two rectangles do not overlap to begin with, retaining its (x, y) coordinates.</p>
193
194</div>
195<h3 id="union_rect">union($rect)</h3>
196<div id="union_rect_CONTENT">
197<p>Returns a new rectangle that completely covers the area of the current Rect and the one passed as an argument. There may be area inside the new Rect that is not covered by the originals.</p>
198
199</div>
200<h3 id="union_ip_rect">union_ip($rect)</h3>
201<div id="union_ip_rect_CONTENT">
202<p>Same as <code>&lt;union</code>&gt; above, but resizes the current Rect in place and returns nothing.</p>
203
204</div>
205<h3 id="unionall_rect1_rect2">unionall( [$rect1, $rect2, ...] )</h3>
206<div id="unionall_rect1_rect2_CONTENT">
207<p>Returns the union of one rectangle with a sequence of many rectangles, passed as an ARRAY REF.</p>
208
209</div>
210<h3 id="unionall_ip_rect1_rect2">unionall_ip( [$rect1, $rect2, ...] )</h3>
211<div id="unionall_ip_rect1_rect2_CONTENT">
212<p>Same as <code>&lt;unionall</code>&gt; above, but resizes the current Rect in place and returns nothing.</p>
213
214</div>
215<h3 id="fit_rect">fit($rect)</h3>
216<div id="fit_rect_CONTENT">
217<p>Returns a new Rect moved and resized to fit the Rect object passed as an argument. The aspect ratio of the original Rect is preserved, so the new rectangle may be smaller than the target in either width or height. </p>
218
219</div>
220<h3 id="fit_ip_rect">fit_ip($rect)</h3>
221<div id="fit_ip_rect_CONTENT">
222<p>Same as <code>&lt;fit</code>&gt; above, but moves/resizes the current Rect in place and returns nothing.</p>
223
224</div>
225<h3 id="normalize">normalize</h3>
226<div id="normalize_CONTENT">
227<p>Corrects negative sizes, flipping width/height of the Rect if they have a negative size. No repositioning is made so the rectangle will remain in the same place, but the negative sides will be swapped. This method returns nothing.</p>
228
229</div>
230<h3 id="contains_rect">contains($rect)</h3>
231<div id="contains_rect_CONTENT">
232<p>Returns true (non-zero) when the argument is completely inside the Rect. Otherwise returns undef.</p>
233
234</div>
235<h3 id="collidepoint_x_y">collidepoint(x, y)</h3>
236<div id="collidepoint_x_y_CONTENT">
237<p>Returns true (non-zero) if the given point is inside the Rect, otherwise returns undef. A point along the right or bottom edge is not considered to be inside the rectangle.</p>
238
239</div>
240<h3 id="colliderect_rect">colliderect($rect)</h3>
241<div id="colliderect_rect_CONTENT">
242<p>Returns true (non-zero) if any portion of either rectangles overlap (except for the top+bottom or left+right edges).</p>
243
244</div>
245<h3 id="collidelist_rect1_rect2">collidelist( [$rect1, $rect2, ...] )</h3>
246<div id="collidelist_rect1_rect2_CONTENT">
247<p>Test whether the rectangle collides with any in a sequence of rectangles, passed as an ARRAY REF. The index of the first collision found is returned. Returns undef if no collisions are found.</p>
248
249</div>
250<h3 id="collidelistall_rect1_rect2">collidelistall( [$rect1, $rect2, ...] )</h3>
251<div id="collidelistall_rect1_rect2_CONTENT">
252<p>Returns an ARRAY REF of all the indices that contain rectangles that collide with the Rect. If no intersecting rectangles are found, an empty list ref is returned. </p>
253
254</div>
255<h3 id="collidehash_key1_gt_rect1_key2_gt_re">collidehash( {key1 =&gt; $rect1, key2 =&gt; $rect2, ...} )</h3>
256<div id="collidehash_key1_gt_rect1_key2_gt_re-2">
257<p>Receives a HASH REF and returns the a (key, value) list with the key and value of the first hash item that collides with the Rect. If no collisions are found, returns (undef, undef).</p>
258
259</div>
260<h3 id="collidehashall_key1_gt_rect1_key2_gt">collidehashall( {key1 =&gt; $rect1, key2 =&gt; $rect2, ...} )</h3>
261<div id="collidehashall_key1_gt_rect1_key2_gt-2">
262<p>Returns a HASH REF of all the key and value pairs that intersect with the Rect. If no collisions are found an empty hash ref is returned. </p>
263
264
265
266
267
268</div>
30fd24c2 269<h1 id="BUGS">BUGS</h1><p><a href="#TOP" class="toplink">Top</a></p>
270<div id="BUGS_CONTENT">
271<p>Please report any bugs or feature requests to the bug tracker. I will be notified, and then you'll automatically be notified of progress on your bug as we make changes.</p>
272
273
274
275
276
277</div>
278<h1 id="SUPPORT">SUPPORT</h1><p><a href="#TOP" class="toplink">Top</a></p>
279<div id="SUPPORT_CONTENT">
280<p>You can find documentation for this module with the perldoc command.</p>
281<pre> perldoc SDLx::Rect
282
30fd24c2 283</pre>
284
285</div>
c7e8d3c6 286<h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
287<div id="AUTHORS_CONTENT">
1dbe1697 288<p>See <a href="/SDL.html#AUTHORS">/SDL.html#AUTHORS</a>.</p>
c7e8d3c6 289
290</div>
30fd24c2 291<h1 id="ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</h1><p><a href="#TOP" class="toplink">Top</a></p>
292<div id="ACKNOWLEDGEMENTS_CONTENT">
c7e8d3c6 293<p>Many thanks to the authors of pygame.rect, in which this particular module is heavily based.</p>
30fd24c2 294
295</div>
296<h1 id="COPYRIGHT_amp_LICENSE">COPYRIGHT &amp; LICENSE</h1><p><a href="#TOP" class="toplink">Top</a></p>
297<div id="COPYRIGHT_amp_LICENSE_CONTENT">
30fd24c2 298<p>This program is free software; you can redistribute it and/or modify it
299under the same terms as Perl itself.</p>
300
301
302
303
304
305</div>
306<h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
307<div id="SEE_ALSO_CONTENT">
308<p>perl, <a href="SDL.html">SDL</a>, <a href="SDL-Rect.html">SDL::Rect</a>, <a href="SDL-Game.html">SDL::Game</a>
309</p>
310
311</div>
312</div>