945 lines
32 KiB
C++
945 lines
32 KiB
C++
#include "scenes.hpp"
|
|
#include "config.hpp"
|
|
#include "functions.hpp"
|
|
#include "global_vars.hpp"
|
|
#include "../sdlpp/sdlpp_mouse.hpp"
|
|
|
|
#ifndef _WIN32
|
|
#include <SDL2/SDL_events.h>
|
|
#include <SDL2/SDL_mouse.h>
|
|
#else
|
|
#include "../sdlpp/SDL2/SDL_events.h"
|
|
#include "../sdlpp/SDL2/SDL_mouse.h"
|
|
#endif
|
|
#include <thread>
|
|
|
|
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;
|
|
constexpr uint64_t OPTIONS_MENU_3D = 2;
|
|
constexpr uint64_t OPTIONS_MENU_SAVE = 3;
|
|
|
|
// Scene preparation
|
|
|
|
void addMainSceneItems( SDLPP::Scene &scene,
|
|
std::shared_ptr< SDLPP::Renderer > &r ) {
|
|
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
|
0, 0, 10, 10, r, colors["background"], true );
|
|
bg->setPermanent();
|
|
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 );
|
|
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 );
|
|
colider->addCollision( SDLPP::RectColider( 0.01, 0.1, 0.98, 0.8 ) );
|
|
colider->setId( COLIDER_ID );
|
|
colider->setStatic();
|
|
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;
|
|
auto line = std::make_shared< SDLPP::LineRenderer >(
|
|
LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
|
|
line->setStatic();
|
|
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
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"] );
|
|
line->setStatic();
|
|
line->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
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_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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
right_barrier->setStatic();
|
|
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 );
|
|
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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
tetris->setStatic();
|
|
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, "NEXT", g_font_config,
|
|
SDLPP_TEXT_CENTER );
|
|
next->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
next->setStatic();
|
|
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::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 >(
|
|
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, "SCORE", g_font_config,
|
|
SDLPP_TEXT_CENTER );
|
|
score_text->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
score_text->setStatic();
|
|
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, "0", g_font_config,
|
|
SDLPP_TEXT_TOP );
|
|
score_texture->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
score_texture->setStatic();
|
|
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 );
|
|
border->setId( BORDER_LEFT_ID );
|
|
border->setStatic();
|
|
border->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
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->setId( BORDER_RIGHT_ID );
|
|
border->setStatic();
|
|
border->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
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 );
|
|
floor->setId( FLOOR_ID );
|
|
floor->setStatic();
|
|
floor->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
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 ) {
|
|
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 );
|
|
y->setText( "PAUSED", g_font_config );
|
|
y->setId( MENU_TEXT_ID );
|
|
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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
resume->setId( MENU_ITEM_ID );
|
|
resume->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_RESUME_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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
options->setId( MENU_ITEM_ID );
|
|
options->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_OPTIONS_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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
restart->setId( MENU_ITEM_ID );
|
|
restart->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_RESTART_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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
quit->setId( MENU_ITEM_ID );
|
|
quit->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, MENU_QUIT_ID ) );
|
|
g_menu_options.push_back( quit );
|
|
scene.addObject( quit );
|
|
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 );
|
|
}
|
|
|
|
void addGameOverSceneItems( SDLPP::Scene &scene,
|
|
std::shared_ptr< SDLPP::Renderer > &r ) {
|
|
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 );
|
|
y->setText( "GAME OVER", g_font_config );
|
|
y->setId( 0 );
|
|
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->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 );
|
|
scene.addObject( restart );
|
|
auto quit =
|
|
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.7, 0.3, 0.1, r );
|
|
quit->setText( "Quit Game", g_font_config );
|
|
quit->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
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 ) {
|
|
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 );
|
|
y->setText( "OPTIONS", g_font_config );
|
|
y->setId( 0 );
|
|
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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
color_scheme->setColor( colors["menu_item_background"] );
|
|
color_scheme->setId( MENU_ITEM_ID );
|
|
color_scheme->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_COLOR_ID ) );
|
|
g_options_options.push_back( color_scheme );
|
|
scene.addObject( color_scheme );
|
|
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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
shadow->setId( MENU_ITEM_ID );
|
|
shadow->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_SHADOW_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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
show3d->setId( MENU_ITEM_ID );
|
|
show3d->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_3D_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->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
|
save->setId( MENU_ITEM_ID );
|
|
save->addCollision(
|
|
SDLPP::RectColider( { 0, 0 }, { 1, 1 }, OPTIONS_SAVE_ID ) );
|
|
g_options_options.push_back( save );
|
|
scene.addObject( save );
|
|
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 );
|
|
}
|
|
|
|
std::shared_ptr< SDLPP::Scene >
|
|
prepareMainScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
|
|
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
|
addMainSceneItems( *scene, renderer );
|
|
return scene;
|
|
}
|
|
|
|
std::shared_ptr< SDLPP::Scene >
|
|
prepareMenuScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
|
|
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
|
addMenuSceneItems( *scene, renderer );
|
|
return scene;
|
|
}
|
|
|
|
std::shared_ptr< SDLPP::Scene >
|
|
prepareGameOverScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
|
|
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
|
addGameOverSceneItems( *scene, renderer );
|
|
return scene;
|
|
}
|
|
|
|
std::shared_ptr< SDLPP::Scene >
|
|
prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
|
|
auto scene = std::make_shared< SDLPP::Scene >( renderer );
|
|
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 );
|
|
g_input_functions.push_back( menuSceneInput );
|
|
#ifdef FEATURE
|
|
pauseBlocks();
|
|
#endif
|
|
break;
|
|
case SDLK_LEFT:
|
|
case SDLK_a:
|
|
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 );
|
|
else
|
|
updateShadow( scene );
|
|
|
|
g_ticks_till_movement = 2 * TICKS_TILL_MOVE;
|
|
g_cur_object->startMovement();
|
|
g_cur_object->addMovement( -1, 0 );
|
|
break;
|
|
case SDLK_RIGHT:
|
|
case SDLK_d:
|
|
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 );
|
|
else
|
|
updateShadow( scene );
|
|
|
|
g_ticks_till_movement = 2 * TICKS_TILL_MOVE;
|
|
g_cur_object->startMovement();
|
|
g_cur_object->addMovement( 1, 0 );
|
|
break;
|
|
case SDLK_DOWN:
|
|
case SDLK_s:
|
|
if ( !g_cur_object )
|
|
break;
|
|
g_ticks_till_descend = 0;
|
|
g_cur_object->startDescend();
|
|
g_cur_object->addMovement( 0, 1 );
|
|
break;
|
|
case SDLK_UP:
|
|
case SDLK_w:
|
|
if ( !g_cur_object )
|
|
break;
|
|
g_cur_object->rotate();
|
|
if ( checkRotation( g_cur_object, scene ) )
|
|
g_cur_shadow->rotate();
|
|
updateShadow( scene );
|
|
break;
|
|
case SDLK_SPACE:
|
|
if ( !g_cur_object )
|
|
break;
|
|
g_cur_object->setPos( g_cur_shadow->getPos() );
|
|
break;
|
|
#ifdef DEBUG
|
|
case SDLK_r:
|
|
scene.getRenderer().setRenderColiders(
|
|
!scene.getRenderer().getRenderColiders() );
|
|
#endif
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
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 );
|
|
}
|
|
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 );
|
|
}
|
|
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 ) ) {
|
|
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 ) {
|
|
for ( auto &x : g_active_scenes )
|
|
g_update_scenes.push_back( x );
|
|
g_update_size = true;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 ) {
|
|
col.second->destroy();
|
|
}
|
|
auto colider_y = colider->getPos().getY();
|
|
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 } );
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
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;
|
|
#ifdef DEBUG
|
|
case SDLK_r:
|
|
g_main_scene->getRenderer().setRenderColiders(
|
|
!g_main_scene->getRenderer().getRenderColiders() );
|
|
break;
|
|
#endif
|
|
case SDLK_s:
|
|
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() )
|
|
g_menu_select = 0;
|
|
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 )
|
|
g_menu_select = g_menu_options.size() - 1;
|
|
g_menu_options[g_menu_select]->setColor(
|
|
colors["menu_item_background"] );
|
|
break;
|
|
case SDLK_RETURN:
|
|
switch ( g_menu_select ) {
|
|
case MAIN_MENU_RESUME: {
|
|
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
|
g_active_scenes.pop_back();
|
|
g_input_functions.pop_back();
|
|
} break;
|
|
case MAIN_MENU_OPTIONS:
|
|
g_update_scenes.push_back( g_options_scene );
|
|
g_active_scenes.push_back( g_options_scene );
|
|
g_input_functions.push_back( optionsSceneInput );
|
|
break;
|
|
case MAIN_MENU_RESTART:
|
|
resetGame();
|
|
break;
|
|
case MAIN_MENU_QUIT:
|
|
quitGame();
|
|
default:
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
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 ) {
|
|
for ( auto &x : g_active_scenes )
|
|
g_update_scenes.push_back( x );
|
|
g_update_size = true;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 ) {
|
|
#ifdef DEBUG
|
|
case SDLK_r:
|
|
g_main_scene->getRenderer().setRenderColiders(
|
|
!g_main_scene->getRenderer().getRenderColiders() );
|
|
break;
|
|
#endif
|
|
case SDLK_s:
|
|
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() )
|
|
g_game_over_select = 0;
|
|
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 )
|
|
g_game_over_select = g_game_over_options.size() - 1;
|
|
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:
|
|
#ifdef FEATURE
|
|
resumeBlocks();
|
|
#endif
|
|
resetGame();
|
|
break;
|
|
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 ) {
|
|
for ( auto &x : g_active_scenes )
|
|
g_update_scenes.push_back( x );
|
|
g_update_size = true;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void gameOverSceneInput(
|
|
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
|
|
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
|
|
pollEventsGameOver();
|
|
}
|
|
|
|
void saveOptions() {
|
|
if ( g_cur_shadow )
|
|
g_cur_shadow->setHidden( !g_show_shadow );
|
|
g_main_scene->setPrevTicks( SDL_GetTicks() );
|
|
}
|
|
|
|
void handleKeyDownOptions( SDL_Keycode key ) {
|
|
switch ( key ) {
|
|
#ifdef DEBUG
|
|
case SDLK_r:
|
|
g_main_scene->getRenderer().setRenderColiders(
|
|
!g_main_scene->getRenderer().getRenderColiders() );
|
|
break;
|
|
#endif
|
|
case SDLK_s:
|
|
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() )
|
|
g_options_select = 0;
|
|
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 )
|
|
g_options_select = g_options_options.size() - 1;
|
|
g_options_options[g_options_select]->setColor(
|
|
colors["menu_item_background"] );
|
|
break;
|
|
case SDLK_RIGHT:
|
|
case SDLK_d:
|
|
switch ( g_options_select ) {
|
|
case OPTIONS_MENU_COLOR_SCHEME:
|
|
selected_color_scheme++;
|
|
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() )
|
|
->changeText( "Color scheme: " +
|
|
color_schemes_names[selected_color_scheme] );
|
|
g_update_colors = true;
|
|
break;
|
|
case OPTIONS_MENU_SHADOW:
|
|
g_show_shadow = !g_show_shadow;
|
|
static_cast< SDLPP::TextRenderer * >(
|
|
g_options_options[OPTIONS_MENU_SHADOW].get() )
|
|
->changeText( std::string( "Show shadow: " ) +
|
|
( g_show_shadow ? "YES" : "NO" ) );
|
|
g_update_objects.push_back(
|
|
g_options_options[OPTIONS_MENU_SHADOW] );
|
|
break;
|
|
case OPTIONS_MENU_3D:
|
|
g_show_3d = !g_show_3d;
|
|
static_cast< SDLPP::TextRenderer * >(
|
|
g_options_options[OPTIONS_MENU_3D].get() )
|
|
->changeText( std::string( "Show block texture: " ) +
|
|
( g_show_3d ? "YES" : "NO" ) );
|
|
g_update_objects.push_back( g_options_options[OPTIONS_MENU_3D] );
|
|
g_update_3d = true;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case SDLK_LEFT:
|
|
case SDLK_a:
|
|
switch ( g_options_select ) {
|
|
case OPTIONS_MENU_COLOR_SCHEME:
|
|
if ( selected_color_scheme == 0 )
|
|
selected_color_scheme = color_schemes_names.size();
|
|
selected_color_scheme--;
|
|
static_cast< SDLPP::TextRenderer * >(
|
|
g_options_options[OPTIONS_MENU_COLOR_SCHEME].get() )
|
|
->changeText( "Color scheme: " +
|
|
color_schemes_names[selected_color_scheme] );
|
|
g_update_colors = true;
|
|
break;
|
|
case OPTIONS_MENU_SHADOW:
|
|
g_show_shadow = !g_show_shadow;
|
|
static_cast< SDLPP::TextRenderer * >(
|
|
g_options_options[OPTIONS_MENU_SHADOW].get() )
|
|
->changeText( std::string( "Show shadow: " ) +
|
|
( g_show_shadow ? "YES" : "NO" ) );
|
|
g_update_objects.push_back(
|
|
g_options_options[OPTIONS_MENU_SHADOW] );
|
|
break;
|
|
case OPTIONS_MENU_3D:
|
|
g_show_3d = !g_show_3d;
|
|
static_cast< SDLPP::TextRenderer * >(
|
|
g_options_options[OPTIONS_MENU_3D].get() )
|
|
->changeText( std::string( "Show block texture: " ) +
|
|
( g_show_3d ? "YES" : "NO" ) );
|
|
g_update_objects.push_back( g_options_options[OPTIONS_MENU_3D] );
|
|
g_update_3d = true;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case SDLK_RETURN:
|
|
switch ( g_options_select ) {
|
|
case OPTIONS_MENU_SAVE:
|
|
saveOptions();
|
|
g_active_scenes.pop_back();
|
|
g_input_functions.pop_back();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case SDLK_ESCAPE:
|
|
saveOptions();
|
|
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 ) {
|
|
for ( auto &x : g_active_scenes )
|
|
g_update_scenes.push_back( x );
|
|
g_update_size = true;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
void optionsSceneInput(
|
|
std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSED*/,
|
|
std::vector< std::shared_ptr< SDLPP::RenderObject > > & /*UNUSED*/ ) {
|
|
pollEventsOptions();
|
|
}
|