2020-11-22 20:58:37 +00:00
|
|
|
#include <cmath>
|
2020-11-21 19:57:40 +00:00
|
|
|
#include "sdlpp_linerenderer.hpp"
|
|
|
|
|
|
|
|
namespace SDLPP {
|
|
|
|
LineRenderer::LineRenderer( double x1, double y1, double x2, double y2,
|
|
|
|
const std::shared_ptr< Renderer > &r )
|
2021-03-12 21:33:46 +00:00
|
|
|
: LineRenderer( { { x1, y1 }, { x2, y2 } }, r ) {}
|
|
|
|
LineRenderer::LineRenderer( double x1, double y1, double x2, double y2,
|
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &color )
|
|
|
|
: LineRenderer( { { x1, y1 }, { x2, y2 } }, r, color ) {}
|
|
|
|
LineRenderer::LineRenderer( const Line< double > &line,
|
|
|
|
const std::shared_ptr< Renderer > &r )
|
2020-11-21 19:57:40 +00:00
|
|
|
: RenderObject( r ) {
|
2021-03-12 21:33:46 +00:00
|
|
|
original = current = line;
|
2020-11-21 19:57:40 +00:00
|
|
|
updateSizeAndPosition();
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
LineRenderer::LineRenderer( const Line< double > &line,
|
2020-11-21 19:57:40 +00:00
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &color )
|
2021-03-12 21:33:46 +00:00
|
|
|
: LineRenderer( line, r ) {
|
2020-11-21 19:57:40 +00:00
|
|
|
setColor( color );
|
|
|
|
}
|
|
|
|
void LineRenderer::setColor( const std::string &color ) {
|
2021-03-15 13:30:01 +00:00
|
|
|
_color = getSDLColorHEX( color );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
void LineRenderer::render() {
|
|
|
|
if ( !getHidden() ) {
|
2021-03-15 13:30:01 +00:00
|
|
|
SDL_SetRenderDrawColor( renderer->getRendererPtr(), _color.r, _color.g,
|
|
|
|
_color.b, _color.a );
|
2021-03-12 21:33:46 +00:00
|
|
|
SDL_RenderDrawLine(
|
|
|
|
renderer->getRendererPtr(), pixel_line.getStart().getX(),
|
|
|
|
pixel_line.getStart().getY(), pixel_line.getEnd().getX(),
|
|
|
|
pixel_line.getEnd().getY() );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
|
|
|
|
for ( const auto &col : getCollisions() )
|
|
|
|
col->render( *renderer, colider_color );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LineRenderer::setPos( double x, double y ) {
|
2021-03-15 13:30:01 +00:00
|
|
|
setPos( Vec2D< double >( x, y ) );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
void LineRenderer::setPos( const std::pair< double, double > &pos ) {
|
2021-03-15 13:30:01 +00:00
|
|
|
setPos( Vec2D< double >( pos.first, pos.second ) );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
void LineRenderer::setPos( const Vec2D< double > &vec ) {
|
2021-03-15 13:30:01 +00:00
|
|
|
auto diff = original.getEnd() - original.getStart();
|
|
|
|
original = { vec, vec + diff };
|
|
|
|
updateSizeAndPosition();
|
2021-03-12 21:33:46 +00:00
|
|
|
}
|
|
|
|
Vec2D< double > LineRenderer::getPos() const {
|
|
|
|
return original.getStart();
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int LineRenderer::leftmost() {
|
2021-03-12 21:33:46 +00:00
|
|
|
return pixel_line.getStart().getX() < pixel_line.getEnd().getX()
|
|
|
|
? pixel_line.getStart().getX()
|
|
|
|
: pixel_line.getEnd().getX();
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int LineRenderer::topmost() {
|
2021-03-12 21:33:46 +00:00
|
|
|
return pixel_line.getStart().getY() < pixel_line.getEnd().getY()
|
|
|
|
? pixel_line.getStart().getY()
|
|
|
|
: pixel_line.getEnd().getY();
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int LineRenderer::rightmost() {
|
2021-03-12 21:33:46 +00:00
|
|
|
return pixel_line.getStart().getX() > pixel_line.getEnd().getX()
|
|
|
|
? pixel_line.getStart().getX()
|
|
|
|
: pixel_line.getEnd().getX();
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int LineRenderer::bottommost() {
|
2021-03-12 21:33:46 +00:00
|
|
|
return pixel_line.getStart().getY() > pixel_line.getEnd().getY()
|
|
|
|
? pixel_line.getStart().getY()
|
|
|
|
: pixel_line.getEnd().getY();
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int LineRenderer::collisionPushX() {
|
|
|
|
return leftmost();
|
|
|
|
}
|
|
|
|
int LineRenderer::collisionPushY() {
|
|
|
|
return topmost();
|
|
|
|
}
|
|
|
|
int LineRenderer::collisionWidth() {
|
|
|
|
return rightmost() - leftmost();
|
|
|
|
}
|
|
|
|
int LineRenderer::collisionHeight() {
|
|
|
|
return bottommost() - topmost();
|
|
|
|
}
|
|
|
|
void LineRenderer::updateSizeAndPosition() {
|
|
|
|
updateXY();
|
|
|
|
auto dimension = renderer->getSmallerSide();
|
2021-03-12 21:33:46 +00:00
|
|
|
pixel_line = Line< int >(
|
|
|
|
Vec2D< int >( std::round( current.getStart().getX() * dimension ),
|
|
|
|
std::round( current.getStart().getY() * dimension ) ),
|
|
|
|
Vec2D< int >( std::round( current.getEnd().getX() * dimension ),
|
|
|
|
std::round( current.getEnd().getY() * dimension ) ) );
|
2020-11-21 19:57:40 +00:00
|
|
|
for ( auto &x : collisions ) {
|
|
|
|
x->updateCollision( collisionPushX(), collisionPushY(),
|
2021-04-26 19:57:31 +00:00
|
|
|
collisionWidth(), collisionHeight(), getId() );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::shared_ptr< RenderObject > LineRenderer::copySelf() {
|
2020-12-18 15:05:38 +00:00
|
|
|
auto ret = std::make_shared< LineRenderer >( *this );
|
2021-03-12 21:33:46 +00:00
|
|
|
copyTo( ret );
|
2020-12-18 15:05:38 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
void LineRenderer::copyTo( std::shared_ptr< RenderObject > other ) {
|
|
|
|
RenderObject::copyTo( other );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Rect LineRenderer::getRect() {
|
|
|
|
return { leftmost(), topmost(), rightmost() - leftmost(),
|
|
|
|
bottommost() - topmost() };
|
|
|
|
}
|
2021-03-13 17:36:29 +00:00
|
|
|
std::pair< Vec2D< double >, Vec2D< double > >
|
2020-11-21 19:57:40 +00:00
|
|
|
LineRenderer::getDoubleRect() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return { original.getStart(), original.getEnd() - original.getStart() };
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LineRenderer::updateXY() {
|
2021-04-27 13:54:30 +00:00
|
|
|
auto additions = computeAlignmentAdditions();
|
|
|
|
current = { original.getStart() + additions,
|
|
|
|
original.getEnd() + additions };
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
} // namespace SDLPP
|