124 lines
4.5 KiB
C++
124 lines
4.5 KiB
C++
#include <cmath>
|
|
#include "sdlpp_linerenderer.hpp"
|
|
|
|
namespace SDLPP {
|
|
LineRenderer::LineRenderer( double x1, double y1, double x2, double y2,
|
|
const std::shared_ptr< Renderer > &r )
|
|
: 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 )
|
|
: RenderObject( r ) {
|
|
original = current = line;
|
|
updateSizeAndPosition();
|
|
}
|
|
LineRenderer::LineRenderer( const Line< double > &line,
|
|
const std::shared_ptr< Renderer > &r,
|
|
const std::string &color )
|
|
: LineRenderer( line, r ) {
|
|
setColor( color );
|
|
}
|
|
void LineRenderer::setColor( const std::string &color ) {
|
|
_color = getSDLColorHEX( color );
|
|
}
|
|
void LineRenderer::render() {
|
|
if ( !getHidden() ) {
|
|
SDL_SetRenderDrawColor( renderer->getRendererPtr(), _color.r, _color.g,
|
|
_color.b, _color.a );
|
|
SDL_RenderDrawLine(
|
|
renderer->getRendererPtr(), pixel_line.getStart().getX(),
|
|
pixel_line.getStart().getY(), pixel_line.getEnd().getX(),
|
|
pixel_line.getEnd().getY() );
|
|
}
|
|
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
|
|
for ( const auto &col : getCollisions() )
|
|
col->render( *renderer, colider_color );
|
|
}
|
|
}
|
|
void LineRenderer::setPos( double x, double y ) {
|
|
setPos( Vec2D< double >( x, y ) );
|
|
}
|
|
void LineRenderer::setPos( const std::pair< double, double > &pos ) {
|
|
setPos( Vec2D< double >( pos.first, pos.second ) );
|
|
}
|
|
void LineRenderer::setPos( const Vec2D< double > &vec ) {
|
|
auto diff = original.getEnd() - original.getStart();
|
|
original = { vec, vec + diff };
|
|
updateSizeAndPosition();
|
|
}
|
|
Vec2D< double > LineRenderer::getPos() const {
|
|
return original.getStart();
|
|
}
|
|
int LineRenderer::leftmost() {
|
|
return pixel_line.getStart().getX() < pixel_line.getEnd().getX()
|
|
? pixel_line.getStart().getX()
|
|
: pixel_line.getEnd().getX();
|
|
}
|
|
int LineRenderer::topmost() {
|
|
return pixel_line.getStart().getY() < pixel_line.getEnd().getY()
|
|
? pixel_line.getStart().getY()
|
|
: pixel_line.getEnd().getY();
|
|
}
|
|
int LineRenderer::rightmost() {
|
|
return pixel_line.getStart().getX() > pixel_line.getEnd().getX()
|
|
? pixel_line.getStart().getX()
|
|
: pixel_line.getEnd().getX();
|
|
}
|
|
int LineRenderer::bottommost() {
|
|
return pixel_line.getStart().getY() > pixel_line.getEnd().getY()
|
|
? pixel_line.getStart().getY()
|
|
: pixel_line.getEnd().getY();
|
|
}
|
|
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();
|
|
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 ) ) );
|
|
for ( auto &x : collisions ) {
|
|
x->updateCollision( collisionPushX(), collisionPushY(),
|
|
collisionWidth(), collisionHeight(), getId() );
|
|
}
|
|
}
|
|
std::shared_ptr< RenderObject > LineRenderer::copySelf() {
|
|
auto ret = std::make_shared< LineRenderer >( *this );
|
|
copyTo( ret );
|
|
return ret;
|
|
}
|
|
void LineRenderer::copyTo( std::shared_ptr< RenderObject > other ) {
|
|
RenderObject::copyTo( other );
|
|
}
|
|
|
|
SDL_Rect LineRenderer::getRect() {
|
|
return { leftmost(), topmost(), rightmost() - leftmost(),
|
|
bottommost() - topmost() };
|
|
}
|
|
std::pair< Vec2D< double >, Vec2D< double > >
|
|
LineRenderer::getDoubleRect() const {
|
|
return { original.getStart(), original.getEnd() - original.getStart() };
|
|
}
|
|
|
|
void LineRenderer::updateXY() {
|
|
auto additions = computeAlignmentAdditions();
|
|
current = { original.getStart() + additions,
|
|
original.getEnd() + additions };
|
|
}
|
|
} // namespace SDLPP
|