Make colors configurable

This commit is contained in:
zvon 2020-09-11 15:05:41 +02:00
parent 4a2f879448
commit 47349c06c1
3 changed files with 123 additions and 45 deletions

View File

@ -11,7 +11,7 @@ tetris: tetris.o sdlpp.o
sdlpp.o: ../sdlpp.cpp ../sdlpp.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
tetris.o: tetris.cpp ../sdlpp.hpp
tetris.o: tetris.cpp ../sdlpp.hpp config.hpp
$(CXX) $(CFLAGS) -c -o $@ $<
clean:

78
tetris/config.hpp Normal file
View File

@ -0,0 +1,78 @@
#include <map>
#include <string>
#include <vector>
std::map<std::string, std::map<std::string, std::string>> color_schemes = {
{ "default", {
{"piece_brick", "#FF0000"},
{"piece_brick_out", "#AA0000"},
{"piece_T", "#00FF00"},
{"piece_T_out", "#00AA00"},
{"piece_L_right", "#0000FF"},
{"piece_L_right_out", "#0000AA"},
{"piece_Z_right", "#FF00FF"},
{"piece_Z_right_out", "#AA00AA"},
{"piece_line", "#FFFF00"},
{"piece_line_out", "#AAAA00"},
{"piece_L_left", "#00FFFF"},
{"piece_L_left_out", "#00AAAA"},
{"piece_Z_left", "#FFFFFF"},
{"piece_Z_left_out", "#AAAAAA"},
{"shadow", "#AAAAAAAA"},
{"background", "#222222"},
{"line", "#888888"},
{"barrier", "#AA0000"},
{"text", "#FFFFFF"},
{"text_out", "#000000"},
{"menu_background", "#00000080"},
{"menu_item_background", "#FFFFFF40"},
}},
{ "gruvbox_dark", {
{"piece_brick", "#cc241d"},
{"piece_brick_out", "#fb4934"},
{"piece_T", "#98971a"},
{"piece_T_out", "#b8bb26"},
{"piece_L_right", "#458588"},
{"piece_L_right_out", "#83a598"},
{"piece_Z_right", "#b16286"},
{"piece_Z_right_out", "#d3869b"},
{"piece_line", "#d79921"},
{"piece_line_out", "#fabd2f"},
{"piece_L_left", "#689d6a"},
{"piece_L_left_out", "#8ec07c"},
{"piece_Z_left", "#a89984"},
{"piece_Z_left_out", "#ebdbb2"},
{"shadow", "#bdae9380"},
{"background", "#282828"},
{"line", "#fbf1c7"},
{"barrier", "#d65d0e"},
{"text", "#ebdbb2"},
{"text_out", "#1d2021"},
{"menu_background", "#28282880"},
{"menu_item_background", "#d5c4a180"},
}},
{ "blackandwhite", {
{"piece_brick", "#FFFFFF"},
{"piece_brick_out", "#000000"},
{"piece_T", "#FFFFFF"},
{"piece_T_out", "#000000"},
{"piece_L_right", "#FFFFFF"},
{"piece_L_right_out", "#000000"},
{"piece_Z_right", "#FFFFFF"},
{"piece_Z_right_out", "#000000"},
{"piece_line", "#FFFFFF"},
{"piece_line_out", "#000000"},
{"piece_L_left", "#FFFFFF"},
{"piece_L_left_out", "#000000"},
{"piece_Z_left", "#FFFFFF"},
{"piece_Z_left_out", "#000000"},
{"shadow", "#FFFFFF80"},
{"background", "#000000"},
{"line", "#FFFFFF"},
{"barrier", "#FFFFFF"},
{"text", "#FFFFFF"},
{"text_out", "#000000"},
{"menu_background", "#00000080"},
{"menu_item_background", "#FFFFFF40"},
}},
};

View File

@ -1,4 +1,5 @@
#include "../sdlpp.hpp"
#include "config.hpp"
#include <thread>
#include <chrono>
#include <mutex>
@ -50,6 +51,8 @@ bool update_score = false;
bool checked_line = false;
bool wait_for_anim = false;
auto &colors = color_schemes["gruvbox_dark"];
std::vector< int > bag = { 28, 28, 28, 28, 28, 28, 28 };
std::shared_ptr< SDLPP::Font > font;
@ -237,7 +240,7 @@ public:
void turnIntoShadow() {
for(auto &block : getObjects() ) {
block->setId(SHADOW_ID);
block->setColor("#AAAAAAAA");
block->setColor(colors["shadow"]);
}
}
std::shared_ptr<TetrisPiece> copySelf() {
@ -305,8 +308,8 @@ std::shared_ptr< TetrisPiece >
tetrisBrick( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#FF0000";
auto outline = "#AA0000";
auto color = colors["piece_brick"];
auto outline = colors["piece_brick_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_BRICK, renderer,
scene ),
@ -330,8 +333,8 @@ std::shared_ptr< TetrisPiece >
tetrisT( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#00FF00";
auto outline = "#00AA00";
auto color = colors["piece_T"];
auto outline = colors["piece_T_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE,
TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_T, renderer, scene ),
@ -353,8 +356,8 @@ std::shared_ptr< TetrisPiece >
tetrisLRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#0000FF";
auto outline = "#0000AA";
auto color = colors["piece_L_right"];
auto outline = colors["piece_L_right_out"];
retPiece->addPiece(
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_L_RIGHT, renderer, scene ),
@ -378,8 +381,8 @@ std::shared_ptr< TetrisPiece >
tetrisZRight( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#FF00FF";
auto outline = "#AA00AA";
auto color = colors["piece_Z_right"];
auto outline = colors["piece_Z_right_out"];
retPiece->addPiece(
createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER + BLOCK_SIZE, color,
outline, TETRIS_Z_RIGHT, renderer, scene ),
@ -402,8 +405,8 @@ std::shared_ptr< TetrisPiece >
tetrisLine( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#FFFF00";
auto outline = "#AAAA00";
auto color = colors["piece_line"];
auto outline = colors["piece_line_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - 2 * BLOCK_SIZE, TOP_BORDER,
color, outline, TETRIS_LINE,
renderer, scene ),
@ -426,8 +429,8 @@ std::shared_ptr< TetrisPiece >
tetrisLLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#00FFFF";
auto outline = "#00AAAA";
auto color = colors["piece_L_left"];
auto outline = colors["piece_L_left_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_L_LEFT, renderer,
scene ),
@ -451,8 +454,8 @@ std::shared_ptr< TetrisPiece >
tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
std::shared_ptr< SDLPP::Scene > scene ) {
auto retPiece = std::make_shared< TetrisPiece >();
auto color = "#FFFFFF";
auto outline = "#AAAAAA";
auto color = colors["piece_Z_left"];
auto outline = colors["piece_Z_left_out"];
retPiece->addPiece( createTetrisBlock( 0.5 - BLOCK_SIZE, TOP_BORDER, color,
outline, TETRIS_Z_LEFT, renderer,
scene ),
@ -480,7 +483,7 @@ std::vector< std::shared_ptr< TetrisPiece > ( * )(
void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#222222", true );
colors["background"], true );
bg->setPermanent();
scene.addObject( bg );
@ -500,7 +503,7 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
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, "#AAAAAA" );
auto line = std::make_shared< SDLPP::LineRenderer >( LEFT_BORDER, posy, RIGHT_BORDER, posy, r, colors["line"] );
line->setStatic();
line->centerX();
scene.addObject( line );
@ -509,36 +512,35 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
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, "#AAAAAA" );
auto line = std::make_shared< SDLPP::LineRenderer >( posx, TOP_BORDER + BLOCK_SIZE, posx, BOTTOM_BORDER, r, colors["line"] );
line->setStatic();
line->centerX();
scene.addObject( line );
}
auto left_barrier = std::make_shared< SDLPP::RectangleRender >(
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, "#AA0000", true );
LEFT_BORDER - 0.02, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"], true );
left_barrier->centerX();
left_barrier->setStatic();
scene.addObject( left_barrier );
auto right_barrier = std::make_shared< SDLPP::RectangleRender >(
RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, "#AA0000", true );
RIGHT_BORDER, 0, 0.02, BOTTOM_BORDER, r, colors["barrier"], true );
right_barrier->centerX();
right_barrier->setStatic();
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, "#AA0000", true );
0.02, r, colors["barrier"], true );
bottom_barrier->centerX();
bottom_barrier->setStatic();
scene.addObject( bottom_barrier );
auto tetris = std::make_shared< SDLPP::TextRenderer >(
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", "FFFFFF", "000000", 5 );
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"], colors["text_out"], 5 );
tetris->centerX();
tetris->setStatic();
scene.addObject( tetris );
auto next = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", "FFFFFF",
"000000", 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();
scene.addObject( next );
@ -552,15 +554,14 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
gameover->setStatic();
scene.addObject( gameover );
auto score_text = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", "#FFFFFF",
"#000000", 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();
scene.addObject( score_text );
score_texture = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, std::to_string( score ),
"FFFFFF", "000000", 5, SDLPP_TEXT_TOP );
colors["text"], colors["text_out"], 5, SDLPP_TEXT_TOP );
score_texture->centerX();
score_texture->setStatic();
scene.addObject( score_texture );
@ -599,37 +600,36 @@ void addStuff( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
}
void updateScore() {
score_texture->setText( *font, std::to_string( score ), "#FFFFFF",
"#000000", 5 );
score_texture->setText( *font, std::to_string( score ), colors["text"], colors["text_out"], 5 );
}
void addPause( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#00000080", true );
colors["menu_background"], true );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "PAUSED", "#FFFFFF", "#000000", 5 );
y->setText( *font, "PAUSED", colors["text"], colors["text_out"], 5 );
y->setId( 0 );
y->centerX();
scene.addObject( y );
auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.51, 0.2, 0.08, r );
resume->setText( *font, "Resume", "#FFFFFF", "#000000", 5 );
resume->setColor( "#FFFFFF40" );
resume->setText( *font, "Resume", colors["text"], colors["text_out"], 5 );
resume->setColor( colors["menu_item_background"] );
resume->centerX();
scene.addObject( resume );
pause_options.push_back( resume );
auto restart =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.61, 0.2, 0.08, r );
restart->setText( *font, "Restart", "#FFFFFF", "#000000", 5 );
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 5 );
restart->centerX();
scene.addObject( restart );
pause_options.push_back( restart );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.71, 0.2, 0.08, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 5 );
quit->centerX();
scene.addObject( quit );
pause_options.push_back( quit );
@ -637,25 +637,25 @@ void addPause( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
void addGameOver( SDLPP::Scene &scene, std::shared_ptr< SDLPP::Renderer > &r ) {
auto bg = std::make_shared< SDLPP::RectangleRender >( 0, 0, 10, 10, r,
"#00000080", true );
colors["menu_background"], true );
bg->setId( 123 );
bg->setPermanent( true );
scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "GAME OVER", "#FFFFFF", "#000000", 5 );
y->setText( *font, "GAME OVER", colors["text"], colors["text_out"], 5 );
y->setId( 0 );
y->centerX();
scene.addObject( y );
auto restart =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
restart->setText( *font, "Restart", "#FFFFFF", "#000000", 5 );
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 5 );
restart->centerX();
restart->setColor( "#FFFFFF40" );
restart->setColor( colors["menu_item_background"] );
scene.addObject( restart );
game_over_options.push_back( restart );
auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", "#FFFFFF", "#000000", 5 );
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 5 );
quit->centerX();
scene.addObject( quit );
game_over_options.push_back( quit );
@ -884,7 +884,7 @@ void handleKeyDownPause( SDL_Keycode key ) {
pause_select++;
if ( pause_select > pause_max )
pause_select = 0;
pause_options[pause_select]->setColor( "FFFFFF40" );
pause_options[pause_select]->setColor( colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
@ -892,7 +892,7 @@ void handleKeyDownPause( SDL_Keycode key ) {
pause_select--;
if ( pause_select < 0 )
pause_select = pause_max;
pause_options[pause_select]->setColor( "FFFFFF40" );
pause_options[pause_select]->setColor( colors["menu_item_background"] );
break;
case SDLK_RETURN:
switch ( pause_select ) {
@ -942,7 +942,7 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
game_over_select++;
if ( game_over_select > game_over_max )
game_over_select = 0;
game_over_options[game_over_select]->setColor( "FFFFFF40" );
game_over_options[game_over_select]->setColor( colors["menu_item_background"] );
break;
case SDLK_w:
case SDLK_UP:
@ -950,7 +950,7 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
game_over_select--;
if ( game_over_select < 0 )
game_over_select = game_over_max;
game_over_options[game_over_select]->setColor( "FFFFFF40" );
game_over_options[game_over_select]->setColor( colors["menu_item_background"] );
break;
case SDLK_RETURN:
switch ( game_over_select ) {