Tetris: fixed when high framerates

This commit is contained in:
zvon 2020-08-23 15:30:45 +02:00
parent aaaf668616
commit 74d8799ab5

View File

@ -14,17 +14,21 @@
#define TOP_BORDER 0.16 #define TOP_BORDER 0.16
#define BLOCK_SIZE 0.04 #define BLOCK_SIZE 0.04
#define TICKS_TILL_FALL 500
#define TICKS_TILL_DESCEND 50
bool pause = false; bool pause = false;
int pause_select = 0; int pause_select = 0;
int pause_max = 1; int pause_max = 1;
int ticks_till_next = 1500; int ticks_till_fall = TICKS_TILL_FALL;
int ticks_till_fall = 500; int ticks_till_descend = TICKS_TILL_DESCEND;
int ticks_till_descend = 50;
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options; std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
std::shared_ptr< SDLPP::TextRenderer > score_texture; std::shared_ptr< SDLPP::TextRenderer > score_texture;
std::shared_ptr< SDLPP::Renderer > active_renderer; std::shared_ptr< SDLPP::Renderer > active_renderer;
int score = 0; int score = 0;
bool update_score = false; bool update_score = false;
bool checked_line = false;
bool wait_for_anim = false;
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;
@ -620,12 +624,12 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
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 = 50; ticks_till_descend = TICKS_TILL_DESCEND;
goto fall; goto fall;
} }
return; return;
} }
ticks_till_fall = 500; 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();
@ -677,7 +681,6 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
auto collisions = scene->getCollisions( *colider, { BRICK_ID } ); auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
while ( collisions.size() == 10 ) { while ( collisions.size() == 10 ) {
score += 10; score += 10;
// updateScore();
update_score = true; update_score = true;
for ( auto &col : collisions ) { for ( auto &col : collisions ) {
col->destroy(); col->destroy();
@ -692,10 +695,14 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
} }
} }
using namespace std::chrono_literals; using namespace std::chrono_literals;
wait_for_anim = true;
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 } );
} }
} }
checked_line = true;
} }
} }
@ -725,7 +732,7 @@ int main() {
pause_scene = std::make_shared< SDLPP::Scene >( renderer ); pause_scene = std::make_shared< SDLPP::Scene >( renderer );
addPause( *pause_scene, renderer ); addPause( *pause_scene, renderer );
int base = SDL_GetTicks(); auto base = SDL_GetTicks();
int frames = 0; int frames = 0;
std::srand( std::time( nullptr ) ); std::srand( std::time( nullptr ) );
@ -738,12 +745,7 @@ int main() {
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 ); if ( cur_object.getObjects().size() == 0 && checked_line ) {
if ( cur_object.getObjects().size() != 0 ) {
ticks_till_next = 1500;
} else {
ticks_till_next -= SDL_GetTicks() - base;
if ( ticks_till_next <= 0 && cur_object.getObjects().size() == 0 ) {
std::lock_guard< std::mutex > guard( movement_mutex ); std::lock_guard< std::mutex > guard( movement_mutex );
cur_object = next_object; cur_object = next_object;
cur_object.setPos( 0.5, TOP_BORDER ); cur_object.setPos( 0.5, TOP_BORDER );
@ -751,7 +753,7 @@ int main() {
tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )]( 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 );
} checked_line = false;
} }
if ( update_score ) { if ( update_score ) {
updateScore(); updateScore();
@ -763,6 +765,7 @@ int main() {
pause_scene->renderScene( false ); pause_scene->renderScene( false );
} }
main_scene->presentScene(); main_scene->presentScene();
wait_for_anim = false;
frames++; frames++;
if ( SDL_GetTicks() - base >= 1000 ) { if ( SDL_GetTicks() - base >= 1000 ) {
base = SDL_GetTicks(); base = SDL_GetTicks();