TETRIS: only change textures in the main thread
This commit is contained in:
parent
ff091a66b9
commit
79557e9edc
@ -98,7 +98,7 @@ check_floor:
|
|||||||
g_cur_object->movePiece( 0, -BLOCK_SIZE );
|
g_cur_object->movePiece( 0, -BLOCK_SIZE );
|
||||||
for ( auto &block : g_cur_object->getObjects() ) {
|
for ( auto &block : g_cur_object->getObjects() ) {
|
||||||
if ( scene->getCollisions( *block, { GAME_OVER } ).size() > 0 ) {
|
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_active_scenes.push_back( g_game_over_scene );
|
||||||
g_input_functions.push_back( gameOverSceneInput );
|
g_input_functions.push_back( gameOverSceneInput );
|
||||||
break;
|
break;
|
||||||
|
@ -53,3 +53,6 @@ std::vector< std::shared_ptr< TetrisPiece > ( * )(
|
|||||||
|
|
||||||
std::shared_ptr< SDLPP::Font > g_font{};
|
std::shared_ptr< SDLPP::Font > g_font{};
|
||||||
std::shared_ptr< SDLPP::FontConfiguration > g_font_config{};
|
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{};
|
||||||
|
@ -57,4 +57,7 @@ extern std::vector< std::shared_ptr< TetrisPiece > ( * )(
|
|||||||
extern std::shared_ptr< SDLPP::Font > g_font;
|
extern std::shared_ptr< SDLPP::Font > g_font;
|
||||||
extern std::shared_ptr< SDLPP::FontConfiguration > g_font_config;
|
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
|
#endif
|
||||||
|
@ -308,7 +308,7 @@ prepareOptionsScene( std::shared_ptr< SDLPP::Renderer > renderer ) {
|
|||||||
void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
|
||||||
switch ( key ) {
|
switch ( key ) {
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
g_menu_scene->updateSizeAndPosition();
|
g_update_scenes.push_back( g_menu_scene );
|
||||||
g_active_scenes.push_back( g_menu_scene );
|
g_active_scenes.push_back( g_menu_scene );
|
||||||
g_input_functions.push_back( menuSceneInput );
|
g_input_functions.push_back( menuSceneInput );
|
||||||
break;
|
break;
|
||||||
@ -422,7 +422,7 @@ void pollEventsMain( SDLPP::Scene &scene ) {
|
|||||||
case SDL_WINDOWEVENT:
|
case SDL_WINDOWEVENT:
|
||||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||||
for ( auto &x : g_active_scenes )
|
for ( auto &x : g_active_scenes )
|
||||||
x->updateSizeAndPosition();
|
g_update_scenes.push_back(x);
|
||||||
g_update_size = true;
|
g_update_size = true;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -523,7 +523,7 @@ void handleKeyDownMenu( SDL_Keycode key ) {
|
|||||||
g_input_functions.pop_back();
|
g_input_functions.pop_back();
|
||||||
} break;
|
} break;
|
||||||
case MAIN_MENU_OPTIONS:
|
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_active_scenes.push_back( g_options_scene );
|
||||||
g_input_functions.push_back( optionsSceneInput );
|
g_input_functions.push_back( optionsSceneInput );
|
||||||
break;
|
break;
|
||||||
@ -554,7 +554,7 @@ void pollEventsMenu() {
|
|||||||
case SDL_WINDOWEVENT:
|
case SDL_WINDOWEVENT:
|
||||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||||
for ( auto &x : g_active_scenes )
|
for ( auto &x : g_active_scenes )
|
||||||
x->updateSizeAndPosition();
|
g_update_scenes.push_back(x);
|
||||||
g_update_size = true;
|
g_update_size = true;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -625,7 +625,7 @@ void pollEventsGameOver() {
|
|||||||
case SDL_WINDOWEVENT:
|
case SDL_WINDOWEVENT:
|
||||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||||
for ( auto &x : g_active_scenes )
|
for ( auto &x : g_active_scenes )
|
||||||
x->updateSizeAndPosition();
|
g_update_scenes.push_back(x);
|
||||||
g_update_size = true;
|
g_update_size = true;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -693,6 +693,7 @@ 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_objects.push_back(g_options_options[OPTIONS_MENU_SHADOW]);
|
||||||
break;
|
break;
|
||||||
case OPTIONS_MENU_3D:
|
case OPTIONS_MENU_3D:
|
||||||
g_show_3d = !g_show_3d;
|
g_show_3d = !g_show_3d;
|
||||||
@ -700,6 +701,7 @@ 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_objects.push_back(g_options_options[OPTIONS_MENU_3D]);
|
||||||
g_update_3d = true;
|
g_update_3d = true;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -724,6 +726,7 @@ 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_objects.push_back(g_options_options[OPTIONS_MENU_SHADOW]);
|
||||||
break;
|
break;
|
||||||
case OPTIONS_MENU_3D:
|
case OPTIONS_MENU_3D:
|
||||||
g_show_3d = !g_show_3d;
|
g_show_3d = !g_show_3d;
|
||||||
@ -731,6 +734,7 @@ 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_objects.push_back(g_options_options[OPTIONS_MENU_3D]);
|
||||||
g_update_3d = true;
|
g_update_3d = true;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -770,7 +774,7 @@ void pollEventsOptions() {
|
|||||||
case SDL_WINDOWEVENT:
|
case SDL_WINDOWEVENT:
|
||||||
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
|
||||||
for ( auto &x : g_active_scenes )
|
for ( auto &x : g_active_scenes )
|
||||||
x->updateSizeAndPosition();
|
g_update_scenes.push_back(x);
|
||||||
g_update_size = true;
|
g_update_size = true;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -147,6 +147,15 @@ int main() {
|
|||||||
updateSize();
|
updateSize();
|
||||||
g_update_size = false;
|
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();
|
renderer->clearRenderer();
|
||||||
for ( auto &x : g_active_scenes ) {
|
for ( auto &x : g_active_scenes ) {
|
||||||
|
Loading…
Reference in New Issue
Block a user