From 79557e9edc55c31a21a10ca480e93dd924aec686 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 30 Jan 2021 23:01:36 +0100 Subject: [PATCH] TETRIS: only change textures in the main thread --- tetris/functions.cpp | 2 +- tetris/global_vars.cpp | 3 +++ tetris/global_vars.hpp | 3 +++ tetris/scenes.cpp | 16 ++++++++++------ tetris/tetris.cpp | 9 +++++++++ 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/tetris/functions.cpp b/tetris/functions.cpp index 97ab3e7..4a9c497 100644 --- a/tetris/functions.cpp +++ b/tetris/functions.cpp @@ -98,7 +98,7 @@ check_floor: g_cur_object->movePiece( 0, -BLOCK_SIZE ); for ( auto &block : g_cur_object->getObjects() ) { if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) { - g_game_over_scene->updateSizeAndPosition(); + g_update_scenes.push_back(g_game_over_scene); g_active_scenes.push_back( g_game_over_scene ); g_input_functions.push_back( gameOverSceneInput ); break; diff --git a/tetris/global_vars.cpp b/tetris/global_vars.cpp index 96a2846..e2f4890 100644 --- a/tetris/global_vars.cpp +++ b/tetris/global_vars.cpp @@ -53,3 +53,6 @@ std::vector< std::shared_ptr< TetrisPiece > ( * )( std::shared_ptr< SDLPP::Font > g_font{}; std::shared_ptr< SDLPP::FontConfiguration > g_font_config{}; + +std::vector> g_update_scenes{}; +std::vector> g_update_objects{}; diff --git a/tetris/global_vars.hpp b/tetris/global_vars.hpp index de63d2e..a15b8cc 100644 --- a/tetris/global_vars.hpp +++ b/tetris/global_vars.hpp @@ -57,4 +57,7 @@ extern std::vector< std::shared_ptr< TetrisPiece > ( * )( extern std::shared_ptr< SDLPP::Font > g_font; extern std::shared_ptr< SDLPP::FontConfiguration > g_font_config; +extern std::vector> g_update_scenes; +extern std::vector> g_update_objects; + #endif diff --git a/tetris/scenes.cpp b/tetris/scenes.cpp index 57b889b..e93f956 100644 --- a/tetris/scenes.cpp +++ b/tetris/scenes.cpp @@ -308,7 +308,7 @@ prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer ) { void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) { switch ( key ) { case SDLK_ESCAPE: - g_menu_scene->updateSizeAndPosition(); + g_update_scenes.push_back( g_menu_scene ); g_active_scenes.push_back( g_menu_scene ); g_input_functions.push_back( menuSceneInput ); break; @@ -422,7 +422,7 @@ void pollEventsMain( SDLPP::Scene &scene ) { case SDL_WINDOWEVENT: if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { for ( auto &x : g_active_scenes ) - x->updateSizeAndPosition(); + g_update_scenes.push_back(x); g_update_size = true; } default: @@ -523,7 +523,7 @@ void handleKeyDownMenu( SDL_Keycode key ) { g_input_functions.pop_back(); } break; case MAIN_MENU_OPTIONS: - g_options_scene->updateSizeAndPosition(); + g_update_scenes.push_back(g_options_scene); g_active_scenes.push_back( g_options_scene ); g_input_functions.push_back( optionsSceneInput ); break; @@ -554,7 +554,7 @@ void pollEventsMenu() { case SDL_WINDOWEVENT: if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { for ( auto &x : g_active_scenes ) - x->updateSizeAndPosition(); + g_update_scenes.push_back(x); g_update_size = true; } default: @@ -625,7 +625,7 @@ void pollEventsGameOver() { case SDL_WINDOWEVENT: if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { for ( auto &x : g_active_scenes ) - x->updateSizeAndPosition(); + g_update_scenes.push_back(x); g_update_size = true; } default: @@ -693,6 +693,7 @@ void handleKeyDownOptions( SDL_Keycode key ) { 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; @@ -700,6 +701,7 @@ void handleKeyDownOptions( SDL_Keycode key ) { 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; @@ -724,6 +726,7 @@ void handleKeyDownOptions( SDL_Keycode key ) { 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; @@ -731,6 +734,7 @@ void handleKeyDownOptions( SDL_Keycode key ) { 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; @@ -770,7 +774,7 @@ void pollEventsOptions() { case SDL_WINDOWEVENT: if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { for ( auto &x : g_active_scenes ) - x->updateSizeAndPosition(); + g_update_scenes.push_back(x); g_update_size = true; } default: diff --git a/tetris/tetris.cpp b/tetris/tetris.cpp index 3377dd1..1fba611 100644 --- a/tetris/tetris.cpp +++ b/tetris/tetris.cpp @@ -147,6 +147,15 @@ int main() { updateSize(); g_update_size = false; } + for(size_t i = 0; i < g_update_scenes.size(); i++) { + g_update_scenes.back()->updateSizeAndPosition(); + g_update_scenes.pop_back(); + } + for(size_t i = 0; i < g_update_objects.size(); i++) { + g_update_objects.back()->updateSizeAndPosition(); + g_update_objects.pop_back(); + } + renderer->clearRenderer(); for ( auto &x : g_active_scenes ) {