Trying to switch to more object-oriented
This commit is contained in:
@@ -4,17 +4,21 @@
|
||||
namespace SDLPP {
|
||||
LineRenderer::LineRenderer( double x1, double y1, double x2, double y2,
|
||||
const std::shared_ptr< Renderer > &r )
|
||||
: RenderObject( r ) {
|
||||
og_x1 = x1_ = x1;
|
||||
og_y1 = y1_ = y1;
|
||||
og_x2 = x2_ = x2;
|
||||
og_y2 = y2_ = y2;
|
||||
updateSizeAndPosition();
|
||||
}
|
||||
: 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 ) {
|
||||
: 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 ) {
|
||||
@@ -26,8 +30,10 @@ void LineRenderer::render() {
|
||||
renderer->getRendererPtr(), std::get< 0 >( _color ),
|
||||
std::get< 1 >( _color ), std::get< 2 >( _color ),
|
||||
std::get< 3 >( _color ) );
|
||||
SDL_RenderDrawLine( renderer->getRendererPtr(), pixel_x1, pixel_y1,
|
||||
pixel_x2, pixel_y2 );
|
||||
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() )
|
||||
@@ -46,41 +52,46 @@ void LineRenderer::move( int ticks ) {
|
||||
if ( std::isnan( addx ) || std::isnan( addy ) )
|
||||
return;
|
||||
|
||||
og_x1 += addx;
|
||||
og_x2 += addx;
|
||||
og_y1 += addy;
|
||||
og_y2 += addy;
|
||||
original.add( { addx, addy } );
|
||||
|
||||
custom_move( ticks );
|
||||
|
||||
updateSizeAndPosition();
|
||||
}
|
||||
void LineRenderer::setPos( double x, double y ) {
|
||||
auto diffx = og_x2 - og_x1;
|
||||
auto diffy = og_y2 - og_y1;
|
||||
og_x1 = x;
|
||||
og_y1 = y;
|
||||
og_x2 = og_x1 + diffx;
|
||||
og_y2 = og_y1 + diffy;
|
||||
auto diffx = original.getEnd().getX() - original.getStart().getX();
|
||||
auto diffy = original.getEnd().getY() - original.getStart().getY();
|
||||
original = { { x, y }, { x + diffx, y + diffy } };
|
||||
updateSizeAndPosition();
|
||||
}
|
||||
void LineRenderer::setPos( const std::pair< double, double > &pos ) {
|
||||
setPos( pos.first, pos.second );
|
||||
}
|
||||
std::pair< double, double > LineRenderer::getPos() const {
|
||||
return { og_x1, og_y1 };
|
||||
void LineRenderer::setPos( const Vec2D<double> &vec ) {
|
||||
setPos( vec.getX(), vec.getY() );
|
||||
}
|
||||
Vec2D< double > LineRenderer::getPos() const {
|
||||
return original.getStart();
|
||||
}
|
||||
int LineRenderer::leftmost() {
|
||||
return pixel_x1 < pixel_x2 ? pixel_x1 : pixel_x2;
|
||||
return pixel_line.getStart().getX() < pixel_line.getEnd().getX()
|
||||
? pixel_line.getStart().getX()
|
||||
: pixel_line.getEnd().getX();
|
||||
}
|
||||
int LineRenderer::topmost() {
|
||||
return pixel_y1 < pixel_y2 ? pixel_y1 : pixel_y2;
|
||||
return pixel_line.getStart().getY() < pixel_line.getEnd().getY()
|
||||
? pixel_line.getStart().getY()
|
||||
: pixel_line.getEnd().getY();
|
||||
}
|
||||
int LineRenderer::rightmost() {
|
||||
return pixel_x1 > pixel_x2 ? pixel_x1 : pixel_x2;
|
||||
return pixel_line.getStart().getX() > pixel_line.getEnd().getX()
|
||||
? pixel_line.getStart().getX()
|
||||
: pixel_line.getEnd().getX();
|
||||
}
|
||||
int LineRenderer::bottommost() {
|
||||
return pixel_y1 > pixel_y2 ? pixel_y1 : pixel_y2;
|
||||
return pixel_line.getStart().getY() > pixel_line.getEnd().getY()
|
||||
? pixel_line.getStart().getY()
|
||||
: pixel_line.getEnd().getY();
|
||||
}
|
||||
int LineRenderer::collisionPushX() {
|
||||
return leftmost();
|
||||
@@ -97,10 +108,11 @@ int LineRenderer::collisionHeight() {
|
||||
void LineRenderer::updateSizeAndPosition() {
|
||||
updateXY();
|
||||
auto dimension = renderer->getSmallerSide();
|
||||
pixel_x1 = std::round( x1_ * dimension );
|
||||
pixel_x2 = std::round( x2_ * dimension );
|
||||
pixel_y1 = std::round( y1_ * dimension );
|
||||
pixel_y2 = std::round( y2_ * dimension );
|
||||
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() );
|
||||
@@ -112,42 +124,41 @@ void LineRenderer::centerX() {
|
||||
}
|
||||
std::shared_ptr< RenderObject > LineRenderer::copySelf() {
|
||||
auto ret = std::make_shared< LineRenderer >( *this );
|
||||
copyTo(ret);
|
||||
copyTo( ret );
|
||||
return ret;
|
||||
}
|
||||
void LineRenderer::copyTo(std::shared_ptr<RenderObject> other) {
|
||||
RenderObject::copyTo(other);
|
||||
void LineRenderer::copyTo( std::shared_ptr< RenderObject > other ) {
|
||||
RenderObject::copyTo( other );
|
||||
}
|
||||
|
||||
SDL_Rect LineRenderer::getRect() {
|
||||
return { leftmost(), topmost(), rightmost() - leftmost(),
|
||||
bottommost() - topmost() };
|
||||
}
|
||||
std::pair< std::pair< double, double >, std::pair< double, double > >
|
||||
std::pair< Vec2D<double>, Vec2D<double> >
|
||||
LineRenderer::getDoubleRect() const {
|
||||
return { { og_x1, og_y1 }, { og_x2 - og_x1, og_y2 - og_y1 } };
|
||||
return { original.getStart(), original.getEnd() - original.getStart() };
|
||||
}
|
||||
|
||||
void LineRenderer::updateXY() {
|
||||
if ( !centerx ) {
|
||||
x1_ = og_x1;
|
||||
y1_ = og_y1;
|
||||
x2_ = og_x2;
|
||||
y2_ = og_y2;
|
||||
current = original;
|
||||
return;
|
||||
}
|
||||
auto width = renderer->getWidth();
|
||||
auto height = renderer->getHeight();
|
||||
double x1_, x2_, y1_, y2_;
|
||||
if ( width > height ) {
|
||||
auto multiplier =
|
||||
static_cast< double >( width ) / static_cast< double >( height );
|
||||
x1_ = og_x1 + static_cast< double >( multiplier - 1 ) / 2;
|
||||
x2_ = og_x2 + static_cast< double >( multiplier - 1 ) / 2;
|
||||
x1_ = original.getStart().getX() + static_cast< double >( multiplier - 1 ) / 2;
|
||||
x2_ = original.getEnd().getX() + static_cast< double >( multiplier - 1 ) / 2;
|
||||
} else {
|
||||
x1_ = og_x1;
|
||||
x2_ = og_x2;
|
||||
x1_ = original.getStart().getX();
|
||||
x2_ = original.getEnd().getX();
|
||||
}
|
||||
y1_ = og_y1;
|
||||
y2_ = og_y2;
|
||||
y1_ = original.getStart().getY();
|
||||
y2_ = original.getEnd().getY();
|
||||
current = {{x1_, y1_}, {x2_, y2_}};
|
||||
}
|
||||
} // namespace SDLPP
|
||||
|
||||
Reference in New Issue
Block a user