Fix possible segfault

This commit is contained in:
zvon 2020-09-12 09:36:08 +02:00
parent 0a06e1d841
commit 74e53a0d67
3 changed files with 28 additions and 18 deletions

View File

@ -113,6 +113,12 @@ public:
bool getRenderColiders() { bool getRenderColiders() {
return render_coliders; return render_coliders;
} }
void clearRenderer() {
SDL_RenderClear( getRendererPtr() );
}
void presentRenderer() {
SDL_RenderPresent( getRendererPtr() );
}
private: private:
SDL_Renderer *renderer = NULL; SDL_Renderer *renderer = NULL;
@ -562,10 +568,10 @@ public:
} }
return ret; return ret;
} }
void renderScene( bool clear_scene = true ) { void renderScene( bool clear_renderer = true ) {
checkKilled(); checkKilled();
render_mutex.lock(); render_mutex.lock();
if ( clear_scene ) if ( clear_renderer )
SDL_RenderClear( renderer->getRendererPtr() ); SDL_RenderClear( renderer->getRendererPtr() );
if ( background && background->getTexturePtr() ) if ( background && background->getTexturePtr() )
SDL_RenderCopy( renderer->getRendererPtr(), SDL_RenderCopy( renderer->getRendererPtr(),

View File

@ -316,6 +316,11 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
g_cur_shadow->rotate(); g_cur_shadow->rotate();
updateShadow(scene); updateShadow(scene);
break; break;
case SDLK_SPACE:
if(!g_cur_object)
break;
g_cur_object->setPos(g_cur_shadow->getPos());
break;
case SDLK_r: case SDLK_r:
scene.getRenderer().setRenderColiders( scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() ); !scene.getRenderer().getRenderColiders() );
@ -565,6 +570,16 @@ void gameOverSceneInput( std::shared_ptr< SDLPP::Scene > /*UNUSED*/, int /*UNUSE
pollEventsGameOver(); pollEventsGameOver();
} }
void saveOptions() {
g_update_colors = true;
if(g_cur_shadow)
g_cur_shadow->setHidden(!g_show_shadow);
g_pause = 0;
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back();
g_input_functions.pop_back();
}
void handleKeyDownOptions( SDL_Keycode key ) { void handleKeyDownOptions( SDL_Keycode key ) {
switch ( key ) { switch ( key ) {
case SDLK_r: case SDLK_r:
@ -626,24 +641,14 @@ void handleKeyDownOptions( SDL_Keycode key ) {
case SDLK_RETURN: case SDLK_RETURN:
switch ( g_options_select ) { switch ( g_options_select ) {
case 2: case 2:
g_update_colors = true; saveOptions();
g_cur_shadow->setHidden(!g_show_shadow);
g_pause = 0;
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back();
g_input_functions.pop_back();
break; break;
default: default:
break; break;
} }
break; break;
case SDLK_ESCAPE: case SDLK_ESCAPE:
g_update_colors = true; saveOptions();
g_cur_shadow->setHidden(!g_show_shadow);
g_pause = 0;
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back();
g_input_functions.pop_back();
default: default:
break; break;
} }

View File

@ -107,12 +107,11 @@ int main() {
g_update_colors = false; g_update_colors = false;
} }
g_main_scene->renderScene(); renderer->clearRenderer();
for(auto &x : g_active_scenes) { for(auto &x : g_active_scenes) {
if(x != g_main_scene)
x->renderScene(false); x->renderScene(false);
} }
g_main_scene->presentScene(); renderer->presentRenderer();
g_wait_for_anim = false; g_wait_for_anim = false;
frames++; frames++;
if ( SDL_GetTicks() - base >= 1000 ) { if ( SDL_GetTicks() - base >= 1000 ) {