game/tetris/scenes.cpp

940 lines
32 KiB
C++
Raw Normal View History

#include "scenes.hpp"
#include "config.hpp"
#include "functions.hpp"
#include "global_vars.hpp"
2021-04-29 17:49:04 +00:00
#include "../sdlpp/sdlpp_mouse.hpp"
2021-04-29 17:49:04 +00:00
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_mouse.h>
#include <thread>
2020-09-26 18:23:03 +00:00
constexpr uint64_t MAIN_MENU_RESUME = 0;
constexpr uint64_t MAIN_MENU_OPTIONS = 1;
constexpr uint64_t MAIN_MENU_RESTART = 2;
constexpr uint64_t MAIN_MENU_QUIT = 3;
constexpr uint64_t GAME_OVER_RESTART = 0;
constexpr uint64_t GAME_OVER_QUIT = 1;
constexpr uint64_t OPTIONS_MENU_COLOR_SCHEME = 0;
constexpr uint64_t OPTIONS_MENU_SHADOW = 1;
2020-09-29 16:22:26 +00:00
constexpr uint64_t OPTIONS_MENU_3D = 2;
constexpr uint64_t OPTIONS_MENU_SAVE = 3;
2020-09-26 18:23:03 +00:00
// Scene preparation
2020-11-21 19:58:52 +00:00
void addMainSceneItems( SDLPP::Scene &scene,
2021-01-30 20:35:25 +00:00
std::shared_ptr< SDLPP::Renderer > &r ) {
2020-11-21 19:58:52 +00:00
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, r, colors["background"], true );
bg->setPermanent();
2020-11-21 19:58:52 +00:00
bg->setId( BACKGROUND_ID );
scene.addObject( bg );
#ifdef FEATURE
auto testcircle = std::make_shared< SDLPP::CircleRender >(
LEFT_BORDER, 0.2, 0.05, r, "#FF00AA", true );
2021-04-29 17:49:04 +00:00
testcircle->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene.addObject( testcircle );
#endif
// create coliders for counting blocks in line
double posy = 1;
for ( int i = 0; i < 20; i++ ) {
posy -= BLOCK_SIZE;
auto colider = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER, posy, RIGHT_BORDER - LEFT_BORDER, BLOCK_SIZE, r );
2020-11-21 19:58:52 +00:00
colider->addCollision( SDLPP::RectColider( 0.01, 0.1, 0.98, 0.8 ) );
colider->setId( COLIDER_ID );
colider->setStatic();
2021-04-29 17:49:04 +00:00
colider->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene.addObject( colider );
}
// create lines on playing field
posy = 1;
for ( int i = 0; i < 20; i++ ) {
posy -= BLOCK_SIZE;
2020-11-21 19:58:52 +00:00
auto line = std::make_shared< SDLPP::LineRenderer >(
LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
line->setStatic();
2021-04-29 17:49:04 +00:00
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
line->setId( LINE_ID );
scene.addObject( line );
}
auto posx = RIGHT_BORDER;
for ( int i = 0; i < 9; i++ ) {
posx -= BLOCK_SIZE;
2020-11-21 19:58:52 +00:00
auto line = std::make_shared< SDLPP::LineRenderer >(
posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r,
colors["line"] );
line->setStatic();
2021-04-29 17:49:04 +00:00
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
line->setId( LINE_ID );
scene.addObject( line );
}
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
2020-11-21 19:58:52 +00:00
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"],
true );
2021-04-29 17:49:04 +00:00
left_barrier->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
left_barrier->setStatic();
2020-11-21 19:58:52 +00:00
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 );
2021-04-29 17:49:04 +00:00
right_barrier->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
right_barrier->setStatic();
2020-11-21 19:58:52 +00:00
right_barrier->setId( BARRIER_ID );
scene.addObject( right_barrier );
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 );
2021-04-29 17:49:04 +00:00
bottom_barrier->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
bottom_barrier->setStatic();
2020-11-21 19:58:52 +00:00
bottom_barrier->setId( BARRIER_ID );
scene.addObject( bottom_barrier );
auto tetris = std::make_shared< SDLPP::TextRenderer >(
2021-01-30 20:35:25 +00:00
0.4, 0, 0.2, 0.1, r, "TETRIS", g_font_config );
2021-04-29 17:49:04 +00:00
tetris->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
tetris->setStatic();
2020-11-21 19:58:52 +00:00
tetris->setId( TEXT_ID );
scene.addObject( tetris );
auto next = std::make_shared< SDLPP::TextRenderer >(
2021-01-30 20:35:25 +00:00
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, "NEXT", g_font_config,
SDLPP_TEXT_CENTER );
2021-04-29 17:49:04 +00:00
next->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
next->setStatic();
2020-11-21 19:58:52 +00:00
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 );
2020-11-21 19:58:52 +00:00
auto gameover_collision = SDLPP::RectColider( -1, 0, -1, 0.9 );
gameover_collision.setInfinite();
gameover->addCollision( gameover_collision );
gameover->setId( GAME_OVER );
gameover->setColiderColor( "FF0000" );
gameover->setStatic();
scene.addObject( gameover );
auto score_text = std::make_shared< SDLPP::TextRenderer >(
2021-01-30 20:35:25 +00:00
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, "SCORE", g_font_config,
SDLPP_TEXT_CENTER );
2021-04-29 17:49:04 +00:00
score_text->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
score_text->setStatic();
2020-11-21 19:58:52 +00:00
score_text->setId( TEXT_ID );
scene.addObject( score_text );
auto score_texture = std::make_shared< SDLPP::TextRenderer >(
2021-01-30 20:35:25 +00:00
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, "0", g_font_config,
SDLPP_TEXT_TOP );
2021-04-29 17:49:04 +00:00
score_texture->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
score_texture->setStatic();
2020-11-21 19:58:52 +00:00
score_texture->setId( SCORE_TEXTURE_ID );
scene.addObject( score_texture );
2020-11-21 19:58:52 +00:00
auto border = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER - 1, 0, 1, BOTTOM_BORDER, r );
border->setId( BORDER_LEFT_ID );
border->setStatic();
2021-04-29 17:49:04 +00:00
border->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
border->addCollision( SDLPP::RectColider( 0, 0, 0.99, 1 ) );
border->setColiderColor( "#FF00FF" );
scene.addObject( border );
2020-11-21 19:58:52 +00:00
border = std::make_shared< SDLPP::RectangleRender >( RIGHT_BORDER, 0, 1,
BOTTOM_BORDER, r );
border->setId( BORDER_RIGHT_ID );
border->setStatic();
2021-04-29 17:49:04 +00:00
border->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
border->addCollision( SDLPP::RectColider( 0.01, 0, 1, 1 ) );
border->setColiderColor( "#FF00FF" );
scene.addObject( border );
2020-11-21 19:58:52 +00:00
auto floor = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER, BOTTOM_BORDER, RIGHT_BORDER - LEFT_BORDER, 1, r );
floor->setId( FLOOR_ID );
floor->setStatic();
2021-04-29 17:49:04 +00:00
floor->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
floor->addCollision( SDLPP::RectColider( 0, 0.01, 1, 1 ) );
floor->setColiderColor( "#00FF00" );
scene.addObject( floor );
}
2020-11-21 19:58:52 +00:00
void addMenuSceneItems( SDLPP::Scene &scene,
2021-01-30 20:35:25 +00:00
std::shared_ptr< SDLPP::Renderer > &r ) {
2020-11-21 19:58:52 +00:00
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 );
2021-01-30 20:35:25 +00:00
y->setText( "PAUSED", g_font_config );
y->setId( MENU_TEXT_ID );
2021-04-29 17:49:04 +00:00
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 );
2021-01-30 20:35:25 +00:00
resume->setText( "Resume", g_font_config );
resume->setColor( colors["menu_item_background"] );
2021-04-29 17:49:04 +00:00
resume->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
resume->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
resume->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_RESUME_ID ) );
2020-11-21 19:58:52 +00:00
g_menu_options.push_back( resume );
scene.addObject( resume );
2020-11-21 19:58:52 +00:00
auto options =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.56, 0.2, 0.08, r );
2021-01-30 20:35:25 +00:00
options->setText( "Options", g_font_config );
2021-04-29 17:49:04 +00:00
options->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
options->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
options->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_OPTIONS_ID ) );
2020-11-21 19:58:52 +00:00
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 );
2021-01-30 20:35:25 +00:00
restart->setText( "Restart", g_font_config );
2021-04-29 17:49:04 +00:00
restart->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
restart->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
restart->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_RESTART_ID ) );
2020-11-21 19:58:52 +00:00
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 );
2021-01-30 20:35:25 +00:00
quit->setText( "Quit Game", g_font_config );
2021-04-29 17:49:04 +00:00
quit->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
quit->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
quit->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_QUIT_ID ) );
2020-11-21 19:58:52 +00:00
g_menu_options.push_back( quit );
scene.addObject( quit );
2021-04-29 17:49:04 +00:00
auto mouse =
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0.01, 0.01, r );
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mouse->setId( MENU_MOUSE_ID );
mouse->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene.addObject( mouse );
}
2020-11-21 19:58:52 +00:00
void addGameOverSceneItems( SDLPP::Scene &scene,
2021-01-30 20:35:25 +00:00
std::shared_ptr< SDLPP::Renderer > &r ) {
2020-11-21 19:58:52 +00:00
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 );
2021-01-30 20:35:25 +00:00
y->setText( "GAME OVER", g_font_config );
y->setId( 0 );
2021-04-29 17:49:04 +00:00
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 );
2021-01-30 20:35:25 +00:00
restart->setText( "Restart", g_font_config );
2021-04-29 17:49:04 +00:00
restart->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
restart->setColor( colors["menu_item_background"] );
2020-11-21 19:58:52 +00:00
restart->setId( MENU_ITEM_ID );
g_game_over_options.push_back( restart );
scene.addObject( restart );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.7, 0.3, 0.1, r );
2021-01-30 20:35:25 +00:00
quit->setText( "Quit Game", g_font_config );
2021-04-29 17:49:04 +00:00
quit->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
quit->setId( MENU_ITEM_ID );
g_game_over_options.push_back( quit );
scene.addObject( quit );
}
2020-11-21 19:58:52 +00:00
void addOptionsSceneItems( SDLPP::Scene &scene,
2021-01-30 20:35:25 +00:00
std::shared_ptr< SDLPP::Renderer > &r ) {
2020-11-21 19:58:52 +00:00
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 );
2021-01-30 20:35:25 +00:00
y->setText( "OPTIONS", g_font_config );
y->setId( 0 );
2021-04-29 17:49:04 +00:00
y->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
scene.addObject( y );
auto color_scheme =
2020-12-18 14:14:08 +00:00
std::make_shared< SDLPP::TextRenderer >( 0.18, 0.35, 0.64, 0.09, r );
2021-01-30 20:35:25 +00:00
color_scheme->setText( "Color scheme: " +
color_schemes_names[selected_color_scheme],
g_font_config );
2021-04-29 17:49:04 +00:00
color_scheme->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
color_scheme->setColor( colors["menu_item_background"] );
2020-11-21 19:58:52 +00:00
color_scheme->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
color_scheme->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_COLOR_ID ) );
2020-11-21 19:58:52 +00:00
g_options_options.push_back( color_scheme );
scene.addObject( color_scheme );
auto shadow =
2020-12-18 14:14:08 +00:00
std::make_shared< SDLPP::TextRenderer >( 0.26, 0.45, 0.48, 0.09, r );
2021-01-30 20:35:25 +00:00
shadow->setText( "Show shadow: YES", g_font_config );
2021-04-29 17:49:04 +00:00
shadow->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
shadow->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
shadow->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_SHADOW_ID ) );
2020-11-21 19:58:52 +00:00
g_options_options.push_back( shadow );
scene.addObject( shadow );
2020-09-29 16:22:26 +00:00
auto show3d =
2020-12-18 14:14:08 +00:00
std::make_shared< SDLPP::TextRenderer >( 0.2, 0.55, 0.6, 0.09, r );
2021-01-30 20:35:25 +00:00
show3d->setText( "Show block texture: NO", g_font_config );
2021-04-29 17:49:04 +00:00
show3d->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
show3d->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
show3d->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_3D_ID ) );
2020-11-21 19:58:52 +00:00
g_options_options.push_back( show3d );
scene.addObject( show3d );
auto save =
2020-12-18 14:14:08 +00:00
std::make_shared< SDLPP::TextRenderer >( 0.42, 0.65, 0.16, 0.09, r );
2021-01-30 20:35:25 +00:00
save->setText( "SAVE", g_font_config );
2021-04-29 17:49:04 +00:00
save->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2020-11-21 19:58:52 +00:00
save->setId( MENU_ITEM_ID );
2021-04-29 17:49:04 +00:00
save->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_SAVE_ID ) );
2020-11-21 19:58:52 +00:00
g_options_options.push_back( save );
scene.addObject( save );
2021-04-29 17:49:04 +00:00
auto mouse =
std::make_shared< SDLPP::RectangleRender >( 0.01, 0.01, 0.01, 0.01, r );
mouse->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
mouse->setId( MENU_MOUSE_ID );
mouse->addCollision(
SDLPP::RectColider( { 0, 0 }, { 1, 1 } ) );
scene.addObject( mouse );
}
2020-11-21 19:58:52 +00:00
std::shared_ptr< SDLPP::Scene >
2021-01-30 20:35:25 +00:00
prepareMainScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
2021-01-30 20:35:25 +00:00
addMainSceneItems( *scene, renderer );
return scene;
}
2020-11-21 19:58:52 +00:00
std::shared_ptr< SDLPP::Scene >
2021-01-30 20:35:25 +00:00
prepareMenuScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
2021-01-30 20:35:25 +00:00
addMenuSceneItems( *scene, renderer );
return scene;
}
2020-11-21 19:58:52 +00:00
std::shared_ptr< SDLPP::Scene >
2021-01-30 20:35:25 +00:00
prepareGameOverScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
2021-01-30 20:35:25 +00:00
addGameOverSceneItems( *scene, renderer );
return scene;
}
2020-11-21 19:58:52 +00:00
std::shared_ptr< SDLPP::Scene >
2021-01-30 20:35:25 +00:00
prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
auto scene = std::make_shared< SDLPP::Scene >( renderer );
2021-01-30 20:35:25 +00:00
addOptionsSceneItems( *scene, renderer );
return scene;
}
// Input handling
void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
case SDLK_ESCAPE:
g_update_scenes.push_back( g_menu_scene );
g_active_scenes.push_back( g_menu_scene );
2020-11-21 19:58:52 +00:00
g_input_functions.push_back( menuSceneInput );
#ifdef FEATURE
pauseBlocks();
#endif
break;
case SDLK_LEFT:
case SDLK_a:
2020-11-21 19:58:52 +00:00
if ( !g_cur_object )
break;
2020-11-21 19:58:52 +00:00
g_cur_object->movePiece( -BLOCK_SIZE, 0 );
if ( !validPos( scene, g_cur_object ) )
g_cur_object->movePiece( BLOCK_SIZE, 0 );
2020-09-13 12:14:39 +00:00
else
2020-11-21 19:58:52 +00:00
updateShadow( scene );
2020-11-21 19:58:52 +00:00
g_ticks_till_movement = 2 * TICKS_TILL_MOVE;
g_cur_object->startMovement();
2020-11-21 19:58:52 +00:00
g_cur_object->addMovement( -1, 0 );
break;
case SDLK_RIGHT:
case SDLK_d:
2020-11-21 19:58:52 +00:00
if ( !g_cur_object )
break;
2020-11-21 19:58:52 +00:00
g_cur_object->movePiece( BLOCK_SIZE, 0 );
if ( !validPos( scene, g_cur_object ) )
g_cur_object->movePiece( -BLOCK_SIZE, 0 );
2020-09-13 12:14:39 +00:00
else
2020-11-21 19:58:52 +00:00
updateShadow( scene );
2020-11-21 19:58:52 +00:00
g_ticks_till_movement = 2 * TICKS_TILL_MOVE;
g_cur_object->startMovement();
2020-11-21 19:58:52 +00:00
g_cur_object->addMovement( 1, 0 );
break;
case SDLK_DOWN:
case SDLK_s:
2020-11-21 19:58:52 +00:00
if ( !g_cur_object )
break;
2020-09-13 12:14:39 +00:00
g_ticks_till_descend = 0;
g_cur_object->startDescend();
2020-11-21 19:58:52 +00:00
g_cur_object->addMovement( 0, 1 );
break;
case SDLK_UP:
case SDLK_w:
2020-11-21 19:58:52 +00:00
if ( !g_cur_object )
break;
g_cur_object->rotate();
2020-11-21 19:58:52 +00:00
if ( checkRotation( g_cur_object, scene ) )
g_cur_shadow->rotate();
2020-11-21 19:58:52 +00:00
updateShadow( scene );
break;
2020-09-12 07:36:08 +00:00
case SDLK_SPACE:
2020-11-21 19:58:52 +00:00
if ( !g_cur_object )
2020-09-12 07:36:08 +00:00
break;
2020-11-21 19:58:52 +00:00
g_cur_object->setPos( g_cur_shadow->getPos() );
2020-09-12 07:36:08 +00:00
break;
2020-09-13 12:14:39 +00:00
#ifdef DEBUG
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
2020-09-13 12:14:39 +00:00
#endif
default:
break;
}
}
void handleKeyUpMain( SDL_Keycode key ) {
2020-11-21 19:58:52 +00:00
switch ( key ) {
case SDLK_DOWN:
case SDLK_s:
if ( !g_cur_object )
break;
2020-11-21 19:58:52 +00:00
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;
2020-11-21 19:58:52 +00:00
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;
2020-11-21 19:58:52 +00:00
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 ) ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDownMain( event.key.keysym.sym, scene );
break;
case SDL_KEYUP:
handleKeyUpMain( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
2020-11-21 19:58:52 +00:00
for ( auto &x : g_active_scenes )
g_update_scenes.push_back( x );
2020-12-18 14:14:08 +00:00
g_update_size = true;
}
default:
break;
}
}
}
2020-11-21 19:58:52 +00:00
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 ) {
moveThem( scene, SDL_GetTicks() - base );
return;
}
short lines = 0;
for ( auto &colider : line_coliders ) {
auto collisions = scene->getCollisions( *colider, { BRICK_ID } );
while ( collisions.size() == 10 ) {
lines += 1;
for ( auto &col : collisions ) {
2021-04-29 11:25:55 +00:00
col.second->destroy();
}
auto colider_y = colider->getPos().getY();
2020-11-21 19:58:52 +00:00
for ( auto &elem : scene->getObjects( { BRICK_ID } ) ) {
auto pos = elem->getPos();
if ( pos.getY() < colider_y && pos.getX() <= RIGHT_BORDER ) {
elem->setPos( pos.getX(), pos.getY() + BLOCK_SIZE );
}
}
using namespace std::chrono_literals;
g_wait_for_anim = true;
while ( g_wait_for_anim ) {
std::this_thread::sleep_for( 0.1s );
}
collisions = scene->getCollisions( *colider, { BRICK_ID } );
}
}
2020-11-21 19:58:52 +00:00
if ( lines > 0 ) {
g_update_score = true;
2020-11-21 19:58:52 +00:00
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;
}
void handleKeyDownMenu( SDL_Keycode key ) {
switch ( key ) {
case SDLK_ESCAPE: {
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back();
g_input_functions.pop_back();
#ifdef FEATURE
resumeBlocks();
#endif
} break;
2020-09-13 12:14:39 +00:00
#ifdef DEBUG
case SDLK_r:
g_main_scene->getRenderer().setRenderColiders(
!g_main_scene->getRenderer().getRenderColiders() );
break;
2020-09-13 12:14:39 +00:00
#endif
case SDLK_s:
case SDLK_DOWN:
g_menu_options[g_menu_select]->unsetColor();
g_menu_select++;
2020-11-21 19:58:52 +00:00
if ( static_cast< size_t >( g_menu_select ) >= g_menu_options.size() )
g_menu_select = 0;
2020-11-21 19:58:52 +00:00
g_menu_options[g_menu_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
g_menu_options[g_menu_select]->unsetColor();
g_menu_select--;
if ( g_menu_select < 0 )
2020-09-29 16:22:26 +00:00
g_menu_select = g_menu_options.size() - 1;
2020-11-21 19:58:52 +00:00
g_menu_options[g_menu_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_RETURN:
switch ( g_menu_select ) {
2020-09-26 18:23:03 +00:00
case MAIN_MENU_RESUME: {
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back();
g_input_functions.pop_back();
} break;
2020-09-26 18:23:03 +00:00
case MAIN_MENU_OPTIONS:
g_update_scenes.push_back( g_options_scene );
2020-11-21 19:58:52 +00:00
g_active_scenes.push_back( g_options_scene );
g_input_functions.push_back( optionsSceneInput );
break;
2020-09-26 18:23:03 +00:00
case MAIN_MENU_RESTART:
resetGame();
2020-11-21 19:58:52 +00:00
break;
2020-09-26 18:23:03 +00:00
case MAIN_MENU_QUIT:
quitGame();
default:
break;
}
default:
break;
}
}
2021-04-29 17:49:04 +00:00
bool g_mouse_down = false;
int selected_menu_down = -1;
int selected_options_down = -1;
void highlightMenuItem(uint64_t id) {
g_menu_options[g_menu_select]->unsetColor();
switch(id) {
case MENU_RESUME_ID:
g_menu_select = 0;
break;
case MENU_OPTIONS_ID:
g_menu_select = 1;
break;
case MENU_RESTART_ID:
g_menu_select = 2;
break;
case MENU_QUIT_ID:
g_menu_select = 3;
default:
break;
}
if(g_mouse_down)
return;
g_menu_options[g_menu_select]->setColor(
colors["menu_item_background"] );
}
void highlightOptionsItem(uint64_t id) {
g_options_options[g_options_select]->unsetColor();
switch(id) {
case OPTIONS_COLOR_ID:
g_options_select = 0;
break;
case OPTIONS_SHADOW_ID:
g_options_select = 1;
break;
case OPTIONS_3D_ID:
g_options_select = 2;
break;
case OPTIONS_SAVE_ID:
g_options_select = 3;
default:
break;
}
if(g_mouse_down)
return;
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
}
void pollEventsMenu() {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDownMenu( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
2020-11-21 19:58:52 +00:00
for ( auto &x : g_active_scenes )
g_update_scenes.push_back( x );
2020-12-18 14:14:08 +00:00
g_update_size = true;
}
2021-04-29 17:49:04 +00:00
break;
case SDL_MOUSEMOTION: {
auto mouse = g_menu_scene->getObjects({MENU_MOUSE_ID})[0];
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(g_menu_scene->getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
auto collisions = g_menu_scene->getCollisions(*mouse);
if(collisions.size() == 2) {
for(auto &collision : collisions) {
if(collision.second->getId() == MENU_ITEM_ID) {
highlightMenuItem(collision.second->getCollisions()[0]->getId());
}
}
}
}
break;
case SDL_MOUSEBUTTONUP:
g_mouse_down = false;
if(selected_menu_down == g_menu_select) {
handleKeyDownMenu(SDLK_RETURN);
}
break;
case SDL_MOUSEBUTTONDOWN:
g_mouse_down = true;
selected_menu_down = g_menu_select;
break;
default:
break;
}
}
}
2020-11-21 19:58:52 +00:00
void menuSceneInput(
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
pollEventsMenu();
}
void handleKeyDownGameOver( SDL_Keycode key ) {
switch ( key ) {
2020-09-13 12:14:39 +00:00
#ifdef DEBUG
case SDLK_r:
g_main_scene->getRenderer().setRenderColiders(
!g_main_scene->getRenderer().getRenderColiders() );
break;
2020-09-13 12:14:39 +00:00
#endif
case SDLK_s:
case SDLK_DOWN:
g_game_over_options[g_game_over_select]->unsetColor();
g_game_over_select++;
2020-11-21 19:58:52 +00:00
if ( static_cast< size_t >( g_game_over_select ) >=
g_game_over_options.size() )
g_game_over_select = 0;
2020-11-21 19:58:52 +00:00
g_game_over_options[g_game_over_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
g_game_over_options[g_game_over_select]->unsetColor();
g_game_over_select--;
if ( g_game_over_select < 0 )
2020-09-29 16:22:26 +00:00
g_game_over_select = g_game_over_options.size() - 1;
2020-11-21 19:58:52 +00:00
g_game_over_options[g_game_over_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_RETURN:
switch ( g_game_over_select ) {
2020-09-26 18:23:03 +00:00
case GAME_OVER_RESTART:
#ifdef FEATURE
resumeBlocks();
#endif
resetGame();
2020-11-21 19:58:52 +00:00
break;
2020-09-26 18:23:03 +00:00
case GAME_OVER_QUIT:
quitGame();
default:
break;
}
default:
break;
}
}
void pollEventsGameOver() {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDownGameOver( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
2020-11-21 19:58:52 +00:00
for ( auto &x : g_active_scenes )
g_update_scenes.push_back( x );
2020-12-18 14:14:08 +00:00
g_update_size = true;
}
default:
break;
}
}
}
2020-11-21 19:58:52 +00:00
void gameOverSceneInput(
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
pollEventsGameOver();
}
2020-09-12 07:36:08 +00:00
void saveOptions() {
2020-11-21 19:58:52 +00:00
if ( g_cur_shadow )
g_cur_shadow->setHidden( !g_show_shadow );
2020-09-12 07:36:08 +00:00
g_main_scene->setPrevTicks( SDL_GetTicks() );
}
void handleKeyDownOptions( SDL_Keycode key ) {
switch ( key ) {
2020-09-13 12:14:39 +00:00
#ifdef DEBUG
case SDLK_r:
g_main_scene->getRenderer().setRenderColiders(
!g_main_scene->getRenderer().getRenderColiders() );
break;
2020-09-13 12:14:39 +00:00
#endif
case SDLK_s:
case SDLK_DOWN:
g_options_options[g_options_select]->unsetColor();
g_options_select++;
2020-11-21 19:58:52 +00:00
if ( static_cast< size_t >( g_options_select ) >=
g_options_options.size() )
g_options_select = 0;
2020-11-21 19:58:52 +00:00
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
g_options_options[g_options_select]->unsetColor();
g_options_select--;
if ( g_options_select < 0 )
2020-09-29 16:22:26 +00:00
g_options_select = g_options_options.size() - 1;
2020-11-21 19:58:52 +00:00
g_options_options[g_options_select]->setColor(
colors["menu_item_background"] );
break;
case SDLK_RIGHT:
case SDLK_d:
2020-11-21 19:58:52 +00:00
switch ( g_options_select ) {
2020-09-26 18:23:03 +00:00
case OPTIONS_MENU_COLOR_SCHEME:
selected_color_scheme++;
2020-11-21 19:58:52 +00:00
if ( static_cast< size_t >( selected_color_scheme ) >=
color_schemes_names.size() )
selected_color_scheme = 0;
static_cast< SDLPP::TextRenderer * >(
g_options_options[OPTIONS_MENU_COLOR_SCHEME].get() )
2020-11-21 19:58:52 +00:00
->changeText( "Color scheme: " +
color_schemes_names[selected_color_scheme] );
g_update_colors = true;
break;
2020-09-26 18:23:03 +00:00
case OPTIONS_MENU_SHADOW:
g_show_shadow = !g_show_shadow;
2021-01-30 15:48:40 +00:00
static_cast< SDLPP::TextRenderer * >(
g_options_options[OPTIONS_MENU_SHADOW].get() )
2020-11-21 19:58:52 +00:00
->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) );
g_update_objects.push_back(
g_options_options[OPTIONS_MENU_SHADOW] );
2020-09-29 16:22:26 +00:00
break;
case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d;
2021-01-30 15:48:40 +00:00
static_cast< SDLPP::TextRenderer * >(
g_options_options[OPTIONS_MENU_3D].get() )
2020-11-21 19:58:52 +00:00
->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) );
g_update_objects.push_back( g_options_options[OPTIONS_MENU_3D] );
2021-01-30 20:35:25 +00:00
g_update_3d = true;
default:
break;
}
break;
case SDLK_LEFT:
case SDLK_a:
2020-11-21 19:58:52 +00:00
switch ( g_options_select ) {
2020-09-26 18:23:03 +00:00
case OPTIONS_MENU_COLOR_SCHEME:
2020-11-21 19:58:52 +00:00
if ( selected_color_scheme == 0 )
selected_color_scheme = color_schemes_names.size();
selected_color_scheme--;
2021-01-30 15:48:40 +00:00
static_cast< SDLPP::TextRenderer * >(
g_options_options[OPTIONS_MENU_COLOR_SCHEME].get() )
2020-11-21 19:58:52 +00:00
->changeText( "Color scheme: " +
color_schemes_names[selected_color_scheme] );
g_update_colors = true;
break;
2020-09-26 18:23:03 +00:00
case OPTIONS_MENU_SHADOW:
g_show_shadow = !g_show_shadow;
2021-01-30 15:48:40 +00:00
static_cast< SDLPP::TextRenderer * >(
g_options_options[OPTIONS_MENU_SHADOW].get() )
2020-11-21 19:58:52 +00:00
->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) );
g_update_objects.push_back(
g_options_options[OPTIONS_MENU_SHADOW] );
2020-09-29 16:22:26 +00:00
break;
case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d;
2021-01-30 15:48:40 +00:00
static_cast< SDLPP::TextRenderer * >(
g_options_options[OPTIONS_MENU_3D].get() )
2020-11-21 19:58:52 +00:00
->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) );
g_update_objects.push_back( g_options_options[OPTIONS_MENU_3D] );
2021-01-30 20:35:25 +00:00
g_update_3d = true;
default:
break;
}
break;
case SDLK_RETURN:
switch ( g_options_select ) {
2020-09-26 18:23:03 +00:00
case OPTIONS_MENU_SAVE:
2020-09-12 07:36:08 +00:00
saveOptions();
2020-09-13 12:14:39 +00:00
g_active_scenes.pop_back();
g_input_functions.pop_back();
break;
default:
break;
}
break;
case SDLK_ESCAPE:
2020-09-12 07:36:08 +00:00
saveOptions();
2020-09-13 12:14:39 +00:00
g_active_scenes.pop_back();
g_input_functions.pop_back();
default:
break;
}
}
void pollEventsOptions() {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
quitGame();
break;
case SDL_KEYDOWN:
if ( !event.key.repeat )
handleKeyDownOptions( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
2020-11-21 19:58:52 +00:00
for ( auto &x : g_active_scenes )
g_update_scenes.push_back( x );
2020-12-18 14:14:08 +00:00
g_update_size = true;
}
2021-04-29 17:49:04 +00:00
break;
case SDL_MOUSEMOTION: {
auto mouse = g_options_scene->getObjects({MENU_MOUSE_ID})[0];
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(g_options_scene->getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
auto collisions = g_options_scene->getCollisions(*mouse);
if(collisions.size() == 2) {
for(auto &collision : collisions) {
if(collision.second->getId() == MENU_ITEM_ID) {
highlightOptionsItem(collision.second->getCollisions()[0]->getId());
}
}
}
}
break;
case SDL_MOUSEBUTTONUP:
g_mouse_down = false;
if(selected_options_down == g_options_select) {
handleKeyDownOptions(SDLK_RIGHT);
handleKeyDownOptions(SDLK_RETURN);
}
break;
case SDL_MOUSEBUTTONDOWN:
g_mouse_down = true;
selected_options_down = g_options_select;
break;
default:
break;
}
}
}
2020-11-21 19:58:52 +00:00
void optionsSceneInput(
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
pollEventsOptions();
}