TETRIS: Show shadow of where the piece will fall

This commit is contained in:
2020-09-10 16:50:36 +02:00
parent 393b975dbe
commit 625e7866a4
2 changed files with 303 additions and 56 deletions
+51 -8
View File
@@ -1,3 +1,4 @@
// TODO mutex guard instead of lock/unlock
#ifndef SDLPP_HPP
#define SDLPP_HPP
@@ -293,7 +294,7 @@ class Scene;
class RenderObject {
public:
RenderObject( std::shared_ptr< Renderer > &r ) : renderer( r ) {}
RenderObject( const std::shared_ptr< Renderer > &r ) : renderer( r ) {}
virtual ~RenderObject() {}
virtual void render() = 0;
virtual int leftmost() = 0;
@@ -307,8 +308,9 @@ public:
virtual void specialAction( int code ) = 0;
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() = 0;
getDoubleRect() const = 0;
virtual void setPos( double x, double y ) = 0;
virtual void setPos(const std::pair<double, double> &pos) = 0;
virtual std::pair< double, double > getPos() const = 0;
bool colidesWith( const RenderObject &other ) const {
if ( !hasCollisions() || !other.hasCollisions() || getHidden() ||
@@ -336,7 +338,7 @@ public:
getCollisions() const {
return collisions;
}
virtual void setTexture( std::shared_ptr< Texture > &t ) {
virtual void setTexture( const std::shared_ptr< Texture > &t ) {
texture = t;
}
virtual void setTexture( const std::string &img_path ) {
@@ -421,6 +423,9 @@ public:
void setStatic(bool stat = true) {
is_static = stat;
}
std::shared_ptr<Renderer> getRenderer() const {
return renderer;
}
protected:
std::vector< std::shared_ptr< CollisionPolygon > > collisions;
@@ -475,6 +480,36 @@ public:
}
render_mutex.unlock();
}
void setZIndex( const std::shared_ptr< RenderObject > &obj, int index ) {
std::lock_guard<std::mutex> guard(render_mutex);
int original_index = 0;
for(long unsigned int i = 0; i < render_objects.size(); i++) {
if(render_objects[i] == obj) {
original_index = i;
}
}
if(original_index == index)
return;
if(original_index > index)
original_index++;
render_objects.insert(render_objects.begin() + index, obj);
render_objects.erase(render_objects.begin() + original_index);
}
void moveDownZ( const std::shared_ptr<RenderObject> &obj ) {
moveZ(obj, -1);
}
void moveUpZ( const std::shared_ptr<RenderObject> &obj ) {
moveZ(obj, 1);
}
void moveZ( const std::shared_ptr<RenderObject> &obj, int addition ) {
int original_index = 0;
for(long unsigned int i = 0; i < render_objects.size(); i++) {
if(render_objects[i] == obj) {
original_index = i;
}
}
std::iter_swap(render_objects.begin() + original_index, render_objects.begin() + original_index + addition);
}
//TODO addCollision
std::shared_ptr< RenderObject > getObject( int index ) {
return render_objects[index];
@@ -847,7 +882,7 @@ public:
RectangleRender() = delete;
virtual ~RectangleRender(){};
RectangleRender( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r )
const std::shared_ptr< Renderer > &r )
: RenderObject( r ) {
og_x = x_ = x;
og_y = y_ = y;
@@ -856,19 +891,20 @@ public:
updateSizeAndPosition();
}
RectangleRender( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Texture > &t )
const std::shared_ptr< Renderer > &r,
const std::shared_ptr< Texture > &t )
: RectangleRender( x, y, w, h, r ) {
setTexture( t );
}
RectangleRender( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r,
const std::shared_ptr< Renderer > &r,
const std::string &img_or_color, bool is_polygon = false )
: RectangleRender( x, y, w, h, r ) {
if ( !is_polygon ) {
setTexture( img_or_color );
} else {
setColor( img_or_color );
color = img_or_color;
}
}
virtual void setColor( const std::string &color ) override {
@@ -924,7 +960,7 @@ public:
virtual void custom_move( int /*UNUSED*/ ) override {}
virtual std::pair< std::pair< double, double >,
std::pair< double, double > >
getDoubleRect() override {
getDoubleRect() const override {
return { { og_x, og_y }, { og_w, og_h } };
}
virtual void setPos( double x, double y ) override {
@@ -932,6 +968,9 @@ public:
og_y = y;
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_x, og_y };
}
@@ -985,6 +1024,9 @@ public:
// TODO ACTUALLY copy, don't just copy pointers to textures and whatnot, create new textures!!!
return std::make_shared<RectangleRender>(*this);
}
std::string getColor() const {
return color;
}
protected:
void updateXY() {
@@ -1014,6 +1056,7 @@ protected:
double h_;
bool centerx = false;
SDL_Rect rect;
std::string color = "";
};
class TextRenderer : public RectangleRender {