game/sdlpp/sdlpp_linerenderer.cpp

124 lines
4.5 KiB
C++
Raw Normal View History

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 )
: 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 ) {
original = current = line;
2020-11-21 19:57:40 +00:00
updateSizeAndPosition();
}
LineRenderer::LineRenderer( const Line< double > &line,
2020-11-21 19:57:40 +00:00
const std::shared_ptr< Renderer > &r,
const std::string &color )
: 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 );
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
}
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();
}
Vec2D< double > LineRenderer::getPos() const {
return original.getStart();
2020-11-21 19:57:40 +00:00
}
int LineRenderer::leftmost() {
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() {
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() {
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() {
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();
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() {
auto ret = std::make_shared< LineRenderer >( *this );
copyTo( ret );
return ret;
}
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() };
}
std::pair< Vec2D< double >, Vec2D< double > >
2020-11-21 19:57:40 +00:00
LineRenderer::getDoubleRect() const {
return { original.getStart(), original.getEnd() - original.getStart() };
2020-11-21 19:57:40 +00:00
}
void LineRenderer::updateXY() {
auto additions = computeAlignmentAdditions();
current = { original.getStart() + additions,
original.getEnd() + additions };
2020-11-21 19:57:40 +00:00
}
} // namespace SDLPP