Tetris: add block texture

This commit is contained in:
zvon 2020-09-29 18:22:26 +02:00
parent eb60c88dac
commit 7621dc8dd4
8 changed files with 61 additions and 27 deletions

BIN
tetris/block.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -17,6 +17,7 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{"piece_Z_left", "#FFFFFF"},
{"piece_Z_left_out", "#AAAAAA"},
{"shadow", "#AAAAAAAA"},
{"shadow_out", "#888888"},
{"background", "#222222"},
{"line", "#888888"},
{"barrier", "#AA0000"},
@ -41,6 +42,7 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{"piece_Z_left", "#a89984"},
{"piece_Z_left_out", "#ebdbb2"},
{"shadow", "#bdae9380"},
{"shadow_out", "#fbf1c7"},
{"background", "#282828"},
{"line", "#fbf1c7"},
{"barrier", "#d65d0e"},
@ -65,6 +67,7 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{"piece_Z_left", "#7c6f64"},
{"piece_Z_left_out", "#3c3836"},
{"shadow", "#665c5480"},
{"shadow_out", "#282828"},
{"background", "#d5c4a1"},
{"line", "#282828"},
{"barrier", "#d65d0e"},
@ -89,6 +92,7 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{"piece_Z_left", "#FFFFFF"},
{"piece_Z_left_out", "#000000"},
{"shadow", "#FFFFFF80"},
{"shadow_out", "#FFFFFF"},
{"background", "#000000"},
{"line", "#FFFFFF"},
{"barrier", "#FFFFFF"},
@ -101,3 +105,4 @@ std::map<std::string, std::map<std::string, std::string>> color_schemes = {
std::vector<std::string> color_schemes_names = { "default", "gruvbox_dark", "gruvbox_light", "blackandwhite" };
long unsigned int selected_color_scheme = 0;
bool g_show_shadow = true;
bool g_show_3d = false;

View File

@ -34,6 +34,7 @@
#define PIECE_LINE 4
#define PIECE_L_LEFT 5
#define PIECE_Z_LEFT 6
#define PIECE_SHADOW 7
#define TICKS_TILL_FALL 500
#define TICKS_TILL_DESCEND 50
@ -58,5 +59,6 @@ extern std::map<std::string, std::map<std::string, std::string>> color_schemes;
extern std::vector<std::string> color_schemes_names;
extern long unsigned int selected_color_scheme;
extern bool g_show_shadow;
extern bool g_show_3d;
#endif

View File

@ -15,6 +15,9 @@ public:
_index = index;
pieces_bag[_index]--;
_scene = scene;
setColors();
if(g_show_3d)
setTexture("block.png");
}
TetrisBlock( const TetrisBlock &other ) : TetrisBlock(other.getDoubleRect().first.first,other.getDoubleRect().first.second,other.getDoubleRect().second.first,other.getDoubleRect().second.second,other.getRenderer(), other.getColor(), true, other._index, other._scene, other.pieces_bag) {}
~TetrisBlock() {
@ -40,14 +43,21 @@ public:
virtual void specialAction( int code ) override {
switch(code) {
case PIECE_ACTION_UPDATE_COLOR: {
auto piece_name = getPieceName();
setColor(colors[piece_name]);
setOutlineColor(colors[piece_name + "_out"]);
setColors();
if(g_show_3d)
setTexture("block.png");
else
unsetTexture();
}
default:
break;
}
}
void turnIntoShadow() {
setId(SHADOW_ID);
_index = PIECE_SHADOW;
setColors();
}
private:
std::string getPieceName() {
@ -66,11 +76,18 @@ private:
return "piece_L_left";
case PIECE_Z_LEFT:
return "piece_Z_left";
case PIECE_SHADOW:
return "shadow";
default:
break;
}
return "";
}
void setColors() {
auto piece_name = getPieceName();
setColor(colors[piece_name]);
setOutlineColor(colors[piece_name + "_out"]);
}
int _index = 0;
std::shared_ptr<SDLPP::Scene> _scene;
std::vector<int> &pieces_bag;
@ -200,10 +217,8 @@ public:
}
void turnIntoShadow() {
for(auto &block : getObjects() ) {
block->setId(SHADOW_ID);
block->setColor(colors["shadow"]);
}
for(auto &block : getObjects() )
block->turnIntoShadow();
setHidden(!g_show_shadow);
}
std::shared_ptr<TetrisPiece> copySelf() {

View File

@ -381,7 +381,7 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
}
void updateColors() {
for(auto &x : g_main_scene->getObjects({BRICK_ID})) {
for(auto &x : g_main_scene->getObjects({BRICK_ID, SHADOW_ID})) {
x->specialAction(PIECE_ACTION_UPDATE_COLOR);
}
for(auto &x : g_main_scene->getObjects({BARRIER_ID})) {

View File

@ -7,11 +7,8 @@ int g_ticks_till_descend = TICKS_TILL_DESCEND;
int g_ticks_till_movement = TICKS_TILL_MOVE;
int g_menu_select = 0;
int g_menu_max = 3;
int g_game_over_select = 0;
int g_game_over_max = 1;
int g_options_select = 0;
int g_options_max = 2;
std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_menu_options{};
std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_game_over_options{};
std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_options_options{};

View File

@ -11,11 +11,8 @@ extern int g_ticks_till_descend;
extern int g_ticks_till_movement;
extern int g_menu_select;
extern int g_menu_max;
extern int g_game_over_select;
extern int g_game_over_max;
extern int g_options_select;
extern int g_options_max;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_menu_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_game_over_options;
extern std::vector< std::shared_ptr< SDLPP::RectangleRender > > g_options_options;

View File

@ -15,7 +15,8 @@ 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_SAVE = 2;
constexpr uint64_t OPTIONS_MENU_3D = 2;
constexpr uint64_t OPTIONS_MENU_SAVE = 3;
// Scene preparation
@ -229,7 +230,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer
y->centerX();
scene.addObject( y );
auto color_scheme =
std::make_shared< SDLPP::TextRenderer >( 0.35, 0.4, 0.3, 0.09, r );
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->centerX();
color_scheme->setColor( colors["menu_item_background"] );
@ -237,12 +238,19 @@ void addOptionsSceneItems( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer
g_options_options.push_back(color_scheme);
scene.addObject( color_scheme );
auto shadow =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.09, r );
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->centerX();
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->centerX();
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 );
@ -454,7 +462,7 @@ void handleKeyDownMenu( SDL_Keycode key ) {
case SDLK_DOWN:
g_menu_options[g_menu_select]->unsetColor();
g_menu_select++;
if ( g_menu_select > g_menu_max )
if ( g_menu_select >= g_menu_options.size() )
g_menu_select = 0;
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
break;
@ -463,7 +471,7 @@ void handleKeyDownMenu( SDL_Keycode key ) {
g_menu_options[g_menu_select]->unsetColor();
g_menu_select--;
if ( g_menu_select < 0 )
g_menu_select = g_menu_max;
g_menu_select = g_menu_options.size() - 1;
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
break;
case SDLK_RETURN:
@ -529,7 +537,7 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
case SDLK_DOWN:
g_game_over_options[g_game_over_select]->unsetColor();
g_game_over_select++;
if ( g_game_over_select > g_game_over_max )
if ( 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;
@ -538,7 +546,7 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
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_max;
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:
@ -601,7 +609,7 @@ void handleKeyDownOptions( SDL_Keycode key ) {
case SDLK_DOWN:
g_options_options[g_options_select]->unsetColor();
g_options_select++;
if ( g_options_select > g_options_max )
if ( g_options_select >= g_options_options.size() )
g_options_select = 0;
g_options_options[g_options_select]->setColor( colors["menu_item_background"] );
break;
@ -610,7 +618,7 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[g_options_select]->unsetColor();
g_options_select--;
if ( g_options_select < 0 )
g_options_select = g_options_max;
g_options_select = g_options_options.size() - 1;
g_options_options[g_options_select]->setColor( colors["menu_item_background"] );
break;
case SDLK_RIGHT:
@ -620,12 +628,17 @@ void handleKeyDownOptions( SDL_Keycode key ) {
selected_color_scheme++;
if(selected_color_scheme >= color_schemes_names.size())
selected_color_scheme = 0;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[0])->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[1])->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"));
g_update_colors = true;
default:
break;
@ -638,12 +651,17 @@ void handleKeyDownOptions( SDL_Keycode key ) {
if(selected_color_scheme == 0)
selected_color_scheme = color_schemes_names.size();
selected_color_scheme--;
std::dynamic_pointer_cast<SDLPP::TextRenderer>(g_options_options[0])->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[1])->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"));
g_update_colors = true;
default:
break;