Tetris: fixed when high framerates
This commit is contained in:
parent
aaaf668616
commit
74d8799ab5
33
tetris.cpp
33
tetris.cpp
@ -14,17 +14,21 @@
|
||||
#define TOP_BORDER 0.16
|
||||
#define BLOCK_SIZE 0.04
|
||||
|
||||
#define TICKS_TILL_FALL 500
|
||||
#define TICKS_TILL_DESCEND 50
|
||||
|
||||
bool pause = false;
|
||||
int pause_select = 0;
|
||||
int pause_max = 1;
|
||||
int ticks_till_next = 1500;
|
||||
int ticks_till_fall = 500;
|
||||
int ticks_till_descend = 50;
|
||||
int ticks_till_fall = TICKS_TILL_FALL;
|
||||
int ticks_till_descend = TICKS_TILL_DESCEND;
|
||||
std::vector< std::shared_ptr< SDLPP::RectangleRender > > pause_options;
|
||||
std::shared_ptr< SDLPP::TextRenderer > score_texture;
|
||||
std::shared_ptr< SDLPP::Renderer > active_renderer;
|
||||
int score = 0;
|
||||
bool update_score = false;
|
||||
bool checked_line = false;
|
||||
bool wait_for_anim = false;
|
||||
|
||||
std::shared_ptr< SDLPP::Font > font;
|
||||
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;
|
||||
if ( ticks_till_fall > 0 ) {
|
||||
if ( cur_object.isDescending() && ticks_till_descend <= 0 ) {
|
||||
ticks_till_descend = 50;
|
||||
ticks_till_descend = TICKS_TILL_DESCEND;
|
||||
goto fall;
|
||||
}
|
||||
return;
|
||||
}
|
||||
ticks_till_fall = 500;
|
||||
ticks_till_fall = TICKS_TILL_FALL;
|
||||
fall:
|
||||
for ( auto &x : cur_object.getObjects() ) {
|
||||
auto pos = x->getPos();
|
||||
@ -677,7 +681,6 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
||||
auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
||||
while ( collisions.size() == 10 ) {
|
||||
score += 10;
|
||||
// updateScore();
|
||||
update_score = true;
|
||||
for ( auto &col : collisions ) {
|
||||
col->destroy();
|
||||
@ -692,10 +695,14 @@ void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
|
||||
}
|
||||
}
|
||||
using namespace std::chrono_literals;
|
||||
std::this_thread::sleep_for( 0.1s );
|
||||
wait_for_anim = true;
|
||||
while(wait_for_anim) {
|
||||
std::this_thread::sleep_for(0.1s);
|
||||
}
|
||||
collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
||||
}
|
||||
}
|
||||
checked_line = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -725,7 +732,7 @@ int main() {
|
||||
pause_scene = std::make_shared< SDLPP::Scene >( renderer );
|
||||
addPause( *pause_scene, renderer );
|
||||
|
||||
int base = SDL_GetTicks();
|
||||
auto base = SDL_GetTicks();
|
||||
int frames = 0;
|
||||
|
||||
std::srand( std::time( nullptr ) );
|
||||
@ -738,12 +745,7 @@ int main() {
|
||||
renderer, main_scene );
|
||||
next_object.setPos( 0.9, 0.5 );
|
||||
while ( !quit ) {
|
||||
SDL_framerateDelay( &gFPS );
|
||||
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 ) {
|
||||
if ( cur_object.getObjects().size() == 0 && checked_line ) {
|
||||
std::lock_guard< std::mutex > guard( movement_mutex );
|
||||
cur_object = next_object;
|
||||
cur_object.setPos( 0.5, TOP_BORDER );
|
||||
@ -751,7 +753,7 @@ int main() {
|
||||
tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
||||
renderer, main_scene );
|
||||
next_object.setPos( 0.9, 0.5 );
|
||||
}
|
||||
checked_line = false;
|
||||
}
|
||||
if ( update_score ) {
|
||||
updateScore();
|
||||
@ -763,6 +765,7 @@ int main() {
|
||||
pause_scene->renderScene( false );
|
||||
}
|
||||
main_scene->presentScene();
|
||||
wait_for_anim = false;
|
||||
frames++;
|
||||
if ( SDL_GetTicks() - base >= 1000 ) {
|
||||
base = SDL_GetTicks();
|
||||
|
Loading…
Reference in New Issue
Block a user