Rect.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Graphics/Rect.h>
00029 #include <SFML/Graphics/Rect.hpp>
00030 #include <SFML/Internal.h>
00031 
00032 
00036 void sfFloatRect_Offset(sfFloatRect* Rect, float OffsetX, float OffsetY)
00037 {
00038     CSFML_CHECK(Rect)
00039     Rect->Left   += OffsetX;
00040     Rect->Right  += OffsetX;
00041     Rect->Top    += OffsetY;
00042     Rect->Bottom += OffsetY;
00043 }
00044 void sfIntRect_Offset(sfIntRect* Rect, int OffsetX, int OffsetY)
00045 {
00046     CSFML_CHECK(Rect)
00047     Rect->Left   += OffsetX;
00048     Rect->Right  += OffsetX;
00049     Rect->Top    += OffsetY;
00050     Rect->Bottom += OffsetY;
00051 }
00052 
00053 
00057 sfBool sfFloatRect_Contains(sfFloatRect* Rect, float X, float Y)
00058 {
00059     CSFML_CHECK_RETURN(Rect, sfFalse)
00060     return sf::FloatRect(Rect->Left, Rect->Top, Rect->Right, Rect->Bottom).Contains(X, Y);
00061 }
00062 sfBool sfIntRect_Contains(sfIntRect* Rect, int X, int Y)
00063 {
00064     CSFML_CHECK_RETURN(Rect, sfFalse)
00065     return sf::IntRect(Rect->Left, Rect->Top, Rect->Right, Rect->Bottom).Contains(X, Y);
00066 }
00067 
00068 
00072 sfBool sfFloatRect_Intersects(sfFloatRect* Rect1, sfFloatRect* Rect2, sfFloatRect* OverlappingRect)
00073 {
00074     CSFML_CHECK_RETURN(Rect1, sfFalse)
00075     CSFML_CHECK_RETURN(Rect2, sfFalse)
00076 
00077     sf::FloatRect SFMLRect1(Rect1->Left, Rect1->Top, Rect1->Right, Rect1->Bottom);
00078     sf::FloatRect SFMLRect2(Rect2->Left, Rect2->Top, Rect2->Right, Rect2->Bottom);
00079 
00080     if (OverlappingRect)
00081     {
00082         sf::FloatRect Overlap;
00083         bool Intersect = SFMLRect1.Intersects(SFMLRect2, &Overlap);
00084 
00085         OverlappingRect->Left   = Overlap.Left;
00086         OverlappingRect->Top    = Overlap.Top;
00087         OverlappingRect->Right  = Overlap.Right;
00088         OverlappingRect->Bottom = Overlap.Bottom;
00089 
00090         return Intersect;
00091     }
00092     else
00093     {
00094         return SFMLRect1.Intersects(SFMLRect2);
00095     }
00096 }
00097 sfBool sfIntRect_Intersects(sfIntRect* Rect1, sfIntRect* Rect2, sfIntRect* OverlappingRect)
00098 {
00099     CSFML_CHECK_RETURN(Rect1, sfFalse)
00100     CSFML_CHECK_RETURN(Rect2, sfFalse)
00101 
00102     sf::IntRect SFMLRect1(Rect1->Left, Rect1->Top, Rect1->Right, Rect1->Bottom);
00103     sf::IntRect SFMLRect2(Rect2->Left, Rect2->Top, Rect2->Right, Rect2->Bottom);
00104 
00105     if (OverlappingRect)
00106     {
00107         sf::IntRect Overlap;
00108         bool Intersect = SFMLRect1.Intersects(SFMLRect2, &Overlap);
00109 
00110         OverlappingRect->Left   = Overlap.Left;
00111         OverlappingRect->Top    = Overlap.Top;
00112         OverlappingRect->Right  = Overlap.Right;
00113         OverlappingRect->Bottom = Overlap.Bottom;
00114 
00115         return Intersect;
00116     }
00117     else
00118     {
00119         return SFMLRect1.Intersects(SFMLRect2);
00120     }
00121 }