diff --git a/tetris.cpp b/tetris.cpp index 0c6f459..760159b 100644 --- a/tetris.cpp +++ b/tetris.cpp @@ -116,7 +116,15 @@ public: } void revert() { for ( unsigned long i = 0; i < pieces.size(); i++ ) { - pieces[i]->setPos( original_pos[i].first, original_pos[i].second ); + auto &piece = pieces[i]; + auto &positions = pieces_rel_position[i]; + piece->setPos( original_pos[i].first, original_pos[i].second ); + auto top = positions[1]; + auto bottom = positions[0]; + positions[1] = positions[3]; + positions[0] = positions[2]; + positions[2] = top; + positions[3] = bottom; } } std::vector< std::shared_ptr< TetrisBlock > > &getObjects() { @@ -495,26 +503,26 @@ void checkRotation( std::shared_ptr piece, SDLPP::Scene &scene ) { for ( auto &block : piece->getObjects() ) { auto pos = block->getPos(); if ( pos.first < LEFT_BORDER - 0.01 ) { - flags = left; + flags |= left; crash = true; break; } else if ( pos.first > RIGHT_BORDER - BLOCK_SIZE + 0.01 ) { crash = true; - flags = right; + flags |= right; break; } else if ( pos.second >= BOTTOM_BORDER ) { crash = true; - flags = bottom; + flags |= bottom; break; } } if ( crash ) { - if ( flags == bottom ) { + if ( flags & bottom || (flags & left && flags & right) ) { piece->revert(); } else { - if( flags == left ) + if( flags & left ) piece->movePiece(BLOCK_SIZE, 0); - else if( flags == right ) + else if( flags & right ) piece->movePiece(-BLOCK_SIZE, 0); } } @@ -561,9 +569,9 @@ void checkRotation( std::shared_ptr piece, SDLPP::Scene &scene ) { void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { bool crash = false; - std::lock_guard< std::mutex > guard( movement_mutex ); switch ( key ) { - case SDLK_ESCAPE: { + case SDLK_ESCAPE: + { pause = true; pause_scene->updateSizeAndPosition(); std::thread pauseThread( doInputPause ); @@ -623,7 +631,8 @@ void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) { void handleKeyUp( SDL_Keycode key ) { if ( key == SDLK_DOWN || key == SDLK_s ) { - cur_object->stopDescend(); + if(cur_object) + cur_object->stopDescend(); } } @@ -719,7 +728,6 @@ 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() ) ticks_till_descend -= ticks; @@ -753,7 +761,7 @@ fall: quitGame(); } } - cur_object->clear(); + cur_object.reset(); } } @@ -765,13 +773,12 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) { while ( !quit && !pause ) { base = SDL_GetTicks(); SDL_framerateDelay( &gFPS ); + std::lock_guard< std::mutex > guard( movement_mutex ); pollEvents( *scene ); - scene->movement(); - if ( cur_object && cur_object->getObjects().size() != 0 ) { + if ( cur_object ) { moveThem( scene, SDL_GetTicks() - base ); continue; } - std::lock_guard< std::mutex > guard( movement_mutex ); for ( auto &colider : line_coliders ) { auto collisions = scene->getCollisions( *colider, { BRICK_ID } ); while ( collisions.size() == 10 ) { @@ -841,9 +848,8 @@ int main() { next_object->setPos( 0.9, 0.5 ); while ( !quit ) { SDL_framerateDelay( &gFPS ); - if ( (!cur_object || cur_object->getObjects().size() == 0) && checked_line ) { + if ( !cur_object && checked_line ) { std::lock_guard< std::mutex > guard( movement_mutex ); - cur_object.reset(); cur_object = next_object; cur_object->setPos( 0.5, TOP_BORDER ); auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );