TETRIS: use new sdlpp library
This commit is contained in:
+92
-84
@@ -1,7 +1,7 @@
|
||||
#ifndef TETRIS_CUSTOM_CLASSES_H
|
||||
#define TETRIS_CUSTOM_CLASSES_H
|
||||
|
||||
#include "../sdlpp.hpp"
|
||||
#include "../sdlpp/sdlpp.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
class TetrisBlock : public SDLPP::RectangleRender {
|
||||
@@ -9,53 +9,61 @@ public:
|
||||
TetrisBlock() = delete;
|
||||
TetrisBlock( double x, double y, double w, double h,
|
||||
const std::shared_ptr< SDLPP::Renderer > &r,
|
||||
const std::string &img_or_color, bool is_polygon,
|
||||
int index, std::shared_ptr<SDLPP::Scene> scene, std::vector<int> &bag )
|
||||
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ), pieces_bag(bag) {
|
||||
const std::string &img_or_color, bool is_polygon, int index,
|
||||
std::shared_ptr< SDLPP::Scene > scene,
|
||||
std::vector< int > &bag )
|
||||
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ),
|
||||
pieces_bag( bag ) {
|
||||
_index = index;
|
||||
pieces_bag[_index]--;
|
||||
_scene = scene;
|
||||
setColors();
|
||||
if(g_show_3d)
|
||||
setTexture("block.png");
|
||||
if ( g_show_3d )
|
||||
setTexture( "block.png" );
|
||||
}
|
||||
TetrisBlock( const TetrisBlock &other ) : TetrisBlock(other.getDoubleRect().first.first,other.getDoubleRect().first.second,other.getDoubleRect().second.first,other.getDoubleRect().second.second,other.getRenderer(), other.getColor(), true, other._index, other._scene, other.pieces_bag) {}
|
||||
TetrisBlock( const TetrisBlock &other )
|
||||
: TetrisBlock( other.getDoubleRect().first.first,
|
||||
other.getDoubleRect().first.second,
|
||||
other.getDoubleRect().second.first,
|
||||
other.getDoubleRect().second.second,
|
||||
other.getRenderer(), other.getColor(), true,
|
||||
other._index, other._scene, other.pieces_bag ) {}
|
||||
~TetrisBlock() {
|
||||
if(_index != PIECE_SHADOW)
|
||||
if ( _index != PIECE_SHADOW )
|
||||
pieces_bag[_index]++;
|
||||
}
|
||||
virtual std::shared_ptr<RenderObject> copySelf() override {
|
||||
return std::make_shared<TetrisBlock>(*this);
|
||||
virtual std::shared_ptr< RenderObject > copySelf() override {
|
||||
return std::make_shared< TetrisBlock >( *this );
|
||||
}
|
||||
std::shared_ptr<TetrisBlock> copyInScene() {
|
||||
auto ret = std::shared_ptr<TetrisBlock>(new TetrisBlock(*this));
|
||||
_scene->addObject(ret);
|
||||
std::shared_ptr< TetrisBlock > copyInScene() {
|
||||
auto ret = std::shared_ptr< TetrisBlock >( new TetrisBlock( *this ) );
|
||||
_scene->addObject( ret );
|
||||
return ret;
|
||||
}
|
||||
bool isSamePos(const SDLPP::RenderObject &other) const {
|
||||
bool isSamePos( const SDLPP::RenderObject &other ) const {
|
||||
auto mypos = getPos();
|
||||
auto otherpos = other.getPos();
|
||||
auto diff1 = mypos.first - otherpos.first;
|
||||
diff1 = (diff1 < 0) * (-1) * diff1 + (diff1 > 0) * diff1;
|
||||
diff1 = ( diff1 < 0 ) * ( -1 ) * diff1 + ( diff1 > 0 ) * diff1;
|
||||
auto diff2 = mypos.second - otherpos.second;
|
||||
diff2 = (diff2 < 0) * (-1) * diff2 + (diff2 > 0) * diff2;
|
||||
diff2 = ( diff2 < 0 ) * ( -1 ) * diff2 + ( diff2 > 0 ) * diff2;
|
||||
return diff1 < 0.0001 && diff2 < 0.0001;
|
||||
}
|
||||
virtual void specialAction( int code ) override {
|
||||
switch(code) {
|
||||
case PIECE_ACTION_UPDATE_COLOR: {
|
||||
setColors();
|
||||
if(g_show_3d)
|
||||
setTexture("block.png");
|
||||
else
|
||||
unsetTexture();
|
||||
}
|
||||
default:
|
||||
break;
|
||||
switch ( code ) {
|
||||
case PIECE_ACTION_UPDATE_COLOR: {
|
||||
setColors();
|
||||
if ( g_show_3d )
|
||||
setTexture( "block.png" );
|
||||
else
|
||||
unsetTexture();
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
void turnIntoShadow() {
|
||||
setId(SHADOW_ID);
|
||||
setId( SHADOW_ID );
|
||||
// shadows don't consume pieces from bag
|
||||
pieces_bag[_index]++;
|
||||
_index = PIECE_SHADOW;
|
||||
@@ -64,36 +72,36 @@ public:
|
||||
|
||||
private:
|
||||
std::string getPieceName() {
|
||||
switch(_index) {
|
||||
case PIECE_BRICK:
|
||||
return "piece_brick";
|
||||
case PIECE_T:
|
||||
return "piece_T";
|
||||
case PIECE_L_RIGHT:
|
||||
return "piece_L_right";
|
||||
case PIECE_Z_RIGHT:
|
||||
return "piece_Z_right";
|
||||
case PIECE_LINE:
|
||||
return "piece_line";
|
||||
case PIECE_L_LEFT:
|
||||
return "piece_L_left";
|
||||
case PIECE_Z_LEFT:
|
||||
return "piece_Z_left";
|
||||
case PIECE_SHADOW:
|
||||
return "shadow";
|
||||
default:
|
||||
break;
|
||||
switch ( _index ) {
|
||||
case PIECE_BRICK:
|
||||
return "piece_brick";
|
||||
case PIECE_T:
|
||||
return "piece_T";
|
||||
case PIECE_L_RIGHT:
|
||||
return "piece_L_right";
|
||||
case PIECE_Z_RIGHT:
|
||||
return "piece_Z_right";
|
||||
case PIECE_LINE:
|
||||
return "piece_line";
|
||||
case PIECE_L_LEFT:
|
||||
return "piece_L_left";
|
||||
case PIECE_Z_LEFT:
|
||||
return "piece_Z_left";
|
||||
case PIECE_SHADOW:
|
||||
return "shadow";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
void setColors() {
|
||||
auto piece_name = getPieceName();
|
||||
setColor(colors[piece_name]);
|
||||
setOutlineColor(colors[piece_name + "_out"]);
|
||||
setColor( colors[piece_name] );
|
||||
setOutlineColor( colors[piece_name + "_out"] );
|
||||
}
|
||||
int _index = 0;
|
||||
std::shared_ptr<SDLPP::Scene> _scene;
|
||||
std::vector<int> &pieces_bag;
|
||||
std::shared_ptr< SDLPP::Scene > _scene;
|
||||
std::vector< int > &pieces_bag;
|
||||
};
|
||||
|
||||
class TetrisPiece {
|
||||
@@ -101,8 +109,7 @@ public:
|
||||
TetrisPiece() {
|
||||
original_pos.reserve( 4 );
|
||||
}
|
||||
void addPiece( std::shared_ptr< TetrisBlock > piece, int x,
|
||||
int y ) {
|
||||
void addPiece( std::shared_ptr< TetrisBlock > piece, int x, int y ) {
|
||||
pieces.push_back( piece );
|
||||
pieces_rel_position.push_back( { 0, 0, 0, 0 } );
|
||||
// done this way for SPEEEEEEED
|
||||
@@ -116,7 +123,7 @@ public:
|
||||
pieces_rel_position.back()[3] = ( y > 0 ) * y;
|
||||
}
|
||||
void rotate() {
|
||||
if(!rotate_allowed)
|
||||
if ( !rotate_allowed )
|
||||
return;
|
||||
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
||||
auto &piece = pieces[i];
|
||||
@@ -160,7 +167,7 @@ public:
|
||||
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
|
||||
auto &piece = pieces[i];
|
||||
auto &positions = pieces_rel_position[i];
|
||||
std::pair<double, double> pos = {x, y};
|
||||
std::pair< double, double > pos = { x, y };
|
||||
pos.first -= positions[0] * BLOCK_SIZE;
|
||||
pos.first += positions[1] * BLOCK_SIZE;
|
||||
pos.second -= positions[2] * BLOCK_SIZE;
|
||||
@@ -168,10 +175,10 @@ public:
|
||||
piece->setPos( pos.first, pos.second );
|
||||
}
|
||||
}
|
||||
void setPos(const std::pair<double,double> &pos) {
|
||||
setPos(pos.first, pos.second);
|
||||
void setPos( const std::pair< double, double > &pos ) {
|
||||
setPos( pos.first, pos.second );
|
||||
}
|
||||
std::pair<double, double> getPos() {
|
||||
std::pair< double, double > getPos() {
|
||||
auto &piece = pieces[0];
|
||||
auto &relpositions = pieces_rel_position[0];
|
||||
auto pos = piece->getPos();
|
||||
@@ -203,13 +210,13 @@ public:
|
||||
bool isMoving() {
|
||||
return userMovement > 0;
|
||||
}
|
||||
bool isLeft(const SDLPP::RenderObject &block) const {
|
||||
return isPosition(block, 0);
|
||||
bool isLeft( const SDLPP::RenderObject &block ) const {
|
||||
return isPosition( block, 0 );
|
||||
}
|
||||
bool isRight(const SDLPP::RenderObject &block) const {
|
||||
return isPosition(block, 1);
|
||||
bool isRight( const SDLPP::RenderObject &block ) const {
|
||||
return isPosition( block, 1 );
|
||||
}
|
||||
void movePiece(double x, double y) {
|
||||
void movePiece( double x, double y ) {
|
||||
for ( auto &block : getObjects() ) {
|
||||
auto pos = block->getPos();
|
||||
block->setPos( pos.first + x, pos.second + y );
|
||||
@@ -220,38 +227,38 @@ public:
|
||||
}
|
||||
|
||||
void turnIntoShadow() {
|
||||
for(auto &block : getObjects() )
|
||||
for ( auto &block : getObjects() )
|
||||
block->turnIntoShadow();
|
||||
setHidden(!g_show_shadow);
|
||||
setHidden( !g_show_shadow );
|
||||
}
|
||||
std::shared_ptr<TetrisPiece> copySelf() {
|
||||
auto ret = std::make_shared<TetrisPiece>();
|
||||
for(int i = 0; i < 4; i++) {
|
||||
std::shared_ptr< TetrisPiece > copySelf() {
|
||||
auto ret = std::make_shared< TetrisPiece >();
|
||||
for ( int i = 0; i < 4; i++ ) {
|
||||
auto block = pieces[i]->copyInScene();
|
||||
block->centerX();
|
||||
ret->addBlockInPos(block, pieces_rel_position[i]);
|
||||
ret->addBlockInPos( block, pieces_rel_position[i] );
|
||||
}
|
||||
if(!rotate_allowed)
|
||||
if ( !rotate_allowed )
|
||||
ret->disableRotation();
|
||||
ret->setHidden(_hidden);
|
||||
ret->setHidden( _hidden );
|
||||
return ret;
|
||||
}
|
||||
void destroy() {
|
||||
for(auto &x : getObjects()) {
|
||||
for ( auto &x : getObjects() ) {
|
||||
x->destroy();
|
||||
}
|
||||
}
|
||||
void addMovement(int x, int y) {
|
||||
void addMovement( int x, int y ) {
|
||||
movement.first += x;
|
||||
movement.second += y;
|
||||
}
|
||||
std::pair<int,int> getMovement() const {
|
||||
std::pair< int, int > getMovement() const {
|
||||
return movement;
|
||||
}
|
||||
void setHidden(bool hidden) {
|
||||
void setHidden( bool hidden ) {
|
||||
_hidden = hidden;
|
||||
for(auto &x : getObjects()) {
|
||||
x->setHidden(hidden);
|
||||
for ( auto &x : getObjects() ) {
|
||||
x->setHidden( hidden );
|
||||
}
|
||||
}
|
||||
bool getHidden() {
|
||||
@@ -259,21 +266,22 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
bool isPosition(const SDLPP::RenderObject &block, int pos) const {
|
||||
for(int i = 0; i < 4; i++) {
|
||||
if(pieces[i]->isSamePos(block)) {
|
||||
bool isPosition( const SDLPP::RenderObject &block, int pos ) const {
|
||||
for ( int i = 0; i < 4; i++ ) {
|
||||
if ( pieces[i]->isSamePos( block ) ) {
|
||||
return pieces_rel_position[i][pos] != 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void resetBlock(int index, std::shared_ptr< TetrisBlock > piece) {
|
||||
piece->setPos(pieces[index]->getPos());
|
||||
void resetBlock( int index, std::shared_ptr< TetrisBlock > piece ) {
|
||||
piece->setPos( pieces[index]->getPos() );
|
||||
pieces[index] = piece;
|
||||
}
|
||||
void addBlockInPos(std::shared_ptr<TetrisBlock> piece, const std::vector<int> &relpos) {
|
||||
void addBlockInPos( std::shared_ptr< TetrisBlock > piece,
|
||||
const std::vector< int > &relpos ) {
|
||||
pieces.push_back( piece );
|
||||
pieces_rel_position.push_back(relpos);
|
||||
pieces_rel_position.push_back( relpos );
|
||||
}
|
||||
std::vector< std::vector< int > > pieces_rel_position;
|
||||
std::vector< std::shared_ptr< TetrisBlock > > pieces;
|
||||
@@ -281,7 +289,7 @@ private:
|
||||
bool descend = false;
|
||||
int userMovement = 0;
|
||||
bool rotate_allowed = true;
|
||||
std::pair<int,int> movement = {0,0};
|
||||
std::pair< int, int > movement = { 0, 0 };
|
||||
bool _hidden = false;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user