TETRIS: lines

This commit is contained in:
2020-09-11 14:24:54 +02:00
parent c7f5e834bb
commit c7f3e7c741
3 changed files with 206 additions and 19 deletions
+167
View File
@@ -877,6 +877,173 @@ private:
int rad_;
};
class LineRenderer : public RenderObject {
public:
LineRenderer() = delete;
virtual ~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( 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 );
}
virtual void setColor( const std::string &color ) override {
_color = getColorsHEX( color );
}
virtual void specialAction( int /*UNUSED*/ ) override{};
virtual void render() override {
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 );
}
}
virtual void move( int ticks ) override {
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();
}
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual void setPos( double x, double y ) override {
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();
}
virtual void setPos(const std::pair<double, double> &pos) override {
setPos(pos.first, pos.second);
}
virtual std::pair< double, double > getPos() const override {
return { og_x1, og_y1 };
}
virtual int leftmost() override {
return pixel_x1 < pixel_x2 ? pixel_x1 : pixel_x2;
}
virtual int topmost() override {
return pixel_y1 < pixel_y2 ? pixel_y1 : pixel_y2;
}
virtual int rightmost() override {
return pixel_x1 > pixel_x2 ? pixel_x1 : pixel_x2;
}
virtual int bottommost() override {
return pixel_y1 > pixel_y2 ? pixel_y1 : pixel_y2;
}
virtual int collisionPushX() override {
return leftmost();
}
virtual int collisionPushY() override {
return topmost();
}
virtual int collisionWidth() override {
return rightmost() - leftmost();
}
virtual int collisionHeight() override {
return bottommost() - topmost();
}
virtual void updateSizeAndPosition() override {
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);
std::cout << "x1: " << pixel_x1 << ", y1: " << pixel_y1 << ", x2: " << pixel_x2 << ", y2: " << pixel_y2 << std::endl;
for ( auto &x : collisions ) {
x->updateCollision( collisionPushX(), collisionPushY(),
collisionWidth(), collisionHeight() );
}
}
virtual void centerX() override {
centerx = true;
updateSizeAndPosition();
}
virtual std::shared_ptr<RenderObject> copySelf() override {
// TODO ACTUALLY copy, don't just copy pointers to textures and whatnot, create new textures!!!
return std::make_shared<LineRenderer>(*this);
}
virtual SDL_Rect getRect() override {
return {leftmost(), topmost(), rightmost() - leftmost(), bottommost() - topmost()};
}
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() const override {
return {{og_x1, og_y1}, {og_x2 - og_x1, og_y2 - og_y1}};
}
void setOutlineColor( const std::string &/*UNUSED*/ ) override {}
protected:
void 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;
}
double og_x1;
double og_y1;
double x1_;
double y1_;
double og_x2;
double og_y2;
double x2_;
double y2_;
int pixel_x1{};
int pixel_y1{};
int pixel_x2{};
int pixel_y2{};
bool centerx = false;
std::tuple< int, int, int, int > _color;
};
class RectangleRender : public RenderObject {
public:
RectangleRender() = delete;