From b65b0bf03436589d25f711a46252fee56a6ccfa7 Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 30 Jan 2021 16:48:40 +0100 Subject: [PATCH] TETRIS: use new TextRenderer interface --- tetris/functions.cpp | 32 +++++++++++--------------- tetris/scenes.cpp | 53 ++++++++++++++++++++++---------------------- tetris/tetris.cpp | 7 +++--- 3 files changed, 42 insertions(+), 50 deletions(-) diff --git a/tetris/functions.cpp b/tetris/functions.cpp index 09e949e..0947176 100644 --- a/tetris/functions.cpp +++ b/tetris/functions.cpp @@ -390,13 +390,6 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer, return retPiece; } -void updateTextSizeInternal(std::shared_ptr input) { - if(input->getKilled()) - return; - static_cast< SDLPP::TextRenderer* >( input.get() )->setTextColor( - *g_font, colors["text"], colors["text_out"], 0.1 ); -} - void updateSize() { // have to resize rectangles for the text to be resized correctly g_main_scene->updateSizeAndPosition(); @@ -404,19 +397,20 @@ void updateSize() { g_game_over_scene->updateSizeAndPosition(); g_options_scene->updateSizeAndPosition(); - std::unordered_set textObjects = { TEXT_ID, MENU_TEXT_ID, MENU_ITEM_ID }; - for( auto &x : g_main_scene->getObjects( textObjects ) ) { - updateTextSizeInternal(x); + std::unordered_set< int > textObjects = { TEXT_ID, MENU_TEXT_ID, + MENU_ITEM_ID }; + for ( auto &x : g_main_scene->getObjects( textObjects ) ) { + x->updateSizeAndPosition(); } - updateTextSizeInternal(g_score_texture); - for( auto &x : g_menu_scene->getObjects( textObjects ) ) { - updateTextSizeInternal(x); + g_score_texture->updateSizeAndPosition(); + for ( auto &x : g_menu_scene->getObjects( textObjects ) ) { + x->updateSizeAndPosition(); } - for( auto &x : g_game_over_scene->getObjects( textObjects ) ) { - updateTextSizeInternal(x); + for ( auto &x : g_game_over_scene->getObjects( textObjects ) ) { + x->updateSizeAndPosition(); } - for( auto &x : g_options_scene->getObjects( textObjects ) ) { - updateTextSizeInternal(x); + for ( auto &x : g_options_scene->getObjects( textObjects ) ) { + x->updateSizeAndPosition(); } } @@ -437,8 +431,8 @@ void updateColors() { x->setColor( colors["line"] ); } for ( auto &x : g_main_scene->getObjects( { TEXT_ID } ) ) { - static_cast< SDLPP::TextRenderer* >( x.get() )->setTextColor( - *g_font, colors["text"], colors["text_out"], 0.1 ); + static_cast< SDLPP::TextRenderer * >( x.get() )->setTextColor( + g_font, colors["text"], colors["text_out"], 0.1 ); } g_menu_options[g_menu_select]->setColor( colors["menu_item_background"] ); g_game_over_options[g_game_over_select]->setColor( diff --git a/tetris/scenes.cpp b/tetris/scenes.cpp index a761b39..fe5c4f3 100644 --- a/tetris/scenes.cpp +++ b/tetris/scenes.cpp @@ -90,15 +90,15 @@ void addMainSceneItems( SDLPP::Scene &scene, scene.addObject( bottom_barrier ); auto tetris = std::make_shared< SDLPP::TextRenderer >( - 0.4, 0, 0.2, 0.1, r, *font, "TETRIS", colors["text"], - colors["text_out"], 0.1 ); + 0.4, 0, 0.2, 0.1, r, font, "TETRIS", colors["text"], colors["text_out"], + 0.1 ); tetris->centerX(); 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, *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 ); next->centerX(); next->setStatic(); @@ -117,7 +117,7 @@ void addMainSceneItems( SDLPP::Scene &scene, scene.addObject( gameover ); 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 ); score_text->centerX(); score_text->setStatic(); @@ -125,7 +125,7 @@ void addMainSceneItems( SDLPP::Scene &scene, scene.addObject( score_text ); 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 ); score_texture->centerX(); score_texture->setStatic(); @@ -169,13 +169,13 @@ void addMenuSceneItems( SDLPP::Scene &scene, 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", colors["text"], colors["text_out"], 0.1 ); + y->setText( font, "PAUSED", colors["text"], colors["text_out"], 0.1 ); y->setId( MENU_TEXT_ID ); y->centerX(); scene.addObject( y ); auto resume = 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->centerX(); resume->setId( MENU_ITEM_ID ); @@ -183,21 +183,23 @@ void addMenuSceneItems( SDLPP::Scene &scene, scene.addObject( resume ); auto options = 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->setId( MENU_ITEM_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( *font, "Restart", colors["text"], colors["text_out"], 0.1 ); + restart->setText( font, "Restart", colors["text"], colors["text_out"], + 0.1 ); restart->centerX(); restart->setId( MENU_ITEM_ID ); g_menu_options.push_back( restart ); scene.addObject( restart ); auto quit = 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->setId( MENU_ITEM_ID ); g_menu_options.push_back( quit ); @@ -213,13 +215,14 @@ void addGameOverSceneItems( SDLPP::Scene &scene, 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", colors["text"], colors["text_out"], 0.1 ); + y->setText( font, "GAME OVER", colors["text"], colors["text_out"], 0.1 ); 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", colors["text"], colors["text_out"], 0.1 ); + restart->setText( font, "Restart", colors["text"], colors["text_out"], + 0.1 ); restart->centerX(); restart->setColor( colors["menu_item_background"] ); restart->setId( MENU_ITEM_ID ); @@ -227,7 +230,7 @@ void addGameOverSceneItems( SDLPP::Scene &scene, scene.addObject( restart ); auto quit = 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->setId( MENU_ITEM_ID ); g_game_over_options.push_back( quit ); @@ -243,14 +246,14 @@ void addOptionsSceneItems( SDLPP::Scene &scene, 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, "OPTIONS", colors["text"], colors["text_out"], 0.1 ); + y->setText( font, "OPTIONS", colors["text"], colors["text_out"], 0.1 ); y->setId( 0 ); y->centerX(); scene.addObject( y ); auto color_scheme = std::make_shared< SDLPP::TextRenderer >( 0.18, 0.35, 0.64, 0.09, r ); 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 ); color_scheme->centerX(); color_scheme->setColor( colors["menu_item_background"] ); @@ -259,7 +262,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene, scene.addObject( color_scheme ); auto shadow = 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 ); shadow->centerX(); shadow->setId( MENU_ITEM_ID ); @@ -267,7 +270,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene, scene.addObject( shadow ); auto show3d = 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 ); show3d->centerX(); show3d->setId( MENU_ITEM_ID ); @@ -275,7 +278,7 @@ void addOptionsSceneItems( SDLPP::Scene &scene, scene.addObject( show3d ); auto save = 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->setId( MENU_ITEM_ID ); g_options_options.push_back( save ); @@ -701,19 +704,17 @@ void handleKeyDownOptions( SDL_Keycode key ) { break; case OPTIONS_MENU_SHADOW: g_show_shadow = !g_show_shadow; - static_cast< SDLPP::TextRenderer* >( + static_cast< SDLPP::TextRenderer * >( g_options_options[OPTIONS_MENU_SHADOW].get() ) ->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; - static_cast< SDLPP::TextRenderer* >( + static_cast< SDLPP::TextRenderer * >( g_options_options[OPTIONS_MENU_3D].get() ) ->changeText( std::string( "Show block texture: " ) + ( g_show_3d ? "YES" : "NO" ) ); - g_update_colors = true; default: break; } @@ -725,7 +726,7 @@ void handleKeyDownOptions( SDL_Keycode key ) { if ( selected_color_scheme == 0 ) selected_color_scheme = color_schemes_names.size(); selected_color_scheme--; - static_cast< SDLPP::TextRenderer* >( + static_cast< SDLPP::TextRenderer * >( g_options_options[OPTIONS_MENU_COLOR_SCHEME].get() ) ->changeText( "Color scheme: " + color_schemes_names[selected_color_scheme] ); @@ -733,19 +734,17 @@ void handleKeyDownOptions( SDL_Keycode key ) { break; case OPTIONS_MENU_SHADOW: g_show_shadow = !g_show_shadow; - static_cast< SDLPP::TextRenderer* >( + static_cast< SDLPP::TextRenderer * >( g_options_options[OPTIONS_MENU_SHADOW].get() ) ->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; - static_cast< SDLPP::TextRenderer* >( + static_cast< SDLPP::TextRenderer * >( g_options_options[OPTIONS_MENU_3D].get() ) ->changeText( std::string( "Show block texture: " ) + ( g_show_3d ? "YES" : "NO" ) ); - g_update_colors = true; default: break; } diff --git a/tetris/tetris.cpp b/tetris/tetris.cpp index a184466..c941815 100644 --- a/tetris/tetris.cpp +++ b/tetris/tetris.cpp @@ -18,9 +18,8 @@ std::vector< std::shared_ptr< SDLPP::RenderObject > > line_coliders{}; -void updateScore( std::shared_ptr< SDLPP::Font > font ) { - g_score_texture->setText( *font, std::to_string( g_score ), colors["text"], - colors["text_out"], 0.1 ); +void updateScore() { + g_score_texture->changeText( std::to_string( g_score ) ); } void doInput() { @@ -131,7 +130,7 @@ int main() { g_checked_line = false; } if ( g_update_score ) { - updateScore( g_font ); + updateScore(); g_update_score = false; } if ( g_update_colors ) {