#include "sdlpp_rectcolider.hpp" namespace SDLPP { double RectColider::width() const { return _size.getX(); } double RectColider::height() const { return _size.getY(); } int RectColider::pixel_width() const { return _size_pixel.getX(); } int RectColider::pixel_height() const { return _size_pixel.getY(); } RectColider::RectColider( double x, double y, double w, double h ) : RectColider( { x, y }, { w, h } ) {} RectColider::RectColider( const Vec2D< double > &top_left, const Vec2D< double > &size ) : CollisionPolygon( top_left ), _size( size ) {} RectColider::RectColider( double x, double y, double w, double h, uint64_t id ) : RectColider( { x, y }, { w, h }, id ) {} RectColider::RectColider( const Vec2D< double > &top_left, const Vec2D< double > &size, uint64_t id ) : RectColider( top_left, size ) { _id = id; } bool RectColider::colidesWith( const SDLPP::CollisionPolygon &other ) const { if ( other.isCircle() ) { return other.colidesWith( *this ); } if ( other.isInfinite() ) { return infinityIntersection( other, *this ); } if ( isInfinite() ) return infinityIntersection( *this, other ); return intersects( *this, other ); } bool RectColider::isCircle() const { return false; } int RectColider::topmost() const { return ( !isInfinite() || original.getY() != -1 ) * getY() + isInfinite() * -1; } int RectColider::bottommost() const { return ( !isInfinite() || height() != -1 ) * ( getY() + pixel_height() ) + isInfinite() * -1; } int RectColider::leftmost() const { return ( !isInfinite() || original.getX() != -1 ) * getX() + isInfinite() * -1; } int RectColider::rightmost() const { return ( !isInfinite() || width() != -1 ) * ( getX() + pixel_width() ) + isInfinite() * -1; } void RectColider::updateCollision( int x, int y, int w, int h, uint64_t id ) { position = Vec2D< int >( original.getX() * w + x, original.getY() * h + y ); _size_pixel = Vec2D< int >( width() * w, height() * h ); if ( _id == static_cast< uint64_t >( -1 ) ) _id = id; } void RectColider::render( Renderer &renderer, const SDL_Color &color, const SDL_Color &outline_color ) { auto rect = getRect(); SDL_SetRenderDrawColor( renderer.getRendererPtr(), color.r, color.g, color.b, color.a ); SDL_RenderFillRect( renderer.getRendererPtr(), &rect ); SDL_SetRenderDrawColor( renderer.getRendererPtr(), outline_color.r, outline_color.g, outline_color.b, outline_color.a ); SDL_RenderDrawRect( renderer.getRendererPtr(), &rect ); } void RectColider::render( Renderer &renderer, const SDL_Color &color ) { auto input_color = color; auto outline_color = color; input_color.a = 0x40; outline_color.a = 0x80; render( renderer, input_color, outline_color ); } void RectColider::render( Renderer &renderer ) { render( renderer, sdl_color, sdl_outline ); } SDL_Rect RectColider::getRect() { if ( !isInfinite() ) return { leftmost(), topmost(), pixel_width(), pixel_height() }; SDL_Rect r = { 0, 0, 0, 0 }; if ( ( r.x = leftmost() ) == -1 ) r.x = 0; if ( ( r.y = topmost() ) == -1 ) r.y = 0; if ( rightmost() == -1 ) r.w = std::numeric_limits< int >::max(); else r.w = pixel_width(); if ( bottommost() == -1 ) r.h = std::numeric_limits< int >::max(); else r.h = pixel_height(); return r; } std::shared_ptr< CollisionPolygon > RectColider::copySelf() { return std::make_shared< RectColider >( *this ); } std::vector< Line< int > > RectColider::getLines() const { std::vector< Line< int > > ret{}; ret.emplace_back( position, position + Vec2D< int >( pixel_width(), 0 ) ); ret.emplace_back( position + Vec2D< int >( pixel_width(), 0 ), position + _size_pixel ); ret.emplace_back( position, position + Vec2D< int >( 0, pixel_height() ) ); ret.emplace_back( position + Vec2D< int >( 0, pixel_height() ), position + _size_pixel ); return ret; } } // namespace SDLPP