SDL Warp Mouse

Started by
5 comments, last by SIGTERMer 15 years, 3 months ago
I'm design the game play for a FPS. The way I handle the direction the play is looking is standard to FPS's, the mouse. BUT, I noticed that the mouse can sometimes go off the screen which doesn't allow me to get the MouseMotion events anymore. So, I figured I'd use the SDL_WarpMouse(x,y) func to always position the mouse back at the center screen. The problem i'm having is that function also creates a MouseMotion event, so I'm trying to make my function filter out that message that says it moved the mouse TO center screen. Here's what I have so far, it works, I can get the basic movement, but it looks like sometimes when I have a key pressed as well, it filters out my key press event

void cWInput::SetMousePosition(
		const sWVector2D &pos, 
		WcrBOOL moveEvent)
	{
		SDL_WarpMouse(pos.X, pos.Y);
		SDL_PumpEvents();

		if (moveEvent == false)
		{
			WUtility::tWVectorList<SDL_Event>	Events;
			SDL_Event Event;
			bool hit = false;

			LastEvent.type = 0;

			while (SDL_PeepEvents(&Event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK))
			{
				if (sWVector2D(Event.motion.x,Event.motion.y) == pos)
				{
					hit = true;
					break;
				}
				else
					Events.Insert(Event);
			}

			if (hit == false)
			{
				SDL_WaitEvent(&Event);
				if (Event.type != SDL_MOUSEMOTION || sWVector2D(Event.motion.x,Event.motion.y) != pos)
				{
					Events.Insert(Event);
					//SDL_WaitEvent(&Event);
				}
			}
			while (Events.GetNetItems() > 0)
			{
				SDL_PushEvent(Events.GetItem(0));
				Events.Remove(0);
			}
		}
	}

This is many times of trying to figure out ways to get around the filter, so it may look a bit redundant... That's the best I have it and I was wondering if anyone has an easier way? Thanks
Eddie RiveronSoftware EngineerCreator of 3D Wrath EngineWebsite soon!
Advertisement
SDL has an input capture API specially for this. Look at the SDL_WM_GrabInput function (defined in the SDL_video.h header, not the SDL_events.h header!).

You probably also will want to hide the mouse cursor, since it will then be pinned to the centre of the screen, so you will need SDL_ShowCursor (in SDL_mouse.h).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

oh hah thanks, that's very simple. Also, for future reference. The mouse needs to be invisible for this to give the full FPS effect. If the mouse is shown and Grab is On, the mouse will stop at the windows edge, but you will no longer get movement if the mouse were to move more toward the windows edge.
Eddie RiveronSoftware EngineerCreator of 3D Wrath EngineWebsite soon!
Quote:Original post by BloodLust666
Also, for future reference. The mouse needs to be invisible for this to give the full FPS effect. If the mouse is shown and Grab is On, the mouse will stop at the windows edge, but you will no longer get movement if the mouse were to move more toward the windows edge.

Ja, I wondered about that. For some reason the OS X port has at times just locked the pointer to the center of the screen when grab is on, though I think they have fixed this recently.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Same problem here, but I don't understand how "SDL_WM_GrabInput" could help me with this?
Finally I've got it running with warping the cursur back to the center of the screen each frame. But this makes the scene very... laggy, not smooth. Is there a better solution?
don't warp the cursor immediately after each mouse event. wait until the cursor reaches the borders then call the warp function. this will solve the lag issue :)

This topic is closed to new replies.

Advertisement