Tetris: use new API
This commit is contained in:
parent
b52dee267b
commit
90dc26251b
@ -10,7 +10,7 @@ OUTPUTFLAG = -Fo
|
||||
else
|
||||
UNAME_S := $(shell uname -s)
|
||||
CXX ?= g++
|
||||
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2 -DDEBUG -DFEATURE #-g -fsanitize=address
|
||||
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2 #-g -fsanitize=address
|
||||
OBJEXT = o
|
||||
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
|
||||
OUTPUTFLAG = -o
|
||||
|
@ -279,7 +279,7 @@ std::shared_ptr< TetrisPiece > TetrisPiece::copySelf() {
|
||||
auto ret = std::make_shared< TetrisPiece >();
|
||||
for ( int i = 0; i < 4; i++ ) {
|
||||
auto block = pieces[i]->copyInScene();
|
||||
block->centerX();
|
||||
block->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
ret->addBlockInPos( block, pieces_rel_position[i] );
|
||||
}
|
||||
if ( !rotate_allowed )
|
||||
|
@ -37,9 +37,9 @@ void updateShadow( SDLPP::Scene &scene ) {
|
||||
for ( auto &col : collisions ) {
|
||||
// if collision with g_cur_object, ignore
|
||||
if ( std::find( invalid_objects.begin(), invalid_objects.end(),
|
||||
col ) != invalid_objects.end() )
|
||||
col.second ) != invalid_objects.end() )
|
||||
continue;
|
||||
auto possible_drop = col->getPos().getY() - curY;
|
||||
auto possible_drop = col.second->getPos().getY() - curY;
|
||||
if ( possible_drop < shadow_drop && possible_drop >= 0 )
|
||||
shadow_drop = possible_drop;
|
||||
}
|
||||
@ -141,7 +141,7 @@ int crashFlags( std::shared_ptr< TetrisPiece > piece,
|
||||
auto collisions = scene.getCollisions(
|
||||
*block, { BORDER_LEFT_ID, BORDER_RIGHT_ID, FLOOR_ID } );
|
||||
for ( auto &col : collisions ) {
|
||||
switch ( col->getId() ) {
|
||||
switch ( col.second->getId() ) {
|
||||
case BORDER_LEFT_ID:
|
||||
retFlags |= left;
|
||||
break;
|
||||
@ -157,9 +157,9 @@ int crashFlags( std::shared_ptr< TetrisPiece > piece,
|
||||
collisions = scene.getCollisions( *block, { BRICK_ID } );
|
||||
if ( collisions.size() > 1 ) {
|
||||
for ( auto &col : collisions ) {
|
||||
if ( piece->isLeft( *col ) )
|
||||
if ( piece->isLeft( *col.second ) )
|
||||
retFlags |= left;
|
||||
else if ( piece->isRight( *col ) )
|
||||
else if ( piece->isRight( *col.second ) )
|
||||
retFlags |= right;
|
||||
else
|
||||
retFlags |= bottom;
|
||||
@ -220,7 +220,7 @@ createTetrisBlock( double x, double y, const std::string &color,
|
||||
ret->setOutlineColor( outline );
|
||||
ret->addCollision( SDLPP::RectColider( 0.1, 0.1, 0.8, 0.8 ) );
|
||||
ret->setId( BRICK_ID );
|
||||
ret->centerX();
|
||||
ret->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
scene->addObject( ret );
|
||||
return ret;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
#ifdef FEATURE
|
||||
auto testcircle = std::make_shared< SDLPP::CircleRender >(
|
||||
LEFT_BORDER, 0.2, 0.05, r, "#FF00AA", true );
|
||||
testcircle->centerX();
|
||||
testcircle->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
scene.addObject( testcircle );
|
||||
#endif
|
||||
|
||||
@ -43,7 +43,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
colider->addCollision( SDLPP::RectColider( 0.01, 0.1, 0.98, 0.8 ) );
|
||||
colider->setId( COLIDER_ID );
|
||||
colider->setStatic();
|
||||
colider->centerX();
|
||||
colider->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
scene.addObject( colider );
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
auto line = std::make_shared< SDLPP::LineRenderer >(
|
||||
LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
|
||||
line->setStatic();
|
||||
line->centerX();
|
||||
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
line->setId( LINE_ID );
|
||||
scene.addObject( line );
|
||||
}
|
||||
@ -66,7 +66,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r,
|
||||
colors["line"] );
|
||||
line->setStatic();
|
||||
line->centerX();
|
||||
line->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
line->setId( LINE_ID );
|
||||
scene.addObject( line );
|
||||
}
|
||||
@ -74,14 +74,14 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"],
|
||||
true );
|
||||
left_barrier->centerX();
|
||||
left_barrier->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
left_barrier->setStatic();
|
||||
left_barrier->setId( BARRIER_ID );
|
||||
scene.addObject( left_barrier );
|
||||
|
||||
auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
|
||||
RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"], true );
|
||||
right_barrier->centerX();
|
||||
right_barrier->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
right_barrier->setStatic();
|
||||
right_barrier->setId( BARRIER_ID );
|
||||
scene.addObject( right_barrier );
|
||||
@ -89,14 +89,14 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER - 0.02, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER + 0.04,
|
||||
0.02, r, colors["barrier"], true );
|
||||
bottom_barrier->centerX();
|
||||
bottom_barrier->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
bottom_barrier->setStatic();
|
||||
bottom_barrier->setId( BARRIER_ID );
|
||||
scene.addObject( bottom_barrier );
|
||||
|
||||
auto tetris = std::make_shared< SDLPP::TextRenderer >(
|
||||
0.4, 0, 0.2, 0.1, r, "TETRIS", g_font_config );
|
||||
tetris->centerX();
|
||||
tetris->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
tetris->setStatic();
|
||||
tetris->setId( TEXT_ID );
|
||||
scene.addObject( tetris );
|
||||
@ -104,7 +104,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
auto next = std::make_shared< SDLPP::TextRenderer >(
|
||||
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, "NEXT", g_font_config,
|
||||
SDLPP_TEXT_CENTER );
|
||||
next->centerX();
|
||||
next->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
next->setStatic();
|
||||
next->setId( TEXT_ID );
|
||||
scene.addObject( next );
|
||||
@ -123,7 +123,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
auto score_text = std::make_shared< SDLPP::TextRenderer >(
|
||||
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, "SCORE", g_font_config,
|
||||
SDLPP_TEXT_CENTER );
|
||||
score_text->centerX();
|
||||
score_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
score_text->setStatic();
|
||||
score_text->setId( TEXT_ID );
|
||||
scene.addObject( score_text );
|
||||
@ -131,7 +131,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
auto score_texture = std::make_shared< SDLPP::TextRenderer >(
|
||||
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, "0", g_font_config,
|
||||
SDLPP_TEXT_TOP );
|
||||
score_texture->centerX();
|
||||
score_texture->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
score_texture->setStatic();
|
||||
score_texture->setId( SCORE_TEXTURE_ID );
|
||||
scene.addObject( score_texture );
|
||||
@ -140,7 +140,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
LEFT_BORDER - 1, 0, 1, BOTTOM_BORDER, r );
|
||||
border->setId( BORDER_LEFT_ID );
|
||||
border->setStatic();
|
||||
border->centerX();
|
||||
border->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
border->addCollision( SDLPP::RectColider( 0, 0, 0.99, 1 ) );
|
||||
border->setColiderColor( "#FF00FF" );
|
||||
scene.addObject( border );
|
||||
@ -149,7 +149,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
BOTTOM_BORDER, r );
|
||||
border->setId( BORDER_RIGHT_ID );
|
||||
border->setStatic();
|
||||
border->centerX();
|
||||
border->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
border->addCollision( SDLPP::RectColider( 0.01, 0, 1, 1 ) );
|
||||
border->setColiderColor( "#FF00FF" );
|
||||
scene.addObject( border );
|
||||
@ -158,7 +158,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
|
||||
LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER, 1, r );
|
||||
floor->setId( FLOOR_ID );
|
||||
floor->setStatic();
|
||||
floor->centerX();
|
||||
floor->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
floor->addCollision( SDLPP::RectColider( 0, 0.01, 1, 1 ) );
|
||||
floor->setColiderColor( "#00FF00" );
|
||||
scene.addObject( floor );
|
||||
@ -174,34 +174,34 @@ void addMenuSceneItems( SDLPP::Scene &scene,
|
||||
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
|
||||
y->setText( "PAUSED", g_font_config );
|
||||
y->setId( MENU_TEXT_ID );
|
||||
y->centerX();
|
||||
y->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
scene.addObject( y );
|
||||
auto resume =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.46, 0.2, 0.08, r );
|
||||
resume->setText( "Resume", g_font_config );
|
||||
resume->setColor( colors["menu_item_background"] );
|
||||
resume->centerX();
|
||||
resume->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
resume->setId( MENU_ITEM_ID );
|
||||
g_menu_options.push_back( resume );
|
||||
scene.addObject( resume );
|
||||
auto options =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.56, 0.2, 0.08, r );
|
||||
options->setText( "Options", g_font_config );
|
||||
options->centerX();
|
||||
options->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
options->setId( MENU_ITEM_ID );
|
||||
g_menu_options.push_back( options );
|
||||
scene.addObject( options );
|
||||
auto restart =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.66, 0.2, 0.08, r );
|
||||
restart->setText( "Restart", g_font_config );
|
||||
restart->centerX();
|
||||
restart->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
restart->setId( MENU_ITEM_ID );
|
||||
g_menu_options.push_back( restart );
|
||||
scene.addObject( restart );
|
||||
auto quit =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.76, 0.3, 0.08, r );
|
||||
quit->setText( "Quit Game", g_font_config );
|
||||
quit->centerX();
|
||||
quit->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
quit->setId( MENU_ITEM_ID );
|
||||
g_menu_options.push_back( quit );
|
||||
scene.addObject( quit );
|
||||
@ -217,12 +217,12 @@ void addGameOverSceneItems( SDLPP::Scene &scene,
|
||||
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
|
||||
y->setText( "GAME OVER", g_font_config );
|
||||
y->setId( 0 );
|
||||
y->centerX();
|
||||
y->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
scene.addObject( y );
|
||||
auto restart =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
|
||||
restart->setText( "Restart", g_font_config );
|
||||
restart->centerX();
|
||||
restart->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
restart->setColor( colors["menu_item_background"] );
|
||||
restart->setId( MENU_ITEM_ID );
|
||||
g_game_over_options.push_back( restart );
|
||||
@ -230,7 +230,7 @@ void addGameOverSceneItems( SDLPP::Scene &scene,
|
||||
auto quit =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.7, 0.3, 0.1, r );
|
||||
quit->setText( "Quit Game", g_font_config );
|
||||
quit->centerX();
|
||||
quit->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
quit->setId( MENU_ITEM_ID );
|
||||
g_game_over_options.push_back( quit );
|
||||
scene.addObject( quit );
|
||||
@ -246,14 +246,14 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
|
||||
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
|
||||
y->setText( "OPTIONS", g_font_config );
|
||||
y->setId( 0 );
|
||||
y->centerX();
|
||||
y->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
scene.addObject( y );
|
||||
auto color_scheme =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.18, 0.35, 0.64, 0.09, r );
|
||||
color_scheme->setText( "Color scheme: " +
|
||||
color_schemes_names[selected_color_scheme],
|
||||
g_font_config );
|
||||
color_scheme->centerX();
|
||||
color_scheme->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
color_scheme->setColor( colors["menu_item_background"] );
|
||||
color_scheme->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( color_scheme );
|
||||
@ -261,21 +261,21 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
|
||||
auto shadow =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.26, 0.45, 0.48, 0.09, r );
|
||||
shadow->setText( "Show shadow: YES", g_font_config );
|
||||
shadow->centerX();
|
||||
shadow->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
shadow->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( shadow );
|
||||
scene.addObject( shadow );
|
||||
auto show3d =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.2, 0.55, 0.6, 0.09, r );
|
||||
show3d->setText( "Show block texture: NO", g_font_config );
|
||||
show3d->centerX();
|
||||
show3d->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
show3d->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( show3d );
|
||||
scene.addObject( show3d );
|
||||
auto save =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.42, 0.65, 0.16, 0.09, r );
|
||||
save->setText( "SAVE", g_font_config );
|
||||
save->centerX();
|
||||
save->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
save->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( save );
|
||||
scene.addObject( save );
|
||||
@ -455,7 +455,7 @@ void mainSceneInput(
|
||||
while ( collisions.size() == 10 ) {
|
||||
lines += 1;
|
||||
for ( auto &col : collisions ) {
|
||||
col->destroy();
|
||||
col.second->destroy();
|
||||
}
|
||||
auto colider_y = colider->getPos().getY();
|
||||
for ( auto &elem : scene->getObjects( { BRICK_ID } ) ) {
|
||||
|
@ -42,7 +42,7 @@ void prepareShadowColider( std::shared_ptr< SDLPP::Renderer > r ) {
|
||||
SDLPP::RectColider( 0.1, 0.01, 0.8, 0.98 ) );
|
||||
g_shadow_colider->setId( COLIDER_ID );
|
||||
g_shadow_colider->setStatic();
|
||||
g_shadow_colider->centerX();
|
||||
g_shadow_colider->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
Loading…
Reference in New Issue
Block a user