Split sdlpp into smaller files
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
#include "sdlpp_linerenderer.hpp"
|
||||
|
||||
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::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 ) {
|
||||
setColor( color );
|
||||
}
|
||||
void LineRenderer::setColor( const std::string &color ) {
|
||||
_color = getColorsHEX( color );
|
||||
}
|
||||
void LineRenderer::render() {
|
||||
if ( !getHidden() ) {
|
||||
SDL_SetRenderDrawColor(
|
||||
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 );
|
||||
}
|
||||
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
|
||||
for ( const auto &col : getCollisions() )
|
||||
col->render( *renderer, colider_color );
|
||||
}
|
||||
}
|
||||
void LineRenderer::move( int ticks ) {
|
||||
if ( permanent )
|
||||
return;
|
||||
auto addx =
|
||||
static_cast< double >( movementSpeed * movementDirection.first ) *
|
||||
( static_cast< double >( ticks ) / 1000 );
|
||||
auto addy =
|
||||
static_cast< double >( movementSpeed * movementDirection.second ) *
|
||||
( static_cast< double >( ticks ) / 1000 );
|
||||
if ( std::isnan( addx ) || std::isnan( addy ) )
|
||||
return;
|
||||
|
||||
og_x1 += addx;
|
||||
og_x2 += addx;
|
||||
og_y1 += addy;
|
||||
og_y2 += 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;
|
||||
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 };
|
||||
}
|
||||
int LineRenderer::leftmost() {
|
||||
return pixel_x1 < pixel_x2 ? pixel_x1 : pixel_x2;
|
||||
}
|
||||
int LineRenderer::topmost() {
|
||||
return pixel_y1 < pixel_y2 ? pixel_y1 : pixel_y2;
|
||||
}
|
||||
int LineRenderer::rightmost() {
|
||||
return pixel_x1 > pixel_x2 ? pixel_x1 : pixel_x2;
|
||||
}
|
||||
int LineRenderer::bottommost() {
|
||||
return pixel_y1 > pixel_y2 ? pixel_y1 : pixel_y2;
|
||||
}
|
||||
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_x1 = std::round( x1_ * dimension );
|
||||
pixel_x2 = std::round( x2_ * dimension );
|
||||
pixel_y1 = std::round( y1_ * dimension );
|
||||
pixel_y2 = std::round( y2_ * dimension );
|
||||
for ( auto &x : collisions ) {
|
||||
x->updateCollision( collisionPushX(), collisionPushY(),
|
||||
collisionWidth(), collisionHeight() );
|
||||
}
|
||||
}
|
||||
void LineRenderer::centerX() {
|
||||
centerx = true;
|
||||
updateSizeAndPosition();
|
||||
}
|
||||
std::shared_ptr< RenderObject > LineRenderer::copySelf() {
|
||||
// TODO ACTUALLY copy, don't just copy pointers to textures and whatnot,
|
||||
// create new textures!!!
|
||||
return std::make_shared< LineRenderer >( *this );
|
||||
}
|
||||
|
||||
SDL_Rect LineRenderer::getRect() {
|
||||
return { leftmost(), topmost(), rightmost() - leftmost(),
|
||||
bottommost() - topmost() };
|
||||
}
|
||||
std::pair< std::pair< double, double >, std::pair< double, double > >
|
||||
LineRenderer::getDoubleRect() const {
|
||||
return { { og_x1, og_y1 }, { og_x2 - og_x1, og_y2 - og_y1 } };
|
||||
}
|
||||
|
||||
void LineRenderer::updateXY() {
|
||||
if ( !centerx ) {
|
||||
x1_ = og_x1;
|
||||
y1_ = og_y1;
|
||||
x2_ = og_x2;
|
||||
y2_ = og_y2;
|
||||
return;
|
||||
}
|
||||
auto width = renderer->getWidth();
|
||||
auto height = renderer->getHeight();
|
||||
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;
|
||||
} else {
|
||||
x1_ = og_x1;
|
||||
x2_ = og_x2;
|
||||
}
|
||||
y1_ = og_y1;
|
||||
y2_ = og_y2;
|
||||
}
|
||||
} // namespace SDLPP
|
||||
Reference in New Issue
Block a user