Ignore mouse movement in-between SDL_SetRelativeMouseMode() toggling

At the start of my program, I have SDL_SetRelativeMouseMode() set to true. I have a camera that uses SDL_GetRelativeMouseState() to get the mouse movement for calculating the view direction. When I press escape, I disable SDL_SetRelativeMouseMode() so I can edit settings within an ImGui window. When I press escape again, I re-enable SDL_SetRelativeMouseMode() to capture my cursor for the camera movement.

My question is, how do I disregard mouse movements that happen while I’m tinkering with my ImGui window? When I re-enable SDL_SetRelativeMouseMode() to capture my cursor, my camera jolts to the new mouse location. I want my camera direction to be the same as when I first pressed escape.

Does this make sense? I’m pretty new to SDL so any help is appreciated.

Immediately after turning relative mouse mode back on, I get the mouse state and throw it away, which should consume all mouse events. Something like:

ImGuiIO &io = ImGui::GetIO();
static bool guiMode = false;
while(SDL_PollEvent(&event)) {
	if(event.type == SDL_KEYUP && event.key.keysym.sym = SDLK_ESCAPE) {
		guiMode = !guiMode;
		if(guiMode) {
			SDL_SetRelativeMouseMode(SDL_FALSE);
			SDL_ShowCursor(SDL_TRUE);
		} else {
			SDL_ShowCursor(SDL_FALSE);
			SDL_SetRelativeMouseMode(SDL_TRUE);
			// consume all pending mouse events, so the camera doesn't go flying
			(void)SDL_GetRelativeMouseState(nullptr, nullptr);
		}
	}
	ImGui_ImplWhatever_ProcessEvent(&event);

	if(!io.WantTextInput && !io.WantCaptureKeyboard) {
		// ImGui doesn't need the keyboard events, so process them as game input here
	}
}
1 Like

Awesome, (void)SDL_GetRelativeMouseState(nullptr, nullptr) did the trick!

1 Like