| 1 | package SDL2::rect { | ||
| 2 | use strictures 2; | ||
| 3 | use SDL2::Utils; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | use FFI::C::ArrayDef; | ||
| 6 | # | ||
| 7 | use SDL2::stdinc; | ||
| 8 | use SDL2::error; | ||
| 9 | use SDL2::pixels; | ||
| 10 | use SDL2::rwops; | ||
| 11 | # | ||
| 12 | package SDL2::Point { | ||
| 13 | use SDL2::Utils; | ||
| 14 | our $TYPE = # Store it | ||
| 15 | has | ||
| 16 | x => 'int', | ||
| 17 | y => 'int'; | ||
| 18 | our $LIST = FFI::C::ArrayDef->new( | ||
| 19 | ffi(), | ||
| 20 | name => 'PointList_t', | ||
| 21 | class => 'PointList', | ||
| 22 | members => [$SDL2::Point::TYPE] | ||
| 23 | ); | ||
| 24 | }; | ||
| 25 | |||
| 26 | package SDL2::FPoint { | ||
| 27 | use SDL2::Utils; | ||
| 28 | our $TYPE = # Store it | ||
| 29 | has x => 'float', y => 'float'; | ||
| 30 | our $LIST = FFI::C::ArrayDef->new( | ||
| 31 | ffi(), | ||
| 32 | name => 'FPointList_t', | ||
| 33 | class => 'FPointList', | ||
| 34 | members => [$SDL2::FPoint::TYPE] | ||
| 35 | ); | ||
| 36 | }; | ||
| 37 | |||
| 38 | package SDL2::FRect { | ||
| 39 | use SDL2::Utils; | ||
| 40 | our $TYPE = # Store it | ||
| 41 | has x => 'float', y => 'float', w => 'float', h => 'float'; | ||
| 42 | our $LIST = FFI::C::ArrayDef->new( | ||
| 43 | ffi(), | ||
| 44 | name => 'FRectList_t', | ||
| 45 | class => 'FRectList', | ||
| 46 | members => [$SDL2::FRect::TYPE] | ||
| 47 | ); | ||
| 48 | }; | ||
| 49 | |||
| 50 | package SDL2::Rect { | ||
| 51 | use SDL2::Utils; | ||
| 52 | our $TYPE = # Store it | ||
| 53 | has x => 'int', y => 'int', w => 'int', h => 'int'; | ||
| 54 | our $LIST = FFI::C::ArrayDef->new( | ||
| 55 | ffi(), | ||
| 56 | name => 'RectList_t', | ||
| 57 | class => 'RectList', | ||
| 58 | members => [$SDL2::Rect::TYPE] | ||
| 59 | ); | ||
| 60 | }; | ||
| 61 | # | ||
| 62 | define rect => [ | ||
| 63 | [ SDL_PointInRect => sub ( $p, $r ) { | ||
| 64 | return ( ( $p->x >= $r->x ) && | ||
| 65 | ( $p->x < ( $r->x + $r->w ) ) && | ||
| 66 | ( $p->y >= $r->y ) && | ||
| 67 | ( $p->y < ( $r->y + $r->h ) ) ); | ||
| 68 | } | ||
| 69 | ], | ||
| 70 | [ SDL_RectEmpty => sub ($r) { | ||
| 71 | return ( ( !$r ) || ( $r->w <= 0 ) || ( $r->h <= 0 ) ); | ||
| 72 | } | ||
| 73 | ], | ||
| 74 | [ SDL_RectEquals => sub ( $l, $r ) { | ||
| 75 | return ( $l && | ||
| 76 | $r && | ||
| 77 | ( $r->x == $l->x ) && | ||
| 78 | ( $l->y == $r->y ) && | ||
| 79 | ( $l->w == $r->w ) && | ||
| 80 | ( $l->h == $r->h ) ); | ||
| 81 | } | ||
| 82 | ], | ||
| 83 | ]; | ||
| 84 | attach rect => { | ||
| 85 | SDL_HasIntersection => [ [ 'SDL_Rect', 'SDL_Rect' ], 'SDL_bool' ], | ||
| 86 | SDL_IntersectRect => [ [ 'SDL_Rect', 'SDL_Rect', 'SDL_Rect' ], 'SDL_bool' ], | ||
| 87 | SDL_UnionRect => [ [ 'SDL_Rect', 'SDL_Rect', 'SDL_Rect' ] ], | ||
| 88 | SDL_EnclosePoints => [ | ||
| 89 | [ 'PointList_t', 'int', 'SDL_Rect', 'SDL_Rect' ], | ||
| 90 | 'SDL_bool' => sub ( $inner, $_points, $count, $clip, $result ) { | ||
| 91 | my $points = $SDL2::Point::LIST->create( | ||
| 92 | [ map { { x => $_->x, y => $_->y } } @$_points ] ); | ||
| 93 | $inner->( $points, $count, $clip, $result ); | ||
| 94 | } | ||
| 95 | ], | ||
| 96 | SDL_IntersectRectAndLine => [ [ 'SDL_Rect', 'int*', 'int*', 'int*', 'int*' ], 'SDL_bool' ], | ||
| 97 | }; | ||
| 98 | |||
| 99 | =encoding utf-8 | ||
| 100 | |||
| 101 | =head1 NAME | ||
| 102 | |||
| 103 | SDL2::rect - SDL2::Rect Management Functions | ||
| 104 | |||
| 105 | =head1 SYNOPSIS | ||
| 106 | |||
| 107 | use SDL2 qw[:rect]; | ||
| 108 | |||
| 109 | =head1 DESCRIPTION | ||
| 110 | |||
| 111 | This package defines functions used to manage L<SDL2::Rect> structures they may | ||
| 112 | be imported by name of with the given tag. | ||
| 113 | |||
| 114 | |||
| 115 | =head2 Functions | ||
| 116 | |||
| 117 | These functions may be imported with the C<:rect> tag. | ||
| 118 | |||
| 119 | =head2 C<SDL_PointInRect( ... )> | ||
| 120 | |||
| 121 | Find out if a given C<point> lies within a C<rectangle>. | ||
| 122 | |||
| 123 | my $dot = SDL2::Point->new( { x => 100, y => 100 } ); | ||
| 124 | my $box1 = SDL2::Rect->new( { x => 0, y => 0, w => 50, h => 50 } ); | ||
| 125 | my $box2 = SDL2::Rect->new( { x => 50, y => 0, w => 100, h => 101 } ); | ||
| 126 | printf "point %dx%d is %sside of box 1\n", $dot->x, $dot->y, | ||
| 127 | SDL_PointInRect( $dot, $box1 ) ? 'in' : 'out'; | ||
| 128 | printf "point %dx%d is %sside of box 2\n", $dot->x, $dot->y, | ||
| 129 | SDL_PointInRect( $dot, $box2 ) ? 'in' : 'out'; | ||
| 130 | |||
| 131 | Expected parameters include: | ||
| 132 | |||
| 133 | =over | ||
| 134 | |||
| 135 | =item C<point> - an L<SDL2::Point> structure | ||
| 136 | |||
| 137 | =item C<rectangle> - an L<SDL2::Rect> structure | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | Returns true if C<point> resides inside a C<rectangle>. | ||
| 142 | |||
| 143 | =head2 C<SDL_RectEmpty( ... )> | ||
| 144 | |||
| 145 | my $box = SDL2::Rect->new( { w => 0, h => 100 } ); | ||
| 146 | printf 'box is %sempty', SDL_RectEmpty($box) ? '' : 'not '; | ||
| 147 | |||
| 148 | Expected parameters include: | ||
| 149 | |||
| 150 | =over | ||
| 151 | |||
| 152 | =item C<rectangle> - an L<SDL2::Rect> structure to query | ||
| 153 | |||
| 154 | =back | ||
| 155 | |||
| 156 | Returns true if the C<rectangle> has no area. | ||
| 157 | |||
| 158 | =head2 C<SDL_RectEquals( ... )> | ||
| 159 | |||
| 160 | Calculates whether or not two given rectangles are positionally and | ||
| 161 | dimensionally the same. | ||
| 162 | |||
| 163 | my $box1 = SDL2::Rect->new( { w => 0, h => 100, x => 5 } ); | ||
| 164 | my $box2 = SDL2::Rect->new( { w => 0, h => 100, x => 10 } ); | ||
| 165 | my $box3 = SDL2::Rect->new( { w => 0, h => 100, x => 10 } ); | ||
| 166 | printf "box 1 is %sthe same as box 2\n", SDL_RectEquals( $box1, $box2 ) ? '' : 'not '; | ||
| 167 | printf "box 2 is %sthe same as box 3\n", SDL_RectEquals( $box2, $box3 ) ? '' : 'not '; | ||
| 168 | printf "box 3 is %sthe same as box 1\n", SDL_RectEquals( $box3, $box1 ) ? '' : 'not '; | ||
| 169 | |||
| 170 | Expected parameters include: | ||
| 171 | |||
| 172 | =over | ||
| 173 | |||
| 174 | =item C<lhs> - first L<SDL2::Rect> structure | ||
| 175 | |||
| 176 | =item C<rhs> - second L<SDL2::Rect> structure | ||
| 177 | |||
| 178 | =back | ||
| 179 | |||
| 180 | Returns a true value if the two rectangles are equal. | ||
| 181 | |||
| 182 | =head2 C<SDL_HasIntersection( ... )> | ||
| 183 | |||
| 184 | Determine whether two rectangles intersect. | ||
| 185 | |||
| 186 | my $box1 = SDL2::Rect->new( { w => 10, h => 100, x => 5, y => 10 } ); | ||
| 187 | my $box2 = SDL2::Rect->new( { w => 10, h => 100, x => 10, y => 0 } ); | ||
| 188 | my $box3 = SDL2::Rect->new( { w => 10, h => 100, x => 100, y => 0 } ); | ||
| 189 | printf "box 1 %s box 2\n", | ||
| 190 | SDL_HasIntersection( $box1, $box2 ) ? 'intersects' : 'does not intersect'; | ||
| 191 | printf "box 2 %s box 3\n", | ||
| 192 | SDL_HasIntersection( $box2, $box3 ) ? 'intersects' : 'does not intersect'; | ||
| 193 | printf "box 3 %s box 1\n", | ||
| 194 | SDL_HasIntersection( $box3, $box1 ) ? 'intersects' : 'does not intersect'; | ||
| 195 | |||
| 196 | If either pointer is undef the function will return C<SDL_FALSE>. | ||
| 197 | |||
| 198 | Expected parameters include: | ||
| 199 | |||
| 200 | =over | ||
| 201 | |||
| 202 | =item C<lhs> - an L<SDL2::Rect> structure representing the first rectangle | ||
| 203 | |||
| 204 | =item C<rhs> - an L<SDL2::Rect> structure representing the second rectangle | ||
| 205 | |||
| 206 | =back | ||
| 207 | |||
| 208 | Returns C<SDL_TRUE> if there is an intersection, C<SDL_FALSE> otherwise. | ||
| 209 | |||
| 210 | =head2 C<SDL_IntersectRect( ... )> | ||
| 211 | |||
| 212 | Calculate the intersection of two rectangles. | ||
| 213 | |||
| 214 | my $box1 = SDL2::Rect->new( { w => 10, h => 100, x => 5, y => 10 } ); | ||
| 215 | my $box2 = SDL2::Rect->new( { w => 10, h => 100, x => 10, y => 0 } ); | ||
| 216 | my $res = SDL2::Rect->new(); | ||
| 217 | printf 'the intersection of boxes 1 and 2 looks like { x => %d, y => %d, w => %d, h => %d }', | ||
| 218 | $res->x, $res->y, $res->w, $res->h | ||
| 219 | if SDL_IntersectRect( $box1, $box2, $res ); | ||
| 220 | |||
| 221 | If C<result> is undef then this function will return C<SDL_FALSE>. | ||
| 222 | |||
| 223 | Expected parameters include: | ||
| 224 | |||
| 225 | =over | ||
| 226 | |||
| 227 | =item C<lhs> - an L<SDL2::Rect> structure representing the first rectangle | ||
| 228 | |||
| 229 | =item C<rhs> - an L<SDL2::Rect> structure representing the second rectangle | ||
| 230 | |||
| 231 | =item C<result> an L<SDL2::Rect> structure which will be filled in with the intersection of rectangles C<lhs> and C<rhs> | ||
| 232 | |||
| 233 | =back | ||
| 234 | |||
| 235 | Returns C<SDL_TRUE> if there is an intersection, C<SDL_FALSE> otherwise. | ||
| 236 | |||
| 237 | =head2 C<SDL_UnionRect( ... )> | ||
| 238 | |||
| 239 | Calculate the union of two rectangles. | ||
| 240 | |||
| 241 | my $box1 = SDL2::Rect->new( { w => 10, h => 100, x => 5, y => 10 } ); | ||
| 242 | my $box2 = SDL2::Rect->new( { w => 10, h => 100, x => 10, y => 0 } ); | ||
| 243 | my $res = SDL2::Rect->new(); | ||
| 244 | SDL_UnionRect( $box1, $box2, $res ); | ||
| 245 | printf 'the union of boxes 1 and 2 looks like { x => %d, y => %d, w => %d, h => %d }', $res->x, | ||
| 246 | $res->y, $res->w, $res->h; | ||
| 247 | |||
| 248 | Expected parameters include: | ||
| 249 | |||
| 250 | =over | ||
| 251 | |||
| 252 | =item C<lhs> - an L<SDL2::Rect> structure representing the first rectangle | ||
| 253 | |||
| 254 | =item C<rhs> - an L<SDL2::Rect> structure representing the second rectangle | ||
| 255 | |||
| 256 | =item C<result> an L<SDL2::Rect> structure which will be filled in with the intersection of rectangles C<lhs> and C<rhs> | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | =head2 C<SDL_EnclosePoints( ... )> | ||
| 261 | |||
| 262 | Calculate a minimal rectangle enclosing a set of points. | ||
| 263 | |||
| 264 | my $p1 = SDL2::Point->new( { x => 1, y => 10 } ); | ||
| 265 | my $p2 = SDL2::Point->new( { x => 5, y => 20 } ); | ||
| 266 | my $res = SDL2::Rect->new(); | ||
| 267 | printf 'points 1 and 2 are enclosed by a box like { x => %d, y => %d, w => %d, h => %d }', | ||
| 268 | $res->x, $res->y, $res->w, $res->h | ||
| 269 | if SDL_EnclosePoints( [ $p1, $p2 ], 2, undef, $res ); | ||
| 270 | |||
| 271 | If C<clip> is not undef then only points inside of the clipping rectangle are | ||
| 272 | considered. | ||
| 273 | |||
| 274 | Expected parameters include: | ||
| 275 | |||
| 276 | =over | ||
| 277 | |||
| 278 | =item C<points> - an array of L<SDL2::Point> structures representing points to be enclosed | ||
| 279 | |||
| 280 | =item C<count> - the number of structures in the C<points> array | ||
| 281 | |||
| 282 | =item C<clip> - an L<SDL2::Rect> used for clipping or C<undef> to enclose all points | ||
| 283 | |||
| 284 | =item C<result> - an L<SDL2::Rect> structure filled in with the minimal enclosing rectangle | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | Returns C<SDL_TRUE> if any points were enclosed or C<SDL_FALSE> if all the | ||
| 289 | points were outside of the clipping rectangle. | ||
| 290 | |||
| 291 | =head2 C<SDL_IntersectRectAndLine( ... )> | ||
| 292 | |||
| 293 | Calculate the intersection of a rectangle and line segment. | ||
| 294 | |||
| 295 | my $box = SDL2::Rect->new( { x => 0, y => 0, w => 32, h => 32 } ); | ||
| 296 | my $x1 = 0; | ||
| 297 | my $y1 = 0; | ||
| 298 | my $x2 = 31; | ||
| 299 | my $y2 = 31; | ||
| 300 | printf 'line fully inside rect was clipped: %d,%d - %d,%d', $x1, $y1, $x2, $y2 | ||
| 301 | if SDL_IntersectRectAndLine( $box, \$x1, \$y1, \$x2, \$y2 ); | ||
| 302 | |||
| 303 | This function is used to clip a line segment to a rectangle. A line segment | ||
| 304 | contained entirely within the rectangle or that does not intersect will remain | ||
| 305 | unchanged. A line segment that crosses the rectangle at either or both ends | ||
| 306 | will be clipped to the boundary of the rectangle and the new coordinates saved | ||
| 307 | in C<X1>, C<Y1>, C<X2>, and/or C<Y2> as necessary. | ||
| 308 | |||
| 309 | Expected parameters include: | ||
| 310 | |||
| 311 | =over | ||
| 312 | |||
| 313 | =item C<rect> - an L<SDL2::Rect> structure representing the rectangle to intersect | ||
| 314 | |||
| 315 | =item C<X1> - a pointer to the starting X-coordinate of the line | ||
| 316 | |||
| 317 | =item C<Y1> - a pointer to the starting Y-coordinate of the line | ||
| 318 | |||
| 319 | =item C<X2> - a pointer to the ending X-coordinate of the line | ||
| 320 | |||
| 321 | =item C<Y2> - a pointer to the ending Y-coordinate of the line | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | Returns C<SDL_TRUE> if there is an intersection, C<SDL_FALSE> otherwise. | ||
| 326 | |||
| 327 | =head1 LICENSE | ||
| 328 | |||
| 329 | Copyright (C) Sanko Robinson. | ||
| 330 | |||
| 331 | This library is free software; you can redistribute it and/or modify it under | ||
| 332 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 333 | conditions may apply to data transmitted through this module. | ||
| 334 | |||
| 335 | =head1 AUTHOR | ||
| 336 | |||
| 337 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 338 | |||
| 339 | =begin stopwords | ||
| 340 | |||
| 341 | |||
| 342 | |||
| 343 | =end stopwords | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | }; | ||
| 348 | 1; |