From 6769b6605347d2d1d89cb39366df239ebdee5f71 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Thu, 13 Feb 2014 01:31:10 -0500 Subject: [PATCH] don't play haptic effects when window does not have focus --- src/io/joy_ff.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++ src/osapi/osapi.cpp | 18 +++++++++-- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/io/joy_ff.cpp b/src/io/joy_ff.cpp index f7326b8..4fbaa22 100644 --- a/src/io/joy_ff.cpp +++ b/src/io/joy_ff.cpp @@ -16,6 +16,7 @@ static int Joy_ff_enabled = 0; +static int Joy_ff_acquired = 0; static SDL_Haptic *haptic = NULL; static int joy_ff_handling_scaler = 0; static int Joy_ff_directional_hit_effect_enabled = 1; @@ -91,6 +92,7 @@ int joy_ff_init() mprintf(("\n")); Joy_ff_enabled = 1; + Joy_ff_acquired = 1; Joy_ff_directional_hit_effect_enabled = os_config_read_uint(NULL, "EnableHitEffect", 1); @@ -110,6 +112,7 @@ void joy_ff_shutdown() SDL_QuitSubSystem(SDL_INIT_HAPTIC); + Joy_ff_acquired = 0; Joy_ff_enabled = 0; } @@ -403,6 +406,10 @@ static void joy_ff_start_effect(haptic_effect_t *eff, const char *name) void joy_ff_stop_effects() { + if ( !Joy_ff_enabled ) { + return; + } + SDL_HapticStopAll(haptic); } @@ -415,10 +422,32 @@ void joy_ff_mission_init(vector v) void joy_reacquire_ff() { + if ( !Joy_ff_enabled ) { + return; + } + + if (Joy_ff_acquired) { + return; + } + + joy_ff_start_effect(&pSpring, "Spring"); + + Joy_ff_acquired = 1; } void joy_unacquire_ff() { + if ( !Joy_ff_enabled ) { + return; + } + + if ( !Joy_ff_acquired ) { + return; + } + + joy_ff_stop_effects(); + + Joy_ff_acquired = 0; } void joy_ff_play_vector_effect(vector *v, float scaler) @@ -449,6 +478,10 @@ void joy_ff_play_dir_effect(float x, float y) return; } + if ( !Joy_ff_acquired ) { + return; + } + // allow for at least one of the effects to work if ( !pHitEffect1.loaded && !pHitEffect2.loaded ) { return; @@ -522,6 +555,10 @@ void joy_ff_play_primary_shoot(int gain) return; } + if ( !Joy_ff_acquired ) { + return; + } + if ( !pShootEffect.loaded && !Joy_rumble ) { return; } @@ -562,6 +599,10 @@ void joy_ff_play_secondary_shoot(int gain) return; } + if ( !Joy_ff_acquired ) { + return; + } + if ( !pSecShootEffect.loaded && !Joy_rumble ) { return; } @@ -604,6 +645,10 @@ void joy_ff_adjust_handling(int speed) return; } + if ( !Joy_ff_acquired ) { + return; + } + if ( !pSpring.loaded ) { return; } @@ -645,6 +690,10 @@ void joy_ff_docked() return; } + if ( !Joy_ff_acquired ) { + return; + } + if ( !pDock.loaded ) { return; } @@ -666,6 +715,10 @@ void joy_ff_play_reload_effect() return; } + if ( !Joy_ff_acquired ) { + return; + } + if ( !pDock.loaded ) { return; } @@ -689,6 +742,10 @@ void joy_ff_afterburn_on() return; } + if ( !Joy_ff_acquired ) { + return; + } + if (Joy_ff_afterburning) { return; } @@ -733,6 +790,10 @@ void joy_ff_afterburn_off() return; } + if ( !Joy_ff_acquired ) { + return; + } + if ( !Joy_ff_afterburning ) { return; } @@ -755,6 +816,10 @@ void joy_ff_explode() if ( !Joy_ff_enabled ) { return; } + + if ( !Joy_ff_acquired ) { + return; + } /* if (pDeathroll1.loaded) { SDL_HapticStopEffect(haptic, pDeathroll1.id); @@ -805,6 +870,10 @@ void joy_ff_fly_by(int mag) return; } + if ( !Joy_ff_acquired ) { + return; + } + if (Joy_ff_afterburning) { return; } @@ -853,6 +922,10 @@ void joy_ff_deathroll() if ( !Joy_ff_enabled ) { return; } + + if ( !Joy_ff_acquired ) { + return; + } /* if (pDeathroll1.loaded) { SDL_HapticStopEffect(haptic, pDeathroll1.id); diff --git a/src/osapi/osapi.cpp b/src/osapi/osapi.cpp index f3475b0..87aa6df 100644 --- a/src/osapi/osapi.cpp +++ b/src/osapi/osapi.cpp @@ -391,11 +391,23 @@ void os_poll() } break; - case SDL_WINDOWEVENT: - if (e.window.event == SDL_WINDOWEVENT_RESIZED) { - gr_opengl_set_viewport(e.window.data1, e.window.data2); + case SDL_WINDOWEVENT: { + switch (e.window.event) { + case SDL_WINDOWEVENT_RESIZED: + gr_opengl_set_viewport(e.window.data1, e.window.data2); + break; + + case SDL_WINDOWEVENT_FOCUS_LOST: + joy_unacquire_ff(); + break; + + case SDL_WINDOWEVENT_FOCUS_GAINED: + joy_reacquire_ff(); + break; } + break; + } default: break; -- 2.39.2