TETRIS: Code cleanup
This commit is contained in:
parent
896b5d131f
commit
96c54454f7
@ -1,5 +1,5 @@
|
|||||||
CXX ?= g++
|
CXX ?= g++
|
||||||
CFLAGS ?= -O2 -Wall -Wextra -g
|
CFLAGS ?= -O2 -Wall -Wextra
|
||||||
PREFIX ?= /usr/local/bin
|
PREFIX ?= /usr/local/bin
|
||||||
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
|
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
|
||||||
|
|
||||||
|
@ -5,15 +5,12 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece) {
|
bool validPos(SDLPP::Scene &scene, std::shared_ptr<TetrisPiece> piece) {
|
||||||
auto ret = true;
|
|
||||||
for ( auto &x : piece->getObjects() ) {
|
for ( auto &x : piece->getObjects() ) {
|
||||||
auto collisions = scene.getCollisions( *x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
|
auto collisions = scene.getCollisions( *x, { BRICK_ID, FLOOR_ID, BORDER_LEFT_ID, BORDER_RIGHT_ID } );
|
||||||
if ( collisions.size() > 1 ) {
|
if ( collisions.size() > 1 )
|
||||||
ret = false;
|
return false;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ret;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateShadow(SDLPP::Scene &scene) {
|
void updateShadow(SDLPP::Scene &scene) {
|
||||||
@ -22,47 +19,46 @@ void updateShadow(SDLPP::Scene &scene) {
|
|||||||
g_cur_shadow.reset();
|
g_cur_shadow.reset();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if(!g_cur_shadow)
|
||||||
|
return;
|
||||||
g_cur_shadow->setPos(g_cur_object->getPos());
|
g_cur_shadow->setPos(g_cur_object->getPos());
|
||||||
double shadow_drop = BOTTOM_BORDER;
|
double shadow_drop = BOTTOM_BORDER;
|
||||||
auto &invalid_objects = g_cur_object->getObjects();
|
auto &invalid_objects = g_cur_object->getObjects();
|
||||||
for( auto &x : g_cur_shadow->getObjects() ) {
|
for( auto &x : g_cur_shadow->getObjects() ) {
|
||||||
if(BOTTOM_BORDER - x->getPos().second < shadow_drop)
|
auto block_pos = x->getPos();
|
||||||
shadow_drop = BOTTOM_BORDER - x->getPos().second;
|
if(BOTTOM_BORDER - block_pos.second < shadow_drop)
|
||||||
g_shadow_colider->setPos(x->getPos().first, TOP_BORDER);
|
shadow_drop = BOTTOM_BORDER - block_pos.second;
|
||||||
|
// set colider column's position to current block's X position
|
||||||
|
g_shadow_colider->setPos(block_pos.first, TOP_BORDER);
|
||||||
auto collisions = scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
|
auto collisions = scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
|
||||||
auto curY = x->getPos().second;
|
auto curY = block_pos.second;
|
||||||
for(auto &col : collisions) {
|
for(auto &col : collisions) {
|
||||||
auto colY = col->getPos().second;
|
// if collision with g_cur_object, ignore
|
||||||
if(std::find(invalid_objects.begin(), invalid_objects.end(), col) != invalid_objects.end())
|
if(std::find(invalid_objects.begin(), invalid_objects.end(), col) != invalid_objects.end())
|
||||||
continue;
|
continue;
|
||||||
auto possible_drop = colY - curY;
|
auto possible_drop = col->getPos().second - curY;
|
||||||
if(possible_drop < shadow_drop && possible_drop >= 0)
|
if(possible_drop < shadow_drop && possible_drop >= 0)
|
||||||
shadow_drop = colY - curY;
|
shadow_drop = possible_drop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// we want the shadow to rest on top of the nearest floor
|
||||||
shadow_drop -= BLOCK_SIZE;
|
shadow_drop -= BLOCK_SIZE;
|
||||||
g_cur_shadow->setPos(g_cur_shadow->getPos().first, g_cur_shadow->getPos().second + shadow_drop);
|
auto shadow_pos = g_cur_shadow->getPos();
|
||||||
|
g_cur_shadow->setPos(shadow_pos.first, shadow_pos.second + shadow_drop);
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
||||||
if ( !g_cur_object )
|
if ( !g_cur_object )
|
||||||
return;
|
return;
|
||||||
|
// get movement generated by player
|
||||||
auto movement = g_cur_object->getMovement();
|
auto movement = g_cur_object->getMovement();
|
||||||
|
|
||||||
g_ticks_till_fall -= ticks;
|
g_ticks_till_fall -= ticks;
|
||||||
if ( g_cur_object->isDescending() )
|
if ( g_cur_object->isDescending() )
|
||||||
g_ticks_till_descend -= ticks;
|
g_ticks_till_descend -= ticks;
|
||||||
if ( g_cur_object->isMoving() )
|
if ( g_cur_object->isMoving() )
|
||||||
g_ticks_till_movement -= ticks;
|
g_ticks_till_movement -= ticks;
|
||||||
if ( g_ticks_till_fall > 0 ) {
|
if ( g_ticks_till_fall > 0 ) {
|
||||||
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
|
|
||||||
g_ticks_till_descend = TICKS_TILL_DESCEND;
|
|
||||||
g_cur_object->movePiece(0, movement.second * BLOCK_SIZE);
|
|
||||||
if(!validPos(*scene, g_cur_object)) {
|
|
||||||
g_cur_object->movePiece(0, movement.second * -BLOCK_SIZE);
|
|
||||||
return;
|
|
||||||
} else
|
|
||||||
goto check_floor;
|
|
||||||
}
|
|
||||||
if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) {
|
if ( g_cur_object->isMoving() && g_ticks_till_movement <= 0 ) {
|
||||||
g_ticks_till_movement = TICKS_TILL_MOVE;
|
g_ticks_till_movement = TICKS_TILL_MOVE;
|
||||||
g_cur_object->movePiece(movement.first * BLOCK_SIZE, 0);
|
g_cur_object->movePiece(movement.first * BLOCK_SIZE, 0);
|
||||||
@ -72,6 +68,15 @@ void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
|
|||||||
} else
|
} else
|
||||||
goto check_floor;
|
goto check_floor;
|
||||||
}
|
}
|
||||||
|
if ( g_cur_object->isDescending() && g_ticks_till_descend <= 0 ) {
|
||||||
|
g_ticks_till_descend = TICKS_TILL_DESCEND;
|
||||||
|
g_cur_object->movePiece(0, movement.second * BLOCK_SIZE);
|
||||||
|
if(!validPos(*scene, g_cur_object)) {
|
||||||
|
g_cur_object->movePiece(0, movement.second * -BLOCK_SIZE);
|
||||||
|
return;
|
||||||
|
} else
|
||||||
|
goto check_floor;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
g_ticks_till_fall = TICKS_TILL_FALL;
|
g_ticks_till_fall = TICKS_TILL_FALL;
|
||||||
@ -80,6 +85,7 @@ check_floor:
|
|||||||
bool fell = false;
|
bool fell = false;
|
||||||
for ( auto &x : g_cur_object->getObjects() ) {
|
for ( auto &x : g_cur_object->getObjects() ) {
|
||||||
auto collisions = scene->getCollisions( *x, { BRICK_ID, FLOOR_ID } );
|
auto collisions = scene->getCollisions( *x, { BRICK_ID, FLOOR_ID } );
|
||||||
|
// we get 1 collision every time with ourselves
|
||||||
if ( collisions.size() > 1 ) {
|
if ( collisions.size() > 1 ) {
|
||||||
fell = true;
|
fell = true;
|
||||||
break;
|
break;
|
||||||
@ -89,7 +95,6 @@ check_floor:
|
|||||||
g_cur_object->movePiece(0, -BLOCK_SIZE);
|
g_cur_object->movePiece(0, -BLOCK_SIZE);
|
||||||
for ( auto &block : g_cur_object->getObjects() ) {
|
for ( auto &block : g_cur_object->getObjects() ) {
|
||||||
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
||||||
g_pause = PAUSE_GAME_OVER;
|
|
||||||
g_game_over_scene->updateSizeAndPosition();
|
g_game_over_scene->updateSizeAndPosition();
|
||||||
g_active_scenes.push_back( g_game_over_scene );
|
g_active_scenes.push_back( g_game_over_scene );
|
||||||
g_input_functions.push_back(gameOverSceneInput);
|
g_input_functions.push_back(gameOverSceneInput);
|
||||||
@ -107,7 +112,6 @@ void quitGame() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void resetGame() {
|
void resetGame() {
|
||||||
g_pause = 0;
|
|
||||||
g_cur_object.reset();
|
g_cur_object.reset();
|
||||||
g_checked_line = true;
|
g_checked_line = true;
|
||||||
g_next_object.reset();
|
g_next_object.reset();
|
||||||
@ -116,12 +120,15 @@ void resetGame() {
|
|||||||
g_main_scene->resetScene();
|
g_main_scene->resetScene();
|
||||||
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
||||||
|
|
||||||
|
// TODO maybe move to main thread
|
||||||
g_next_object = g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
g_next_object = g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
||||||
g_main_scene->getRendererShared(), g_main_scene );
|
g_main_scene->getRendererShared(), g_main_scene );
|
||||||
g_next_object->setPos( 0.9, 0.5 );
|
g_next_object->setPos( 0.9, 0.5 );
|
||||||
g_active_scenes.push_back( g_main_scene );
|
g_active_scenes.push_back( g_main_scene );
|
||||||
g_active_scenes = {g_main_scene};
|
g_active_scenes = {g_main_scene};
|
||||||
g_input_functions = {mainSceneInput};
|
g_input_functions = {mainSceneInput};
|
||||||
|
for(int i = 0; i < 7; i++)
|
||||||
|
g_bag[i] = 28;
|
||||||
}
|
}
|
||||||
|
|
||||||
int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock> block, SDLPP::Scene &scene, int left, int right, int bottom) {
|
int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock> block, SDLPP::Scene &scene, int left, int right, int bottom) {
|
||||||
@ -136,7 +143,7 @@ int crashFlags( std::shared_ptr<TetrisPiece> piece, std::shared_ptr<TetrisBlock>
|
|||||||
retFlags |= right;
|
retFlags |= right;
|
||||||
break;
|
break;
|
||||||
case FLOOR_ID:
|
case FLOOR_ID:
|
||||||
retFlags |= right;
|
retFlags |= bottom;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
int g_ticks_till_fall = TICKS_TILL_FALL;
|
int g_ticks_till_fall = TICKS_TILL_FALL;
|
||||||
int g_ticks_till_descend = TICKS_TILL_DESCEND;
|
int g_ticks_till_descend = TICKS_TILL_DESCEND;
|
||||||
int g_ticks_till_movement = TICKS_TILL_MOVE;
|
int g_ticks_till_movement = TICKS_TILL_MOVE;
|
||||||
int g_pause = 0;
|
|
||||||
|
|
||||||
int g_menu_select = 0;
|
int g_menu_select = 0;
|
||||||
int g_menu_max = 3;
|
int g_menu_max = 3;
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
extern int g_ticks_till_fall;
|
extern int g_ticks_till_fall;
|
||||||
extern int g_ticks_till_descend;
|
extern int g_ticks_till_descend;
|
||||||
extern int g_ticks_till_movement;
|
extern int g_ticks_till_movement;
|
||||||
extern int g_pause;
|
|
||||||
|
|
||||||
extern int g_menu_select;
|
extern int g_menu_select;
|
||||||
extern int g_menu_max;
|
extern int g_menu_max;
|
||||||
|
@ -269,7 +269,6 @@ std::shared_ptr<SDLPP::Scene> prepareOptionsScene(std::shared_ptr<SDLPP::Rendere
|
|||||||
void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
g_pause = PAUSE_PAUSE;
|
|
||||||
g_menu_scene->updateSizeAndPosition();
|
g_menu_scene->updateSizeAndPosition();
|
||||||
g_active_scenes.push_back( g_menu_scene );
|
g_active_scenes.push_back( g_menu_scene );
|
||||||
g_input_functions.push_back(menuSceneInput);
|
g_input_functions.push_back(menuSceneInput);
|
||||||
@ -281,7 +280,8 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
g_cur_object->movePiece(-BLOCK_SIZE, 0);
|
g_cur_object->movePiece(-BLOCK_SIZE, 0);
|
||||||
if(!validPos(scene, g_cur_object))
|
if(!validPos(scene, g_cur_object))
|
||||||
g_cur_object->movePiece(BLOCK_SIZE, 0);
|
g_cur_object->movePiece(BLOCK_SIZE, 0);
|
||||||
updateShadow(scene);
|
else
|
||||||
|
updateShadow(scene);
|
||||||
|
|
||||||
g_ticks_till_movement = 2*TICKS_TILL_MOVE;
|
g_ticks_till_movement = 2*TICKS_TILL_MOVE;
|
||||||
g_cur_object->startMovement();
|
g_cur_object->startMovement();
|
||||||
@ -294,7 +294,8 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
g_cur_object->movePiece(BLOCK_SIZE, 0);
|
g_cur_object->movePiece(BLOCK_SIZE, 0);
|
||||||
if(!validPos(scene, g_cur_object))
|
if(!validPos(scene, g_cur_object))
|
||||||
g_cur_object->movePiece(-BLOCK_SIZE, 0);
|
g_cur_object->movePiece(-BLOCK_SIZE, 0);
|
||||||
updateShadow(scene);
|
else
|
||||||
|
updateShadow(scene);
|
||||||
|
|
||||||
g_ticks_till_movement = 2*TICKS_TILL_MOVE;
|
g_ticks_till_movement = 2*TICKS_TILL_MOVE;
|
||||||
g_cur_object->startMovement();
|
g_cur_object->startMovement();
|
||||||
@ -304,6 +305,7 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
if(!g_cur_object)
|
if(!g_cur_object)
|
||||||
break;
|
break;
|
||||||
|
g_ticks_till_descend = 0;
|
||||||
g_cur_object->startDescend();
|
g_cur_object->startDescend();
|
||||||
g_cur_object->addMovement(0,1);
|
g_cur_object->addMovement(0,1);
|
||||||
break;
|
break;
|
||||||
@ -321,9 +323,11 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|||||||
break;
|
break;
|
||||||
g_cur_object->setPos(g_cur_shadow->getPos());
|
g_cur_object->setPos(g_cur_shadow->getPos());
|
||||||
break;
|
break;
|
||||||
|
#ifdef DEBUG
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
scene.getRenderer().setRenderColiders(
|
scene.getRenderer().setRenderColiders(
|
||||||
!scene.getRenderer().getRenderColiders() );
|
!scene.getRenderer().getRenderColiders() );
|
||||||
|
#endif
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -338,7 +342,6 @@ void handleKeyUpMain( SDL_Keycode key ) {
|
|||||||
if(g_cur_object->isDescending()) {
|
if(g_cur_object->isDescending()) {
|
||||||
g_cur_object->stopDescend();
|
g_cur_object->stopDescend();
|
||||||
g_cur_object->addMovement(0,-1);
|
g_cur_object->addMovement(0,-1);
|
||||||
g_ticks_till_descend = TICKS_TILL_DESCEND;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
@ -348,8 +351,6 @@ void handleKeyUpMain( SDL_Keycode key ) {
|
|||||||
if(g_cur_object->isMoving()) {
|
if(g_cur_object->isMoving()) {
|
||||||
g_cur_object->stopMovement();
|
g_cur_object->stopMovement();
|
||||||
g_cur_object->addMovement(1,0);
|
g_cur_object->addMovement(1,0);
|
||||||
if(g_cur_object->isMoving())
|
|
||||||
g_ticks_till_movement = TICKS_TILL_MOVE;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
@ -359,8 +360,6 @@ void handleKeyUpMain( SDL_Keycode key ) {
|
|||||||
if(g_cur_object->isMoving()) {
|
if(g_cur_object->isMoving()) {
|
||||||
g_cur_object->stopDescend();
|
g_cur_object->stopDescend();
|
||||||
g_cur_object->addMovement(-1,0);
|
g_cur_object->addMovement(-1,0);
|
||||||
if(g_cur_object->isMoving())
|
|
||||||
g_ticks_till_movement = TICKS_TILL_MOVE;
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -429,15 +428,16 @@ void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vecto
|
|||||||
void handleKeyDownMenu( SDL_Keycode key ) {
|
void handleKeyDownMenu( SDL_Keycode key ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
case SDLK_ESCAPE: {
|
case SDLK_ESCAPE: {
|
||||||
g_pause = 0;
|
|
||||||
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
||||||
g_active_scenes.pop_back();
|
g_active_scenes.pop_back();
|
||||||
g_input_functions.pop_back();
|
g_input_functions.pop_back();
|
||||||
} break;
|
} break;
|
||||||
|
#ifdef DEBUG
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
g_main_scene->getRenderer().setRenderColiders(
|
g_main_scene->getRenderer().setRenderColiders(
|
||||||
!g_main_scene->getRenderer().getRenderColiders() );
|
!g_main_scene->getRenderer().getRenderColiders() );
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
g_menu_options[g_menu_select]->unsetColor();
|
g_menu_options[g_menu_select]->unsetColor();
|
||||||
@ -455,15 +455,14 @@ void handleKeyDownMenu( SDL_Keycode key ) {
|
|||||||
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
|
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
|
||||||
break;
|
break;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
|
// TODO maybe define macros for these
|
||||||
switch ( g_menu_select ) {
|
switch ( g_menu_select ) {
|
||||||
case 0: {
|
case 0: {
|
||||||
g_pause = 0;
|
|
||||||
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
||||||
g_active_scenes.pop_back();
|
g_active_scenes.pop_back();
|
||||||
g_input_functions.pop_back();
|
g_input_functions.pop_back();
|
||||||
} break;
|
} break;
|
||||||
case 1:
|
case 1:
|
||||||
g_pause = PAUSE_PAUSE;
|
|
||||||
g_options_scene->updateSizeAndPosition();
|
g_options_scene->updateSizeAndPosition();
|
||||||
g_active_scenes.push_back(g_options_scene);
|
g_active_scenes.push_back(g_options_scene);
|
||||||
g_input_functions.push_back(optionsSceneInput);
|
g_input_functions.push_back(optionsSceneInput);
|
||||||
@ -509,10 +508,12 @@ void menuSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
|
|||||||
|
|
||||||
void handleKeyDownGameOver( SDL_Keycode key ) {
|
void handleKeyDownGameOver( SDL_Keycode key ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
|
#ifdef DEBUG
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
g_main_scene->getRenderer().setRenderColiders(
|
g_main_scene->getRenderer().setRenderColiders(
|
||||||
!g_main_scene->getRenderer().getRenderColiders() );
|
!g_main_scene->getRenderer().getRenderColiders() );
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
g_game_over_options[g_game_over_select]->unsetColor();
|
g_game_over_options[g_game_over_select]->unsetColor();
|
||||||
@ -530,6 +531,7 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
|
|||||||
g_game_over_options[g_game_over_select]->setColor( colors["menu_item_background"] );
|
g_game_over_options[g_game_over_select]->setColor( colors["menu_item_background"] );
|
||||||
break;
|
break;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
|
//TODO maybe define macros for these
|
||||||
switch ( g_game_over_select ) {
|
switch ( g_game_over_select ) {
|
||||||
case 0:
|
case 0:
|
||||||
resetGame();
|
resetGame();
|
||||||
@ -574,18 +576,17 @@ void saveOptions() {
|
|||||||
g_update_colors = true;
|
g_update_colors = true;
|
||||||
if(g_cur_shadow)
|
if(g_cur_shadow)
|
||||||
g_cur_shadow->setHidden(!g_show_shadow);
|
g_cur_shadow->setHidden(!g_show_shadow);
|
||||||
g_pause = 0;
|
|
||||||
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
||||||
g_active_scenes.pop_back();
|
|
||||||
g_input_functions.pop_back();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleKeyDownOptions( SDL_Keycode key ) {
|
void handleKeyDownOptions( SDL_Keycode key ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
|
#ifdef DEBUG
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
g_main_scene->getRenderer().setRenderColiders(
|
g_main_scene->getRenderer().setRenderColiders(
|
||||||
!g_main_scene->getRenderer().getRenderColiders() );
|
!g_main_scene->getRenderer().getRenderColiders() );
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
g_options_options[g_options_select]->unsetColor();
|
g_options_options[g_options_select]->unsetColor();
|
||||||
@ -604,6 +605,7 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
|
//TODO define macros
|
||||||
switch( g_options_select ) {
|
switch( g_options_select ) {
|
||||||
case 0:
|
case 0:
|
||||||
selected_color_scheme++;
|
selected_color_scheme++;
|
||||||
@ -622,6 +624,7 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
|
//TODO define macros
|
||||||
switch( g_options_select ) {
|
switch( g_options_select ) {
|
||||||
case 0:
|
case 0:
|
||||||
if(selected_color_scheme == 0)
|
if(selected_color_scheme == 0)
|
||||||
@ -639,9 +642,12 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SDLK_RETURN:
|
case SDLK_RETURN:
|
||||||
|
//TODO define macros
|
||||||
switch ( g_options_select ) {
|
switch ( g_options_select ) {
|
||||||
case 2:
|
case 2:
|
||||||
saveOptions();
|
saveOptions();
|
||||||
|
g_active_scenes.pop_back();
|
||||||
|
g_input_functions.pop_back();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -649,6 +655,8 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
|||||||
break;
|
break;
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
saveOptions();
|
saveOptions();
|
||||||
|
g_active_scenes.pop_back();
|
||||||
|
g_input_functions.pop_back();
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -89,21 +89,29 @@ int main() {
|
|||||||
SDL_framerateDelay( &gFPS );
|
SDL_framerateDelay( &gFPS );
|
||||||
if ( !g_cur_object && g_checked_line ) {
|
if ( !g_cur_object && g_checked_line ) {
|
||||||
std::lock_guard< std::mutex > guard( g_movement_mutex );
|
std::lock_guard< std::mutex > guard( g_movement_mutex );
|
||||||
|
|
||||||
g_cur_object = g_next_object;
|
g_cur_object = g_next_object;
|
||||||
g_cur_object->setPos( 0.5, TOP_BORDER - BLOCK_SIZE );
|
g_cur_object->setPos( 0.5, TOP_BORDER - BLOCK_SIZE );
|
||||||
g_cur_shadow = g_cur_object->copySelf();
|
g_cur_shadow = g_cur_object->copySelf();
|
||||||
g_cur_shadow->turnIntoShadow();
|
g_cur_shadow->turnIntoShadow();
|
||||||
for(auto &piece : g_cur_shadow->getObjects()) {
|
for(auto &piece : g_cur_shadow->getObjects()) {
|
||||||
|
// make it so shadow gets covered by g_cur_object
|
||||||
g_main_scene->moveZ(piece, -4);
|
g_main_scene->moveZ(piece, -4);
|
||||||
}
|
}
|
||||||
updateShadow(*g_main_scene);
|
updateShadow(*g_main_scene);
|
||||||
|
|
||||||
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 ( g_bag[rand_index] < 4 ) {
|
while ( g_bag[rand_index] < 4 ) {
|
||||||
rand_index = ( rand_index + 1 ) % 7;
|
rand_index = ( rand_index + 1 ) % 7;
|
||||||
retries++;
|
retries++;
|
||||||
if ( retries == 7 )
|
// bag is empty
|
||||||
quitGame();
|
if ( retries == 7 ) {
|
||||||
|
g_game_over_scene->updateSizeAndPosition();
|
||||||
|
g_active_scenes.push_back( g_game_over_scene );
|
||||||
|
g_input_functions.push_back(gameOverSceneInput);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
g_next_object.reset();
|
g_next_object.reset();
|
||||||
g_next_object = g_tetrisFunctions[rand_index]( renderer, g_main_scene );
|
g_next_object = g_tetrisFunctions[rand_index]( renderer, g_main_scene );
|
||||||
|
Loading…
Reference in New Issue
Block a user