SDL2::rect - SDL2::Rect Management Functions
use SDL2 qw[:rect];
This package defines functions used to manage SDL2::Rect structures they may be imported by name of with the given tag.
These functions may be imported with the :rect tag.
:rect
SDL_PointInRect( ... )
Find out if a given point lies within a rectangle.
point
rectangle
my $dot = SDL2::Point->new( { x => 100, y => 100 } ); my $box1 = SDL2::Rect->new( { x => 0, y => 0, w => 50, h => 50 } ); my $box2 = SDL2::Rect->new( { x => 50, y => 0, w => 100, h => 101 } ); printf "point %dx%d is %sside of box 1\n", $dot->x, $dot->y, SDL_PointInRect( $dot, $box1 ) ? 'in' : 'out'; printf "point %dx%d is %sside of box 2\n", $dot->x, $dot->y, SDL_PointInRect( $dot, $box2 ) ? 'in' : 'out';
Expected parameters include:
Returns true if point resides inside a rectangle.
SDL_RectEmpty( ... )
my $box = SDL2::Rect->new( { w => 0, h => 100 } ); printf 'box is %sempty', SDL_RectEmpty($box) ? '' : 'not ';
Returns true if the rectangle has no area.
SDL_RectEquals( ... )
Calculates whether or not two given rectangles are positionally and dimensionally the same.
my $box1 = SDL2::Rect->new( { w => 0, h => 100, x => 5 } ); my $box2 = SDL2::Rect->new( { w => 0, h => 100, x => 10 } ); my $box3 = SDL2::Rect->new( { w => 0, h => 100, x => 10 } ); printf "box 1 is %sthe same as box 2\n", SDL_RectEquals( $box1, $box2 ) ? '' : 'not '; printf "box 2 is %sthe same as box 3\n", SDL_RectEquals( $box2, $box3 ) ? '' : 'not '; printf "box 3 is %sthe same as box 1\n", SDL_RectEquals( $box3, $box1 ) ? '' : 'not ';
lhs
rhs
Returns a true value if the two rectangles are equal.
SDL_HasIntersection( ... )
Determine whether two rectangles intersect.
my $box1 = SDL2::Rect->new( { w => 10, h => 100, x => 5, y => 10 } ); my $box2 = SDL2::Rect->new( { w => 10, h => 100, x => 10, y => 0 } ); my $box3 = SDL2::Rect->new( { w => 10, h => 100, x => 100, y => 0 } ); printf "box 1 %s box 2\n", SDL_HasIntersection( $box1, $box2 ) ? 'intersects' : 'does not intersect'; printf "box 2 %s box 3\n", SDL_HasIntersection( $box2, $box3 ) ? 'intersects' : 'does not intersect'; printf "box 3 %s box 1\n", SDL_HasIntersection( $box3, $box1 ) ? 'intersects' : 'does not intersect';
If either pointer is undef the function will return SDL_FALSE.
SDL_FALSE
Returns SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
SDL_TRUE
SDL_IntersectRect( ... )
Calculate the intersection of two rectangles.
my $box1 = SDL2::Rect->new( { w => 10, h => 100, x => 5, y => 10 } ); my $box2 = SDL2::Rect->new( { w => 10, h => 100, x => 10, y => 0 } ); my $res = SDL2::Rect->new(); printf 'the intersection of boxes 1 and 2 looks like { x => %d, y => %d, w => %d, h => %d }', $res->x, $res->y, $res->w, $res->h if SDL_IntersectRect( $box1, $box2, $res );
If result is undef then this function will return SDL_FALSE.
result
SDL_UnionRect( ... )
Calculate the union of two rectangles.
my $box1 = SDL2::Rect->new( { w => 10, h => 100, x => 5, y => 10 } ); my $box2 = SDL2::Rect->new( { w => 10, h => 100, x => 10, y => 0 } ); my $res = SDL2::Rect->new(); SDL_UnionRect( $box1, $box2, $res ); printf 'the union of boxes 1 and 2 looks like { x => %d, y => %d, w => %d, h => %d }', $res->x, $res->y, $res->w, $res->h;
SDL_EnclosePoints( ... )
Calculate a minimal rectangle enclosing a set of points.
my $p1 = SDL2::Point->new( { x => 1, y => 10 } ); my $p2 = SDL2::Point->new( { x => 5, y => 20 } ); my $res = SDL2::Rect->new(); printf 'points 1 and 2 are enclosed by a box like { x => %d, y => %d, w => %d, h => %d }', $res->x, $res->y, $res->w, $res->h if SDL_EnclosePoints( [ $p1, $p2 ], 2, undef, $res );
If clip is not undef then only points inside of the clipping rectangle are considered.
clip
points
count
undef
Returns SDL_TRUE if any points were enclosed or SDL_FALSE if all the points were outside of the clipping rectangle.
SDL_IntersectRectAndLine( ... )
Calculate the intersection of a rectangle and line segment.
my $box = SDL2::Rect->new( { x => 0, y => 0, w => 32, h => 32 } ); my $x1 = 0; my $y1 = 0; my $x2 = 31; my $y2 = 31; printf 'line fully inside rect was clipped: %d,%d - %d,%d', $x1, $y1, $x2, $y2 if SDL_IntersectRectAndLine( $box, \$x1, \$y1, \$x2, \$y2 );
This function is used to clip a line segment to a rectangle. A line segment contained entirely within the rectangle or that does not intersect will remain unchanged. A line segment that crosses the rectangle at either or both ends will be clipped to the boundary of the rectangle and the new coordinates saved in X1, Y1, X2, and/or Y2 as necessary.
X1
Y1
X2
Y2
rect
Copyright (C) Sanko Robinson.
This library is free software; you can redistribute it and/or modify it under the terms found in the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted through this module.
Sanko Robinson <sanko@cpan.org>
To install SDL2::FFI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SDL2::FFI
CPAN shell
perl -MCPAN -e shell install SDL2::FFI
For more information on module installation, please visit the detailed CPAN module installation guide.