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