Tetris pieces should be shared_ptr
This commit is contained in:
parent
5d18c3b923
commit
07fb33c2dd
360
tetris.cpp
360
tetris.cpp
@ -38,7 +38,7 @@ bool update_score = false;
|
|||||||
bool checked_line = false;
|
bool checked_line = false;
|
||||||
bool wait_for_anim = false;
|
bool wait_for_anim = false;
|
||||||
|
|
||||||
std::vector<int> bag = {28, 28, 28, 28, 28, 28, 28};
|
std::vector< int > bag = { 28, 28, 28, 28, 28, 28, 28 };
|
||||||
|
|
||||||
std::shared_ptr< SDLPP::Font > font;
|
std::shared_ptr< SDLPP::Font > font;
|
||||||
std::shared_ptr< SDLPP::Scene > active_scene;
|
std::shared_ptr< SDLPP::Scene > active_scene;
|
||||||
@ -48,16 +48,17 @@ class TetrisBlock : public SDLPP::RectangleRender {
|
|||||||
public:
|
public:
|
||||||
TetrisBlock() = delete;
|
TetrisBlock() = delete;
|
||||||
TetrisBlock( double x, double y, double w, double h,
|
TetrisBlock( double x, double y, double w, double h,
|
||||||
std::shared_ptr< SDLPP::Renderer > &r,
|
std::shared_ptr< SDLPP::Renderer > &r,
|
||||||
const std::string &img_or_color, bool is_polygon = false,
|
const std::string &img_or_color, bool is_polygon = false,
|
||||||
int index = 0)
|
int index = 0 )
|
||||||
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ) {
|
: RectangleRender( x, y, w, h, r, img_or_color, is_polygon ) {
|
||||||
_index = index;
|
_index = index;
|
||||||
bag[_index]--;
|
bag[_index]--;
|
||||||
}
|
}
|
||||||
~TetrisBlock() {
|
~TetrisBlock() {
|
||||||
bag[_index]++;
|
bag[_index]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _index = 0;
|
int _index = 0;
|
||||||
};
|
};
|
||||||
@ -149,8 +150,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
|
std::vector< std::shared_ptr< SDLPP::RectangleRender > > line_coliders;
|
||||||
TetrisPiece cur_object;
|
std::shared_ptr< TetrisPiece > cur_object;
|
||||||
TetrisPiece next_object;
|
std::shared_ptr< TetrisPiece > next_object;
|
||||||
|
|
||||||
void doInput( std::shared_ptr< SDLPP::Scene > scene );
|
void doInput( std::shared_ptr< SDLPP::Scene > scene );
|
||||||
void doInputPause();
|
void doInputPause();
|
||||||
@ -163,8 +164,8 @@ createTetrisBlock( double x, double y, const std::string &color,
|
|||||||
const std::string &outline, int index,
|
const std::string &outline, int index,
|
||||||
std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
auto ret = std::make_shared< TetrisBlock >(
|
auto ret = std::make_shared< TetrisBlock >( x, y, BLOCK_SIZE, BLOCK_SIZE,
|
||||||
x, y, BLOCK_SIZE, BLOCK_SIZE, renderer, color, true, index );
|
renderer, color, true, index );
|
||||||
ret->setOutlineColor( outline );
|
ret->setOutlineColor( outline );
|
||||||
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
|
ret->addCollision( SDLPP::Rect( 0.1, 0.1, 0.8, 0.8 ) );
|
||||||
ret->setId( BRICK_ID );
|
ret->setId( BRICK_ID );
|
||||||
@ -173,164 +174,184 @@ createTetrisBlock( double x, double y, const std::string &color,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FF0000";
|
auto color = "#FF0000";
|
||||||
auto outline = "#AA0000";
|
auto outline = "#AA0000";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_BRICK, renderer, scene ),
|
outline, TETRIS_BRICK, renderer,
|
||||||
0, 0 );
|
scene ),
|
||||||
retPiece.addPiece(
|
0, 0 );
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_BRICK, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
|
TETRIS_BRICK, renderer, scene ),
|
||||||
|
0, 0 );
|
||||||
|
retPiece->addPiece(
|
||||||
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
|
outline, TETRIS_BRICK, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
outline, TETRIS_BRICK, renderer,
|
||||||
outline, TETRIS_BRICK, renderer, scene ),
|
scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
outline, TETRIS_BRICK, renderer, scene ),
|
|
||||||
0, 0 );
|
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#00FF00";
|
auto color = "#00FF00";
|
||||||
auto outline = "#00AA00";
|
auto outline = "#00AA00";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_T, renderer, scene ),
|
outline, TETRIS_T, renderer, scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_T, renderer, scene ),
|
outline, TETRIS_T, renderer, scene ),
|
||||||
0, 0 );
|
0, 0 );
|
||||||
retPiece.addPiece(
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_T, renderer, scene ),
|
TETRIS_T, renderer, scene ),
|
||||||
0, -1 );
|
0, -1 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_T, renderer, scene ),
|
outline, TETRIS_T, renderer, scene ),
|
||||||
1, 0 );
|
1, 0 );
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#0000FF";
|
auto color = "#0000FF";
|
||||||
auto outline = "#0000AA";
|
auto outline = "#0000AA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
outline, TETRIS_L_RIGHT, renderer, scene ),
|
||||||
-2, 0 );
|
-2, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
outline, TETRIS_L_RIGHT, renderer,
|
||||||
-1, 0 );
|
scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
-1, 0 );
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece(
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
0, 0 );
|
outline, TETRIS_L_RIGHT, renderer, scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
0, 0 );
|
||||||
outline, TETRIS_L_RIGHT, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
||||||
0, -1 );
|
outline, TETRIS_L_RIGHT, renderer,
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
scene ),
|
||||||
|
0, -1 );
|
||||||
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FF00FF";
|
auto color = "#FF00FF";
|
||||||
auto outline = "#AA00AA";
|
auto outline = "#AA00AA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
retPiece->addPiece(
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
||||||
-1, 0 );
|
-1, 0 );
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
outline, TETRIS_Z_RIGHT, renderer,
|
||||||
0, 0 );
|
scene ),
|
||||||
retPiece.addPiece(
|
0, 0 );
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_Z_RIGHT, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
0, -1 );
|
TETRIS_Z_RIGHT, renderer, scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
0, -1 );
|
||||||
outline, TETRIS_Z_RIGHT, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
||||||
1, -1 );
|
outline, TETRIS_Z_RIGHT, renderer,
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
scene ),
|
||||||
|
1, -1 );
|
||||||
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FFFF00";
|
auto color = "#FFFF00";
|
||||||
auto outline = "#AAAA00";
|
auto outline = "#AAAA00";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
|
retPiece->addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
|
||||||
color, outline, TETRIS_LINE, renderer, scene ),
|
color, outline, TETRIS_LINE,
|
||||||
-1, 0 );
|
renderer, scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
-1, 0 );
|
||||||
outline, TETRIS_LINE, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
0, 0 );
|
outline, TETRIS_LINE, renderer,
|
||||||
retPiece.addPiece(
|
scene ),
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_LINE, renderer, scene ),
|
0, 0 );
|
||||||
1, 0 );
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
TETRIS_LINE, renderer, scene ),
|
||||||
outline, TETRIS_LINE, renderer, scene ),
|
1, 0 );
|
||||||
2, 0 );
|
retPiece->addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER, color,
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
outline, TETRIS_LINE, renderer,
|
||||||
|
scene ),
|
||||||
|
2, 0 );
|
||||||
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#00FFFF";
|
auto color = "#00FFFF";
|
||||||
auto outline = "#00AAAA";
|
auto outline = "#00AAAA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
outline, TETRIS_L_LEFT, renderer,
|
||||||
0, -1 );
|
scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
|
0, -1 );
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
retPiece->addPiece(
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
0, 0 );
|
outline, TETRIS_L_LEFT, renderer, scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
0, 0 );
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
1, 0 );
|
outline, TETRIS_L_LEFT, renderer,
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
scene ),
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
1, 0 );
|
||||||
outline, TETRIS_L_LEFT, renderer, scene ),
|
retPiece->addPiece(
|
||||||
2, 0 );
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
outline, TETRIS_L_LEFT, renderer, scene ),
|
||||||
|
2, 0 );
|
||||||
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
TetrisPiece tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
std::shared_ptr< TetrisPiece >
|
||||||
std::shared_ptr< SDLPP::Scene > scene ) {
|
tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||||
TetrisPiece retPiece{};
|
std::shared_ptr< SDLPP::Scene > scene ) {
|
||||||
|
auto retPiece = std::make_shared< TetrisPiece >();
|
||||||
auto color = "#FFFFFF";
|
auto color = "#FFFFFF";
|
||||||
auto outline = "#AAAAAA";
|
auto outline = "#AAAAAA";
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
|
||||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
outline, TETRIS_Z_LEFT, renderer,
|
||||||
-1, 0 );
|
scene ),
|
||||||
retPiece.addPiece(
|
-1, 0 );
|
||||||
createTetrisBlock( 0.5, TOP_BORDER, color, outline, TETRIS_Z_LEFT, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER, color, outline,
|
||||||
0, 0 );
|
TETRIS_Z_LEFT, renderer, scene ),
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
0, 0 );
|
||||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
retPiece->addPiece( createTetrisBlock( 0.5, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
0, 1 );
|
outline, TETRIS_Z_LEFT, renderer,
|
||||||
retPiece.addPiece( createTetrisBlock( 0.5 + BLOCK_SIZE,
|
scene ),
|
||||||
TOP_BORDER + BLOCK_SIZE, color,
|
0, 1 );
|
||||||
outline, TETRIS_Z_LEFT, renderer, scene ),
|
retPiece->addPiece(
|
||||||
1, 1 );
|
createTetrisBlock( 0.5 + BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
|
||||||
retPiece.setDefPos( 0.5, TOP_BORDER );
|
outline, TETRIS_Z_LEFT, renderer, scene ),
|
||||||
|
1, 1 );
|
||||||
|
retPiece->setDefPos( 0.5, TOP_BORDER );
|
||||||
return retPiece;
|
return retPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector< TetrisPiece ( * )( std::shared_ptr< SDLPP::Renderer >,
|
std::vector< std::shared_ptr< TetrisPiece > ( * )(
|
||||||
std::shared_ptr< SDLPP::Scene > ) >
|
std::shared_ptr< SDLPP::Renderer >, std::shared_ptr< SDLPP::Scene > ) >
|
||||||
tetrisFunctions = {
|
tetrisFunctions = {
|
||||||
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
|
tetrisBrick, tetrisT, tetrisLRight, tetrisZRight,
|
||||||
tetrisLine, tetrisLLeft, tetrisZLeft,
|
tetrisLine, tetrisLLeft, tetrisZLeft,
|
||||||
@ -432,7 +453,7 @@ void quitGame() {
|
|||||||
quit = true;
|
quit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkRotation( TetrisPiece &piece, SDLPP::Scene &scene ) {
|
void checkRotation( std::shared_ptr<TetrisPiece> piece, SDLPP::Scene &scene ) {
|
||||||
bool crash = true;
|
bool crash = true;
|
||||||
int left = 0x01;
|
int left = 0x01;
|
||||||
int right = 0x02;
|
int right = 0x02;
|
||||||
@ -441,7 +462,7 @@ void checkRotation( TetrisPiece &piece, SDLPP::Scene &scene ) {
|
|||||||
while ( crash ) {
|
while ( crash ) {
|
||||||
crash = false;
|
crash = false;
|
||||||
flags = 0;
|
flags = 0;
|
||||||
for ( auto &block : piece.getObjects() ) {
|
for ( auto &block : piece->getObjects() ) {
|
||||||
auto pos = block->getPos();
|
auto pos = block->getPos();
|
||||||
if ( pos.first < LEFT_BORDER - 0.01 ) {
|
if ( pos.first < LEFT_BORDER - 0.01 ) {
|
||||||
flags = left;
|
flags = left;
|
||||||
@ -459,9 +480,9 @@ void checkRotation( TetrisPiece &piece, SDLPP::Scene &scene ) {
|
|||||||
}
|
}
|
||||||
if ( crash ) {
|
if ( crash ) {
|
||||||
if ( flags == bottom ) {
|
if ( flags == bottom ) {
|
||||||
piece.revert();
|
piece->revert();
|
||||||
} else {
|
} else {
|
||||||
for ( auto &block : piece.getObjects() ) {
|
for ( auto &block : piece->getObjects() ) {
|
||||||
auto pos = block->getPos();
|
auto pos = block->getPos();
|
||||||
switch ( flags ) {
|
switch ( flags ) {
|
||||||
case 1:
|
case 1:
|
||||||
@ -489,20 +510,22 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
} break;
|
} break;
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
// 0.01 because doubles
|
// 0.01 because doubles
|
||||||
if ( pos.first < ( LEFT_BORDER + 0.01 ) )
|
if ( pos.first < ( LEFT_BORDER + 0.01 ) )
|
||||||
crash = true;
|
crash = true;
|
||||||
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
||||||
if ( collisions.size() > 1 )
|
if ( collisions.size() > 1 )
|
||||||
crash = true;
|
crash = true;
|
||||||
}
|
}
|
||||||
if ( crash ) {
|
if ( crash ) {
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
@ -510,7 +533,9 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
// 0.01 because doubles
|
// 0.01 because doubles
|
||||||
if ( pos.first > RIGHT_BORDER - BLOCK_SIZE - 0.01 ) {
|
if ( pos.first > RIGHT_BORDER - BLOCK_SIZE - 0.01 ) {
|
||||||
@ -518,14 +543,14 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
}
|
}
|
||||||
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
x->setPos( pos.first + BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
auto collisions = scene.getCollisions( *x, { BRICK_ID } );
|
||||||
if ( collisions.size() > 1 ) {
|
if ( collisions.size() > 1 ) {
|
||||||
crash = true;
|
crash = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( crash ) {
|
if ( crash ) {
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
x->setPos( pos.first - BLOCK_SIZE, pos.second );
|
||||||
}
|
}
|
||||||
@ -533,11 +558,15 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
cur_object.startDescend();
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
cur_object->startDescend();
|
||||||
break;
|
break;
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
cur_object.rotate();
|
if(!cur_object)
|
||||||
|
break;
|
||||||
|
cur_object->rotate();
|
||||||
checkRotation( cur_object, scene );
|
checkRotation( cur_object, scene );
|
||||||
break;
|
break;
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
@ -550,7 +579,7 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
|
|
||||||
void handleKeyUp( SDL_Keycode key ) {
|
void handleKeyUp( SDL_Keycode key ) {
|
||||||
if ( key == SDLK_DOWN || key == SDLK_s ) {
|
if ( key == SDLK_DOWN || key == SDLK_s ) {
|
||||||
cur_object.stopDescend();
|
cur_object->stopDescend();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -648,10 +677,10 @@ void pollEventsPause() {
|
|||||||
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
||||||
std::lock_guard< std::mutex > guard( movement_mutex );
|
std::lock_guard< std::mutex > guard( movement_mutex );
|
||||||
ticks_till_fall -= ticks;
|
ticks_till_fall -= ticks;
|
||||||
if ( cur_object.isDescending() )
|
if ( cur_object->isDescending() )
|
||||||
ticks_till_descend -= ticks;
|
ticks_till_descend -= ticks;
|
||||||
if ( ticks_till_fall > 0 ) {
|
if ( ticks_till_fall > 0 ) {
|
||||||
if ( cur_object.isDescending() && ticks_till_descend <= 0 ) {
|
if ( cur_object->isDescending() && ticks_till_descend <= 0 ) {
|
||||||
ticks_till_descend = TICKS_TILL_DESCEND;
|
ticks_till_descend = TICKS_TILL_DESCEND;
|
||||||
goto fall;
|
goto fall;
|
||||||
}
|
}
|
||||||
@ -659,12 +688,12 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
|||||||
}
|
}
|
||||||
ticks_till_fall = TICKS_TILL_FALL;
|
ticks_till_fall = TICKS_TILL_FALL;
|
||||||
fall:
|
fall:
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first, pos.second + BLOCK_SIZE );
|
x->setPos( pos.first, pos.second + BLOCK_SIZE );
|
||||||
}
|
}
|
||||||
bool fell = false;
|
bool fell = false;
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto collisions = scene->getCollisions( *x, { BRICK_ID } );
|
auto collisions = scene->getCollisions( *x, { BRICK_ID } );
|
||||||
if ( collisions.size() > 1 ) {
|
if ( collisions.size() > 1 ) {
|
||||||
fell = true;
|
fell = true;
|
||||||
@ -676,17 +705,17 @@ fall:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( fell ) {
|
if ( fell ) {
|
||||||
for ( auto &x : cur_object.getObjects() ) {
|
for ( auto &x : cur_object->getObjects() ) {
|
||||||
auto pos = x->getPos();
|
auto pos = x->getPos();
|
||||||
x->setPos( pos.first, pos.second - BLOCK_SIZE );
|
x->setPos( pos.first, pos.second - BLOCK_SIZE );
|
||||||
}
|
}
|
||||||
for ( auto &block : cur_object.getObjects() ) {
|
for ( auto &block : cur_object->getObjects() ) {
|
||||||
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
||||||
std::cout << "You lost" << std::endl;
|
std::cout << "You lost" << std::endl;
|
||||||
quitGame();
|
quitGame();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur_object.clear();
|
cur_object->clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -700,7 +729,7 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
|||||||
SDL_framerateDelay( &gFPS );
|
SDL_framerateDelay( &gFPS );
|
||||||
pollEvents( *scene );
|
pollEvents( *scene );
|
||||||
scene->movement();
|
scene->movement();
|
||||||
if ( cur_object.getObjects().size() != 0 ) {
|
if ( cur_object && cur_object->getObjects().size() != 0 ) {
|
||||||
moveThem( scene, SDL_GetTicks() - base );
|
moveThem( scene, SDL_GetTicks() - base );
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -724,8 +753,8 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
|||||||
}
|
}
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
wait_for_anim = true;
|
wait_for_anim = true;
|
||||||
while(wait_for_anim) {
|
while ( wait_for_anim ) {
|
||||||
std::this_thread::sleep_for(0.1s);
|
std::this_thread::sleep_for( 0.1s );
|
||||||
}
|
}
|
||||||
collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
||||||
}
|
}
|
||||||
@ -771,23 +800,25 @@ int main() {
|
|||||||
inputThread.detach();
|
inputThread.detach();
|
||||||
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
next_object = tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
||||||
renderer, main_scene );
|
renderer, main_scene );
|
||||||
next_object.setPos( 0.9, 0.5 );
|
next_object->setPos( 0.9, 0.5 );
|
||||||
while ( !quit ) {
|
while ( !quit ) {
|
||||||
SDL_framerateDelay(&gFPS);
|
SDL_framerateDelay( &gFPS );
|
||||||
if ( cur_object.getObjects().size() == 0 && checked_line ) {
|
if ( (!cur_object || cur_object->getObjects().size() == 0) && checked_line ) {
|
||||||
std::lock_guard< std::mutex > guard( movement_mutex );
|
std::lock_guard< std::mutex > guard( movement_mutex );
|
||||||
|
cur_object.reset();
|
||||||
cur_object = next_object;
|
cur_object = next_object;
|
||||||
cur_object.setPos( 0.5, TOP_BORDER );
|
cur_object->setPos( 0.5, TOP_BORDER );
|
||||||
auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );
|
auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );
|
||||||
int retries = 0;
|
int retries = 0;
|
||||||
while(bag[rand_index] < 4) {
|
while ( bag[rand_index] < 4 ) {
|
||||||
rand_index = (rand_index + 1)%7;
|
rand_index = ( rand_index + 1 ) % 7;
|
||||||
retries++;
|
retries++;
|
||||||
if(retries == 7)
|
if ( retries == 7 )
|
||||||
quitGame();
|
quitGame();
|
||||||
}
|
}
|
||||||
next_object = tetrisFunctions[rand_index](renderer, main_scene);
|
next_object.reset();
|
||||||
next_object.setPos( 0.9, 0.5 );
|
next_object = tetrisFunctions[rand_index]( renderer, main_scene );
|
||||||
|
next_object->setPos( 0.9, 0.5 );
|
||||||
checked_line = false;
|
checked_line = false;
|
||||||
}
|
}
|
||||||
if ( update_score ) {
|
if ( update_score ) {
|
||||||
@ -808,4 +839,5 @@ int main() {
|
|||||||
frames = 0;
|
frames = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user