2020-11-21 19:57:40 +00:00
|
|
|
#include "sdlpp_rectcolider.hpp"
|
|
|
|
|
|
|
|
namespace SDLPP {
|
2021-03-12 21:33:46 +00:00
|
|
|
|
|
|
|
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();
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 21:33:46 +00:00
|
|
|
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 ) {}
|
|
|
|
|
2021-04-26 19:57:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:57:40 +00:00
|
|
|
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 {
|
2021-03-12 21:33:46 +00:00
|
|
|
return ( !isInfinite() || original.getY() != -1 ) * getY() +
|
|
|
|
isInfinite() * -1;
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int RectColider::bottommost() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return ( !isInfinite() || height() != -1 ) * ( getY() + pixel_height() ) +
|
2020-11-21 19:57:40 +00:00
|
|
|
isInfinite() * -1;
|
|
|
|
}
|
|
|
|
int RectColider::leftmost() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return ( !isInfinite() || original.getX() != -1 ) * getX() +
|
|
|
|
isInfinite() * -1;
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int RectColider::rightmost() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return ( !isInfinite() || width() != -1 ) * ( getX() + pixel_width() ) +
|
2020-11-21 19:57:40 +00:00
|
|
|
isInfinite() * -1;
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:57:31 +00:00
|
|
|
void RectColider::updateCollision( int x, int y, int w, int h, uint64_t id ) {
|
2021-03-12 21:33:46 +00:00
|
|
|
position = Vec2D< int >( original.getX() * w + x, original.getY() * h + y );
|
|
|
|
_size_pixel = Vec2D< int >( width() * w, height() * h );
|
2021-04-26 19:57:31 +00:00
|
|
|
if ( _id == static_cast< uint64_t >( -1 ) )
|
|
|
|
_id = id;
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 14:05:16 +00:00
|
|
|
void RectColider::render( Renderer &renderer, const SDL_Color &color,
|
|
|
|
const SDL_Color &outline_color ) {
|
2020-11-21 19:57:40 +00:00
|
|
|
auto rect = getRect();
|
2021-03-13 14:05:16 +00:00
|
|
|
SDL_SetRenderDrawColor( renderer.getRendererPtr(), color.r, color.g,
|
|
|
|
color.b, color.a );
|
2020-11-21 19:57:40 +00:00
|
|
|
SDL_RenderFillRect( renderer.getRendererPtr(), &rect );
|
2021-03-13 14:05:16 +00:00
|
|
|
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 );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RectColider::render( Renderer &renderer ) {
|
2021-03-13 14:05:16 +00:00
|
|
|
render( renderer, sdl_color, sdl_outline );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Rect RectColider::getRect() {
|
|
|
|
if ( !isInfinite() )
|
2021-03-12 21:33:46 +00:00
|
|
|
return { leftmost(), topmost(), pixel_width(), pixel_height() };
|
2020-11-21 19:57:40 +00:00
|
|
|
|
|
|
|
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
|
2021-03-12 21:33:46 +00:00
|
|
|
r.w = pixel_width();
|
2020-11-21 19:57:40 +00:00
|
|
|
if ( bottommost() == -1 )
|
|
|
|
r.h = std::numeric_limits< int >::max();
|
|
|
|
else
|
2021-03-12 21:33:46 +00:00
|
|
|
r.h = pixel_height();
|
2020-11-21 19:57:40 +00:00
|
|
|
return r;
|
|
|
|
}
|
2020-12-18 15:02:02 +00:00
|
|
|
|
|
|
|
std::shared_ptr< CollisionPolygon > RectColider::copySelf() {
|
|
|
|
return std::make_shared< RectColider >( *this );
|
|
|
|
}
|
|
|
|
|
2021-03-13 14:05:16 +00:00
|
|
|
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 );
|
2021-03-12 21:33:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:57:40 +00:00
|
|
|
} // namespace SDLPP
|