TETRIS: only change textures in the main thread

This commit is contained in:
zvon 2021-01-30 23:01:36 +01:00
parent ff091a66b9
commit 79557e9edc
5 changed files with 26 additions and 7 deletions

View File

@ -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;

View File

@ -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<std::shared_ptr<SDLPP::Scene>> g_update_scenes{};
std::vector<std::shared_ptr<SDLPP::RenderObject>> g_update_objects{};

View File

@ -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<std::shared_ptr<SDLPP::Scene>> g_update_scenes;
extern std::vector<std::shared_ptr<SDLPP::RenderObject>> g_update_objects;
#endif

View File

@ -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:

View File

@ -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 ) {