TETRIS: use new sdlpp library
This commit is contained in:
+243
-179
@@ -20,11 +20,13 @@ constexpr uint64_t OPTIONS_MENU_SAVE = 3;
|
||||
|
||||
// Scene preparation
|
||||
|
||||
void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
||||
colors["background"], true );
|
||||
void addMainSceneItems( SDLPP::Scene &scene,
|
||||
std::shared_ptr< SDLPP::Renderer > &r,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 0, 10, 10, r, colors["background"], true );
|
||||
bg->setPermanent();
|
||||
bg->setId(BACKGROUND_ID);
|
||||
bg->setId( BACKGROUND_ID );
|
||||
scene.addObject( bg );
|
||||
|
||||
// create coliders for counting blocks in line
|
||||
@@ -33,7 +35,7 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
|
||||
posy -= BLOCK_SIZE;
|
||||
auto colider = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER, posy, RIGHT_BORDER - LEFT_BORDER, BLOCK_SIZE, r );
|
||||
colider->addCollision(SDLPP::Rect( 0.01, 0.1, 0.98, 0.8 ));
|
||||
colider->addCollision( SDLPP::RectColider( 0.01, 0.1, 0.98, 0.8 ) );
|
||||
colider->setId( COLIDER_ID );
|
||||
colider->setStatic();
|
||||
colider->centerX();
|
||||
@@ -44,35 +46,39 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
|
||||
posy = 1;
|
||||
for ( int i = 0; i < 20; i++ ) {
|
||||
posy -= BLOCK_SIZE;
|
||||
auto line = std::make_shared< SDLPP::LineRenderer >( LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
|
||||
auto line = std::make_shared< SDLPP::LineRenderer >(
|
||||
LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
|
||||
line->setStatic();
|
||||
line->centerX();
|
||||
line->setId(LINE_ID);
|
||||
line->setId( LINE_ID );
|
||||
scene.addObject( line );
|
||||
}
|
||||
|
||||
auto posx = RIGHT_BORDER;
|
||||
for ( int i = 0; i < 9; i++ ) {
|
||||
posx -= BLOCK_SIZE;
|
||||
auto line = std::make_shared< SDLPP::LineRenderer >( posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r, colors["line"] );
|
||||
auto line = std::make_shared< SDLPP::LineRenderer >(
|
||||
posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r,
|
||||
colors["line"] );
|
||||
line->setStatic();
|
||||
line->centerX();
|
||||
line->setId(LINE_ID);
|
||||
line->setId( LINE_ID );
|
||||
scene.addObject( line );
|
||||
}
|
||||
|
||||
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"], true );
|
||||
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"],
|
||||
true );
|
||||
left_barrier->centerX();
|
||||
left_barrier->setStatic();
|
||||
left_barrier->setId(BARRIER_ID);
|
||||
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->setStatic();
|
||||
right_barrier->setId(BARRIER_ID);
|
||||
right_barrier->setId( BARRIER_ID );
|
||||
scene.addObject( right_barrier );
|
||||
|
||||
auto bottom_barrier = std::make_shared< SDLPP::RectangleRender >(
|
||||
@@ -80,27 +86,29 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
|
||||
0.02, r, colors["barrier"], true );
|
||||
bottom_barrier->centerX();
|
||||
bottom_barrier->setStatic();
|
||||
bottom_barrier->setId(BARRIER_ID);
|
||||
bottom_barrier->setId( BARRIER_ID );
|
||||
scene.addObject( bottom_barrier );
|
||||
|
||||
auto tetris = std::make_shared< SDLPP::TextRenderer >(
|
||||
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"], colors["text_out"], 5 );
|
||||
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"],
|
||||
colors["text_out"], 5 );
|
||||
tetris->centerX();
|
||||
tetris->setStatic();
|
||||
tetris->setId(TEXT_ID);
|
||||
tetris->setId( TEXT_ID );
|
||||
scene.addObject( tetris );
|
||||
|
||||
auto next = std::make_shared< SDLPP::TextRenderer >(
|
||||
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", colors["text"], colors["text_out"], 5, SDLPP_TEXT_CENTER );
|
||||
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", colors["text"],
|
||||
colors["text_out"], 5, SDLPP_TEXT_CENTER );
|
||||
next->centerX();
|
||||
next->setStatic();
|
||||
next->setId(TEXT_ID);
|
||||
next->setId( TEXT_ID );
|
||||
scene.addObject( next );
|
||||
|
||||
// gameover colider
|
||||
auto gameover = std::make_shared< SDLPP::RectangleRender >(
|
||||
0.5, 0, 0, TOP_BORDER + BLOCK_SIZE, r );
|
||||
auto gameover_collision = SDLPP::Rect( -1, 0, -1, 0.9 );
|
||||
auto gameover_collision = SDLPP::RectColider( -1, 0, -1, 0.9 );
|
||||
gameover_collision.setInfinite();
|
||||
gameover->addCollision( gameover_collision );
|
||||
gameover->setId( GAME_OVER );
|
||||
@@ -109,50 +117,55 @@ void addMainSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
|
||||
scene.addObject( gameover );
|
||||
|
||||
auto score_text = std::make_shared< SDLPP::TextRenderer >(
|
||||
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", colors["text"], colors["text_out"], 5, SDLPP_TEXT_CENTER );
|
||||
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", colors["text"],
|
||||
colors["text_out"], 5, SDLPP_TEXT_CENTER );
|
||||
score_text->centerX();
|
||||
score_text->setStatic();
|
||||
score_text->setId(TEXT_ID);
|
||||
score_text->setId( TEXT_ID );
|
||||
scene.addObject( score_text );
|
||||
|
||||
auto score_texture = std::make_shared< SDLPP::TextRenderer >(
|
||||
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, "0",
|
||||
colors["text"], colors["text_out"], 5, SDLPP_TEXT_TOP );
|
||||
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, "0", colors["text"],
|
||||
colors["text_out"], 5, SDLPP_TEXT_TOP );
|
||||
score_texture->centerX();
|
||||
score_texture->setStatic();
|
||||
score_texture->setId(SCORE_TEXTURE_ID);
|
||||
score_texture->setId( SCORE_TEXTURE_ID );
|
||||
scene.addObject( score_texture );
|
||||
|
||||
|
||||
auto border = std::make_shared< SDLPP::RectangleRender >( LEFT_BORDER - 1, 0, 1, BOTTOM_BORDER, r);
|
||||
auto border = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER - 1, 0, 1, BOTTOM_BORDER, r );
|
||||
border->setId( BORDER_LEFT_ID );
|
||||
border->setStatic();
|
||||
border->centerX();
|
||||
border->addCollision(SDLPP::Rect( 0, 0, 0.99, 1));
|
||||
border->setColiderColor("#FF00FF");
|
||||
scene.addObject(border);
|
||||
border->addCollision( SDLPP::RectColider( 0, 0, 0.99, 1 ) );
|
||||
border->setColiderColor( "#FF00FF" );
|
||||
scene.addObject( border );
|
||||
|
||||
border = std::make_shared< SDLPP::RectangleRender >( RIGHT_BORDER, 0, 1, BOTTOM_BORDER, r);
|
||||
border = std::make_shared< SDLPP::RectangleRender >( RIGHT_BORDER, 0, 1,
|
||||
BOTTOM_BORDER, r );
|
||||
border->setId( BORDER_RIGHT_ID );
|
||||
border->setStatic();
|
||||
border->centerX();
|
||||
border->addCollision(SDLPP::Rect( 0.01, 0, 1, 1));
|
||||
border->setColiderColor("#FF00FF");
|
||||
scene.addObject(border);
|
||||
border->addCollision( SDLPP::RectColider( 0.01, 0, 1, 1 ) );
|
||||
border->setColiderColor( "#FF00FF" );
|
||||
scene.addObject( border );
|
||||
|
||||
auto floor = std::make_shared< SDLPP::RectangleRender >( LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER, 1, r);
|
||||
auto floor = std::make_shared< SDLPP::RectangleRender >(
|
||||
LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER, 1, r );
|
||||
floor->setId( FLOOR_ID );
|
||||
floor->setStatic();
|
||||
floor->centerX();
|
||||
floor->addCollision(SDLPP::Rect(0, 0.01, 1, 1));
|
||||
floor->setColiderColor("#00FF00");
|
||||
scene.addObject(floor);
|
||||
floor->addCollision( SDLPP::RectColider( 0, 0.01, 1, 1 ) );
|
||||
floor->setColiderColor( "#00FF00" );
|
||||
scene.addObject( floor );
|
||||
}
|
||||
|
||||
void addMenuSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
||||
colors["menu_background"], true );
|
||||
bg->setId(MENU_BACKGROUND_ID);
|
||||
void addMenuSceneItems( SDLPP::Scene &scene,
|
||||
std::shared_ptr< SDLPP::Renderer > &r,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 0, 10, 10, r, colors["menu_background"], true );
|
||||
bg->setId( MENU_BACKGROUND_ID );
|
||||
bg->setPermanent( true );
|
||||
scene.addObject( bg );
|
||||
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
|
||||
@@ -165,34 +178,37 @@ void addMenuSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer >
|
||||
resume->setText( *font, "Resume", colors["text"], colors["text_out"], 5 );
|
||||
resume->setColor( colors["menu_item_background"] );
|
||||
resume->centerX();
|
||||
resume->setId(MENU_ITEM_ID);
|
||||
g_menu_options.push_back(resume);
|
||||
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(*font, "Options", colors["text"], colors["text_out"], 5);
|
||||
auto options =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.56, 0.2, 0.08, r );
|
||||
options->setText( *font, "Options", colors["text"], colors["text_out"], 5 );
|
||||
options->centerX();
|
||||
options->setId(MENU_ITEM_ID);
|
||||
g_menu_options.push_back(options);
|
||||
scene.addObject(options);
|
||||
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( *font, "Restart", colors["text"], colors["text_out"], 5 );
|
||||
restart->centerX();
|
||||
restart->setId(MENU_ITEM_ID);
|
||||
g_menu_options.push_back(restart);
|
||||
restart->setId( MENU_ITEM_ID );
|
||||
g_menu_options.push_back( restart );
|
||||
scene.addObject( restart );
|
||||
auto quit =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.76, 0.2, 0.08, r );
|
||||
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 5 );
|
||||
quit->centerX();
|
||||
quit->setId(MENU_ITEM_ID);
|
||||
g_menu_options.push_back(quit);
|
||||
quit->setId( MENU_ITEM_ID );
|
||||
g_menu_options.push_back( quit );
|
||||
scene.addObject( quit );
|
||||
}
|
||||
|
||||
void addGameOverSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
||||
colors["menu_background"], true );
|
||||
void addGameOverSceneItems( SDLPP::Scene &scene,
|
||||
std::shared_ptr< SDLPP::Renderer > &r,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 0, 10, 10, r, colors["menu_background"], true );
|
||||
bg->setId( MENU_BACKGROUND_ID );
|
||||
bg->setPermanent( true );
|
||||
scene.addObject( bg );
|
||||
@@ -206,21 +222,23 @@ void addGameOverSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Rendere
|
||||
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 5 );
|
||||
restart->centerX();
|
||||
restart->setColor( colors["menu_item_background"] );
|
||||
restart->setId(MENU_ITEM_ID);
|
||||
g_game_over_options.push_back(restart);
|
||||
restart->setId( MENU_ITEM_ID );
|
||||
g_game_over_options.push_back( restart );
|
||||
scene.addObject( restart );
|
||||
auto quit =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
|
||||
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 5 );
|
||||
quit->centerX();
|
||||
quit->setId(MENU_ITEM_ID);
|
||||
g_game_over_options.push_back(quit);
|
||||
quit->setId( MENU_ITEM_ID );
|
||||
g_game_over_options.push_back( quit );
|
||||
scene.addObject( quit );
|
||||
}
|
||||
|
||||
void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r, std::shared_ptr<SDLPP::Font> font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
|
||||
colors["menu_background"], true );
|
||||
void addOptionsSceneItems( SDLPP::Scene &scene,
|
||||
std::shared_ptr< SDLPP::Renderer > &r,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
||||
0, 0, 10, 10, r, colors["menu_background"], true );
|
||||
bg->setId( MENU_BACKGROUND_ID );
|
||||
bg->setPermanent( true );
|
||||
scene.addObject( bg );
|
||||
@@ -231,56 +249,68 @@ void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer
|
||||
scene.addObject( y );
|
||||
auto color_scheme =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.3, 0.3, 0.09, r );
|
||||
color_scheme->setText( *font, "Color scheme: " + color_schemes_names[selected_color_scheme], colors["text"], colors["text_out"], 5 );
|
||||
color_scheme->setText(
|
||||
*font, "Color scheme: " + color_schemes_names[selected_color_scheme],
|
||||
colors["text"], colors["text_out"], 5 );
|
||||
color_scheme->centerX();
|
||||
color_scheme->setColor( colors["menu_item_background"] );
|
||||
color_scheme->setId(MENU_ITEM_ID);
|
||||
g_options_options.push_back(color_scheme);
|
||||
color_scheme->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( color_scheme );
|
||||
scene.addObject( color_scheme );
|
||||
auto shadow =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.4, 0.2, 0.09, r );
|
||||
shadow->setText( *font, "Show shadow: YES", colors["text"], colors["text_out"], 5 );
|
||||
shadow->setText( *font, "Show shadow: YES", colors["text"],
|
||||
colors["text_out"], 5 );
|
||||
shadow->centerX();
|
||||
shadow->setId(MENU_ITEM_ID);
|
||||
g_options_options.push_back(shadow);
|
||||
scene.addObject(shadow);
|
||||
shadow->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( shadow );
|
||||
scene.addObject( shadow );
|
||||
auto show3d =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.09, r );
|
||||
show3d->setText( *font, "Show block texture: NO", colors["text"], colors["text_out"], 5 );
|
||||
show3d->setText( *font, "Show block texture: NO", colors["text"],
|
||||
colors["text_out"], 5 );
|
||||
show3d->centerX();
|
||||
show3d->setId(MENU_ITEM_ID);
|
||||
g_options_options.push_back(show3d);
|
||||
scene.addObject(show3d);
|
||||
show3d->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( show3d );
|
||||
scene.addObject( show3d );
|
||||
auto save =
|
||||
std::make_shared< SDLPP::TextRenderer >( 0.45, 0.6, 0.1, 0.09, r );
|
||||
save->setText( *font, "SAVE", colors["text"], colors["text_out"], 5 );
|
||||
save->centerX();
|
||||
save->setId(MENU_ITEM_ID);
|
||||
g_options_options.push_back(save);
|
||||
save->setId( MENU_ITEM_ID );
|
||||
g_options_options.push_back( save );
|
||||
scene.addObject( save );
|
||||
}
|
||||
|
||||
std::shared_ptr<SDLPP::Scene> prepareMainScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
|
||||
std::shared_ptr< SDLPP::Scene >
|
||||
prepareMainScene( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
||||
addMainSceneItems( *scene, renderer, font );
|
||||
return scene;
|
||||
}
|
||||
|
||||
std::shared_ptr<SDLPP::Scene> prepareMenuScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
|
||||
std::shared_ptr< SDLPP::Scene >
|
||||
prepareMenuScene( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
||||
addMenuSceneItems(*scene, renderer, font);
|
||||
addMenuSceneItems( *scene, renderer, font );
|
||||
return scene;
|
||||
}
|
||||
|
||||
std::shared_ptr<SDLPP::Scene> prepareGameOverScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
|
||||
std::shared_ptr< SDLPP::Scene >
|
||||
prepareGameOverScene( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
||||
addGameOverSceneItems(*scene, renderer, font);
|
||||
addGameOverSceneItems( *scene, renderer, font );
|
||||
return scene;
|
||||
}
|
||||
|
||||
std::shared_ptr<SDLPP::Scene> prepareOptionsScene(std::shared_ptr<SDLPP::Renderer> renderer, std::shared_ptr<SDLPP::Font> font) {
|
||||
std::shared_ptr< SDLPP::Scene >
|
||||
prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer,
|
||||
std::shared_ptr< SDLPP::Font > font ) {
|
||||
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
||||
addOptionsSceneItems(*scene, renderer, font);
|
||||
addOptionsSceneItems( *scene, renderer, font );
|
||||
return scene;
|
||||
}
|
||||
|
||||
@@ -291,57 +321,57 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
||||
case SDLK_ESCAPE:
|
||||
g_menu_scene->updateSizeAndPosition();
|
||||
g_active_scenes.push_back( g_menu_scene );
|
||||
g_input_functions.push_back(menuSceneInput);
|
||||
g_input_functions.push_back( menuSceneInput );
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
if(!g_cur_object)
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
g_cur_object->movePiece(-BLOCK_SIZE, 0);
|
||||
if(!validPos(scene, g_cur_object))
|
||||
g_cur_object->movePiece(BLOCK_SIZE, 0);
|
||||
g_cur_object->movePiece( -BLOCK_SIZE, 0 );
|
||||
if ( !validPos( scene, g_cur_object ) )
|
||||
g_cur_object->movePiece( BLOCK_SIZE, 0 );
|
||||
else
|
||||
updateShadow(scene);
|
||||
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->addMovement(-1,0);
|
||||
g_cur_object->addMovement( -1, 0 );
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
if(!g_cur_object)
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
g_cur_object->movePiece(BLOCK_SIZE, 0);
|
||||
if(!validPos(scene, g_cur_object))
|
||||
g_cur_object->movePiece(-BLOCK_SIZE, 0);
|
||||
g_cur_object->movePiece( BLOCK_SIZE, 0 );
|
||||
if ( !validPos( scene, g_cur_object ) )
|
||||
g_cur_object->movePiece( -BLOCK_SIZE, 0 );
|
||||
else
|
||||
updateShadow(scene);
|
||||
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->addMovement(1,0);
|
||||
g_cur_object->addMovement( 1, 0 );
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
if(!g_cur_object)
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
g_ticks_till_descend = 0;
|
||||
g_cur_object->startDescend();
|
||||
g_cur_object->addMovement(0,1);
|
||||
g_cur_object->addMovement( 0, 1 );
|
||||
break;
|
||||
case SDLK_UP:
|
||||
case SDLK_w:
|
||||
if(!g_cur_object)
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
g_cur_object->rotate();
|
||||
if( checkRotation( g_cur_object, scene ) )
|
||||
if ( checkRotation( g_cur_object, scene ) )
|
||||
g_cur_shadow->rotate();
|
||||
updateShadow(scene);
|
||||
updateShadow( scene );
|
||||
break;
|
||||
case SDLK_SPACE:
|
||||
if(!g_cur_object)
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
g_cur_object->setPos(g_cur_shadow->getPos());
|
||||
g_cur_object->setPos( g_cur_shadow->getPos() );
|
||||
break;
|
||||
#ifdef DEBUG
|
||||
case SDLK_r:
|
||||
@@ -354,39 +384,38 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
||||
}
|
||||
|
||||
void handleKeyUpMain( SDL_Keycode key ) {
|
||||
switch(key) {
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
if(!g_cur_object)
|
||||
break;
|
||||
if(g_cur_object->isDescending()) {
|
||||
g_cur_object->stopDescend();
|
||||
g_cur_object->addMovement(0,-1);
|
||||
}
|
||||
switch ( key ) {
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
if(!g_cur_object)
|
||||
break;
|
||||
if(g_cur_object->isMoving()) {
|
||||
g_cur_object->stopMovement();
|
||||
g_cur_object->addMovement(1,0);
|
||||
}
|
||||
if ( g_cur_object->isDescending() ) {
|
||||
g_cur_object->stopDescend();
|
||||
g_cur_object->addMovement( 0, -1 );
|
||||
}
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
if(!g_cur_object)
|
||||
break;
|
||||
if(g_cur_object->isMoving()) {
|
||||
g_cur_object->stopDescend();
|
||||
g_cur_object->addMovement(-1,0);
|
||||
}
|
||||
default:
|
||||
if ( g_cur_object->isMoving() ) {
|
||||
g_cur_object->stopMovement();
|
||||
g_cur_object->addMovement( 1, 0 );
|
||||
}
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
if ( !g_cur_object )
|
||||
break;
|
||||
if ( g_cur_object->isMoving() ) {
|
||||
g_cur_object->stopDescend();
|
||||
g_cur_object->addMovement( -1, 0 );
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void pollEventsMain( SDLPP::Scene &scene ) {
|
||||
SDL_Event event;
|
||||
while ( SDLPP::getSDLEvent( event ) ) {
|
||||
@@ -403,7 +432,7 @@ void pollEventsMain( SDLPP::Scene &scene ) {
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||
for(auto &x : g_active_scenes)
|
||||
for ( auto &x : g_active_scenes )
|
||||
x->updateSizeAndPosition();
|
||||
}
|
||||
default:
|
||||
@@ -412,7 +441,9 @@ void pollEventsMain( SDLPP::Scene &scene ) {
|
||||
}
|
||||
}
|
||||
|
||||
void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vector<std::shared_ptr<SDLPP::RenderObject>> &line_coliders ) {
|
||||
void mainSceneInput(
|
||||
std::shared_ptr< SDLPP::Scene > scene, int base,
|
||||
std::vector< std::shared_ptr< SDLPP::RenderObject > > &line_coliders ) {
|
||||
std::lock_guard< std::mutex > guard( g_movement_mutex );
|
||||
pollEventsMain( *scene );
|
||||
if ( g_cur_object ) {
|
||||
@@ -428,7 +459,7 @@ void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vecto
|
||||
col->destroy();
|
||||
}
|
||||
auto colider_y = colider->getPos().second;
|
||||
for ( auto &elem : scene->getObjects({BRICK_ID}) ) {
|
||||
for ( auto &elem : scene->getObjects( { BRICK_ID } ) ) {
|
||||
auto pos = elem->getPos();
|
||||
if ( pos.second < colider_y && pos.first <= RIGHT_BORDER ) {
|
||||
elem->setPos( pos.first, pos.second + BLOCK_SIZE );
|
||||
@@ -442,22 +473,22 @@ void mainSceneInput( std::shared_ptr< SDLPP::Scene > scene, int base, std::vecto
|
||||
collisions = scene->getCollisions( *colider, { BRICK_ID } );
|
||||
}
|
||||
}
|
||||
if(lines > 0) {
|
||||
if ( lines > 0 ) {
|
||||
g_update_score = true;
|
||||
switch(lines) {
|
||||
case 1:
|
||||
g_score += 40;
|
||||
break;
|
||||
case 2:
|
||||
g_score += 100;
|
||||
break;
|
||||
case 3:
|
||||
g_score += 300;
|
||||
break;
|
||||
case 4:
|
||||
g_score += 1200;
|
||||
default:
|
||||
break;
|
||||
switch ( lines ) {
|
||||
case 1:
|
||||
g_score += 40;
|
||||
break;
|
||||
case 2:
|
||||
g_score += 100;
|
||||
break;
|
||||
case 3:
|
||||
g_score += 300;
|
||||
break;
|
||||
case 4:
|
||||
g_score += 1200;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
g_checked_line = true;
|
||||
@@ -480,9 +511,10 @@ void handleKeyDownMenu( SDL_Keycode key ) {
|
||||
case SDLK_DOWN:
|
||||
g_menu_options[g_menu_select]->unsetColor();
|
||||
g_menu_select++;
|
||||
if ( static_cast<size_t>(g_menu_select) >= g_menu_options.size() )
|
||||
if ( static_cast< size_t >( g_menu_select ) >= g_menu_options.size() )
|
||||
g_menu_select = 0;
|
||||
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
|
||||
g_menu_options[g_menu_select]->setColor(
|
||||
colors["menu_item_background"] );
|
||||
break;
|
||||
case SDLK_w:
|
||||
case SDLK_UP:
|
||||
@@ -490,7 +522,8 @@ void handleKeyDownMenu( SDL_Keycode key ) {
|
||||
g_menu_select--;
|
||||
if ( g_menu_select < 0 )
|
||||
g_menu_select = g_menu_options.size() - 1;
|
||||
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
|
||||
g_menu_options[g_menu_select]->setColor(
|
||||
colors["menu_item_background"] );
|
||||
break;
|
||||
case SDLK_RETURN:
|
||||
switch ( g_menu_select ) {
|
||||
@@ -501,12 +534,12 @@ void handleKeyDownMenu( SDL_Keycode key ) {
|
||||
} break;
|
||||
case MAIN_MENU_OPTIONS:
|
||||
g_options_scene->updateSizeAndPosition();
|
||||
g_active_scenes.push_back(g_options_scene);
|
||||
g_input_functions.push_back(optionsSceneInput);
|
||||
g_active_scenes.push_back( g_options_scene );
|
||||
g_input_functions.push_back( optionsSceneInput );
|
||||
break;
|
||||
case MAIN_MENU_RESTART:
|
||||
resetGame();
|
||||
break;
|
||||
break;
|
||||
case MAIN_MENU_QUIT:
|
||||
quitGame();
|
||||
default:
|
||||
@@ -530,7 +563,7 @@ void pollEventsMenu() {
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||
for(auto &x : g_active_scenes)
|
||||
for ( auto &x : g_active_scenes )
|
||||
x->updateSizeAndPosition();
|
||||
}
|
||||
default:
|
||||
@@ -539,7 +572,9 @@ void pollEventsMenu() {
|
||||
}
|
||||
}
|
||||
|
||||
void menuSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/, std::vector<std::shared_ptr<SDLPP::RenderObject>> &/*UNUSED*/ ) {
|
||||
void menuSceneInput(
|
||||
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
|
||||
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
|
||||
pollEventsMenu();
|
||||
}
|
||||
|
||||
@@ -555,9 +590,11 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
|
||||
case SDLK_DOWN:
|
||||
g_game_over_options[g_game_over_select]->unsetColor();
|
||||
g_game_over_select++;
|
||||
if ( static_cast<size_t>(g_game_over_select) >= g_game_over_options.size() )
|
||||
if ( static_cast< size_t >( g_game_over_select ) >=
|
||||
g_game_over_options.size() )
|
||||
g_game_over_select = 0;
|
||||
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;
|
||||
case SDLK_w:
|
||||
case SDLK_UP:
|
||||
@@ -565,13 +602,14 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
|
||||
g_game_over_select--;
|
||||
if ( g_game_over_select < 0 )
|
||||
g_game_over_select = g_game_over_options.size() - 1;
|
||||
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;
|
||||
case SDLK_RETURN:
|
||||
switch ( g_game_over_select ) {
|
||||
case GAME_OVER_RESTART:
|
||||
resetGame();
|
||||
break;
|
||||
break;
|
||||
case GAME_OVER_QUIT:
|
||||
quitGame();
|
||||
default:
|
||||
@@ -595,7 +633,7 @@ void pollEventsGameOver() {
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||
for(auto &x : g_active_scenes)
|
||||
for ( auto &x : g_active_scenes )
|
||||
x->updateSizeAndPosition();
|
||||
}
|
||||
default:
|
||||
@@ -604,14 +642,16 @@ void pollEventsGameOver() {
|
||||
}
|
||||
}
|
||||
|
||||
void gameOverSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/, std::vector<std::shared_ptr<SDLPP::RenderObject>> &/*UNUSED*/ ) {
|
||||
void gameOverSceneInput(
|
||||
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
|
||||
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
|
||||
pollEventsGameOver();
|
||||
}
|
||||
|
||||
void saveOptions() {
|
||||
g_update_colors = true;
|
||||
if(g_cur_shadow)
|
||||
g_cur_shadow->setHidden(!g_show_shadow);
|
||||
if ( g_cur_shadow )
|
||||
g_cur_shadow->setHidden( !g_show_shadow );
|
||||
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
||||
}
|
||||
|
||||
@@ -627,9 +667,11 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
||||
case SDLK_DOWN:
|
||||
g_options_options[g_options_select]->unsetColor();
|
||||
g_options_select++;
|
||||
if ( static_cast<size_t>(g_options_select) >= g_options_options.size() )
|
||||
if ( static_cast< size_t >( g_options_select ) >=
|
||||
g_options_options.size() )
|
||||
g_options_select = 0;
|
||||
g_options_options[g_options_select]->setColor( colors["menu_item_background"] );
|
||||
g_options_options[g_options_select]->setColor(
|
||||
colors["menu_item_background"] );
|
||||
break;
|
||||
case SDLK_w:
|
||||
case SDLK_UP:
|
||||
@@ -637,26 +679,37 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
||||
g_options_select--;
|
||||
if ( g_options_select < 0 )
|
||||
g_options_select = g_options_options.size() - 1;
|
||||
g_options_options[g_options_select]->setColor( colors["menu_item_background"] );
|
||||
g_options_options[g_options_select]->setColor(
|
||||
colors["menu_item_background"] );
|
||||
break;
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
switch( g_options_select ) {
|
||||
switch ( g_options_select ) {
|
||||
case OPTIONS_MENU_COLOR_SCHEME:
|
||||
selected_color_scheme++;
|
||||
if(static_cast<size_t>(selected_color_scheme) >= color_schemes_names.size())
|
||||
if ( static_cast< size_t >( selected_color_scheme ) >=
|
||||
color_schemes_names.size() )
|
||||
selected_color_scheme = 0;
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_COLOR_SCHEME])->changeText("Color scheme: " + color_schemes_names[selected_color_scheme]);
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
||||
g_options_options[OPTIONS_MENU_COLOR_SCHEME] )
|
||||
->changeText( "Color scheme: " +
|
||||
color_schemes_names[selected_color_scheme] );
|
||||
g_update_colors = true;
|
||||
break;
|
||||
case OPTIONS_MENU_SHADOW:
|
||||
g_show_shadow = !g_show_shadow;
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_SHADOW])->changeText(std::string("Show shadow: ") + (g_show_shadow ? "YES" : "NO"));
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
||||
g_options_options[OPTIONS_MENU_SHADOW] )
|
||||
->changeText( std::string( "Show shadow: " ) +
|
||||
( g_show_shadow ? "YES" : "NO" ) );
|
||||
g_update_colors = true;
|
||||
break;
|
||||
case OPTIONS_MENU_3D:
|
||||
g_show_3d = !g_show_3d;
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_3D])->changeText(std::string("Show block texture: ") + (g_show_3d ? "YES" : "NO"));
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
||||
g_options_options[OPTIONS_MENU_3D] )
|
||||
->changeText( std::string( "Show block texture: " ) +
|
||||
( g_show_3d ? "YES" : "NO" ) );
|
||||
g_update_colors = true;
|
||||
default:
|
||||
break;
|
||||
@@ -664,22 +717,31 @@ void handleKeyDownOptions( SDL_Keycode key ) {
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
switch( g_options_select ) {
|
||||
switch ( g_options_select ) {
|
||||
case OPTIONS_MENU_COLOR_SCHEME:
|
||||
if(selected_color_scheme == 0)
|
||||
if ( selected_color_scheme == 0 )
|
||||
selected_color_scheme = color_schemes_names.size();
|
||||
selected_color_scheme--;
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_COLOR_SCHEME])->changeText("Color scheme: " + color_schemes_names[selected_color_scheme]);
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
||||
g_options_options[OPTIONS_MENU_COLOR_SCHEME] )
|
||||
->changeText( "Color scheme: " +
|
||||
color_schemes_names[selected_color_scheme] );
|
||||
g_update_colors = true;
|
||||
break;
|
||||
case OPTIONS_MENU_SHADOW:
|
||||
g_show_shadow = !g_show_shadow;
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_SHADOW])->changeText(std::string("Show shadow: ") + (g_show_shadow ? "YES" : "NO"));
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
||||
g_options_options[OPTIONS_MENU_SHADOW] )
|
||||
->changeText( std::string( "Show shadow: " ) +
|
||||
( g_show_shadow ? "YES" : "NO" ) );
|
||||
g_update_colors = true;
|
||||
break;
|
||||
case OPTIONS_MENU_3D:
|
||||
g_show_3d = !g_show_3d;
|
||||
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[OPTIONS_MENU_3D])->changeText(std::string("Show block texture: ") + (g_show_3d ? "YES" : "NO"));
|
||||
std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
||||
g_options_options[OPTIONS_MENU_3D] )
|
||||
->changeText( std::string( "Show block texture: " ) +
|
||||
( g_show_3d ? "YES" : "NO" ) );
|
||||
g_update_colors = true;
|
||||
default:
|
||||
break;
|
||||
@@ -718,7 +780,7 @@ void pollEventsOptions() {
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||
for(auto &x : g_active_scenes)
|
||||
for ( auto &x : g_active_scenes )
|
||||
x->updateSizeAndPosition();
|
||||
}
|
||||
default:
|
||||
@@ -727,6 +789,8 @@ void pollEventsOptions() {
|
||||
}
|
||||
}
|
||||
|
||||
void optionsSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/, std::vector<std::shared_ptr<SDLPP::RenderObject>> &/*UNUSED*/ ) {
|
||||
void optionsSceneInput(
|
||||
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
|
||||
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
|
||||
pollEventsOptions();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user