TETRIS: use new TextRenderer interface

This commit is contained in:
zv0n 2021-01-30 16:48:40 +01:00
parent a818c567fc
commit b65b0bf034
3 changed files with 42 additions and 50 deletions

View File

@ -390,13 +390,6 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
return retPiece; return retPiece;
} }
void updateTextSizeInternal(std::shared_ptr<SDLPP::RenderObject> input) {
if(input->getKilled())
return;
static_cast< SDLPP::TextRenderer* >( input.get() )->setTextColor(
*g_font, colors["text"], colors["text_out"], 0.1 );
}
void updateSize() { void updateSize() {
// have to resize rectangles for the text to be resized correctly // have to resize rectangles for the text to be resized correctly
g_main_scene->updateSizeAndPosition(); g_main_scene->updateSizeAndPosition();
@ -404,19 +397,20 @@ void updateSize() {
g_game_over_scene->updateSizeAndPosition(); g_game_over_scene->updateSizeAndPosition();
g_options_scene->updateSizeAndPosition(); g_options_scene->updateSizeAndPosition();
std::unordered_set<int> textObjects = { TEXT_ID, MENU_TEXT_ID, MENU_ITEM_ID }; std::unordered_set< int > textObjects = { TEXT_ID, MENU_TEXT_ID,
MENU_ITEM_ID };
for ( auto &x : g_main_scene->getObjects( textObjects ) ) { for ( auto &x : g_main_scene->getObjects( textObjects ) ) {
updateTextSizeInternal(x); x->updateSizeAndPosition();
} }
updateTextSizeInternal(g_score_texture); g_score_texture->updateSizeAndPosition();
for ( auto &x : g_menu_scene->getObjects( textObjects ) ) { for ( auto &x : g_menu_scene->getObjects( textObjects ) ) {
updateTextSizeInternal(x); x->updateSizeAndPosition();
} }
for ( auto &x : g_game_over_scene->getObjects( textObjects ) ) { for ( auto &x : g_game_over_scene->getObjects( textObjects ) ) {
updateTextSizeInternal(x); x->updateSizeAndPosition();
} }
for ( auto &x : g_options_scene->getObjects( textObjects ) ) { for ( auto &x : g_options_scene->getObjects( textObjects ) ) {
updateTextSizeInternal(x); x->updateSizeAndPosition();
} }
} }
@ -438,7 +432,7 @@ void updateColors() {
} }
for ( auto &x : g_main_scene->getObjects( { TEXT_ID } ) ) { for ( auto &x : g_main_scene->getObjects( { TEXT_ID } ) ) {
static_cast< SDLPP::TextRenderer * >( x.get() )->setTextColor( static_cast< SDLPP::TextRenderer * >( x.get() )->setTextColor(
*g_font, colors["text"], colors["text_out"], 0.1 ); g_font, colors["text"], colors["text_out"], 0.1 );
} }
g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] ); g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] );
g_game_over_options[g_game_over_select]->setColor( g_game_over_options[g_game_over_select]->setColor(

View File

@ -90,15 +90,15 @@ void addMainSceneItems( SDLPP::Scene &scene,
scene.addObject( bottom_barrier ); scene.addObject( bottom_barrier );
auto tetris = std::make_shared< SDLPP::TextRenderer >( auto tetris = std::make_shared< SDLPP::TextRenderer >(
0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"], 0.4, 0, 0.2, 0.1, r, font, "TETRIS", colors["text"], colors["text_out"],
colors["text_out"], 0.1 ); 0.1 );
tetris->centerX(); tetris->centerX();
tetris->setStatic(); tetris->setStatic();
tetris->setId( TEXT_ID ); tetris->setId( TEXT_ID );
scene.addObject( tetris ); scene.addObject( tetris );
auto next = std::make_shared< SDLPP::TextRenderer >( auto next = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, *font, "NEXT", colors["text"], RIGHT_BORDER + 0.1, 0.35, 0.2, 0.1, r, font, "NEXT", colors["text"],
colors["text_out"], 0.1, SDLPP_TEXT_CENTER ); colors["text_out"], 0.1, SDLPP_TEXT_CENTER );
next->centerX(); next->centerX();
next->setStatic(); next->setStatic();
@ -117,7 +117,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
scene.addObject( gameover ); scene.addObject( gameover );
auto score_text = std::make_shared< SDLPP::TextRenderer >( auto score_text = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, *font, "SCORE", colors["text"], RIGHT_BORDER + 0.1, 0.1, 0.2, 0.1, r, font, "SCORE", colors["text"],
colors["text_out"], 0.1, SDLPP_TEXT_CENTER ); colors["text_out"], 0.1, SDLPP_TEXT_CENTER );
score_text->centerX(); score_text->centerX();
score_text->setStatic(); score_text->setStatic();
@ -125,7 +125,7 @@ void addMainSceneItems( SDLPP::Scene &scene,
scene.addObject( score_text ); scene.addObject( score_text );
auto score_texture = std::make_shared< SDLPP::TextRenderer >( auto score_texture = std::make_shared< SDLPP::TextRenderer >(
RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, *font, "0", colors["text"], RIGHT_BORDER + 0.1, 0.2, 0.2, 0.1, r, font, "0", colors["text"],
colors["text_out"], 0.1, SDLPP_TEXT_TOP ); colors["text_out"], 0.1, SDLPP_TEXT_TOP );
score_texture->centerX(); score_texture->centerX();
score_texture->setStatic(); score_texture->setStatic();
@ -169,13 +169,13 @@ void addMenuSceneItems( SDLPP::Scene &scene,
bg->setPermanent( true ); bg->setPermanent( true );
scene.addObject( bg ); scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r ); auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "PAUSED", colors["text"], colors["text_out"], 0.1 ); y->setText( font, "PAUSED", colors["text"], colors["text_out"], 0.1 );
y->setId( MENU_TEXT_ID ); y->setId( MENU_TEXT_ID );
y->centerX(); y->centerX();
scene.addObject( y ); scene.addObject( y );
auto resume = auto resume =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.46, 0.2, 0.08, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.46, 0.2, 0.08, r );
resume->setText( *font, "Resume", colors["text"], colors["text_out"], 0.1 ); resume->setText( font, "Resume", colors["text"], colors["text_out"], 0.1 );
resume->setColor( colors["menu_item_background"] ); resume->setColor( colors["menu_item_background"] );
resume->centerX(); resume->centerX();
resume->setId( MENU_ITEM_ID ); resume->setId( MENU_ITEM_ID );
@ -183,21 +183,23 @@ void addMenuSceneItems( SDLPP::Scene &scene,
scene.addObject( resume ); scene.addObject( resume );
auto options = auto options =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.56, 0.2, 0.08, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.56, 0.2, 0.08, r );
options->setText( *font, "Options", colors["text"], colors["text_out"], 0.1 ); options->setText( font, "Options", colors["text"], colors["text_out"],
0.1 );
options->centerX(); options->centerX();
options->setId( MENU_ITEM_ID ); options->setId( MENU_ITEM_ID );
g_menu_options.push_back( options ); g_menu_options.push_back( options );
scene.addObject( options ); scene.addObject( options );
auto restart = auto restart =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.66, 0.2, 0.08, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.66, 0.2, 0.08, r );
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 0.1 ); restart->setText( font, "Restart", colors["text"], colors["text_out"],
0.1 );
restart->centerX(); restart->centerX();
restart->setId( MENU_ITEM_ID ); restart->setId( MENU_ITEM_ID );
g_menu_options.push_back( restart ); g_menu_options.push_back( restart );
scene.addObject( restart ); scene.addObject( restart );
auto quit = auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.76, 0.2, 0.08, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.76, 0.2, 0.08, r );
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 0.1 ); quit->setText( font, "Quit Game", colors["text"], colors["text_out"], 0.1 );
quit->centerX(); quit->centerX();
quit->setId( MENU_ITEM_ID ); quit->setId( MENU_ITEM_ID );
g_menu_options.push_back( quit ); g_menu_options.push_back( quit );
@ -213,13 +215,14 @@ void addGameOverSceneItems( SDLPP::Scene &scene,
bg->setPermanent( true ); bg->setPermanent( true );
scene.addObject( bg ); scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r ); auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "GAME OVER", colors["text"], colors["text_out"], 0.1 ); y->setText( font, "GAME OVER", colors["text"], colors["text_out"], 0.1 );
y->setId( 0 ); y->setId( 0 );
y->centerX(); y->centerX();
scene.addObject( y ); scene.addObject( y );
auto restart = auto restart =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.5, 0.2, 0.1, r );
restart->setText( *font, "Restart", colors["text"], colors["text_out"], 0.1 ); restart->setText( font, "Restart", colors["text"], colors["text_out"],
0.1 );
restart->centerX(); restart->centerX();
restart->setColor( colors["menu_item_background"] ); restart->setColor( colors["menu_item_background"] );
restart->setId( MENU_ITEM_ID ); restart->setId( MENU_ITEM_ID );
@ -227,7 +230,7 @@ void addGameOverSceneItems( SDLPP::Scene &scene,
scene.addObject( restart ); scene.addObject( restart );
auto quit = auto quit =
std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r ); std::make_shared< SDLPP::TextRenderer >( 0.4, 0.7, 0.2, 0.1, r );
quit->setText( *font, "Quit Game", colors["text"], colors["text_out"], 0.1 ); quit->setText( font, "Quit Game", colors["text"], colors["text_out"], 0.1 );
quit->centerX(); quit->centerX();
quit->setId( MENU_ITEM_ID ); quit->setId( MENU_ITEM_ID );
g_game_over_options.push_back( quit ); g_game_over_options.push_back( quit );
@ -243,14 +246,14 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
bg->setPermanent( true ); bg->setPermanent( true );
scene.addObject( bg ); scene.addObject( bg );
auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r ); auto y = std::make_shared< SDLPP::TextRenderer >( 0.25, 0.1, 0.5, 0.3, r );
y->setText( *font, "OPTIONS", colors["text"], colors["text_out"], 0.1 ); y->setText( font, "OPTIONS", colors["text"], colors["text_out"], 0.1 );
y->setId( 0 ); y->setId( 0 );
y->centerX(); y->centerX();
scene.addObject( y ); scene.addObject( y );
auto color_scheme = auto color_scheme =
std::make_shared< SDLPP::TextRenderer >( 0.18, 0.35, 0.64, 0.09, r ); std::make_shared< SDLPP::TextRenderer >( 0.18, 0.35, 0.64, 0.09, r );
color_scheme->setText( color_scheme->setText(
*font, "Color scheme: " + color_schemes_names[selected_color_scheme], font, "Color scheme: " + color_schemes_names[selected_color_scheme],
colors["text"], colors["text_out"], 0.1 ); colors["text"], colors["text_out"], 0.1 );
color_scheme->centerX(); color_scheme->centerX();
color_scheme->setColor( colors["menu_item_background"] ); color_scheme->setColor( colors["menu_item_background"] );
@ -259,7 +262,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
scene.addObject( color_scheme ); scene.addObject( color_scheme );
auto shadow = auto shadow =
std::make_shared< SDLPP::TextRenderer >( 0.26, 0.45, 0.48, 0.09, r ); std::make_shared< SDLPP::TextRenderer >( 0.26, 0.45, 0.48, 0.09, r );
shadow->setText( *font, "Show shadow: YES", colors["text"], shadow->setText( font, "Show shadow: YES", colors["text"],
colors["text_out"], 0.1 ); colors["text_out"], 0.1 );
shadow->centerX(); shadow->centerX();
shadow->setId( MENU_ITEM_ID ); shadow->setId( MENU_ITEM_ID );
@ -267,7 +270,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
scene.addObject( shadow ); scene.addObject( shadow );
auto show3d = auto show3d =
std::make_shared< SDLPP::TextRenderer >( 0.2, 0.55, 0.6, 0.09, r ); std::make_shared< SDLPP::TextRenderer >( 0.2, 0.55, 0.6, 0.09, r );
show3d->setText( *font, "Show block texture: NO", colors["text"], show3d->setText( font, "Show block texture: NO", colors["text"],
colors["text_out"], 0.1 ); colors["text_out"], 0.1 );
show3d->centerX(); show3d->centerX();
show3d->setId( MENU_ITEM_ID ); show3d->setId( MENU_ITEM_ID );
@ -275,7 +278,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene,
scene.addObject( show3d ); scene.addObject( show3d );
auto save = auto save =
std::make_shared< SDLPP::TextRenderer >( 0.42, 0.65, 0.16, 0.09, r ); std::make_shared< SDLPP::TextRenderer >( 0.42, 0.65, 0.16, 0.09, r );
save->setText( *font, "SAVE", colors["text"], colors["text_out"], 0.1 ); save->setText( font, "SAVE", colors["text"], colors["text_out"], 0.1 );
save->centerX(); save->centerX();
save->setId( MENU_ITEM_ID ); save->setId( MENU_ITEM_ID );
g_options_options.push_back( save ); g_options_options.push_back( save );
@ -705,7 +708,6 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_SHADOW].get() ) g_options_options[OPTIONS_MENU_SHADOW].get() )
->changeText( std::string( "Show shadow: " ) + ->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) ); ( g_show_shadow ? "YES" : "NO" ) );
g_update_colors = true;
break; break;
case OPTIONS_MENU_3D: case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d; g_show_3d = !g_show_3d;
@ -713,7 +715,6 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_3D].get() ) g_options_options[OPTIONS_MENU_3D].get() )
->changeText( std::string( "Show block texture: " ) + ->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) ); ( g_show_3d ? "YES" : "NO" ) );
g_update_colors = true;
default: default:
break; break;
} }
@ -737,7 +738,6 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_SHADOW].get() ) g_options_options[OPTIONS_MENU_SHADOW].get() )
->changeText( std::string( "Show shadow: " ) + ->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) ); ( g_show_shadow ? "YES" : "NO" ) );
g_update_colors = true;
break; break;
case OPTIONS_MENU_3D: case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d; g_show_3d = !g_show_3d;
@ -745,7 +745,6 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_3D].get() ) g_options_options[OPTIONS_MENU_3D].get() )
->changeText( std::string( "Show block texture: " ) + ->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) ); ( g_show_3d ? "YES" : "NO" ) );
g_update_colors = true;
default: default:
break; break;
} }

View File

@ -18,9 +18,8 @@
std::vector< std::shared_ptr< SDLPP::RenderObject > > line_coliders{}; std::vector< std::shared_ptr< SDLPP::RenderObject > > line_coliders{};
void updateScore( std::shared_ptr< SDLPP::Font > font ) { void updateScore() {
g_score_texture->setText( *font, std::to_string( g_score ), colors["text"], g_score_texture->changeText( std::to_string( g_score ) );
colors["text_out"], 0.1 );
} }
void doInput() { void doInput() {
@ -131,7 +130,7 @@ int main() {
g_checked_line = false; g_checked_line = false;
} }
if ( g_update_score ) { if ( g_update_score ) {
updateScore( g_font ); updateScore();
g_update_score = false; g_update_score = false;
} }
if ( g_update_colors ) { if ( g_update_colors ) {